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