source: CIVL/examples/contracts/contractsMPI/allgather2.c@ 552ed59

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

commit progress

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

  • Property mode set to 100644
File size: 3.9 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 <= root && root < \mpi_comm_size;
13 @ requires \mpi_agree(root) && \mpi_agree(count * \mpi_extent(datatype));
14 @ requires 0 <= count && count * \mpi_extent(datatype) < 10;
15 @ requires \mpi_valid(buf, count, datatype);
16 @ ensures \mpi_equals(buf, count, datatype, \on(root, buf));
17 @ waitsfor (root .. root);
18 @*/
19int broadcast(void * buf, int count,
20 MPI_Datatype datatype, int root, MPI_Comm comm) {
21 int nprocs, rank;
22 int tag = 999;
23
24 MPI_Comm_size(comm, &nprocs);
25 MPI_Comm_rank(comm, &rank);
26 if (rank == root) {
27 for (int i = 0; i < nprocs; i++)
28 if (i != root)
29 MPI_Send(buf, count, datatype, i, tag, comm);
30 } else
31 MPI_Recv(buf, count, datatype, root, tag, comm,
32 MPI_STATUS_IGNORE);
33 return 0;
34}
35
36/*@
37 @ \mpi_collective(comm, P2P) :
38 @ requires \mpi_agree(root) && \mpi_agree(sendcount * \mpi_extent(sendtype));
39 @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) < 10;
40 @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) < 10;
41 @ requires 0 <= root && root < \mpi_comm_size;
42 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
43 @ behavior imroot:
44 @ assumes \mpi_comm_rank == root;
45 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
46 @ requires recvcount * \mpi_extent(recvtype) ==
47 @ sendcount * \mpi_extent(sendtype);
48 @ ensures \mpi_equals(\mpi_offset(recvbuf, root * sendcount, sendtype), sendcount, sendtype, sendbuf);
49 @ behavior imnroot:
50 @ assumes \mpi_comm_rank != root;
51 @ ensures \mpi_equals(sendbuf, sendcount, sendtype,
52 @ \mpi_offset(\on(root, recvbuf), \mpi_comm_rank * sendcount, sendtype));
53 @*/
54int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
55 void* recvbuf, int recvcount, MPI_Datatype recvtype,
56 int root, MPI_Comm comm){
57 int rank, nprocs;
58 MPI_Status status;
59 int tag = 998;
60
61 MPI_Comm_rank(comm, &rank);
62 MPI_Comm_size(comm, &nprocs);
63 if(root == rank) {
64 void *ptr;
65
66 ptr = $mpi_pointer_add(recvbuf, root * recvcount, recvtype);
67 $elaborate(recvcount);
68 memcpy(ptr, sendbuf, recvcount * sizeofDatatype(recvtype));
69 }else
70 MPI_Send(sendbuf, sendcount, sendtype, root, tag, comm);
71 if(rank == root) {
72 int real_recvcount;
73 int offset;
74
75 for(int i=0; i<nprocs; i++) {
76 if(i != root) {
77 void * ptr;
78
79 offset = i * recvcount;
80 ptr = $mpi_pointer_add(recvbuf, offset, recvtype);
81 MPI_Recv(ptr, recvcount, recvtype, i, tag, comm,
82 &status);
83 }
84 }
85 }
86 return 0;
87}
88
89/*@ \mpi_collective(comm, P2P):
90 @ requires \mpi_agree(sendcount * \mpi_extent(sendtype));
91 @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) * \mpi_comm_size < 5;
92 @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) * \mpi_comm_size < 5;
93 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
94 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
95 @ requires \mpi_extent(recvtype) * recvcount == \mpi_extent(sendtype) * sendcount;
96 @ //assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
97 @ ensures \mpi_agree(\mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype));
98 @ ensures \mpi_equals(sendbuf, sendcount, sendtype,
99 @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount, recvtype));
100 @
101 */
102int allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
103 void *recvbuf, int recvcount, MPI_Datatype recvtype,
104 MPI_Comm comm){
105 int place;
106 int nprocs;
107
108 MPI_Comm_rank(comm, &place);
109 MPI_Comm_size(comm, &nprocs);
110 gather(sendbuf, sendcount, sendtype,
111 recvbuf, recvcount, recvtype,
112 0, comm);
113 broadcast(recvbuf, recvcount*nprocs, recvtype, 0, comm);
114 return 0;
115}
Note: See TracBrowser for help on using the repository browser.