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

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

added more tests for mpi collective operations

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

  • Property mode set to 100644
File size: 1.1 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 }
32 rcvBuf = (int*)malloc(sizeof(int)*procs);
33
34 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
35
36 *sendBuf = *rcvBuf + rank;
37
38 MPI_Allgather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, MPI_COMM_WORLD);
39
40 printf("receive buffer of process %d:\n", rank);
41 for(int i=0; i<procs; i++){
42 printf("rcvBuf[%d]=%d\n", i, rcvBuf[i]);
43 assert(i*2 == rcvBuf[i]);
44 }
45 free(sendBuf);
46 free(rcvBuf);
47 return 0;
48}
Note: See TracBrowser for help on using the repository browser.