source: CIVL/examples/mpi/collective/scatterGather.c@ 2261cec

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

added tests for mpi colletive operations.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@2088 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()
12{
13 int argc;
14 char** argv;
15 int rank;
16 int procs;
17 int* sendBuf;
18 int* rcvBuf;
19
20 MPI_Init(&argc,&argv);
21 MPI_Comm_size(MPI_COMM_WORLD, &procs);
22 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23
24 if (rank == 0) {
25 sendBuf = (int*)malloc(sizeof(int)*procs);
26 rcvBuf = (int*)malloc(sizeof(int)*procs);
27 for(int i=0; i < procs; i++)
28 sendBuf[i] = i;
29 }else{
30 sendBuf = (int*)malloc(sizeof(int));
31 rcvBuf = (int*)malloc(sizeof(int));
32 }
33
34 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
35
36 if(rank != 0){
37 *sendBuf = *rcvBuf + rank;
38 }
39
40 MPI_Gather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
41
42 if(rank == 0){
43 for(int i=0; i<procs; i++){
44 printf("sendBuf[%d]=%d, rcvBuf[%d]=%d\n", i, sendBuf[i], i, rcvBuf[i]);
45 assert(sendBuf[i]*2 == rcvBuf[i]);
46 }
47 }
48 free(sendBuf);
49 free(rcvBuf);
50 return 0;
51}
Note: See TracBrowser for help on using the repository browser.