source: CIVL/examples/contracts/contractsMPI/reduce_sum.c@ ec2d85de

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

fix the transformation of waitsfor clauses

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@3631 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):
[a09800d]10 @ requires datatype == MPI_INT;
[79068a6]11 @ requires 0<count && count*\mpi_extent(datatype) < 5;
[ace0430]12 @ requires \mpi_valid(sendbuf, count, datatype);
13 @ requires \mpi_valid(recvbuf, count, datatype);
[a09800d]14 @ requires \mpi_agree(root) && \mpi_agree(count * datatype);
[e01801f]15 @ requires 0 <= root && root < \mpi_comm_size;
[c0518e21]16 @ behavior root:
17 @ assumes \mpi_comm_rank == root;
18 @ ensures \forall integer i; 0<= i && i <count ==>
[d196c2c]19 @ recvbuf[i] == \sum(0, \mpi_comm_size-1,
20 @ \lambda int k; \on(k, sendbuf)[i]);
[e01801f]21 @ waitsfor root;
22 @
23 @*/
[a09800d]24int reduce_sum(int * sendbuf, int * recvbuf, MPI_Datatype datatype,
[7f8124c]25 int count, int root, MPI_Comm comm) {
26 int rank;
27
28 MPI_Comm_rank(comm, &rank);
29 if (rank != root)
30 MPI_Send(sendbuf, count, datatype, root, REDUCE_TAG, comm);
31 else {
32 int nprocs;
33 int size;
34 int * sum;
35
36 MPI_Comm_size(comm, &nprocs);
37 size = count * sizeof(int);
38 sum = (int *)malloc(sizeof(int) * count);
[c0518e21]39 memcpy(sum, sendbuf, size);
[7f8124c]40 for (int i = 0; i<nprocs; i++) {
41 if (i != root){
42 MPI_Recv(recvbuf, count, datatype, i, REDUCE_TAG, comm, MPI_STATUS_IGNORE);
43
44 for (int i = 0; i < count; i++)
[a09800d]45 sum[i] = sum[i] + recvbuf[i];
[7f8124c]46 }
47 }
48 memcpy(recvbuf, sum, size);
[a09800d]49 free(sum);
[7f8124c]50 }
[a09800d]51
[7f8124c]52 return 0;
53}
Note: See TracBrowser for help on using the repository browser.