| 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 | @*/
|
|---|
| 14 | int broadcast(int * buf, int count,
|
|---|
| 15 | MPI_Datatype datatype, int root, MPI_Comm comm) {
|
|---|
| 16 | int nprocs, rank;
|
|---|
| 17 | int tag = 999;
|
|---|
| 18 |
|
|---|
| 19 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 20 | MPI_Comm_rank(comm, &rank);
|
|---|
| 21 | if (rank == root) {
|
|---|
| 22 | for (int i = 0; i < nprocs; i++)
|
|---|
| 23 | if (i != root)
|
|---|
| 24 | MPI_Send(buf, count, MPI_INT, i, tag, comm);
|
|---|
| 25 | } else
|
|---|
| 26 | MPI_Recv(buf, count, MPI_INT, root, tag, comm,
|
|---|
| 27 | MPI_STATUS_IGNORE);
|
|---|
| 28 | return 0;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | /*@
|
|---|
| 33 | @ \mpi_collective(comm, P2P) :
|
|---|
| 34 | @ requires \mpi_agree(root) && \mpi_agree(sendcount);
|
|---|
| 35 | @ requires sendcount > 0 && sendcount < 10;
|
|---|
| 36 | @ requires recvcount > 0 && recvcount < 10;
|
|---|
| 37 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 38 | @ requires \valid(sendbuf + (0 .. sendcount));
|
|---|
| 39 | @ behavior imroot:
|
|---|
| 40 | @ assumes \mpi_comm_rank == root;
|
|---|
| 41 | @ requires \valid(recvbuf + (0 .. recvcount * \mpi_comm_size));
|
|---|
| 42 | @ requires recvcount == sendcount;
|
|---|
| 43 | @ ensures \mpi_equals((recvbuf + root * sendcount), sendcount, MPI_INT, sendbuf);
|
|---|
| 44 | @ behavior imnroot:
|
|---|
| 45 | @ assumes \mpi_comm_rank != root;
|
|---|
| 46 | @ ensures \mpi_equals(sendbuf, sendcount, MPI_INT,
|
|---|
| 47 | @ (\remote(recvbuf, root) + \mpi_comm_rank * sendcount));
|
|---|
| 48 | @*/
|
|---|
| 49 | int gather(int* sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 50 | int* recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 51 | int root, MPI_Comm comm){
|
|---|
| 52 | int rank, nprocs;
|
|---|
| 53 | MPI_Status status;
|
|---|
| 54 | int tag = 998;
|
|---|
| 55 |
|
|---|
| 56 | MPI_Comm_rank(comm, &rank);
|
|---|
| 57 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 58 | if(root == rank) {
|
|---|
| 59 | void *ptr;
|
|---|
| 60 |
|
|---|
| 61 | ptr = $mpi_pointerAdd(recvbuf, root * recvcount, MPI_INT);
|
|---|
| 62 | $elaborate(recvcount);
|
|---|
| 63 | memcpy(ptr, sendbuf, recvcount * sizeofDatatype(MPI_INT));
|
|---|
| 64 | }else
|
|---|
| 65 | MPI_Send(sendbuf, sendcount, MPI_INT, root, tag, comm);
|
|---|
| 66 | if(rank == root) {
|
|---|
| 67 | int real_recvcount;
|
|---|
| 68 | int offset;
|
|---|
| 69 |
|
|---|
| 70 | for(int i=0; i<nprocs; i++) {
|
|---|
| 71 | if(i != root) {
|
|---|
| 72 | void * ptr;
|
|---|
| 73 |
|
|---|
| 74 | offset = i * recvcount;
|
|---|
| 75 | ptr = $mpi_pointerAdd(recvbuf, offset, MPI_INT);
|
|---|
| 76 | MPI_Recv(ptr, recvcount, MPI_INT, i, tag, comm,
|
|---|
| 77 | &status);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | return 0;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | int main() {
|
|---|
| 86 | broadcast(NULL, 0, MPI_INT, 0, (MPI_Comm)0);
|
|---|
| 87 | gather(NULL, 0, MPI_INT, NULL, 0, MPI_INT, 0, (MPI_Comm)0);
|
|---|
| 88 | return 0;
|
|---|
| 89 | }
|
|---|