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 void free_datatype(int datatype) {
87 xbt_dynar_remove_at(datatype_lookup, datatype, NULL);
90 static int new_op(MPI_Op op) {
91 xbt_dynar_push(op_lookup, &op);
92 return (int)xbt_dynar_length(op_lookup) - 1;
95 static MPI_Op get_op(int op) {
97 ? *(MPI_Op*)xbt_dynar_get_ptr(op_lookup, op)
101 void mpi_init_(int* ierr) {
102 comm_lookup = xbt_dynar_new(sizeof(MPI_Comm), NULL);
103 new_comm(MPI_COMM_WORLD);
104 group_lookup = xbt_dynar_new(sizeof(MPI_Group), NULL);
106 request_lookup = xbt_dict_new_homogeneous(NULL);
108 datatype_lookup = xbt_dynar_new(sizeof(MPI_Datatype), NULL);
109 new_datatype(MPI_BYTE);
110 new_datatype(MPI_CHAR);
111 new_datatype(MPI_INT);
112 new_datatype(MPI_INT);
113 new_datatype(MPI_INT8_T);
114 new_datatype(MPI_INT16_T);
115 new_datatype(MPI_INT32_T);
116 new_datatype(MPI_INT64_T);
117 new_datatype(MPI_FLOAT);
118 new_datatype(MPI_FLOAT);
119 new_datatype(MPI_DOUBLE);
120 new_datatype(MPI_DOUBLE);
121 new_datatype(MPI_C_FLOAT_COMPLEX);
122 new_datatype(MPI_C_DOUBLE_COMPLEX);
123 new_datatype(MPI_2INT);
124 new_datatype(MPI_UINT8_T);
125 new_datatype(MPI_UINT16_T);
126 new_datatype(MPI_UINT32_T);
127 new_datatype(MPI_UINT64_T);
128 new_datatype(MPI_2FLOAT);
129 new_datatype(MPI_2DOUBLE);
132 op_lookup = xbt_dynar_new(sizeof(MPI_Op), NULL);
146 /* smpif2c is responsible for generating a call with the final arguments */
147 *ierr = MPI_Init(NULL, NULL);
150 void mpi_finalize_(int* ierr) {
151 *ierr = MPI_Finalize();
152 xbt_dynar_free(&op_lookup);
154 xbt_dynar_free(&datatype_lookup);
155 datatype_lookup = NULL;
156 xbt_dict_free(&request_lookup);
157 request_lookup = NULL;
158 xbt_dynar_free(&comm_lookup);
162 void mpi_abort_(int* comm, int* errorcode, int* ierr) {
163 *ierr = MPI_Abort(get_comm(*comm), *errorcode);
166 void mpi_comm_rank_(int* comm, int* rank, int* ierr) {
167 *ierr = MPI_Comm_rank(get_comm(*comm), rank);
170 void mpi_comm_size_(int* comm, int* size, int* ierr) {
171 *ierr = MPI_Comm_size(get_comm(*comm), size);
174 double mpi_wtime_(void) {
178 double mpi_wtick_(void) {
182 void mpi_comm_dup_(int* comm, int* newcomm, int* ierr) {
185 *ierr = MPI_Comm_dup(get_comm(*comm), &tmp);
186 if(*ierr == MPI_SUCCESS) {
187 *newcomm = new_comm(tmp);
191 void mpi_comm_create_(int* comm, int* group, int* newcomm, int* ierr) {
194 *ierr = MPI_Comm_create(get_comm(*comm),get_group(*group), &tmp);
195 if(*ierr == MPI_SUCCESS) {
196 *newcomm = new_comm(tmp);
201 void mpi_comm_free_(int* comm, int* ierr) {
202 MPI_Comm tmp = get_comm(*comm);
204 *ierr = MPI_Comm_free(&tmp);
206 if(*ierr == MPI_SUCCESS) {
211 void mpi_comm_split_(int* comm, int* color, int* key, int* comm_out, int* ierr) {
214 *ierr = MPI_Comm_split(get_comm(*comm), *color, *key, &tmp);
215 if(*ierr == MPI_SUCCESS) {
216 *comm_out = new_comm(tmp);
220 void mpi_group_incl_(int* group, int* n, int* ranks, int* group_out, int* ierr) {
223 *ierr = MPI_Group_incl(get_group(*group), *n, ranks, &tmp);
224 if(*ierr == MPI_SUCCESS) {
225 *group_out = new_group(tmp);
229 void mpi_comm_group_(int* comm, int* group_out, int* ierr) {
232 *ierr = MPI_Comm_group(get_comm(*comm), &tmp);
233 if(*ierr == MPI_SUCCESS) {
234 *group_out = new_group(tmp);
239 void mpi_initialized_(int* flag, int* ierr){
240 *ierr = MPI_Initialized(flag);
243 void mpi_send_init_(void *buf, int* count, int* datatype, int* dst, int* tag,
244 int* comm, int* request, int* ierr) {
247 *ierr = MPI_Send_init(buf, *count, get_datatype(*datatype), *dst, *tag,
248 get_comm(*comm), &req);
249 if(*ierr == MPI_SUCCESS) {
250 *request = new_request(req);
254 void mpi_isend_(void *buf, int* count, int* datatype, int* dst,
255 int* tag, int* comm, int* request, int* ierr) {
258 *ierr = MPI_Isend(buf, *count, get_datatype(*datatype), *dst, *tag,
259 get_comm(*comm), &req);
260 if(*ierr == MPI_SUCCESS) {
261 *request = new_request(req);
265 void mpi_irsend_(void *buf, int* count, int* datatype, int* dst,
266 int* tag, int* comm, int* request, int* ierr) {
269 *ierr = MPI_Irsend(buf, *count, get_datatype(*datatype), *dst, *tag,
270 get_comm(*comm), &req);
271 if(*ierr == MPI_SUCCESS) {
272 *request = new_request(req);
276 void mpi_send_(void* buf, int* count, int* datatype, int* dst,
277 int* tag, int* comm, int* ierr) {
278 *ierr = MPI_Send(buf, *count, get_datatype(*datatype), *dst, *tag,
282 void mpi_rsend_(void* buf, int* count, int* datatype, int* dst,
283 int* tag, int* comm, int* ierr) {
284 *ierr = MPI_Rsend(buf, *count, get_datatype(*datatype), *dst, *tag,
288 void mpi_sendrecv_(void* sendbuf, int* sendcount, int* sendtype, int* dst,
289 int* sendtag, void *recvbuf, int* recvcount,
290 int* recvtype, int* src, int* recvtag,
291 int* comm, MPI_Status* status, int* ierr) {
292 *ierr = MPI_Sendrecv(sendbuf, *sendcount, get_datatype(*sendtype), *dst,
293 *sendtag, recvbuf, *recvcount,get_datatype(*recvtype), *src, *recvtag,
294 get_comm(*comm), status);
297 void mpi_recv_init_(void *buf, int* count, int* datatype, int* src, int* tag,
298 int* comm, int* request, int* ierr) {
301 *ierr = MPI_Recv_init(buf, *count, get_datatype(*datatype), *src, *tag,
302 get_comm(*comm), &req);
303 if(*ierr == MPI_SUCCESS) {
304 *request = new_request(req);
308 void mpi_irecv_(void *buf, int* count, int* datatype, int* src, int* tag,
309 int* comm, int* request, int* ierr) {
312 *ierr = MPI_Irecv(buf, *count, get_datatype(*datatype), *src, *tag,
313 get_comm(*comm), &req);
314 if(*ierr == MPI_SUCCESS) {
315 *request = new_request(req);
319 void mpi_recv_(void* buf, int* count, int* datatype, int* src,
320 int* tag, int* comm, MPI_Status* status, int* ierr) {
321 *ierr = MPI_Recv(buf, *count, get_datatype(*datatype), *src, *tag,
322 get_comm(*comm), status);
325 void mpi_start_(int* request, int* ierr) {
326 MPI_Request req = find_request(*request);
328 *ierr = MPI_Start(&req);
331 void mpi_startall_(int* count, int* requests, int* ierr) {
335 reqs = xbt_new(MPI_Request, *count);
336 for(i = 0; i < *count; i++) {
337 reqs[i] = find_request(requests[i]);
339 *ierr = MPI_Startall(*count, reqs);
343 void mpi_wait_(int* request, MPI_Status* status, int* ierr) {
344 MPI_Request req = find_request(*request);
346 *ierr = MPI_Wait(&req, status);
349 void mpi_waitany_(int* count, int* requests, int* index, MPI_Status* status, int* ierr) {
353 reqs = xbt_new(MPI_Request, *count);
354 for(i = 0; i < *count; i++) {
355 reqs[i] = find_request(requests[i]);
357 *ierr = MPI_Waitany(*count, reqs, index, status);
361 void mpi_waitall_(int* count, int* requests, MPI_Status* status, int* ierr) {
365 reqs = xbt_new(MPI_Request, *count);
366 for(i = 0; i < *count; i++) {
367 reqs[i] = find_request(requests[i]);
369 *ierr = MPI_Waitall(*count, reqs, status);
373 void mpi_barrier_(int* comm, int* ierr) {
374 *ierr = MPI_Barrier(get_comm(*comm));
377 void mpi_bcast_(void *buf, int* count, int* datatype, int* root, int* comm, int* ierr) {
378 *ierr = MPI_Bcast(buf, *count, get_datatype(*datatype), *root, get_comm(*comm));
381 void mpi_reduce_(void* sendbuf, void* recvbuf, int* count,
382 int* datatype, int* op, int* root, int* comm, int* ierr) {
383 *ierr = MPI_Reduce(sendbuf, recvbuf, *count,
384 get_datatype(*datatype), get_op(*op), *root, get_comm(*comm));
387 void mpi_allreduce_(void* sendbuf, void* recvbuf, int* count, int* datatype,
388 int* op, int* comm, int* ierr) {
389 *ierr = MPI_Allreduce(sendbuf, recvbuf, *count, get_datatype(*datatype),
390 get_op(*op), get_comm(*comm));
393 void mpi_reduce_scatter_(void* sendbuf, void* recvbuf, int* recvcounts, int* datatype,
394 int* op, int* comm, int* ierr) {
395 *ierr = MPI_Reduce_scatter(sendbuf, recvbuf, recvcounts, get_datatype(*datatype),
396 get_op(*op), get_comm(*comm));
399 void mpi_scatter_(void* sendbuf, int* sendcount, int* sendtype,
400 void* recvbuf, int* recvcount, int* recvtype,
401 int* root, int* comm, int* ierr) {
402 *ierr = MPI_Scatter(sendbuf, *sendcount, get_datatype(*sendtype),
403 recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
407 void mpi_scatterv_(void* sendbuf, int* sendcounts, int* displs, int* sendtype,
408 void* recvbuf, int* recvcount, int* recvtype,
409 int* root, int* comm, int* ierr) {
410 *ierr = MPI_Scatterv(sendbuf, sendcounts, displs, get_datatype(*sendtype),
411 recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
414 void mpi_gather_(void* sendbuf, int* sendcount, int* sendtype,
415 void* recvbuf, int* recvcount, int* recvtype,
416 int* root, int* comm, int* ierr) {
417 *ierr = MPI_Gather(sendbuf, *sendcount, get_datatype(*sendtype),
418 recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
421 void mpi_gatherv_(void* sendbuf, int* sendcount, int* sendtype,
422 void* recvbuf, int* recvcounts, int* displs, int* recvtype,
423 int* root, int* comm, int* ierr) {
424 *ierr = MPI_Gatherv(sendbuf, *sendcount, get_datatype(*sendtype),
425 recvbuf, recvcounts, displs, get_datatype(*recvtype), *root, get_comm(*comm));
428 void mpi_allgather_(void* sendbuf, int* sendcount, int* sendtype,
429 void* recvbuf, int* recvcount, int* recvtype,
430 int* comm, int* ierr) {
431 *ierr = MPI_Allgather(sendbuf, *sendcount, get_datatype(*sendtype),
432 recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
435 void mpi_allgatherv_(void* sendbuf, int* sendcount, int* sendtype,
436 void* recvbuf, int* recvcounts,int* displs, int* recvtype,
437 int* comm, int* ierr) {
438 *ierr = MPI_Allgatherv(sendbuf, *sendcount, get_datatype(*sendtype),
439 recvbuf, recvcounts, displs, get_datatype(*recvtype), get_comm(*comm));
442 void mpi_scan_(void* sendbuf, void* recvbuf, int* count, int* datatype,
443 int* op, int* comm, int* ierr) {
444 *ierr = MPI_Scan(sendbuf, recvbuf, *count, get_datatype(*datatype),
445 get_op(*op), get_comm(*comm));
448 void mpi_alltoall_(void* sendbuf, int* sendcount, int* sendtype,
449 void* recvbuf, int* recvcount, int* recvtype, int* comm, int* ierr) {
450 *ierr = MPI_Alltoall(sendbuf, *sendcount, get_datatype(*sendtype),
451 recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
454 void mpi_alltoallv_(void* sendbuf, int* sendcounts, int* senddisps, int* sendtype,
455 void* recvbuf, int* recvcounts, int* recvdisps, int* recvtype, int* comm, int* ierr) {
456 *ierr = MPI_Alltoallv(sendbuf, sendcounts, senddisps, get_datatype(*sendtype),
457 recvbuf, recvcounts, recvdisps, get_datatype(*recvtype), get_comm(*comm));
460 void mpi_test_ (int * request, int *flag, MPI_Status * status, int* ierr){
461 MPI_Request req = find_request(*request);
462 *ierr= MPI_Test(&req, flag, status);
466 void mpi_testall_ (int* count, int * requests, int *flag, MPI_Status * statuses, int* ierr){
469 reqs = xbt_new(MPI_Request, *count);
470 for(i = 0; i < *count; i++) {
471 reqs[i] = find_request(requests[i]);
473 *ierr= MPI_Testall(*count, reqs, flag, statuses);
477 void mpi_get_processor_name_(char *name, int *resultlen, int* ierr){
478 *ierr = MPI_Get_processor_name(name, resultlen);
481 void mpi_get_count_(MPI_Status * status, int* datatype, int *count, int* ierr){
482 *ierr = MPI_Get_count(status, get_datatype(*datatype), count);
485 void mpi_attr_get_(int* comm, int* keyval, void* attr_value, int* flag, int* ierr ){
486 *ierr = MPI_Attr_get(get_comm(*comm), *keyval, attr_value, flag);
489 void mpi_type_extent_(int* datatype, MPI_Aint * extent, int* ierr){
490 *ierr= MPI_Type_extent(get_datatype(*datatype), extent);
493 void mpi_type_commit_(int* datatype, int* ierr){
494 MPI_Datatype tmp= get_datatype(*datatype);
495 *ierr= MPI_Type_commit(&tmp);
498 void mpi_type_vector_(int* count, int* blocklen, int* stride, int* old_type, int* newtype, int* ierr){
500 *ierr= MPI_Type_vector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
501 if(*ierr == MPI_SUCCESS) {
502 *newtype = new_datatype(tmp);
506 void mpi_type_create_vector_(int* count, int* blocklen, int* stride, int* old_type, int* newtype, int* ierr){
508 *ierr= MPI_Type_vector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
509 if(*ierr == MPI_SUCCESS) {
510 *newtype = new_datatype(tmp);
514 void mpi_type_hvector_(int* count, int* blocklen, MPI_Aint* stride, int* old_type, int* newtype, int* ierr){
516 *ierr= MPI_Type_hvector (*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
517 if(*ierr == MPI_SUCCESS) {
518 *newtype = new_datatype(tmp);
522 void mpi_type_create_hvector_(int* count, int* blocklen, MPI_Aint* stride, int* old_type, int* newtype, int* ierr){
524 *ierr= MPI_Type_hvector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
525 if(*ierr == MPI_SUCCESS) {
526 *newtype = new_datatype(tmp);
530 void mpi_type_free_(int* datatype, int* ierr){
531 MPI_Datatype tmp= get_datatype(*datatype);
532 *ierr= MPI_Type_free (&tmp);
533 if(*ierr == MPI_SUCCESS) {
534 free_datatype(*datatype);
538 void mpi_type_ub_(int* datatype, MPI_Aint * disp, int* ierr){
539 *ierr= MPI_Type_ub(get_datatype(*datatype), disp);
542 void mpi_type_lb_(int* datatype, MPI_Aint * extent, int* ierr){
543 *ierr= MPI_Type_extent(get_datatype(*datatype), extent);
546 void mpi_type_size_(int* datatype, int *size, int* ierr)
548 *ierr = MPI_Type_size(get_datatype(*datatype), size);
551 void mpi_error_string_(int* errorcode, char* string, int* resultlen, int* ierr){
552 *ierr = MPI_Error_string(*errorcode, string, resultlen);
555 void mpi_win_fence_( int* assert, int* win, int* ierr){
556 *ierr = MPI_Win_fence(* assert, *(MPI_Win*)win);
559 void mpi_win_free_( int* win, int* ierr){
560 *ierr = MPI_Win_free( (MPI_Win*)win);
563 void mpi_win_create_( int *base, MPI_Aint* size, int* disp_unit, int* info, int* comm, int *win, int* ierr){
564 *ierr = MPI_Win_create( (void*)base, *size, *disp_unit, *(MPI_Info*)info, get_comm(*comm),(MPI_Win*)win);
567 void mpi_info_create_( int *info, int* ierr){
568 *ierr = MPI_Info_create( (MPI_Info *)info);
571 void mpi_info_set_( int *info, char *key, char *value, int* ierr){
572 *ierr = MPI_Info_set( (MPI_Info *)info, key, value);
575 void mpi_info_free_(int* info, int* ierr){
576 *ierr = MPI_Info_free((MPI_Info *) info);
579 void mpi_get_( int *origin_addr, int* origin_count, int* origin_datatype, int *target_rank,
580 MPI_Aint* target_disp, int *target_count, int* target_datatype, int* win, int* ierr){
581 *ierr = MPI_Get( (void*)origin_addr,*origin_count, get_datatype(*origin_datatype),*target_rank,
582 *target_disp, *target_count,get_datatype(*target_datatype), *(MPI_Win *)win);