Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5aaf1dca454b9d3b8cd0b89dc28fc44ae83d15f0
[simgrid.git] / src / smpi / smpi_f77.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <limits.h>
8 #include <stdio.h>
9
10 #include "private.h"
11 #include "xbt.h"
12
13 extern int xargc;
14 extern char** xargv;
15
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;
21
22 #define KEY_SIZE (sizeof(int) * 2 + 1)
23
24 static int new_comm(MPI_Comm comm) {
25   xbt_dynar_push(comm_lookup, &comm);
26   return (int)xbt_dynar_length(comm_lookup) - 1;
27 }
28
29 static void free_comm(int comm) {
30   xbt_dynar_remove_at(comm_lookup, comm, NULL);
31 }
32
33 static MPI_Comm get_comm(int comm) {
34   if(comm == -2) {
35     return MPI_COMM_SELF;
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);
38   }
39   return MPI_COMM_NULL;
40 }
41
42 static int new_group(MPI_Group group) {
43   xbt_dynar_push(group_lookup, &group);
44   return (int)xbt_dynar_length(group_lookup) - 1;
45 }
46
47 static MPI_Group get_group(int group) {
48   if(group == -2) {
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);
52   }
53   return MPI_COMM_NULL;
54 }
55
56 static char* get_key(char* key, int id) {
57   snprintf(key, KEY_SIZE, "%x", id);
58   return key;
59 }
60
61 static int new_request(MPI_Request req) {
62   static int request_id = INT_MIN;
63   char key[KEY_SIZE];
64
65   xbt_dict_set(request_lookup, get_key(key, request_id), req, NULL);
66   return request_id++;
67 }
68
69 static MPI_Request find_request(int req) {
70   char key[KEY_SIZE];
71    
72   return (MPI_Request)xbt_dict_get(request_lookup, get_key(key, req));
73 }
74
75 static int new_datatype(MPI_Datatype datatype) {
76   xbt_dynar_push(datatype_lookup, &datatype);
77   return (int)xbt_dynar_length(datatype_lookup) - 1;
78 }
79
80 static MPI_Datatype get_datatype(int datatype) {
81   return datatype >= 0
82          ? *(MPI_Datatype*)xbt_dynar_get_ptr(datatype_lookup, datatype)
83          : MPI_DATATYPE_NULL;
84 }
85
86 static void free_datatype(int datatype) {
87   xbt_dynar_remove_at(datatype_lookup, datatype, NULL);
88 }
89
90 static int new_op(MPI_Op op) {
91   xbt_dynar_push(op_lookup, &op);
92   return (int)xbt_dynar_length(op_lookup) - 1;
93 }
94
95 static MPI_Op get_op(int op) {
96    return op >= 0
97           ? *(MPI_Op*)xbt_dynar_get_ptr(op_lookup, op)
98           : MPI_OP_NULL;
99 }
100
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);
105
106    request_lookup = xbt_dict_new_homogeneous(NULL);
107
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);
130
131
132    op_lookup = xbt_dynar_new(sizeof(MPI_Op), NULL);
133    new_op(MPI_MAX);
134    new_op(MPI_MIN);
135    new_op(MPI_MAXLOC);
136    new_op(MPI_MINLOC);
137    new_op(MPI_SUM);
138    new_op(MPI_PROD);
139    new_op(MPI_LAND);
140    new_op(MPI_LOR);
141    new_op(MPI_LXOR);
142    new_op(MPI_BAND);
143    new_op(MPI_BOR);
144    new_op(MPI_BXOR);
145
146    /* smpif2c is responsible for generating a call with the final arguments */
147    *ierr = MPI_Init(NULL, NULL);
148 }
149
150 void mpi_finalize__(int* ierr) {
151    *ierr = MPI_Finalize();
152    xbt_dynar_free(&op_lookup);
153    op_lookup = NULL;
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);
159    comm_lookup = NULL;
160 }
161
162 void mpi_abort__(int* comm, int* errorcode, int* ierr) {
163   *ierr = MPI_Abort(get_comm(*comm), *errorcode);
164 }
165
166 void mpi_comm_rank__(int* comm, int* rank, int* ierr) {
167    *ierr = MPI_Comm_rank(get_comm(*comm), rank);
168 }
169
170 void mpi_comm_size__(int* comm, int* size, int* ierr) {
171    *ierr = MPI_Comm_size(get_comm(*comm), size);
172 }
173
174 double mpi_wtime__(void) {
175    return MPI_Wtime();
176 }
177
178 double mpi_wtick__(void) {
179   return MPI_Wtick();
180 }
181
182 void mpi_comm_dup__(int* comm, int* newcomm, int* ierr) {
183   MPI_Comm tmp;
184
185   *ierr = MPI_Comm_dup(get_comm(*comm), &tmp);
186   if(*ierr == MPI_SUCCESS) {
187     *newcomm = new_comm(tmp);
188   }
189 }
190
191 void mpi_comm_create__(int* comm, int* group, int* newcomm, int* ierr) {
192   MPI_Comm tmp;
193
194   *ierr = MPI_Comm_create(get_comm(*comm),get_group(*group), &tmp);
195   if(*ierr == MPI_SUCCESS) {
196     *newcomm = new_comm(tmp);
197   }
198 }
199
200
201 void mpi_comm_free__(int* comm, int* ierr) {
202   MPI_Comm tmp = get_comm(*comm);
203
204   *ierr = MPI_Comm_free(&tmp);
205
206   if(*ierr == MPI_SUCCESS) {
207     free_comm(*comm);
208   }
209 }
210
211 void mpi_comm_split__(int* comm, int* color, int* key, int* comm_out, int* ierr) {
212   MPI_Comm tmp;
213
214   *ierr = MPI_Comm_split(get_comm(*comm), *color, *key, &tmp);
215   if(*ierr == MPI_SUCCESS) {
216     *comm_out = new_comm(tmp);
217   }
218 }
219
220 void mpi_group_incl__(int* group, int* n, int* ranks, int* group_out, int* ierr) {
221   MPI_Group tmp;
222
223   *ierr = MPI_Group_incl(get_group(*group), *n, ranks, &tmp);
224   if(*ierr == MPI_SUCCESS) {
225     *group_out = new_group(tmp);
226   }
227 }
228
229 void mpi_comm_group__(int* comm, int* group_out,  int* ierr) {
230   MPI_Group tmp;
231
232   *ierr = MPI_Comm_group(get_comm(*comm), &tmp);
233   if(*ierr == MPI_SUCCESS) {
234     *group_out = new_group(tmp);
235   }
236 }
237
238
239 void mpi_initialized__(int* flag, int* ierr){
240   *ierr = MPI_Initialized(flag);
241 }
242
243 void mpi_send_init__(void *buf, int* count, int* datatype, int* dst, int* tag,
244                      int* comm, int* request, int* ierr) {
245   MPI_Request req;
246
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);
251   }
252 }
253
254 void mpi_isend__(void *buf, int* count, int* datatype, int* dst,
255                  int* tag, int* comm, int* request, int* ierr) {
256   MPI_Request req;
257
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);
262   }
263 }
264
265 void mpi_irsend__(void *buf, int* count, int* datatype, int* dst,
266                  int* tag, int* comm, int* request, int* ierr) {
267   MPI_Request req;
268
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);
273   }
274 }
275
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,
279                     get_comm(*comm));
280 }
281
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,
285                     get_comm(*comm));
286 }
287
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);
295 }
296
297 void mpi_recv_init__(void *buf, int* count, int* datatype, int* src, int* tag,
298                      int* comm, int* request, int* ierr) {
299   MPI_Request req;
300
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);
305   }
306 }
307
308 void mpi_irecv__(void *buf, int* count, int* datatype, int* src, int* tag,
309                  int* comm, int* request, int* ierr) {
310   MPI_Request req;
311
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);
316   }
317 }
318
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);
323 }
324
325 void mpi_start__(int* request, int* ierr) {
326   MPI_Request req = find_request(*request);
327
328   *ierr = MPI_Start(&req);
329 }
330
331 void mpi_startall__(int* count, int* requests, int* ierr) {
332   MPI_Request* reqs;
333   int i;
334
335   reqs = xbt_new(MPI_Request, *count);
336   for(i = 0; i < *count; i++) {
337     reqs[i] = find_request(requests[i]);
338   }
339   *ierr = MPI_Startall(*count, reqs);
340   free(reqs);
341 }
342
343 void mpi_wait__(int* request, MPI_Status* status, int* ierr) {
344    MPI_Request req = find_request(*request);
345    
346    *ierr = MPI_Wait(&req, status);
347 }
348
349 void mpi_waitany__(int* count, int* requests, int* index, MPI_Status* status, int* ierr) {
350   MPI_Request* reqs;
351   int i;
352
353   reqs = xbt_new(MPI_Request, *count);
354   for(i = 0; i < *count; i++) {
355     reqs[i] = find_request(requests[i]);
356   }
357   *ierr = MPI_Waitany(*count, reqs, index, status);
358   free(reqs);
359 }
360
361 void mpi_waitall__(int* count, int* requests, MPI_Status* status, int* ierr) {
362   MPI_Request* reqs;
363   int i;
364
365   reqs = xbt_new(MPI_Request, *count);
366   for(i = 0; i < *count; i++) {
367     reqs[i] = find_request(requests[i]);
368   }
369   *ierr = MPI_Waitall(*count, reqs, status);
370   free(reqs);
371 }
372
373 void mpi_barrier__(int* comm, int* ierr) {
374   *ierr = MPI_Barrier(get_comm(*comm));
375 }
376
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));
379 }
380
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));
385 }
386
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));
391 }
392
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));
397 }
398
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));
404 }
405
406
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));
412 }
413
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));
419 }
420
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));
426 }
427
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));
433 }
434
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));
440 }
441
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));
446 }
447
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));
452 }
453
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));
458 }
459
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);
463 }
464
465
466 void mpi_testall__ (int* count, int * requests,  int *flag, MPI_Status * statuses, int* ierr){
467   MPI_Request* reqs;
468   int i;
469   reqs = xbt_new(MPI_Request, *count);
470   for(i = 0; i < *count; i++) {
471     reqs[i] = find_request(requests[i]);
472   }
473   *ierr= MPI_Testall(*count, reqs, flag, statuses);
474 }
475
476
477 void mpi_get_processor_name__(char *name, int *resultlen, int* ierr){
478   *ierr = MPI_Get_processor_name(name, resultlen);
479 }
480
481 void mpi_get_count__(MPI_Status * status, int* datatype, int *count, int* ierr){
482   *ierr = MPI_Get_count(status, get_datatype(*datatype), count);
483 }
484
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);
487 }
488
489 void mpi_type_extent__(int* datatype, MPI_Aint * extent, int* ierr){
490   *ierr= MPI_Type_extent(get_datatype(*datatype),  extent);
491 }
492
493 void mpi_type_commit__(int* datatype,  int* ierr){
494   MPI_Datatype tmp= get_datatype(*datatype);
495   *ierr= MPI_Type_commit(&tmp);
496 }
497
498 void mpi_type_vector__(int* count, int* blocklen, int* stride, int* old_type, int* newtype,  int* ierr){
499   MPI_Datatype tmp;
500   *ierr= MPI_Type_vector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
501   if(*ierr == MPI_SUCCESS) {
502     *newtype = new_datatype(tmp);
503   }
504 }
505
506 void mpi_type_create_vector__(int* count, int* blocklen, int* stride, int* old_type, int* newtype,  int* ierr){
507   MPI_Datatype tmp;
508   *ierr= MPI_Type_vector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
509   if(*ierr == MPI_SUCCESS) {
510     *newtype = new_datatype(tmp);
511   }
512 }
513
514 void mpi_type_hvector__(int* count, int* blocklen, MPI_Aint* stride, int* old_type, int* newtype,  int* ierr){
515   MPI_Datatype tmp;
516   *ierr= MPI_Type_hvector (*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
517   if(*ierr == MPI_SUCCESS) {
518     *newtype = new_datatype(tmp);
519   }
520 }
521
522 void mpi_type_create_hvector__(int* count, int* blocklen, MPI_Aint* stride, int* old_type, int* newtype,  int* ierr){
523   MPI_Datatype tmp;
524   *ierr= MPI_Type_hvector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
525   if(*ierr == MPI_SUCCESS) {
526     *newtype = new_datatype(tmp);
527   }
528 }
529
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);
535   }
536 }
537
538 void mpi_type_ub__(int* datatype, MPI_Aint * disp, int* ierr){
539   *ierr= MPI_Type_ub(get_datatype(*datatype), disp);
540 }
541
542 void mpi_type_lb__(int* datatype, MPI_Aint * extent, int* ierr){
543   *ierr= MPI_Type_extent(get_datatype(*datatype), extent);
544 }
545
546 void mpi_type_size__(int* datatype, int *size, int* ierr)
547 {
548   *ierr = MPI_Type_size(get_datatype(*datatype), size);
549 }
550
551 void mpi_error_string__(int* errorcode, char* string, int* resultlen, int* ierr){
552   *ierr = MPI_Error_string(*errorcode, string, resultlen);
553 }
554
555 void mpi_win_fence__( int* assert,  int* win, int* ierr){
556   *ierr =  MPI_Win_fence(* assert, *(MPI_Win*)win);
557 }
558
559 void mpi_win_free__( int* win, int* ierr){
560   *ierr =  MPI_Win_free(  (MPI_Win*)win);
561 }
562
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);
565 }
566
567 void mpi_info_create__( int *info, int* ierr){
568   *ierr =  MPI_Info_create( (MPI_Info *)info);
569 }
570
571 void mpi_info_set__( int *info, char *key, char *value, int* ierr){
572   *ierr =  MPI_Info_set( (MPI_Info *)info, key, value);
573 }
574
575 void mpi_info_free__(int* info, int* ierr){
576   *ierr =  MPI_Info_free((MPI_Info *) info);
577 }
578
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);
583 }