开发者

MPI: all to all broadcast on one dimensional array

开发者 https://www.devze.com 2023-03-05 15:26 出处:网络
I have number of processors and one array, and each processor fills their work into, one dimensional array like:

I have number of processors and one array, and each processor fills their work into, one dimensional array like:

  • dense_process_array for process 1: |process1|0000000000000|
  • dense_process_array for process 2: |000000|process2|000000|

each process fills their interval, then I want to all processo开发者_Go百科rs have others results into same array.

  • Process 1 => dense_process_array |process1|process2|.....|processN|
  • Process 2 ... Process N

(like all2all bcast) Therefore, Every process calls this function:

void doCommunication(int id, int numprocs, int start_point, int end_point) {
    int size_of_length = end_point - start_point + 1;
    MPI_Scatter(dense_process_array+start_point, size_of_length, MPI_DOUBLE, dense _process_array +start_point, size_of_length, MPI_DOUBLE, id, MPI_COMM_WORLD;                                                                                                                                                                                                                                                                             
}

But, in the end when I looked my array from any process, I seen that it can not get results of other processes, can you suggest anything?

not: I'm new in MPI, and basicly I want to all2all bcast.


I believe you're looking for MPI_Allgather:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

void printdata(int size, int rank, int n, int *data) {
    printf("Rank %d\n",rank);
    for (int j=0; j<size*n; j++)
        printf("%d ",data[j]);
    printf("\n");
}

int main(int argc, char **argv) {
    const int n=3;
    int ierr, rank, size;
    int *datain, *dataout;

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

    datain = (int *)malloc(n*size*sizeof(int));
    dataout = (int *)malloc(n*size*sizeof(int));
    for (int i=0; i<n*size; i++)
        datain[i]=9;
    for (int i=0; i<n; i++)
        datain[rank*n+i]=rank;

    if (rank == 0) printf("Before:\n");
    printdata(size, rank, n, datain);

    MPI_Allgather(&(datain[rank*n]), n, MPI_INT, dataout, n, MPI_INT, MPI_COMM_WORLD);

    if (rank == 0) printf("After:\n");
    printdata(size, rank, n, dataout);

    free(datain);
    free(dataout);
    MPI_Finalize();
    return 0;
}

Running gives

$ mpirun -np 3 ./allgather
Before:
Rank 0
0 0 0 9 9 9 9 9 9 
Rank 1
9 9 9 1 1 1 9 9 9 
Rank 2
9 9 9 9 9 9 2 2 2 
After:
Rank 0
0 0 0 1 1 1 2 2 2 
Rank 1
0 0 0 1 1 1 2 2 2 
Rank 2
0 0 0 1 1 1 2 2 2 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号