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

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

added tests for barrier and collective communication.

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

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#include<mpi.h>
2#include<stdio.h>
3
4int main(int argc, char* argv[]){
5 int rank;
6 int procs;
7 int localsum, sum;
8
9 MPI_Init(&argc,&argv);
10 MPI_Comm_size(MPI_COMM_WORLD, &procs);
11 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
12
13 localsum = 0;
14 for(int i=0; i<=rank; i++){
15 localsum += i;
16 }
17 printf("process %d has local sum of %d\n", rank, localsum);
18#ifdef ORDER
19 if(rank%2){
20 printf("process %d enters barrier\n", rank);
21 MPI_Barrier(MPI_COMM_WORLD);
22 printf("process %d exits barrier\n", rank);
23 MPI_Reduce(&localsum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
24 }else{
25 MPI_Reduce(&localsum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
26 printf("process %d enters barrier\n", rank);
27 MPI_Barrier(MPI_COMM_WORLD);
28 printf("process %d exits barrier\n", rank);
29 }
30#else
31 printf("process %d enters barrier\n", rank);
32 MPI_Barrier(MPI_COMM_WORLD);
33 printf("process %d exits barrier\n", rank);
34 MPI_Reduce(&localsum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
35#endif
36 if(rank == 0)
37 printf("total sum is %d\n", sum);
38 MPI_Finalize();
39 return 0;
40}
Note: See TracBrowser for help on using the repository browser.