source: CIVL/examples/messagePassing/mpi.cvl@ 30a979d

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 30a979d was 30a979d, checked in by Stephen Siegel <siegel@…>, 12 years ago

Factored out general mpi library from diffusion1d_good.cvl into mpi.cvl.

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

  • Property mode set to 100644
File size: 2.7 KB
Line 
1
2typedef $comm MPI_Comm;
3
4typedef enum {
5 MPI_INT,
6 MPI_FLOAT,
7 MPI_DOUBLE,
8 MPI_CHAR
9} MPI_Datatype;
10
11#define BCAST_TAG 999
12
13#define MPI_PROC_NULL -3
14
15typedef struct {
16 int MPI_SOURCE;
17 int MPI_TAG;
18 int MPI_ERROR;
19 int size;
20} MPI_Status;
21
22#define MPI_STATUS_IGNORE NULL
23
24#define MPI_STATUSES_IGNORE NULL
25
26int sizeofDatatype(MPI_Datatype datatype) {
27 switch (datatype) {
28 case MPI_INT:
29 return sizeof(int);
30 case MPI_FLOAT:
31 return sizeof(float);
32 case MPI_DOUBLE:
33 return sizeof(double);
34 case MPI_CHAR:
35 return sizeof(char);
36 default:
37 $assert(0, "Unreachable");
38 }
39}
40
41int MPI_Init() {
42 return 0;
43}
44
45int MPI_Finalize() {
46 return 0;
47}
48
49int MPI_Comm_size(MPI_Comm comm, int *size) {
50 *size = $comm_size(comm);
51 return 0;
52}
53
54int MPI_Comm_rank(MPI_Comm comm, int *rank) {
55 *rank = $comm_place(comm);
56 return 0;
57}
58
59/* Sends a message. */
60int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest,
61 int tag, MPI_Comm comm) {
62 if (dest >= 0) {
63 int size = count*sizeofDatatype(datatype);
64 $message out =
65 $message_pack(comm->place, dest, tag, buf, size);
66
67 $comm_enqueue(comm, out);
68 }
69 return 0;
70}
71
72/* Receives a message. */
73int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
74 int tag, MPI_Comm comm, MPI_Status *status) {
75 if (source >= 0) {
76 $message in = $comm_dequeue(comm, source, tag);
77 int size = count*sizeofDatatype(datatype);
78
79 $message_unpack(in, buf, size);
80 if (status != MPI_STATUS_IGNORE) {
81 status->size = $message_size(in);
82 status->MPI_SOURCE = $message_source(in);
83 status->MPI_TAG = $message_tag(in);
84 status->MPI_ERROR = 0;
85 }
86 }
87 return 0;
88}
89
90int MPI_Get_count(MPI_Status *status, MPI_Datatype datatype, int *count) {
91 *count = status->size/sizeofDatatype(datatype);
92 return 0;
93}
94
95int MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
96 int dest, int sendtag,
97 void *recvbuf, int recvcount, MPI_Datatype recvtype,
98 int source, int recvtag,
99 MPI_Comm comm, MPI_Status *status) {
100 MPI_Send(sendbuf, sendcount, sendtype, dest, sendtag, comm);
101 MPI_Recv(recvbuf, recvcount, recvtype, source, recvtag, comm, status);
102 return 0;
103}
104
105/* Broadcasts a message from root to everyone else.
106 * Need to use a differnt comm.
107 */
108int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
109 MPI_Comm comm) {
110
111
112 int place = $comm_place(comm);
113
114#ifdef DEBUG
115 printf("MPI_Bcast: place=%d, count=%d, root=%d\n", place, count, root);
116#endif
117 if (place == root) {
118 int nprocs = $comm_size(comm);
119
120 for (int i=0; i<nprocs; i++)
121 if (i != root)
122 MPI_Send(buf, count, datatype, i, BCAST_TAG, comm);
123 } else {
124 MPI_Recv(buf, count, datatype, root, BCAST_TAG, comm, MPI_STATUS_IGNORE);
125 }
126 return 0;
127}
Note: See TracBrowser for help on using the repository browser.