| 1 | #include<mpi.h>
|
|---|
| 2 | #include<civl-mpi.cvh>
|
|---|
| 3 | #include<civlc.cvh>
|
|---|
| 4 | #include<string.h>
|
|---|
| 5 |
|
|---|
| 6 | /*@ \mpi_collective(comm, P2P):
|
|---|
| 7 | @ requires \mpi_agree(root) && \mpi_agree(recvcount * \mpi_extent(recvtype));
|
|---|
| 8 | @ requires \mpi_valid(recvbuf, recvcount, recvtype);
|
|---|
| 9 | @ requires 0 <= root && root < \mpi_comm_size;
|
|---|
| 10 | @ requires sendcount >= 0 && sendcount * \mpi_extent(sendtype) < 8;
|
|---|
| 11 | @ requires recvcount >= 0 && recvcount * \mpi_extent(recvtype) < 8;
|
|---|
| 12 | @ assigns \mpi_region(recvbuf, recvcount, recvtype);
|
|---|
| 13 | @ behavior imroot:
|
|---|
| 14 | @ assumes \mpi_comm_rank == root;
|
|---|
| 15 | @ requires \mpi_extent(sendtype) * sendcount ==
|
|---|
| 16 | @ \mpi_extent(recvtype) * recvcount;
|
|---|
| 17 | @ requires \mpi_valid(sendbuf, sendcount * \mpi_comm_size, sendtype);
|
|---|
| 18 | @ ensures \mpi_equals(recvbuf, recvcount, recvtype,
|
|---|
| 19 | @ \mpi_offset(sendbuf, \mpi_comm_rank * sendcount,
|
|---|
| 20 | @ sendtype));
|
|---|
| 21 | @ behavior noroot:
|
|---|
| 22 | @ assumes \mpi_comm_rank != root;
|
|---|
| 23 | @ ensures \mpi_equals(recvbuf, recvcount, recvtype,
|
|---|
| 24 | @ \mpi_offset(\on(root, sendbuf),
|
|---|
| 25 | recvcount * \mpi_comm_rank, recvtype));
|
|---|
| 26 | */
|
|---|
| 27 | int scatter(const void* sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 28 | void* recvbuf, int recvcount, MPI_Datatype recvtype, int root,
|
|---|
| 29 | MPI_Comm comm){
|
|---|
| 30 | int rank, nprocs;
|
|---|
| 31 | int tag = 999;
|
|---|
| 32 |
|
|---|
| 33 | MPI_Comm_rank(comm, &rank);
|
|---|
| 34 | MPI_Comm_size(comm, &nprocs);
|
|---|
| 35 |
|
|---|
| 36 | if (rank == root) {
|
|---|
| 37 | void * ptr;
|
|---|
| 38 | int offset;
|
|---|
| 39 |
|
|---|
| 40 | ptr = $mpi_pointer_add(sendbuf, root*sendcount, sendtype);
|
|---|
| 41 | memcpy(recvbuf, ptr, sizeofDatatype(recvtype)*recvcount);
|
|---|
| 42 | for(int i=0; i<nprocs; i++){
|
|---|
| 43 | if(i != root) {
|
|---|
| 44 | void * ptr;
|
|---|
| 45 |
|
|---|
| 46 | offset = i * sendcount;
|
|---|
| 47 | ptr = $mpi_pointer_add(sendbuf, offset, sendtype);
|
|---|
| 48 | MPI_Send(ptr, sendcount, sendtype, i, tag, comm);
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | }else
|
|---|
| 52 | MPI_Recv(recvbuf, recvcount, recvtype,
|
|---|
| 53 | root, tag, comm, MPI_STATUS_IGNORE);
|
|---|
| 54 | return 0;
|
|---|
| 55 | }
|
|---|