source: CIVL/examples/contracts/contractsMPI/civl_mpi_collectives/scatter.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

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