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
RevLine 
[68c784e]1// How to get rid of BOUNDS ?
2/************************** source code **************************/
[537f004]3#include<mpi.h>
4#include<civl-mpi.cvh>
5#include<civlc.cvh>
6#include<string.h>
7
[68c784e]8#define BUFFER_BOUND 3
[1b69190]9#pragma PARSE_ACSL
[537f004]10/*@
11 @ \mpi_collective(comm, P2P):
12 @ requires 0 <= root && root < \mpi_comm_size;
[68c784e]13 @ requires \mpi_agree(root) && \mpi_agree(count * \mpi_extent(datatype));
[5aff938]14 @ requires 0 <= count && count * \mpi_extent(datatype) < 5;
[bd0cedf]15 @ requires \mpi_valid(buf, count, datatype);
16 @ behavior root:
[68c784e]17 @ assumes \mpi_comm_rank == root;
[bd0cedf]18 @ behavior others:
[68c784e]19 @ assumes \mpi_comm_rank != root;
[bd0cedf]20 @ assigns \mpi_region(buf, count, datatype);
[1b69190]21 @ ensures \mpi_equals(\mpi_region(buf, count, datatype),
22 @ \mpi_region(\on(root, buf), count, datatype));
[c0518e21]23 @ waitsfor root;
[537f004]24 @*/
[68c784e]25int broadcast(void * buf, int count,
[537f004]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)
[68c784e]35 MPI_Send(buf, count, datatype, i, tag, comm);
[537f004]36 } else
[68c784e]37 MPI_Recv(buf, count, datatype, root, tag, comm,
[537f004]38 MPI_STATUS_IGNORE);
39 return 0;
40}
41
42/*@
43 @ \mpi_collective(comm, P2P) :
[bd0cedf]44 @ requires \mpi_agree(root) && \mpi_agree(sendcount * \mpi_extent(sendtype));
[5aff938]45 @ requires sendcount * \mpi_extent(sendtype) >= 0 && sendcount * \mpi_extent(sendtype) < 5;
46 @ requires recvcount * \mpi_extent(recvtype) >= 0 && recvcount * \mpi_extent(recvtype) < 5;
[537f004]47 @ requires 0 <= root && root < \mpi_comm_size;
[68c784e]48 @ requires \mpi_valid(sendbuf, sendcount, sendtype);
[bd0cedf]49 @ behavior imroot:
[537f004]50 @ assumes \mpi_comm_rank == root;
[bd0cedf]51 @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
[68c784e]52 @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
53 @ requires recvcount * \mpi_extent(recvtype) ==
54 @ sendcount * \mpi_extent(sendtype);
[1b69190]55 @ ensures \mpi_equals(\mpi_region(\mpi_offset(recvbuf, root * sendcount, sendtype),
56 @ recvcount, recvtype),
57 @ \mpi_region(sendbuf, sendcount, sendtype)
58 @ );
59 @
[a0317b4]60 @ waitsfor (0 .. \mpi_comm_size-1);
[bd0cedf]61 @ behavior imnroot:
[1b69190]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 @ );
[537f004]67 @*/
[68c784e]68int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
69 void* recvbuf, int recvcount, MPI_Datatype recvtype,
[537f004]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
[68c784e]80 ptr = $mpi_pointer_add(recvbuf, root * recvcount, recvtype);
81 memcpy(ptr, sendbuf, recvcount * sizeofDatatype(recvtype));
[537f004]82 }else
[68c784e]83 MPI_Send(sendbuf, sendcount, sendtype, root, tag, comm);
[537f004]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;
[68c784e]93 ptr = $mpi_pointer_add(recvbuf, offset, recvtype);
94 MPI_Recv(ptr, recvcount, recvtype, i, tag, comm,
[537f004]95 &status);
96 }
97 }
98 }
99 return 0;
100}
101
[a0317b4]102/*@ \mpi_collective(comm, P2P):
[68c784e]103 @ requires \mpi_agree(sendcount * \mpi_extent(sendtype));
[bd0cedf]104 @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) * \mpi_comm_size < 5;
105 @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) * \mpi_comm_size < 5;
[68c784e]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;
[214ffce]109 @ //assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
[68c784e]110 @ ensures \mpi_agree(\mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype));
[1b69190]111 @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
112 @ \mpi_region(
113 @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount, recvtype),
114 @ recvcount, recvtype));
[a0317b4]115 @
116 */
[68c784e]117int allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
118 void *recvbuf, int recvcount, MPI_Datatype recvtype,
[a0317b4]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.