source: CIVL/examples/contracts/contractsMPI/civl_mpi_collectives/allgather.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 6.1 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#pragma CIVL ACSL
9/*@
10 @ \mpi_collective(comm, P2P):
11 @ requires 0 <= root && root < \mpi_comm_size;
12 @ requires \mpi_agree(root) && \mpi_agree(count * \mpi_extent(datatype));
13 @ requires 0 <= count && 0 <= count * \mpi_extent(datatype);
14 @ requires \mpi_valid(buf, count, datatype);
15 @ ensures \mpi_agree(\mpi_region(buf, count, datatype));
16 @ behavior nonroot:
17 @ assumes \mpi_comm_rank != root;
18 @ assigns \mpi_region(buf, count, datatype);
19 @ behavior root:
20 @ assumes \mpi_comm_rank == root;
21 @ assigns \nothing;
22 @ waitsfor root;
23 @*/
24int broadcast(void * buf, int count,
25 MPI_Datatype datatype, int root, MPI_Comm comm) {
26 int nprocs, rank;
27 int tag = 999;
28
29 MPI_Comm_size(comm, &nprocs);
30 MPI_Comm_rank(comm, &rank);
31 if (rank == root) {
32 for (int i = 0; i < nprocs; i++)
33 if (i != root)
34 MPI_Send(buf, count, datatype, i, tag, comm);
35 } else
36 MPI_Recv(buf, count, datatype, root, tag, comm,
37 MPI_STATUS_IGNORE);
38 return 0;
39}
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;
46 @ requires recvcount * \mpi_extent(recvtype) >= 0;
47 @ requires 0 <= root && root < \mpi_comm_size;
48 @ behavior imroot_not_inplace:
49 @ assumes \mpi_comm_rank == root && sendbuf != MPI_IN_PLACE;
50 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
51 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
52 @ requires recvcount * \mpi_extent(recvtype) == sendcount * \mpi_extent(sendtype);
53 @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
54 @ ensures \mpi_equals(\mpi_region(\mpi_offset(recvbuf, root * recvcount, recvtype), recvcount, recvtype),
55 @ \mpi_region(sendbuf, sendcount, sendtype));
56 @ waitsfor (0 .. \mpi_comm_size-1);
57 @ behavior imroot_inplace:
58 @ assumes \mpi_comm_rank == root && sendbuf == MPI_IN_PLACE;
59 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
60 @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
61 @ ensures \mpi_equals(\old(
62 @ \mpi_region(\mpi_offset(recvbuf, root * recvcount, recvtype), recvcount, recvtype)),
63 @ \mpi_region(\mpi_offset(recvbuf, root * recvcount, recvtype), recvcount, recvtype));
64 @ waitsfor (0 .. \mpi_comm_size-1);
65 @ behavior not_root:
66 @ assumes \mpi_comm_rank != root;
67 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
68 @ requires \on(root, recvcount * \mpi_extent(recvtype)) == sendcount * \mpi_extent(sendtype);
69 @ assigns \nothing;
70 @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
71 @ \mpi_region(\mpi_offset(\on(root, recvbuf), \mpi_comm_rank * sendcount, sendtype), sendcount, sendtype));
72 @*/
73int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
74 void* recvbuf, int recvcount, MPI_Datatype recvtype,
75 int root, MPI_Comm comm){
76 int rank, nprocs;
77 MPI_Status status;
78 int tag = 998;
79
80 MPI_Comm_rank(comm, &rank);
81 MPI_Comm_size(comm, &nprocs);
82 if(root == rank) {
83 if(sendbuf != MPI_IN_PLACE) {
84 void *ptr;
85
86 ptr = $mpi_pointer_add(recvbuf, root * recvcount, recvtype);
87 memcpy(ptr, sendbuf, recvcount * sizeofDatatype(recvtype));
88 }
89 } else
90 MPI_Send(sendbuf, sendcount, sendtype, root, tag, comm);
91 if(rank == root) {
92 int real_recvcount;
93 int offset;
94
95 for(int i=0; i<nprocs; i++) {
96 if(i != root) {
97 void * ptr;
98
99 offset = i * recvcount;
100 ptr = $mpi_pointer_add(recvbuf, offset, recvtype);
101 MPI_Recv(ptr, recvcount, recvtype, i, tag, comm,
102 &status);
103 }
104 }
105 }
106 return 0;
107}
108
109
110/*@ \mpi_collective(comm, P2P):
111 @ requires \mpi_agree(sendcount * \mpi_extent(sendtype));
112 @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) * \mpi_comm_size >= 0;
113 @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) * \mpi_comm_size >= 0;
114 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
115 @ requires \mpi_extent(recvtype) * recvcount == \mpi_extent(sendtype) * sendcount;
116 @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
117 @ ensures \mpi_agree(\mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype));
118 @ behavior not_inplace:
119 @ assumes sendbuf != MPI_IN_PLACE;
120 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
121 @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
122 @ \mpi_region(
123 @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount, recvtype),
124 @ recvcount, recvtype));
125 @ behavior inplace:
126 @ assumes sendbuf == MPI_IN_PLACE;
127 @ requires \mpi_agree(sendbuf == MPI_IN_PLACE);
128 @ ensures \mpi_equals(\old(\mpi_region(
129 @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount, recvtype),
130 @ recvcount, recvtype)),
131 @ \mpi_region(
132 @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount, recvtype),
133 @ recvcount, recvtype));
134 @
135 @*/
136int allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
137 void *recvbuf, int recvcount, MPI_Datatype recvtype,
138 MPI_Comm comm){
139 int place;
140 int nprocs;
141
142 MPI_Comm_rank(comm, &place);
143 MPI_Comm_size(comm, &nprocs);
144 if (sendbuf != MPI_IN_PLACE) {
145 gather(sendbuf, sendcount, sendtype,
146 recvbuf, recvcount, recvtype,
147 0, comm);
148 } else {
149 void * buf;
150
151 if (place == 0)
152 buf = MPI_IN_PLACE;
153 else
154 buf = $mpi_pointer_add(recvbuf, recvcount * place, recvtype);
155 gather(buf, recvcount, recvtype,
156 recvbuf, recvcount, recvtype,
157 0, comm);
158 }
159 broadcast(recvbuf, recvcount*nprocs, recvtype, 0, comm);
160 return 0;
161}
Note: See TracBrowser for help on using the repository browser.