source: CIVL/examples/contracts/contractsMPI/allgather.c@ 8b2639d

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

implement the contract transformer

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

  • Property mode set to 100644
File size: 4.4 KB
Line 
1// How to get rid of BOUNDS ?
2/************************** source code **************************/
3#include<mpi.h>
4#include<civl-mpi.cvh>
5#include<civlc.cvh>
6#include<string.h>
7
8#define BUFFER_BOUND 3
9
10/*@
11 @ \mpi_collective(comm, P2P):
12 @ requires 0 < count && count <= BUFFER_BOUND;
13 @ requires \mpi_valid(buf, count, datatype);
14 @ requires 0 <= root && root < \mpi_comm_size;
15 @ requires \mpi_agree(root) && \mpi_agree(count * \mpi_extent(datatype));
16 @ ensures \mpi_equals(buf, count, datatype, \on(root, buf));
17 @ ensures \result == 0;
18 @ behavior on_root:
19 @ assumes \mpi_comm_rank == root;
20 @ assigns \nothing;
21 @ behavior on_nonroot:
22 @ assumes \mpi_comm_rank != root;
23 @ assigns \mpi_region(buf, count, datatype);
24 @ waitsfor root;
25 @*/
26int broadcast(void * buf, int count,
27 MPI_Datatype datatype, int root, MPI_Comm comm) {
28 int nprocs, rank;
29 int tag = 999;
30
31 MPI_Comm_size(comm, &nprocs);
32 MPI_Comm_rank(comm, &rank);
33 if (rank == root) {
34 for (int i = 0; i < nprocs; i++)
35 if (i != root)
36 MPI_Send(buf, count, datatype, i, tag, comm);
37 } else
38 MPI_Recv(buf, count, datatype, root, tag, comm,
39 MPI_STATUS_IGNORE);
40 return 0;
41}
42
43
44/*@
45 @ \mpi_collective(comm, P2P) :
46 @ requires \mpi_agree(root) && \mpi_agree(\mpi_extent(sendtype) * sendcount);
47 @ requires sendcount > 0 && sendcount < BUFFER_BOUND;
48 @ requires recvcount > 0 && recvcount < BUFFER_BOUND;
49 @ requires 0 <= root && root < \mpi_comm_size;
50 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
51 @ behavior on_root:
52 @ assumes \mpi_comm_rank == root;
53 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
54 @ requires recvcount * \mpi_extent(recvtype) ==
55 @ sendcount * \mpi_extent(sendtype);
56 @ assigns \mpi_region(recvbuf, recvcount, recvtype);
57 @ ensures \mpi_equals(\mpi_offset(recvbuf, root * sendcount, sendtype),
58 @ sendcount, sendtype, sendbuf);
59 @ ensures \forall int i; i >= 0 && i < \mpi_comm_size ==>
60 @ \mpi_equals(\mpi_offset(recvbuf, i * sendcount, recvtype),
61 @ sendcount, sendtype, \on(i, sendbuf));
62 @ waitsfor (0 .. \mpi_comm_size-1);
63 @ behavior on_nonroot:
64 @ assumes \mpi_comm_rank != root;
65 @ assigns \nothing;
66 @*/
67int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
68 void* recvbuf, int recvcount, MPI_Datatype recvtype,
69 int root, MPI_Comm comm){
70 int rank, nprocs;
71 MPI_Status status;
72 int tag = 998;
73
74 MPI_Comm_rank(comm, &rank);
75 MPI_Comm_size(comm, &nprocs);
76 if(root == rank) {
77 void *ptr;
78
79 ptr = $mpi_pointer_add(recvbuf, root * recvcount, recvtype);
80 $elaborate(recvcount);
81 memcpy(ptr, sendbuf, recvcount * sizeofDatatype(recvtype));
82 }else
83 MPI_Send(sendbuf, sendcount, sendtype, root, tag, comm);
84 if(rank == root) {
85 int real_recvcount;
86 int offset;
87
88 for(int i=0; i<nprocs; i++) {
89 if(i != root) {
90 void * ptr;
91
92 offset = i * recvcount;
93 ptr = $mpi_pointer_add(recvbuf, offset, recvtype);
94 MPI_Recv(ptr, recvcount, recvtype, i, tag, comm,
95 &status);
96 }
97 }
98 }
99 return 0;
100}
101
102/*@ \mpi_collective(comm, P2P):
103 @ requires \mpi_agree(sendcount * \mpi_extent(sendtype));
104 @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) * \mpi_comm_size < BUFFER_BOUND;
105 @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) * \mpi_comm_size < BUFFER_BOUND;
106 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
107 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
108 @ requires \mpi_extent(recvtype) * recvcount == \mpi_extent(sendtype) * sendcount;
109 @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
110 @ ensures \mpi_agree(\mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype));
111 @ ensures \mpi_equals(sendbuf, sendcount, sendtype,
112 @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount, recvtype));
113 @
114 */
115int allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
116 void *recvbuf, int recvcount, MPI_Datatype recvtype,
117 MPI_Comm comm){
118 int place;
119 int nprocs;
120
121 $elaborate(recvcount);
122 $elaborate(sendcount);
123 MPI_Comm_rank(comm, &place);
124 MPI_Comm_size(comm, &nprocs);
125 gather(sendbuf, sendcount, sendtype,
126 recvbuf, recvcount, recvtype,
127 0, comm);
128 broadcast(recvbuf, recvcount*nprocs, recvtype, 0, comm);
129 return 0;
130}
Note: See TracBrowser for help on using the repository browser.