source: CIVL/examples/mpi/collective/scatterGather.c@ b88db56

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since b88db56 was 8f2c865, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

minor correction to make the example executable by MPICH

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

  • Property mode set to 100644
File size: 1.2 KB
Line 
1/**
2 * This is a simple program to demonstrate the correct usage of two MPI colletive operations,
3 * i.e., they have to be executed in the same order for each process.
4 */
5
6#include<mpi.h>
7#include<stdlib.h>
8#include<assert.h>
9#include<stdio.h>
10
11int main(int argc, char * argv[])
12{
13 int rank;
14 int procs;
15 int* sendBuf;
16 int* rcvBuf;
17
18 MPI_Init(&argc,&argv);
19 MPI_Comm_size(MPI_COMM_WORLD, &procs);
20 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
21
22 if (rank == 0) {
23 sendBuf = (int*)malloc(sizeof(int)*procs);
24 rcvBuf = (int*)malloc(sizeof(int)*procs);
25 for(int i=0; i < procs; i++)
26 sendBuf[i] = i;
27 }else{
28 sendBuf = (int*)malloc(sizeof(int));
29 rcvBuf = (int*)malloc(sizeof(int));
30 }
31
32 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
33
34 if(rank != 0){
35 *sendBuf = *rcvBuf + rank;
36 }
37
38 MPI_Gather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
39
40 if(rank == 0){
41 for(int i=0; i<procs; i++){
42 printf("sendBuf[%d]=%d, rcvBuf[%d]=%d\n", i, sendBuf[i], i, rcvBuf[i]);
43 assert(sendBuf[i]*2 == rcvBuf[i]);
44 }
45 }
46 free(sendBuf);
47 free(rcvBuf);
48 return 0;
49}
Note: See TracBrowser for help on using the repository browser.