Как получить один код, который работает последовательно и параллельно в соответствии с переменной CMake - PullRequest
0 голосов
/ 09 октября 2018

Выдержка из CMake, показывающая, как я определяю флаг PARALLEL для передачи в код:

option(ENABLE_MPI "Enable MPI parallelization" OFF)

SET(PARALLEL 0 CACHE INTEGER "Defines a Parallel Build")

if(ENABLE_MPI)
    find_package(MPI REQUIRED)
    include_directories(${MPI_INCLUDE_PATH})
    set(CMAKE_C_FLAGS "${CMAKE_FLAGS} ${MPI_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MPI_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MPI_EXE_LINKER_FLAGS}")
    add_subdirectory(third-party/parmetis-4.0.3)
    unset(PARALLEL CACHE)
    SET(PARALLEL 1 CACHE INTEGER "Defines a Parallel Build")
    add_definitions( -DPARALLEL=${PARALLEL} )
endif(ENABLE_MPI)

В моем коде C у меня есть такая опция:

#ifndef PARALLEL
#  define PARALLEL @PARALLEL@
#endif

int main(int argc, char **argv){
    int world_rank =0;
    int world_size=0;

    if (PARALLEL) {
        printf("This is a parallel build!\n");
        // Initialize the MPI environment
        MPI_Init(NULL, NULL);

        // Get the number of processes
        int world_size;
        MPI_Comm_size(MPI_COMM_WORLD, &world_size);

        // Get the rank of the process
        int world_rank;
        MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

        // Get the name of the processor
        char processor_name[MPI_MAX_PROCESSOR_NAME];
        int name_len;
        MPI_Get_processor_name(processor_name, &name_len);

        // Print off a hello world message
        printf("Hello world from processor %s, rank %d out of %d processors\n",
            processor_name, world_rank, world_size);
         if (world_rank == 0) {
            //read inputfile and initalize
            printf("rank ID %d\n",world_rank);
        }
    }
    else {
        printf("This is NOT a parallel build!\n");
    }

    if (world_rank == 0) {
            //read inputfile and initalize
       printf("rank ID %d: Read Inputfile \n",world_rank);
       if (PARALLEL) {
           printf("rank ID %d: Parition Mesh with %d Partitions\n",world_rank,world_size);
       }else{printf("rank ID %d: Serial Simulation - No Mesh Partitioning \n", world_rank); }
     }

   //ReadInputFile(argv[1]);
   //WriteVTU(argv[1]);
   //FreeArrays();

   if (PARALLEL) {
       // Finalize the MPI environment.
       MPI_Finalize();
   }

   return 0;
}

Вывод, который я получаю из этого (с параллельной сборкой):

This is a parallel build!
Hello world from processor -----, rank 0 out of 2 processors
rank ID 0
rank ID 0: Read Inputfile
rank ID 0: Parition Mesh with 0 Partitions
This is a parallel build!
Hello world from processor ----, rank 1 out of 2 processors
rank ID 0: Read Inputfile
rank ID 0: Parition Mesh with 0 Partitions

Есть ли способ написать одну программу, которая может быть скомпилирована в последовательную (скажем, с помощью cc), а также параллельно (с помощью mpicc)?

...