source: CIVL/examples/mpi/collective/BcastReduce2.c@ 75982de

1.23 2.0 main test-branch
Last change on this file since 75982de was 4ccacf8, checked in by Manchun Zheng <zmanchun@…>, 11 years ago

added some more MPI collective tests.

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

  • Property mode set to 100644
File size: 874 bytes
Line 
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
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);
22 if(rank != 5)
23 MPI_Reduce(&recv, &num, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
24 MPI_Allreduce(&num, &recv, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
25 printf("recv = %d\n", recv);
26 assert(recv == (3*nprocs*nprocs + 3*(nprocs-1)));
27 MPI_Finalize();
28 return 0;
29}
Note: See TracBrowser for help on using the repository browser.