MPI - Printing in an order

后端 未结 5 1290
臣服心动
臣服心动 2020-12-03 20:12

I\'m trying to write a function in C where every processor prints it\'s own data. Here is what i have:

void print_mesh(int p,int myid,int** U0,int X,int Y){
         


        
5条回答
  •  生来不讨喜
    2020-12-03 20:51

    So, you can do something like this:

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (rank == 0) {
        MPI_Send(&message, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
        printf("1 SIZE = %d RANK = %d MESSAGE = %d \n",size,rank, message);
    } else {
        int buffer;
        MPI_Status status;
        MPI_Probe(MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
        MPI_Get_count(&status, MPI_INT, &buffer);
        if (buffer == 1) {
            printf("2 SIZE = %d RANK = %d MESSAGE = %d \n",size,rank, message);
            MPI_Recv(&message, buffer, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
            if (rank + 1 != size) {
                MPI_Send(&message, 1, MPI_INT, ++rank, 0, MPI_COMM_WORLD);
            }
        };
    };
    MPI_Finalize();
    

    After execute:

    $ mpirun -n 5 ./a.out 
    1 SIZE = 5 RANK = 0 MESSAGE = 999 
    2 SIZE = 5 RANK = 1 MESSAGE = 999 
    2 SIZE = 5 RANK = 2 MESSAGE = 999 
    2 SIZE = 5 RANK = 3 MESSAGE = 999 
    2 SIZE = 5 RANK = 4 MESSAGE = 999 
    

提交回复
热议问题