MPI_gather/MPI_gatherv测试

每个进程上都有一组数据,将它们收集到进程编号为root的进程中,并按进程编号的顺序存放

program gather include 'mpif.h' integer,parameter::maxbuf = 200, len = 10, mp = 5 integer myid, p, mycomm, ierr, root, ia(len), iga(maxbuf), & i, displs(mp), counts(mp) call MPI_Init(ierr) call MPI_Comm_dup(MPI_COMM_WORLD, mycomm, ierr) call MPI_Comm_rank(mycomm, myid, ierr) call MPI_Comm_size(mycomm, p, ierr) print *, 'Process ', myid, ' of', p, 'is running' do i = 1, len ia(i) = i + myid*len enddo do i = 1, p displs(i) = 20*i - 20 counts(i) = len enddo root = 0 call MPI_gather(ia, len, MPI_INTEGER, iga, len, & MPI_INTEGER, root, mycomm, ierr) if(myid.eq.root) then print *, 'The gather values are:', & iga(1), iga(len+1), iga(2*len + 1) endif call MPI_gatherv(ia, len, MPI_INTEGER, iga, counts, & displs,MPI_INTEGER, root, mycomm, ierr) if(myid.eq.root) then print *, 'The gatherv values are:', & iga(2), iga(2*len+2), iga(4*len + 2) endif call MPI_Comm_free(mycomm, ierr) call MPI_Finalize(ierr) stop end

 

result:

[root@c0108 ~]# mpiexec -n 4 ./gather Process 0 of 4 is running The gather values are: 1 11 21 The gatherv values are: 2 12 22 Process 1 of 4 is running Process 3 of 4 is running Process 2 of 4 is running

这个程序的输出值分别为: 1, 11, 21 和2, 12, 22.

 

MPI_GATHER 接收到的数据在接收缓冲区中是连续存放的, 而MPI_GATHERV 接收到的数据在接收缓冲区中是不连续存放的, 在我们这里给出的例子中, 由于COUNTS 的所有分量是相同的, 可以看成是每隔步长为20 的位置存放一个新接收的数据.

 

你可能感兴趣的:(c,测试,Integer)