source: CIVL/examples/contracts/contractsMPI/reduce_sum.c@ 98598d7f

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 98598d7f was 79068a6, checked in by Manchun Zheng <zmanchun@…>, 10 years ago

cleaned up the translation of expressions; fixed errors in reduce_sum.c

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

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[7f8124c]1#include<mpi.h>
[e01801f]2#include<civl-mpi.cvh>
[7f8124c]3#include<civlc.cvh>
4#include<string.h>
5#include<stdlib.h>
6
7/* A collective sum-reduction operation */
8
[e01801f]9/*@ \mpi_collective(comm, P2P):
10 @ requires count > 0;
[ace0430]11 @ requires datatype==MPI_INT;
[79068a6]12 @ requires 0<count && count*\mpi_extent(datatype) < 5;
[ace0430]13 @ requires \mpi_valid(sendbuf, count, datatype);
14 @ requires \mpi_valid(recvbuf, count, datatype);
[e01801f]15 @ requires \mpi_agree(root) && \mpi_agree(count);
16 @ requires 0 <= root && root < \mpi_comm_size;
[79068a6]17 @ ensures \forall integer i; 0<= i && i <count ==>
18 @ (int)recvbuf[i] == \sum(0, \mpi_comm_size,
19 @ \lambda int k; \on(k, ((int)sendbuf[i])));
[e01801f]20 @ waitsfor root;
21 @
22 @*/
23int reduce_sum(const void* sendbuf, void* recvbuf, MPI_Datatype datatype,
[7f8124c]24 int count, int root, MPI_Comm comm) {
25 int rank;
26
27 MPI_Comm_rank(comm, &rank);
28 if (rank != root)
29 MPI_Send(sendbuf, count, datatype, root, REDUCE_TAG, comm);
30 else {
31 int nprocs;
32 int size;
33 int * sum;
34
35 MPI_Comm_size(comm, &nprocs);
36 size = count * sizeof(int);
37 memcpy(recvbuf, sendbuf, size);
38 sum = (int *)malloc(sizeof(int) * count);
39 for (int i = 0; i<nprocs; i++) {
40 if (i != root){
41 MPI_Recv(recvbuf, count, datatype, i, REDUCE_TAG, comm, MPI_STATUS_IGNORE);
42
43 for (int i = 0; i < count; i++)
[ace0430]44 sum[i] = (int)sum[i] + (int)recvbuf[i];
[7f8124c]45 }
46 }
47 memcpy(recvbuf, sum, size);
48 }
49 return 0;
50}
Note: See TracBrowser for help on using the repository browser.