C中的MPI前缀和

use*_*454 2 c mpi

我很难用MPI实现前缀和.我想我错过了几行,但我不知道哪些缺失,应该放在哪里.这是我有的:

int main(int argc, char** argv){  
 int i, size, nprocs, rank;
 int array[atoi(argv[1])];

int Destination, Destination_tag;
int Source, Source_tag, RecvData;

int len = sizeof(array)/sizeof(int);

for(i = 0; i < size; i++) 
     {
    array[i] = i+rank*size;
}

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

int id = rank;

      //I believe the error is here
for(i = 0; i < size, i++)
{
    message = (rank - pow(2,x));

    Destination = message ;
    Destination_tag = array.id; 
    MPI_Send(&message, 1, MPI_INT, Destination, Destination_tag, MPI_COMM_WORLD); 

    Source = message ;
    Source_tag = message; 
    MPI_Recv(&RecvData, 1, MPI_INT, Source, Source_tag, MPI_COMM_WORLD, &Status); 
   //End of problem area
    printf("My rank is  %d n =%d \n",i,size); 

    MPI_Finalize();
    return 0;
Run Code Online (Sandbox Code Playgroud)

}

小智 8

Instead of computing a prefix sum manually with MPI_Send and MPI_Recv, use MPI_Scan. MPI_Scan performs a partial inclusive reduction of elements across process_{0} to process_{your rank}, allowing you to perform a prefix sum very easily (and effectively!).

For example, assume single integers are spread across processes like this:
Process 0 = 2
Process 1 = 3
Process 2 = 4
Process 3 = 5

After calling MPI_Scan using MPI_SUM as your reduction operation, the result will be this:
Process 0 = 2
Process 1 = 5
Process 2 = 9
Process 3 = 14

Call MPI_Exscan to do an exclusive scan, meaning the reduction will not include the data held by the calling process in the reduction. The result will look like this:
Process 0 = undefined (set your recv_buffer to 0 or there will be garbage there)
Process 1 = 2
Process 2 = 5
Process 3 = 9