| 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 * \mpi_extent(sendtype));
|
|---|
| 10 | @ requires sendcount * \mpi_extent(sendtype) >= 0 && sendcount * \mpi_extent(sendtype) < 5;
|
|---|
| 11 | @ requires recvcount * \mpi_extent(recvtype) >= 0 && recvcount * \mpi_extent(recvtype) < 5;
|
|---|
| 12 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 13 | @ requires \mpi_valid(sendbuf, sendcount, sendtype);
|
|---|
| 14 | @ behavior imroot:
|
|---|
| 15 | @ assumes \mpi_comm_rank == root;
|
|---|
| 16 | @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 17 | @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 18 | @ requires recvcount * \mpi_extent(recvtype) ==
|
|---|
| 19 | @ sendcount * \mpi_extent(sendtype);
|
|---|
| 20 | @ ensures \mpi_equals(\mpi_offset(recvbuf, root * sendcount, sendtype), sendcount, sendtype, sendbuf);
|
|---|
| 21 | @ waitsfor (0 .. \mpi_comm_size-1);
|
|---|
| 22 | @ behavior imnroot:
|
|---|
| 23 | @ assumes \mpi_comm_rank != root;
|
|---|
| 24 | @ ensures \mpi_equals(sendbuf, sendcount, sendtype,
|
|---|
| 25 | @ \mpi_offset(\on(root, recvbuf), \mpi_comm_rank * sendcount, sendtype));
|
|---|
| 26 | @*/
|
|---|
| 27 | int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 28 | void* recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 29 | int root, MPI_Comm comm){
|
|---|
| 30 | int rank, nprocs;
|
|---|
| 31 | MPI_Status status;
|
|---|
| 32 | int tag = 998;
|
|---|
| 33 |
|
|---|
| 34 | MPI_Comm_rank(comm, &rank);
|
|---|
| 35 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 36 | if(root == rank) {
|
|---|
| 37 | void *ptr;
|
|---|
| 38 |
|
|---|
| 39 | ptr = $mpi_pointer_add(recvbuf, root * recvcount, recvtype);
|
|---|
| 40 | memcpy(ptr, sendbuf, recvcount * sizeofDatatype(recvtype));
|
|---|
| 41 | }else if(rank%2)
|
|---|
| 42 | MPI_Send(sendbuf, sendcount, sendtype, root, tag, comm);
|
|---|
| 43 | if(rank == root) {
|
|---|
| 44 | int real_recvcount;
|
|---|
| 45 | int offset;
|
|---|
| 46 |
|
|---|
| 47 | for(int i=0; i<nprocs; i++) {
|
|---|
| 48 | if(i != root && i%2) {
|
|---|
| 49 | void * ptr;
|
|---|
| 50 |
|
|---|
| 51 | offset = i * recvcount;
|
|---|
| 52 | ptr = $mpi_pointer_add(recvbuf, offset, recvtype);
|
|---|
| 53 | MPI_Recv(ptr, recvcount, recvtype, i, tag, comm,
|
|---|
| 54 | &status);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | return 0;
|
|---|
| 59 | }
|
|---|