1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
16 static xbt_dynar_t comm_lookup = NULL;
17 static xbt_dynar_t group_lookup = NULL;
18 static xbt_dict_t request_lookup = NULL;
19 static xbt_dynar_t datatype_lookup = NULL;
20 static xbt_dynar_t op_lookup = NULL;
22 #define KEY_SIZE (sizeof(int) * 2 + 1)
24 static int new_comm(MPI_Comm comm) {
25 xbt_dynar_push(comm_lookup, &comm);
26 return (int)xbt_dynar_length(comm_lookup) - 1;
29 static void free_comm(int comm) {
30 xbt_dynar_remove_at(comm_lookup, comm, NULL);
33 static MPI_Comm get_comm(int comm) {
36 } else if(comm_lookup && comm >= 0 && comm < (int)xbt_dynar_length(comm_lookup)) {
37 return *(MPI_Comm*)xbt_dynar_get_ptr(comm_lookup, comm);
42 static int new_group(MPI_Group group) {
43 xbt_dynar_push(group_lookup, &group);
44 return (int)xbt_dynar_length(group_lookup) - 1;
47 static MPI_Group get_group(int group) {
49 return MPI_GROUP_EMPTY;
50 } else if(group_lookup && group >= 0 && group < (int)xbt_dynar_length(group_lookup)) {
51 return *(MPI_Group*)xbt_dynar_get_ptr(group_lookup, group);
56 static char* get_key(char* key, int id) {
57 snprintf(key, KEY_SIZE, "%x", id);
61 static int new_request(MPI_Request req) {
62 static int request_id = INT_MIN;
65 xbt_dict_set(request_lookup, get_key(key, request_id), req, NULL);
69 static MPI_Request find_request(int req) {
72 return (MPI_Request)xbt_dict_get(request_lookup, get_key(key, req));
75 static int new_datatype(MPI_Datatype datatype) {
76 xbt_dynar_push(datatype_lookup, &datatype);
77 return (int)xbt_dynar_length(datatype_lookup) - 1;
80 static MPI_Datatype get_datatype(int datatype) {
82 ? *(MPI_Datatype*)xbt_dynar_get_ptr(datatype_lookup, datatype)
86 static int new_op(MPI_Op op) {
87 xbt_dynar_push(op_lookup, &op);
88 return (int)xbt_dynar_length(op_lookup) - 1;
91 static MPI_Op get_op(int op) {
93 ? *(MPI_Op*)xbt_dynar_get_ptr(op_lookup, op)
97 void mpi_init__(int* ierr) {
98 comm_lookup = xbt_dynar_new(sizeof(MPI_Comm), NULL);
99 new_comm(MPI_COMM_WORLD);
100 group_lookup = xbt_dynar_new(sizeof(MPI_Group), NULL);
102 request_lookup = xbt_dict_new_homogeneous(NULL);
104 datatype_lookup = xbt_dynar_new(sizeof(MPI_Datatype), NULL);
105 new_datatype(MPI_BYTE);
106 new_datatype(MPI_CHAR);
107 new_datatype(MPI_INT);
108 new_datatype(MPI_INT);
109 new_datatype(MPI_INT8_T);
110 new_datatype(MPI_INT16_T);
111 new_datatype(MPI_INT32_T);
112 new_datatype(MPI_INT64_T);
113 new_datatype(MPI_FLOAT);
114 new_datatype(MPI_FLOAT);
115 new_datatype(MPI_DOUBLE);
116 new_datatype(MPI_DOUBLE);
117 new_datatype(MPI_C_FLOAT_COMPLEX);
118 new_datatype(MPI_C_DOUBLE_COMPLEX);
119 new_datatype(MPI_2INT);
120 new_datatype(MPI_UINT8_T);
121 new_datatype(MPI_UINT16_T);
122 new_datatype(MPI_UINT32_T);
123 new_datatype(MPI_UINT64_T);
124 new_datatype(MPI_2FLOAT);
125 new_datatype(MPI_2DOUBLE);
128 op_lookup = xbt_dynar_new(sizeof(MPI_Op), NULL);
142 /* smpif2c is responsible for generating a call with the final arguments */
143 *ierr = MPI_Init(NULL, NULL);
146 void mpi_finalize__(int* ierr) {
147 *ierr = MPI_Finalize();
148 xbt_dynar_free(&op_lookup);
150 xbt_dynar_free(&datatype_lookup);
151 datatype_lookup = NULL;
152 xbt_dict_free(&request_lookup);
153 request_lookup = NULL;
154 xbt_dynar_free(&comm_lookup);
158 void mpi_abort__(int* comm, int* errorcode, int* ierr) {
159 *ierr = MPI_Abort(get_comm(*comm), *errorcode);
162 void mpi_comm_rank__(int* comm, int* rank, int* ierr) {
163 *ierr = MPI_Comm_rank(get_comm(*comm), rank);
166 void mpi_comm_size__(int* comm, int* size, int* ierr) {
167 *ierr = MPI_Comm_size(get_comm(*comm), size);
170 double mpi_wtime__(void) {
174 double mpi_wtick__(void) {
178 void mpi_comm_dup__(int* comm, int* newcomm, int* ierr) {
181 *ierr = MPI_Comm_dup(get_comm(*comm), &tmp);
182 if(*ierr == MPI_SUCCESS) {
183 *newcomm = new_comm(tmp);
187 void mpi_comm_create__(int* comm, int* group, int* newcomm, int* ierr) {
190 *ierr = MPI_Comm_create(get_comm(*comm),get_group(*group), &tmp);
191 if(*ierr == MPI_SUCCESS) {
192 *newcomm = new_comm(tmp);
197 void mpi_comm_free__(int* comm, int* ierr) {
198 MPI_Comm tmp = get_comm(*comm);
200 *ierr = MPI_Comm_free(&tmp);
202 if(*ierr == MPI_SUCCESS) {
207 void mpi_comm_split__(int* comm, int* color, int* key, int* comm_out, int* ierr) {
210 *ierr = MPI_Comm_split(get_comm(*comm), *color, *key, &tmp);
211 if(*ierr == MPI_SUCCESS) {
212 *comm_out = new_comm(tmp);
216 void mpi_group_incl__(int* group, int* n, int* ranks, int* group_out, int* ierr) {
219 *ierr = MPI_Group_incl(get_group(*group), *n, ranks, &tmp);
220 if(*ierr == MPI_SUCCESS) {
221 *group_out = new_group(tmp);
225 void mpi_comm_group__(int* comm, int* group_out, int* ierr) {
228 *ierr = MPI_Comm_group(get_comm(*comm), &tmp);
229 if(*ierr == MPI_SUCCESS) {
230 *group_out = new_group(tmp);
235 void mpi_initialized__(int* flag, int* ierr){
236 *ierr = MPI_Initialized(flag);
239 void mpi_send_init__(void *buf, int* count, int* datatype, int* dst, int* tag,
240 int* comm, int* request, int* ierr) {
243 *ierr = MPI_Send_init(buf, *count, get_datatype(*datatype), *dst, *tag,
244 get_comm(*comm), &req);
245 if(*ierr == MPI_SUCCESS) {
246 *request = new_request(req);
250 void mpi_isend__(void *buf, int* count, int* datatype, int* dst,
251 int* tag, int* comm, int* request, int* ierr) {
254 *ierr = MPI_Isend(buf, *count, get_datatype(*datatype), *dst, *tag,
255 get_comm(*comm), &req);
256 if(*ierr == MPI_SUCCESS) {
257 *request = new_request(req);
261 void mpi_irsend__(void *buf, int* count, int* datatype, int* dst,
262 int* tag, int* comm, int* request, int* ierr) {
265 *ierr = MPI_Irsend(buf, *count, get_datatype(*datatype), *dst, *tag,
266 get_comm(*comm), &req);
267 if(*ierr == MPI_SUCCESS) {
268 *request = new_request(req);
272 void mpi_send__(void* buf, int* count, int* datatype, int* dst,
273 int* tag, int* comm, int* ierr) {
274 *ierr = MPI_Send(buf, *count, get_datatype(*datatype), *dst, *tag,
278 void mpi_rsend__(void* buf, int* count, int* datatype, int* dst,
279 int* tag, int* comm, int* ierr) {
280 *ierr = MPI_Rsend(buf, *count, get_datatype(*datatype), *dst, *tag,
284 void mpi_sendrecv__(void* sendbuf, int* sendcount, int* sendtype, int* dst,
285 int* sendtag, void *recvbuf, int* recvcount,
286 int* recvtype, int* src, int* recvtag,
287 int* comm, MPI_Status* status, int* ierr) {
288 *ierr = MPI_Sendrecv(sendbuf, *sendcount, get_datatype(*sendtype), *dst,
289 *sendtag, recvbuf, *recvcount,get_datatype(*recvtype), *src, *recvtag,
290 get_comm(*comm), status);
293 void mpi_recv_init__(void *buf, int* count, int* datatype, int* src, int* tag,
294 int* comm, int* request, int* ierr) {
297 *ierr = MPI_Recv_init(buf, *count, get_datatype(*datatype), *src, *tag,
298 get_comm(*comm), &req);
299 if(*ierr == MPI_SUCCESS) {
300 *request = new_request(req);
304 void mpi_irecv__(void *buf, int* count, int* datatype, int* src, int* tag,
305 int* comm, int* request, int* ierr) {
308 *ierr = MPI_Irecv(buf, *count, get_datatype(*datatype), *src, *tag,
309 get_comm(*comm), &req);
310 if(*ierr == MPI_SUCCESS) {
311 *request = new_request(req);
315 void mpi_recv__(void* buf, int* count, int* datatype, int* src,
316 int* tag, int* comm, MPI_Status* status, int* ierr) {
317 *ierr = MPI_Recv(buf, *count, get_datatype(*datatype), *src, *tag,
318 get_comm(*comm), status);
321 void mpi_start__(int* request, int* ierr) {
322 MPI_Request req = find_request(*request);
324 *ierr = MPI_Start(&req);
327 void mpi_startall__(int* count, int* requests, int* ierr) {
331 reqs = xbt_new(MPI_Request, *count);
332 for(i = 0; i < *count; i++) {
333 reqs[i] = find_request(requests[i]);
335 *ierr = MPI_Startall(*count, reqs);
339 void mpi_wait__(int* request, MPI_Status* status, int* ierr) {
340 MPI_Request req = find_request(*request);
342 *ierr = MPI_Wait(&req, status);
345 void mpi_waitany__(int* count, int* requests, int* index, MPI_Status* status, int* ierr) {
349 reqs = xbt_new(MPI_Request, *count);
350 for(i = 0; i < *count; i++) {
351 reqs[i] = find_request(requests[i]);
353 *ierr = MPI_Waitany(*count, reqs, index, status);
357 void mpi_waitall__(int* count, int* requests, MPI_Status* status, int* ierr) {
361 reqs = xbt_new(MPI_Request, *count);
362 for(i = 0; i < *count; i++) {
363 reqs[i] = find_request(requests[i]);
365 *ierr = MPI_Waitall(*count, reqs, status);
369 void mpi_barrier__(int* comm, int* ierr) {
370 *ierr = MPI_Barrier(get_comm(*comm));
373 void mpi_bcast__(void *buf, int* count, int* datatype, int* root, int* comm, int* ierr) {
374 *ierr = MPI_Bcast(buf, *count, get_datatype(*datatype), *root, get_comm(*comm));
377 void mpi_reduce__(void* sendbuf, void* recvbuf, int* count,
378 int* datatype, int* op, int* root, int* comm, int* ierr) {
379 *ierr = MPI_Reduce(sendbuf, recvbuf, *count,
380 get_datatype(*datatype), get_op(*op), *root, get_comm(*comm));
383 void mpi_allreduce__(void* sendbuf, void* recvbuf, int* count, int* datatype,
384 int* op, int* comm, int* ierr) {
385 *ierr = MPI_Allreduce(sendbuf, recvbuf, *count, get_datatype(*datatype),
386 get_op(*op), get_comm(*comm));
389 void mpi_reduce_scatter__(void* sendbuf, void* recvbuf, int* recvcounts, int* datatype,
390 int* op, int* comm, int* ierr) {
391 *ierr = MPI_Reduce_scatter(sendbuf, recvbuf, recvcounts, get_datatype(*datatype),
392 get_op(*op), get_comm(*comm));
395 void mpi_scatter__(void* sendbuf, int* sendcount, int* sendtype,
396 void* recvbuf, int* recvcount, int* recvtype,
397 int* root, int* comm, int* ierr) {
398 *ierr = MPI_Scatter(sendbuf, *sendcount, get_datatype(*sendtype),
399 recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
403 void mpi_scatterv__(void* sendbuf, int* sendcounts, int* displs, int* sendtype,
404 void* recvbuf, int* recvcount, int* recvtype,
405 int* root, int* comm, int* ierr) {
406 *ierr = MPI_Scatterv(sendbuf, sendcounts, displs, get_datatype(*sendtype),
407 recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
410 void mpi_gather__(void* sendbuf, int* sendcount, int* sendtype,
411 void* recvbuf, int* recvcount, int* recvtype,
412 int* root, int* comm, int* ierr) {
413 *ierr = MPI_Gather(sendbuf, *sendcount, get_datatype(*sendtype),
414 recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
417 void mpi_gatherv__(void* sendbuf, int* sendcount, int* sendtype,
418 void* recvbuf, int* recvcounts, int* displs, int* recvtype,
419 int* root, int* comm, int* ierr) {
420 *ierr = MPI_Gatherv(sendbuf, *sendcount, get_datatype(*sendtype),
421 recvbuf, recvcounts, displs, get_datatype(*recvtype), *root, get_comm(*comm));
424 void mpi_allgather__(void* sendbuf, int* sendcount, int* sendtype,
425 void* recvbuf, int* recvcount, int* recvtype,
426 int* comm, int* ierr) {
427 *ierr = MPI_Allgather(sendbuf, *sendcount, get_datatype(*sendtype),
428 recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
431 void mpi_allgatherv__(void* sendbuf, int* sendcount, int* sendtype,
432 void* recvbuf, int* recvcounts,int* displs, int* recvtype,
433 int* comm, int* ierr) {
434 *ierr = MPI_Allgatherv(sendbuf, *sendcount, get_datatype(*sendtype),
435 recvbuf, recvcounts, displs, get_datatype(*recvtype), get_comm(*comm));
438 void mpi_scan__(void* sendbuf, void* recvbuf, int* count, int* datatype,
439 int* op, int* comm, int* ierr) {
440 *ierr = MPI_Scan(sendbuf, recvbuf, *count, get_datatype(*datatype),
441 get_op(*op), get_comm(*comm));
444 void mpi_alltoall__(void* sendbuf, int* sendcount, int* sendtype,
445 void* recvbuf, int* recvcount, int* recvtype, int* comm, int* ierr) {
446 *ierr = MPI_Alltoall(sendbuf, *sendcount, get_datatype(*sendtype),
447 recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
450 void mpi_alltoallv__(void* sendbuf, int* sendcounts, int* senddisps, int* sendtype,
451 void* recvbuf, int* recvcounts, int* recvdisps, int* recvtype, int* comm, int* ierr) {
452 *ierr = MPI_Alltoallv(sendbuf, sendcounts, senddisps, get_datatype(*sendtype),
453 recvbuf, recvcounts, recvdisps, get_datatype(*recvtype), get_comm(*comm));
456 void mpi_test__ (int * request, int *flag, MPI_Status * status, int* ierr){
457 MPI_Request req = find_request(*request);
458 *ierr= MPI_Test(&req, flag, status);
462 void mpi_testall__ (int* count, int * requests, int *flag, MPI_Status * statuses, int* ierr){
465 reqs = xbt_new(MPI_Request, *count);
466 for(i = 0; i < *count; i++) {
467 reqs[i] = find_request(requests[i]);
469 *ierr= MPI_Testall(*count, reqs, flag, statuses);
473 void mpi_get_processor_name__(char *name, int *resultlen, int* ierr){
474 *ierr = MPI_Get_processor_name(name, resultlen);
477 void mpi_get_count__(MPI_Status * status, int* datatype, int *count, int* ierr){
478 *ierr = MPI_Get_count(status, get_datatype(*datatype), count);
481 void mpi_attr_get__(int* comm, int* keyval, void* attr_value, int* flag, int* ierr ){
482 *ierr = MPI_Attr_get(get_comm(*comm), *keyval, attr_value, flag);
485 void mpi_type_extent__(int* datatype, MPI_Aint * extent, int* ierr){
486 *ierr= MPI_Type_extent(get_datatype(*datatype), extent);
489 void mpi_type_ub__(int* datatype, MPI_Aint * disp, int* ierr){
490 *ierr= MPI_Type_ub(get_datatype(*datatype), disp);
493 void mpi_type_lb__(int* datatype, MPI_Aint * extent, int* ierr){
494 *ierr= MPI_Type_extent(get_datatype(*datatype), extent);
497 void mpi_type_size__(int* datatype, int *size, int* ierr)
499 *ierr = MPI_Type_size(get_datatype(*datatype), size);
502 void mpi_error_string__(int* errorcode, char* string, int* resultlen, int* ierr){
503 *ierr = MPI_Error_string(*errorcode, string, resultlen);