source: CIVL/examples/contracts/contractsMPI/reduce_sum.c@ 3e02d5cb

1.23 2.0 main test-branch
Last change on this file since 3e02d5cb was a09800d, checked in by Ziqing Luo <ziqing@…>, 10 years ago

change reduce_sum test and improve contract transformer

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

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