source: CIVL/examples/mpi/collective/BcastReduce2.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: 843 bytes
RevLine 
[4ccacf8]1/**
2 * This example demonstrates the usage of MPI collective operations,
3 * which should be called in the same orders for all MPI processes.
4 * This example has an error when there are more than five MPI processes.
5 */
6
[ec2d23f2]7#include <mpi.h>
8#include <assert.h>
9#include <stdio.h>
10
11int main(int argc, char * argv[]) {
12 int nprocs, rank;
13 int num;
14 int recv;
15
16 MPI_Init(&argc, &argv);
17 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
18 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
19 if(rank == 0) num = 3;
20 MPI_Bcast(&num, 1, MPI_INT, 0, MPI_COMM_WORLD);
21 MPI_Allreduce(&num, &recv, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
[4ccacf8]22 if(rank != 5)
23 MPI_Reduce(&recv, &num, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
[ec2d23f2]24 MPI_Allreduce(&num, &recv, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
25 assert(recv == (3*nprocs*nprocs + 3*(nprocs-1)));
26 MPI_Finalize();
27 return 0;
28}
Note: See TracBrowser for help on using the repository browser.