| 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 | int nprocs, rank;
|
|---|
| 25 | int tag = 999;
|
|---|
| 26 |
|
|---|
| 27 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 28 | MPI_Comm_rank(comm, &rank);
|
|---|
| 29 | if (rank == root) {
|
|---|
| 30 | for (int i = 0; i < nprocs; i++)
|
|---|
| 31 | if (i != root)
|
|---|
| 32 | MPI_Send(buf, count, datatype, i, tag, comm);
|
|---|
| 33 | } else
|
|---|
| 34 | MPI_Recv(buf, count, datatype, root, tag, comm,
|
|---|
| 35 | MPI_STATUS_IGNORE);
|
|---|
| 36 | return 0;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /*@
|
|---|
| 40 | @ \mpi_collective(comm, P2P) :
|
|---|
| 41 | @ requires \mpi_agree(root) && \mpi_agree(sendcount * \mpi_extent(sendtype));
|
|---|
| 42 | @ requires sendcount * \mpi_extent(sendtype) >= 0 && sendcount * \mpi_extent(sendtype) < DATA_LIMIT;
|
|---|
| 43 | @ requires recvcount * \mpi_extent(recvtype) >= 0 && recvcount * \mpi_extent(recvtype) < DATA_LIMIT;
|
|---|
| 44 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 45 | @ requires \mpi_valid(sendbuf, sendcount, sendtype);
|
|---|
| 46 | @ behavior imroot:
|
|---|
| 47 | @ assumes \mpi_comm_rank == root;
|
|---|
| 48 | @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 49 | @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 50 | @ requires recvcount * \mpi_extent(recvtype) ==
|
|---|
| 51 | @ sendcount * \mpi_extent(sendtype);
|
|---|
| 52 | @ ensures \mpi_equals(\mpi_region(\mpi_offset(recvbuf, root * sendcount, sendtype),
|
|---|
| 53 | @ recvcount, recvtype),
|
|---|
| 54 | @ \mpi_region(sendbuf, sendcount, sendtype)
|
|---|
| 55 | @ );
|
|---|
| 56 | @
|
|---|
| 57 | @ waitsfor (0 .. \mpi_comm_size-1);
|
|---|
| 58 | @ behavior imnroot:
|
|---|
| 59 | @ assumes \mpi_comm_rank != root;
|
|---|
| 60 | @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
|
|---|
| 61 | @ \mpi_region(\mpi_offset(\on(root, recvbuf),
|
|---|
| 62 | @ \mpi_comm_rank * sendcount, sendtype),
|
|---|
| 63 | @ sendcount, sendtype)
|
|---|
| 64 | @ );
|
|---|
| 65 | @*/
|
|---|
| 66 | int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 67 | void* recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 68 | int root, MPI_Comm comm){
|
|---|
| 69 | int rank, nprocs;
|
|---|
| 70 | MPI_Status status;
|
|---|
| 71 | int tag = 998;
|
|---|
| 72 |
|
|---|
| 73 | MPI_Comm_rank(comm, &rank);
|
|---|
| 74 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 75 | if(root == rank) {
|
|---|
| 76 | void *ptr;
|
|---|
| 77 |
|
|---|
| 78 | ptr = $mpi_pointer_add(recvbuf, root * recvcount, recvtype);
|
|---|
| 79 | memcpy(ptr, sendbuf, recvcount * sizeofDatatype(recvtype));
|
|---|
| 80 | }else
|
|---|
| 81 | MPI_Send(sendbuf, sendcount, sendtype, root, tag, comm);
|
|---|
| 82 | if(rank == root) {
|
|---|
| 83 | int real_recvcount;
|
|---|
| 84 | int offset;
|
|---|
| 85 |
|
|---|
| 86 | for(int i=0; i<nprocs; i++) {
|
|---|
| 87 | if(i != root) {
|
|---|
| 88 | void * ptr;
|
|---|
| 89 |
|
|---|
| 90 | offset = i * recvcount;
|
|---|
| 91 | ptr = $mpi_pointer_add(recvbuf, offset, recvtype);
|
|---|
| 92 | MPI_Recv(ptr, recvcount, recvtype, i, tag, comm,
|
|---|
| 93 | &status);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | return 0;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /*@ \mpi_collective(comm, P2P):
|
|---|
| 101 | @ requires \mpi_agree(sendcount * \mpi_extent(sendtype));
|
|---|
| 102 | @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) < DATA_LIMIT;
|
|---|
| 103 | @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) < DATA_LIMIT;
|
|---|
| 104 | @ requires \mpi_valid(sendbuf, sendcount, sendtype);
|
|---|
| 105 | @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 106 | @ requires \mpi_extent(recvtype) * recvcount == \mpi_extent(sendtype) * sendcount;
|
|---|
| 107 | @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 108 | @ ensures \mpi_agree(\mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype));
|
|---|
| 109 | @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
|
|---|
| 110 | @ \mpi_region(
|
|---|
| 111 | @ \mpi_offset(recvbuf, \mpi_comm_rank * recvcount, recvtype),
|
|---|
| 112 | @ recvcount, recvtype));
|
|---|
| 113 | @
|
|---|
| 114 | */
|
|---|
| 115 | int allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 116 | void *recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 117 | MPI_Comm comm){
|
|---|
| 118 | int place;
|
|---|
| 119 | int nprocs;
|
|---|
| 120 |
|
|---|
| 121 | MPI_Comm_rank(comm, &place);
|
|---|
| 122 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 123 | gather(sendbuf, sendcount, sendtype,
|
|---|
| 124 | recvbuf, recvcount, recvtype,
|
|---|
| 125 | 0, comm);
|
|---|
| 126 | broadcast(recvbuf, recvcount*nprocs, recvtype, 0, comm);
|
|---|
| 127 | return 0;
|
|---|
| 128 | }
|
|---|