source: CIVL/examples/translation/mpi/mpi_gather_inPlace.c@ 8a50139

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 8a50139 was e9c1691, checked in by Ziqing Luo <ziqing@…>, 12 years ago

add new examples for scatter and gather

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1534 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <mpi.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <assert.h>
5#define SIZE 4
6
7int main (int argc, char *argv[])
8{
9 int numtasks, rank, sendcount, recvcount, source;
10 float sendbuf[SIZE];
11 float recvbuf[SIZE * SIZE];
12
13 MPI_Init(&argc,&argv);
14 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
15 MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
16
17 if (numtasks == SIZE) {
18 source = 1;
19 sendcount = SIZE;
20 recvcount = SIZE;
21 //init sendbuf
22 for(int i=0; i<SIZE; i++)
23 sendbuf[i] = (float)(rank * SIZE + i);
24 if(rank == source){
25 for(int i=0; i<SIZE; i++)
26 recvbuf[rank * SIZE + i] = sendbuf[i];
27 MPI_Gather(MPI_IN_PLACE,sendcount,MPI_FLOAT,recvbuf,recvcount,
28 MPI_FLOAT,source,MPI_COMM_WORLD);
29 }
30 else
31 MPI_Gather(sendbuf,sendcount,MPI_FLOAT,recvbuf,recvcount,
32 MPI_FLOAT,source,MPI_COMM_WORLD);
33 //assertions
34 if(rank == source){
35 for(int i=0; i<(SIZE*SIZE); i++){
36 printf("recvbuf[%d] : %f\n", i, recvbuf[i]);
37 assert(recvbuf[i] == i);
38 }
39 }
40 }
41 else
42 printf("Must specify %d processors. Terminating.\n",SIZE);
43
44 MPI_Finalize();
45}
Note: See TracBrowser for help on using the repository browser.