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

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[2d81ab2]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
[8f2c865]11int main(int argc, char * argv[])
[2d81ab2]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
[b12731c]32#ifdef ROOT
33 if(rank != 2)
34 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
35 else
36 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 2, MPI_COMM_WORLD);
37#elif defined ORDER
38 if(rank%2)
39 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
40 else
41 MPI_Gather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
42#else
[2d81ab2]43 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
[b12731c]44#endif
45
[2d81ab2]46 if(rank != 0){
47 *sendBuf = *rcvBuf + rank;
48 }
49
[b12731c]50#ifdef ORDER
51 if(rank%2)
52 MPI_Gather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
53 else
54 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
55#else
[2d81ab2]56 MPI_Gather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
[b12731c]57#endif
58
[2d81ab2]59 if(rank == 0){
60 for(int i=0; i<procs; i++){
61 printf("sendBuf[%d]=%d, rcvBuf[%d]=%d\n", i, sendBuf[i], i, rcvBuf[i]);
62 assert(sendBuf[i]*2 == rcvBuf[i]);
63 }
64 }
65 free(sendBuf);
66 free(rcvBuf);
[34f73ef]67 MPI_Finalize();
[2d81ab2]68 return 0;
69}
Note: See TracBrowser for help on using the repository browser.