| 1 | #include<mpi.h>
|
|---|
| 2 | #include<civl-mpi.cvh>
|
|---|
| 3 | #include<civlc.cvh>
|
|---|
| 4 | #include<string.h>
|
|---|
| 5 | #include<stdio.h>
|
|---|
| 6 |
|
|---|
| 7 | #pragma CIVL ACSL
|
|---|
| 8 |
|
|---|
| 9 | /*@
|
|---|
| 10 | @ \mpi_collective(comm, P2P) :
|
|---|
| 11 | @ requires \mpi_agree(root) && \mpi_agree(sendcount * \mpi_extent(sendtype));
|
|---|
| 12 | @ requires sendcount * \mpi_extent(sendtype) >= 0;
|
|---|
| 13 | @ requires recvcount * \mpi_extent(recvtype) >= 0;
|
|---|
| 14 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 15 | @ behavior imroot_not_inplace:
|
|---|
| 16 | @ assumes \mpi_comm_rank == root && sendbuf != MPI_IN_PLACE;
|
|---|
| 17 | @ requires \mpi_valid(sendbuf, sendcount, sendtype);
|
|---|
| 18 | @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 19 | @ requires recvcount * \mpi_extent(recvtype) == sendcount * \mpi_extent(sendtype);
|
|---|
| 20 | @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 21 | @ ensures \mpi_equals(\mpi_region(\mpi_offset(recvbuf, root * recvcount, recvtype), recvcount, recvtype),
|
|---|
| 22 | @ \mpi_region(sendbuf, sendcount, sendtype));
|
|---|
| 23 | @ waitsfor (0 .. \mpi_comm_size-1);
|
|---|
| 24 | @ behavior imroot_inplace:
|
|---|
| 25 | @ assumes \mpi_comm_rank == root && sendbuf == MPI_IN_PLACE;
|
|---|
| 26 | @ requires \mpi_valid(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 27 | @ assigns \mpi_region(recvbuf, recvcount * \mpi_comm_size, recvtype);
|
|---|
| 28 | @ ensures \mpi_equals(\old(
|
|---|
| 29 | @ \mpi_region(\mpi_offset(recvbuf, root * recvcount, recvtype), recvcount, recvtype)),
|
|---|
| 30 | @ \mpi_region(\mpi_offset(recvbuf, root * recvcount, recvtype), recvcount, recvtype));
|
|---|
| 31 | @ waitsfor (0 .. \mpi_comm_size-1);
|
|---|
| 32 | @ behavior not_root:
|
|---|
| 33 | @ assumes \mpi_comm_rank != root;
|
|---|
| 34 | @ requires \mpi_valid(sendbuf, sendcount, sendtype);
|
|---|
| 35 | @ requires \on(root, recvcount * \mpi_extent(recvtype)) == sendcount * \mpi_extent(sendtype);
|
|---|
| 36 | @ assigns \nothing;
|
|---|
| 37 | @ ensures \mpi_equals(\mpi_region(sendbuf, sendcount, sendtype),
|
|---|
| 38 | @ \mpi_region(\mpi_offset(\on(root, recvbuf), \mpi_comm_rank * sendcount, sendtype), sendcount, sendtype));
|
|---|
| 39 | @*/
|
|---|
| 40 | int gather(void* sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 41 | void* recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 42 | int root, MPI_Comm comm){
|
|---|
| 43 | int rank, nprocs;
|
|---|
| 44 | MPI_Status status;
|
|---|
| 45 | int tag = 998;
|
|---|
| 46 |
|
|---|
| 47 | MPI_Comm_rank(comm, &rank);
|
|---|
| 48 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 49 | if(root == rank) {
|
|---|
| 50 | if(sendbuf != MPI_IN_PLACE) {
|
|---|
| 51 | void *ptr;
|
|---|
| 52 |
|
|---|
| 53 | ptr = $mpi_pointer_add(recvbuf, root * recvcount, recvtype);
|
|---|
| 54 | memcpy(ptr, sendbuf, recvcount * sizeofDatatype(recvtype));
|
|---|
| 55 | }
|
|---|
| 56 | } else
|
|---|
| 57 | MPI_Send(sendbuf, sendcount, sendtype, root, tag, comm);
|
|---|
| 58 | if(rank == root) {
|
|---|
| 59 | int real_recvcount;
|
|---|
| 60 | int offset;
|
|---|
| 61 |
|
|---|
| 62 | for(int i=0; i<nprocs; i++) {
|
|---|
| 63 | if(i != root) {
|
|---|
| 64 | void * ptr;
|
|---|
| 65 |
|
|---|
| 66 | offset = i * recvcount;
|
|---|
| 67 | ptr = $mpi_pointer_add(recvbuf, offset, recvtype);
|
|---|
| 68 | MPI_Recv(ptr, recvcount, recvtype, i, tag, comm,
|
|---|
| 69 | &status);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | return 0;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|