| [537f004] | 1 | #include<mpi.h>
|
|---|
| 2 | #include<civl-mpi.cvh>
|
|---|
| 3 | #include<civlc.cvh>
|
|---|
| 4 | #include<string.h>
|
|---|
| 5 | #include<stdio.h>
|
|---|
| 6 |
|
|---|
| 7 | /*@
|
|---|
| 8 | @ \mpi_collective(comm, P2P) :
|
|---|
| 9 | @ requires \mpi_agree(root) && \mpi_agree(sendcount);
|
|---|
| 10 | @ requires sendcount > 0 && sendcount < 10;
|
|---|
| 11 | @ requires recvcount > 0 && recvcount < 10;
|
|---|
| 12 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 13 | @ requires \valid(sendbuf + (0 .. sendcount));
|
|---|
| 14 | @ behavior imroot:
|
|---|
| 15 | @ assumes \mpi_comm_rank == root;
|
|---|
| 16 | @ requires \valid(recvbuf + (0 .. recvcount * \mpi_comm_size));
|
|---|
| 17 | @ requires recvcount == sendcount;
|
|---|
| 18 | @ ensures \mpi_equals((recvbuf + root * sendcount), sendcount, MPI_INT, sendbuf);
|
|---|
| 19 | @ behavior imnroot:
|
|---|
| 20 | @ assumes \mpi_comm_rank != root;
|
|---|
| 21 | @ ensures \mpi_equals(sendbuf, sendcount, MPI_INT,
|
|---|
| 22 | @ (\remote(recvbuf, root) + \mpi_comm_rank * sendcount));
|
|---|
| 23 | @*/
|
|---|
| 24 | int gather(int* sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 25 | int* recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 26 | int root, MPI_Comm comm){
|
|---|
| 27 | int rank, nprocs;
|
|---|
| 28 | MPI_Status status;
|
|---|
| 29 | int tag = 998;
|
|---|
| 30 |
|
|---|
| 31 | MPI_Comm_rank(comm, &rank);
|
|---|
| 32 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 33 | if(root == rank) {
|
|---|
| 34 | void *ptr;
|
|---|
| 35 |
|
|---|
| 36 | ptr = $mpi_pointerAdd(recvbuf, root * recvcount, MPI_INT);
|
|---|
| 37 | $elaborate(recvcount);
|
|---|
| 38 | memcpy(ptr, sendbuf, recvcount * sizeofDatatype(MPI_INT));
|
|---|
| 39 | }else
|
|---|
| 40 | MPI_Send(sendbuf, sendcount, MPI_INT, root, tag, comm);
|
|---|
| 41 | if(rank == root) {
|
|---|
| 42 | int real_recvcount;
|
|---|
| 43 | int offset;
|
|---|
| 44 |
|
|---|
| 45 | for(int i=0; i<nprocs; i++) {
|
|---|
| 46 | if(i != root) {
|
|---|
| 47 | void * ptr;
|
|---|
| 48 |
|
|---|
| 49 | offset = i * recvcount;
|
|---|
| 50 | ptr = $mpi_pointerAdd(recvbuf, offset, MPI_INT);
|
|---|
| 51 | MPI_Recv(ptr, recvcount, MPI_INT, i, tag, comm,
|
|---|
| 52 | &status);
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 | int main() {
|
|---|
| 61 | gather(NULL, 0, MPI_INT, NULL, 0, MPI_INT, 0, (MPI_Comm)0);
|
|---|
| 62 | return 0;
|
|---|
| 63 | }
|
|---|