source: CIVL/examples/mpi/collective/scatterAllgather.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.2 KB
RevLine 
[5b67e5b]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[])
[5b67e5b]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 }
30 rcvBuf = (int*)malloc(sizeof(int)*procs);
31
32 MPI_Scatter(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, 0, MPI_COMM_WORLD);
33
34 *sendBuf = *rcvBuf + rank;
35
36 MPI_Allgather(sendBuf, 1, MPI_INT, rcvBuf, 1, MPI_INT, MPI_COMM_WORLD);
37
38 printf("receive buffer of process %d:\n", rank);
39 for(int i=0; i<procs; i++){
40 printf("rcvBuf[%d]=%d\n", i, rcvBuf[i]);
41 assert(i*2 == rcvBuf[i]);
42 }
43 free(sendBuf);
44 free(rcvBuf);
[34f73ef]45 MPI_Finalize();
[5b67e5b]46 return 0;
47}
Note: See TracBrowser for help on using the repository browser.