An integer is given in each process. Using MPI_Send and MPI_Recv functions, perform a cyclic shift of data for all processes in increments of 1, transfer the number from process 0 to process 1, from process 1 to process 2, ..., from the last process to process 0. In each process, output the resulting number.

int flag; MPI_Initialized(&flag); if (flag == 0) return; int rank, size; MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Status s; int n; pt >> n; for (int i = 0; i < size; ++i) { MPI_Send(&n,1,MPI_INT,i,0,MPI_COMM_WORLD); } 

    1 answer 1

    Simply use the value of +1 modulo the size of the communicator as the recipient process for MPI_Send, and the sender process for MPI_Recv can be calculated using the inverse formula:

     MPI_Send(&n, 1, MPI_INT, (i + 1) % size, 0, MPI_COMM_WORLD); MPI_Recv(&n, 1, MPI_INT, (size + (i - 1)) % size, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); 
    • As for MPI_Send (), I thought so, I thought more precisely, but I didn’t write, but I don’t understand with Recv a bit. - Vitaliy Kopelyuk
    • I can not understand where there is 4, for what. - Vitaliy Kopelyuk
    • one
      now I understand, 4 is a common size - Vitaliy Kopelyuk
    • Damn, sorry, yes, this is a common size, of course, for now I’ve deduced a formula using my example in response to automatically writing down this value :) Updated the answer. - iksuy