source: CIVL/examples/contracts/contractsMPI/allgather.c@ 3c8cc6c

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 3c8cc6c was 1b69190, checked in by Ziqing Luo <ziqing@…>, 9 years ago

Merged from the contract branch. A contract transformer was added.

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

  • Property mode set to 100644
File size: 4.7 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#pragma PARSE_ACSL
10/*@
11 @ \mpi_collective(comm, P2P):
12 @ requires 0 <= root && root < \mpi_comm_size;
13 @ requires \mpi_agree(root) && \mpi_agree(count * \mpi_extent(datatype));
14 @ requires 0 <= count && count * \mpi_extent(datatype) < 5;
15 @ requires \mpi_valid(buf, count, datatype);
16 @ behavior root:
17 @ assumes \mpi_comm_rank == root;
18 @ behavior others:
19 @ assumes \mpi_comm_rank != root;
20 @ assigns \mpi_region(buf, count, datatype);
21 @ ensures \mpi_equals(\mpi_region(buf, count, datatype),
22 @ \mpi_region(\on(root, buf), count, datatype));
23 @ waitsfor root;
24 @*/
25int broadcast(void * buf, int count,
26 MPI_Datatype datatype, int root, MPI_Comm comm) {
27 int nprocs, rank;
28 int tag = 999;
29
30 MPI_Comm_size(comm, &nprocs);
31 MPI_Comm_rank(comm, &rank);
32 if (rank == root) {
33 for (int i = 0; i < nprocs; i++)
34 if (i != root)
35 MPI_Send(buf, count, datatype, i, tag, comm);
36 } else
37 MPI_Recv(buf, count, datatype, root, tag, comm,
38 MPI_STATUS_IGNORE);
39 return 0;
40}
41
42/*@
43 @ \mpi_collective(comm, P2P) :
44 @ requires \mpi_agree(root) && \mpi_agree(sendcount * \mpi_extent(sendtype));
45 @ requires sendcount * \mpi_extent(sendtype) >= 0 && sendcount * \mpi_extent(sendtype) < 5;
46 @ requires recvcount * \mpi_extent(recvtype) >= 0 && recvcount * \mpi_extent(recvtype) < 5;
47 @ requires 0 <= root && root < \mpi_comm_size;
48 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
49 @ behavior imroot:
50 @ assumes \mpi_comm_rank == root;
51 @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
52 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
53 @ requires recvcount * \mpi_extent(recvtype) ==
54 @ sendcount * \mpi_extent(sendtype);
55 @ ensures \mpi_equals(\mpi_region(\mpi_offset(recvbuf, root * sendcount, sendtype),
56 @ recvcount, recvtype),
57 @ \mpi_region(sendbuf, sendcount, sendtype)
58 @ );
59 @
60 @ waitsfor (0 .. \mpi_comm_size-1);
61 @ behavior imnroot:
62 @ assumes \mpi_comm_rank != root;
63 @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
64 @ \mpi_region(\mpi_offset(\on(root, recvbuf), \mpi_comm_rank * sendcount, sendtype),
65 @ recvcount, recvtype)
66 @ );
67 @*/
68int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
69 void* recvbuf, int recvcount, MPI_Datatype recvtype,
70 int root, MPI_Comm comm){
71 int rank, nprocs;
72 MPI_Status status;
73 int tag = 998;
74
75 MPI_Comm_rank(comm, &rank);
76 MPI_Comm_size(comm, &nprocs);
77 if(root == rank) {
78 void *ptr;
79
80 ptr = $mpi_pointer_add(recvbuf, root * recvcount, recvtype);
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 < 5;
105 @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) * \mpi_comm_size < 5;
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(\mpi_region(sendbuf, sendcount, sendtype),
112 @ \mpi_region(
113 @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount, recvtype),
114 @ recvcount, recvtype));
115 @
116 */
117int allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
118 void *recvbuf, int recvcount, MPI_Datatype recvtype,
119 MPI_Comm comm){
120 int place;
121 int nprocs;
122
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.