source: CIVL/text/include/mpi.cvl@ 0fbd024

1.23 2.0 main test-branch
Last change on this file since 0fbd024 was f69a3d3, checked in by Ziqing Luo <ziqing@…>, 12 years ago

fix a littile bug

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

  • Property mode set to 100644
File size: 7.3 KB
Line 
1#ifndef __MPI_CVL__
2#define __MPI_CVL__
3//TODO make a Datatype struct, which has a field "int size;" Define one of these objects for MPI_INT, MPI_DOUBLE, etc.
4//TODO Then provide methods like MPI provides for creating new ones.
5//TODO then support MPI_Type_contig(datatype, int n).
6
7#define BCAST_TAG 999
8#define REDUCE_TAG 998
9
10/* Completed definition for mpi-common.h */
11struct MPI_Status{
12 int MPI_SOURCE;
13 int MPI_TAG;
14 int MPI_ERROR;
15 int size;
16};
17
18/* Definition of CIVL-MPI */
19typedef enum {
20 __UNINIT,
21 __INIT,
22 __FINALIZED
23}__MPI_Sys_status__;
24
25struct MPI_Request{
26 int id;
27};
28
29/* Definition of CMPI_Gcomm and MPI_Comm */
30typedef struct CMPI_Gcomm {
31 $gcomm p2p; // point-to-point communication
32 $gcomm col; // collective communication
33 $gbarrier gbarrier;
34} CMPI_Gcomm;
35
36struct MPI_Comm {
37 $comm p2p; // point-to-point communication
38 $comm col; // collective communication
39 $barrier barrier;
40 __MPI_Sys_status__ status;
41};
42
43/********************************** State **************************************/
44
45/* The number of times the MPI_Wtime function has been called */
46int CMPI_time_count = 0;
47
48/****************************** Helper Functions **********************************/
49int sizeofDatatype(MPI_Datatype datatype) {
50 switch (datatype) {
51 case MPI_INT:
52 return sizeof(int);
53 case MPI_FLOAT:
54 return sizeof(float);
55 case MPI_DOUBLE:
56 return sizeof(double);
57 case MPI_CHAR:
58 return sizeof(char);
59 default:
60 $assert(0, "Unreachable");
61 }
62}
63
64/************************** MPI LIB Implementations *******************************/
65
66$abstract double CMPI_time(int count);
67
68double MPI_Wtime() {
69 double result = CMPI_time(CMPI_time_count);
70
71 CMPI_time_count++;
72 return result;
73}
74
75CMPI_Gcomm CMPI_Gcomm_create($scope scope, int size) {
76 CMPI_Gcomm result;
77
78 result.p2p = $gcomm_create(scope, size);
79 result.col = $gcomm_create(scope, size);
80 result.gbarrier = $gbarrier_create(scope, size);
81 return result;
82}
83
84void CMPI_Gcomm_destroy(CMPI_Gcomm gc) {
85 $gcomm_destroy(gc.p2p);
86 $gcomm_destroy(gc.col);
87 $gbarrier_destroy(gc.gbarrier);
88}
89
90MPI_Comm CMPI_Comm_create($scope scope, CMPI_Gcomm gc, int rank) {
91 MPI_Comm result;
92
93 result.p2p = $comm_create(scope, gc.p2p, rank);
94 result.col = $comm_create(scope, gc.col, rank);
95 result.barrier = $barrier_create(scope, gc.gbarrier, rank);
96 result.status = __UNINIT;
97 return result;
98}
99
100void CMPI_Comm_destroy(MPI_Comm comm) {
101 $comm_destroy(comm.p2p);
102 $comm_destroy(comm.col);
103 $barrier_destroy(comm.barrier);
104}
105
106int __MPI_Init(MPI_Comm *comm) {
107 comm->status = __INIT;
108 return 0;
109}
110
111int __MPI_Finalize(MPI_Comm *comm) {
112 comm->status = __FINALIZED;
113 return 0;
114}
115
116int MPI_Comm_size(MPI_Comm comm, int *size) {
117 $assert(comm.status == __INIT, "MPI_Comm_size() cannot be invoked without MPI_Init() being called before.\n");
118 *size = $comm_size(comm.p2p);
119 return 0;
120}
121
122int MPI_Comm_rank(MPI_Comm comm, int *rank) {
123 $assert(comm.status == __INIT, "MPI_Comm_rank() cannot be invoked without MPI_Init() being called before.\n");
124 *rank = $comm_place(comm.p2p);
125 return 0;
126}
127
128
129int CMPI_Send(void *buf, int count, MPI_Datatype datatype, int dest,
130 int tag, $comm comm) {
131 if (dest >= 0) {
132 int size = count*sizeofDatatype(datatype);
133 int place = $comm_place(comm);
134 $message out = $message_pack(place, dest, tag, buf, size);
135
136 $comm_enqueue(comm, out);
137 }
138 return 0;
139}
140
141int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest,
142 int tag, MPI_Comm comm) {
143 $assert(comm.status == __INIT, "MPI_Send() cannot be invoked without MPI_Init() being called before.\n");
144 return CMPI_Send(buf, count, datatype, dest, tag, comm.p2p);
145}
146
147
148int CMPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
149 int tag, $comm comm, MPI_Status *status) {
150
151 if (source >= 0 || source == -1) {
152 $message in = $comm_dequeue(comm, source, tag);
153 int size = count*sizeofDatatype(datatype);
154
155 $message_unpack(in, buf, size);
156 if (status != MPI_STATUS_IGNORE) {
157 status->size = $message_size(in);
158 status->MPI_SOURCE = $message_source(in);
159 status->MPI_TAG = $message_tag(in);
160 status->MPI_ERROR = 0;
161 }
162 }
163 return 0;
164}
165
166int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
167 int tag, MPI_Comm comm, MPI_Status *status) {
168 $assert(comm.status == __INIT, "MPI_Recv() cannot be invoked without MPI_Init() being called before.\n");
169 return CMPI_Recv(buf, count, datatype, source, tag, comm.p2p, status);
170}
171
172int MPI_Get_count(MPI_Status *status, MPI_Datatype datatype, int *count) {
173 //$assert(__my_status == __INIT, "MPI status is not INIT.\n");
174 *count = status->size/sizeofDatatype(datatype);
175 return 0;
176}
177
178int MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
179 int dest, int sendtag,
180 void *recvbuf, int recvcount, MPI_Datatype recvtype,
181 int source, int recvtag,
182 MPI_Comm comm, MPI_Status *status) {
183 $assert(comm.status == __INIT, "MPI_Sendrecv() cannot be invoked without MPI_Init() being called before.\n");
184 MPI_Send(sendbuf, sendcount, sendtype, dest, sendtag, comm);
185 MPI_Recv(recvbuf, recvcount, recvtype, source, recvtag, comm, status);
186 return 0;
187}
188
189/* Broadcasts a message from root to everyone else.
190 * Need to use a differnt comm.
191 */
192int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
193 MPI_Comm comm) {
194 int place = $comm_place(comm.col);
195
196 $assert(comm.status == __INIT, "MPI_Bcast() cannot be invoked without MPI_Init() being called before.\n");
197 place = $comm_place(comm.col);
198 if (place == root) {
199 int nprocs = $comm_size(comm.col);
200
201 for (int i=0; i<nprocs; i++)
202 if (i != root)
203 CMPI_Send(buf, count, datatype, i, BCAST_TAG, comm.col);
204 } else {
205 CMPI_Recv(buf, count, datatype, root, BCAST_TAG, comm.col,
206 MPI_STATUS_IGNORE);
207 }
208 return 0;
209}
210
211/* Reduces values on all processes to a single value */
212int MPI_Reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype,
213 MPI_Op op, int root, MPI_Comm comm){
214 int place, nprocs;
215 MPI_Status status;
216
217 $assert(comm.status == __INIT, "MPI_Reduce() cannot be invoked without MPI_Init() being called before.\n");
218 place = $comm_place(comm.col);
219 CMPI_Send(sendbuf, count, datatype, root, REDUCE_TAG, comm.col);
220 //------Only root do following:
221 if(place == root){
222 int size;
223
224 $message in;
225 nprocs = $comm_size(comm.col);
226
227 for(int i = 0; i<nprocs; i++){
228 in = $comm_dequeue(comm.col, i, REDUCE_TAG);
229 size = count * sizeofDatatype(datatype);
230
231 $bundle_unpack_apply(in.data, recvbuf, count, op);
232 $assert(in.size <= size,
233 "Message of size %d exceeds the specified size %d.", in.size, size);
234 }
235 }
236 return 0;
237}
238
239/* Combines values from all processes and distributes the result back to all processes */
240/* default root is 0 */
241int MPI_Allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype,
242 MPI_Op op, MPI_Comm comm){
243 int place;
244 int root;
245 MPI_Status status;
246
247 $assert(comm.status == __INIT, "MPI_Allreduce() cannot be invoked without MPI_Init() being called before.\n");
248 place = $comm_place(comm.col);
249 root = 0;
250 MPI_Reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
251 MPI_Bcast(recvbuf, count, datatype, root, comm);
252 return 0;
253}
254
255int MPI_Barrier(MPI_Comm comm){
256
257 $assert(comm.status == __INIT, "MPI_Allreduce() cannot be invoked without MPI_Init() being called before.\n");
258 $barrier_call(comm.barrier);
259}
260#endif
Note: See TracBrowser for help on using the repository browser.