| 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 DATA_LIMIT 1024
|
|---|
| 9 | #pragma CIVL 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) < DATA_LIMIT;
|
|---|
| 15 | @ requires \mpi_valid(buf, count, datatype);
|
|---|
| 16 | @ ensures \mpi_agree(\mpi_region(buf, count, datatype));
|
|---|
| 17 | @ behavior nonroot:
|
|---|
| 18 | @ assumes \mpi_comm_rank != root;
|
|---|
| 19 | @ assigns \mpi_region(buf, count, datatype);
|
|---|
| 20 | @ waitsfor root;
|
|---|
| 21 | @*/
|
|---|
| 22 | int broadcast(void * buf, int count,
|
|---|
| 23 | MPI_Datatype datatype, int root, MPI_Comm comm);
|
|---|
| 24 |
|
|---|
| 25 | /*@
|
|---|
| 26 | @ \mpi_collective(comm, P2P) :
|
|---|
| 27 | @ requires \mpi_agree(root) && \mpi_agree(sendcount * \mpi_extent(sendtype));
|
|---|
| 28 | @ requires sendcount * \mpi_extent(sendtype) >= 0 && sendcount * \mpi_extent(sendtype) < DATA_LIMIT;
|
|---|
| 29 | @ requires recvcount * \mpi_extent(recvtype) >= 0 && recvcount * \mpi_extent(recvtype) < DATA_LIMIT;
|
|---|
| 30 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 31 | @ requires \mpi_valid(sendbuf, sendcount, sendtype);
|
|---|
| 32 | @ behavior imroot:
|
|---|
| 33 | @ assumes \mpi_comm_rank == root;
|
|---|
| 34 | @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 35 | @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 36 | @ requires recvcount * \mpi_extent(recvtype) ==
|
|---|
| 37 | @ sendcount * \mpi_extent(sendtype);
|
|---|
| 38 | @ ensures \mpi_equals(\mpi_region(\mpi_offset(recvbuf, root * sendcount, sendtype),
|
|---|
| 39 | @ recvcount, recvtype),
|
|---|
| 40 | @ \mpi_region(sendbuf, sendcount, sendtype)
|
|---|
| 41 | @ );
|
|---|
| 42 | @
|
|---|
| 43 | @ waitsfor (0 .. \mpi_comm_size-1);
|
|---|
| 44 | @ behavior imnroot:
|
|---|
| 45 | @ assumes \mpi_comm_rank != root;
|
|---|
| 46 | @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
|
|---|
| 47 | @ \mpi_region(\mpi_offset(\on(root, recvbuf),
|
|---|
| 48 | @ \mpi_comm_rank * sendcount, sendtype),
|
|---|
| 49 | @ sendcount, sendtype)
|
|---|
| 50 | @ );
|
|---|
| 51 | @*/
|
|---|
| 52 | int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 53 | void* recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 54 | int root, MPI_Comm comm);
|
|---|
| 55 |
|
|---|
| 56 | /*@ \mpi_collective(comm, P2P):
|
|---|
| 57 | @ requires \mpi_agree(sendcount * \mpi_extent(sendtype));
|
|---|
| 58 | @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) * \mpi_comm_size < DATA_LIMIT;
|
|---|
| 59 | @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) * \mpi_comm_size < DATA_LIMIT;
|
|---|
| 60 | @ requires \mpi_valid(sendbuf, sendcount, sendtype);
|
|---|
| 61 | @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 62 | @ requires \mpi_extent(recvtype) * recvcount == \mpi_extent(sendtype) * sendcount;
|
|---|
| 63 | @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 64 | @ ensures \mpi_agree(\mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype));
|
|---|
| 65 | @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
|
|---|
| 66 | @ \mpi_region(
|
|---|
| 67 | @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount + 1, recvtype),
|
|---|
| 68 | @ recvcount, recvtype));
|
|---|
| 69 | @
|
|---|
| 70 | */
|
|---|
| 71 | int allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 72 | void *recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 73 | MPI_Comm comm){
|
|---|
| 74 | int place;
|
|---|
| 75 | int nprocs;
|
|---|
| 76 |
|
|---|
| 77 | MPI_Comm_rank(comm, &place);
|
|---|
| 78 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 79 | gather(sendbuf, sendcount, sendtype,
|
|---|
| 80 | recvbuf, recvcount, recvtype,
|
|---|
| 81 | 0, comm);
|
|---|
| 82 | broadcast(recvbuf, recvcount*nprocs, recvtype, 0, comm);
|
|---|
| 83 | return 0;
|
|---|
| 84 | }
|
|---|