| 1 | #include<mpi.h>
|
|---|
| 2 | #include<civl-mpi.cvh>
|
|---|
| 3 | #include<civlc.cvh>
|
|---|
| 4 | #include<string.h>
|
|---|
| 5 |
|
|---|
| 6 | /* This error cannot be caught because of the way that we deal with \valid expressions.
|
|---|
| 7 | * So far, (\valid(p) || p == MPI_IN_PLACE) is processed unsoundly.
|
|---|
| 8 | */
|
|---|
| 9 |
|
|---|
| 10 | #pragma CIVL ACSL
|
|---|
| 11 |
|
|---|
| 12 | /*@ \mpi_collective(comm, COL):
|
|---|
| 13 | @ requires \mpi_agree(root) && \mpi_agree(recvcount * \mpi_extent(recvtype));
|
|---|
| 14 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 15 | @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) >= 0;
|
|---|
| 16 | @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) >= 0;
|
|---|
| 17 | @ assigns \mpi_region(recvbuf, recvcount, recvtype);
|
|---|
| 18 | @ waitsfor root;
|
|---|
| 19 | @ behavior imroot_not_inplace:
|
|---|
| 20 | @ assumes \mpi_comm_rank == root && recvbuf != MPI_IN_PLACE;
|
|---|
| 21 | @ requires \mpi_valid(recvbuf, recvcount, recvtype);
|
|---|
| 22 | @ requires \mpi_extent(sendtype) * sendcount ==
|
|---|
| 23 | @ \mpi_extent(recvtype) * recvcount;
|
|---|
| 24 | @ requires \mpi_valid(sendbuf, sendcount * \mpi_comm_size, sendtype);
|
|---|
| 25 | @ ensures \mpi_equals(
|
|---|
| 26 | @ \mpi_region(recvbuf, recvcount, recvtype),
|
|---|
| 27 | @ \mpi_region(\mpi_offset(sendbuf, \mpi_comm_rank * sendcount, sendtype),
|
|---|
| 28 | @ sendcount, sendtype));
|
|---|
| 29 | @ behavior imroot_inplace:
|
|---|
| 30 | @ assumes \mpi_comm_rank == root && recvbuf == MPI_IN_PLACE;
|
|---|
| 31 | @ requires \mpi_extent(sendtype) * sendcount ==
|
|---|
| 32 | @ \mpi_extent(recvtype) * recvcount;
|
|---|
| 33 | @ requires \mpi_valid(sendbuf, sendcount * \mpi_comm_size, sendtype);
|
|---|
| 34 | @ ensures \mpi_equals(
|
|---|
| 35 | @ \mpi_region(recvbuf, recvcount, recvtype),
|
|---|
| 36 | @ \mpi_region(\mpi_offset(sendbuf, \mpi_comm_rank * sendcount, sendtype),
|
|---|
| 37 | @ sendcount, sendtype));
|
|---|
| 38 | @ behavior noroot:
|
|---|
| 39 | @ assumes \mpi_comm_rank != root;
|
|---|
| 40 | @ requires recvbuf != MPI_IN_PLACE;
|
|---|
| 41 | @ requires \mpi_valid(recvbuf, recvcount, recvtype);
|
|---|
| 42 | @ ensures \mpi_equals(\mpi_region(recvbuf, recvcount, recvtype),
|
|---|
| 43 | @ \mpi_region(\mpi_offset(\on(root, sendbuf), recvcount * \mpi_comm_rank, recvtype),
|
|---|
| 44 | @ recvcount, recvtype));
|
|---|
| 45 | */
|
|---|
| 46 | int scatter(const void* sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 47 | void* recvbuf, int recvcount, MPI_Datatype recvtype, int root,
|
|---|
| 48 | MPI_Comm comm){
|
|---|
| 49 | int rank, nprocs;
|
|---|
| 50 | int tag = 999;
|
|---|
| 51 |
|
|---|
| 52 | MPI_Comm_rank(comm, &rank);
|
|---|
| 53 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 54 | /* MPI_standard requirement:
|
|---|
| 55 | * Only root process can use MPI_IN_PLACE */
|
|---|
| 56 | if (rank == root) {
|
|---|
| 57 | void * ptr;
|
|---|
| 58 |
|
|---|
| 59 | ptr = $mpi_pointer_add(sendbuf, root*sendcount, sendtype);
|
|---|
| 60 | memcpy(recvbuf, ptr, sizeofDatatype(recvtype)*recvcount);
|
|---|
| 61 | }
|
|---|
| 62 | /* Root process scatters data to other processes */
|
|---|
| 63 | if(rank == root)
|
|---|
| 64 | for(int i=0; i<nprocs; i++){
|
|---|
| 65 | if(i != root) {
|
|---|
| 66 | void * ptr;
|
|---|
| 67 |
|
|---|
| 68 | ptr = $mpi_pointer_add(sendbuf, i * sendcount, sendtype);
|
|---|
| 69 | $mpi_collective_send(ptr, sendcount, sendtype, i, tag, comm);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | /* Non-root processes receive data */
|
|---|
| 73 | if(!(root == rank)){
|
|---|
| 74 | int real_recvcount;
|
|---|
| 75 | MPI_Status status;
|
|---|
| 76 |
|
|---|
| 77 | $mpi_collective_recv(recvbuf, recvcount, recvtype,
|
|---|
| 78 | root, tag, comm, &status, "MPI_Scatter");
|
|---|
| 79 | real_recvcount = status.size/sizeofDatatype(recvtype);
|
|---|
| 80 | }
|
|---|
| 81 | return 0;
|
|---|
| 82 | }
|
|---|