source: CIVL/examples/contracts/contractsMPI/reduce_sum.c@ 17d3729

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 17d3729 was b114f32, checked in by Ziqing Luo <ziqing@…>, 10 years ago

commit progress

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

  • Property mode set to 100644
File size: 855 bytes
Line 
1#include<mpi.h>
2#include<civlc.cvh>
3#include<string.h>
4#include<stdlib.h>
5
6/* A collective sum-reduction operation */
7
8int reduce_sum(void* sendbuf, void* recvbuf, MPI_Datatype datatype,
9 int count, int root, MPI_Comm comm) {
10 int rank;
11 int REDUCE_TAG = 999;
12
13 MPI_Comm_rank(comm, &rank);
14 if (rank != root)
15 MPI_Send(sendbuf, count, datatype, root, REDUCE_TAG, comm);
16 else {
17 int nprocs;
18 int size;
19 int * sum;
20
21 MPI_Comm_size(comm, &nprocs);
22 size = count * sizeof(int);
23 memcpy(recvbuf, sendbuf, size);
24 sum = (int *)malloc(sizeof(int) * count);
25 for (int i = 0; i<nprocs; i++) {
26 if (i != root){
27 MPI_Recv(recvbuf, count, datatype, i, REDUCE_TAG, comm, MPI_STATUS_IGNORE);
28
29 for (int i = 0; i < count; i++)
30 sum[i] = sum[i] + recvbuf[i];
31 }
32 }
33 memcpy(recvbuf, sum, size);
34 }
35 return 0;
36}
Note: See TracBrowser for help on using the repository browser.