Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make fortran new bindings almost functional
[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_dict_t comm_lookup = NULL;
17 static xbt_dict_t group_lookup = NULL;
18 static xbt_dict_t request_lookup = NULL;
19 static xbt_dict_t datatype_lookup = NULL;
20 static xbt_dict_t op_lookup = NULL;
21 static int running_processes = 0;
22
23
24
25 /* Convert between Fortran and C MPI_BOTTOM */
26 #define F2C_BOTTOM(addr)    ((addr!=MPI_IN_PLACE && *(int*)addr == MPI_FORTRAN_BOTTOM) ? MPI_BOTTOM : (addr))
27 #define F2C_IN_PLACE(addr)  ((addr!=MPI_BOTTOM &&*(int*)addr == MPI_FORTRAN_IN_PLACE) ? MPI_IN_PLACE : (addr))
28
29 #define KEY_SIZE (sizeof(int) * 2 + 1)
30
31
32 static char* get_key(char* key, int id) {
33   snprintf(key, KEY_SIZE, "%x",id);
34   return key;
35 }
36 static char* get_key_id(char* key, int id) {
37   snprintf(key, KEY_SIZE, "%x_%d",id, smpi_process_index());
38   return key;
39 }
40
41 static int new_comm(MPI_Comm comm) {
42   static int comm_id = 0;
43   char key[KEY_SIZE];
44   xbt_dict_set(comm_lookup, comm==MPI_COMM_WORLD? get_key(key, comm_id) : get_key_id(key, comm_id), comm, NULL);
45   comm_id++;
46   return comm_id-1;
47 }
48
49 static void free_comm(int comm) {
50   char key[KEY_SIZE];
51   xbt_dict_remove(comm_lookup, comm==0? get_key(key, comm) : get_key_id(key, comm));
52 }
53
54 static MPI_Comm get_comm(int comm) {
55   if(comm == -2) {
56     return MPI_COMM_SELF;
57   }else if(comm==0){
58     return MPI_COMM_WORLD;
59   }     else if(comm_lookup && comm >= 0) {
60
61       char key[KEY_SIZE];
62       MPI_Comm tmp =  (MPI_Comm)xbt_dict_get_or_null(comm_lookup,get_key_id(key, comm));
63       return tmp != NULL ? tmp : MPI_COMM_NULL ;
64   }
65   return MPI_COMM_NULL;
66 }
67
68 static int new_group(MPI_Group group) {
69   static int group_id = 0;
70   char key[KEY_SIZE];
71   xbt_dict_set(group_lookup, get_key(key, group_id), group, NULL);
72   group_id++;
73   return group_id-1;
74 }
75
76 static MPI_Group get_group(int group) {
77   if(group == -2) {
78     return MPI_GROUP_EMPTY;
79   } else if(group_lookup && group >= 0) {
80     char key[KEY_SIZE];
81     return (MPI_Group)xbt_dict_get_or_null(group_lookup, get_key(key, group));
82   }
83   return MPI_GROUP_NULL;
84 }
85
86 static void free_group(int group) {
87   char key[KEY_SIZE];
88   xbt_dict_remove(group_lookup, get_key(key, group));
89 }
90
91
92
93 static int new_request(MPI_Request req) {
94   static int request_id = INT_MIN;
95   char key[KEY_SIZE];
96   xbt_dict_set(request_lookup, get_key_id(key, request_id), req, NULL);
97   request_id++;
98   return request_id-1;
99 }
100
101 static MPI_Request find_request(int req) {
102   char key[KEY_SIZE];
103   if(req==MPI_FORTRAN_REQUEST_NULL)return MPI_REQUEST_NULL;
104   return (MPI_Request)xbt_dict_get(request_lookup, get_key_id(key, req));
105 }
106
107 static void free_request(int request) {
108   char key[KEY_SIZE];
109   if(request!=MPI_FORTRAN_REQUEST_NULL)
110   xbt_dict_remove(request_lookup, get_key_id(key, request));
111 }
112
113 static int new_datatype(MPI_Datatype datatype) {
114   static int datatype_id = 0;
115   char key[KEY_SIZE];
116   xbt_dict_set(datatype_lookup, get_key(key, datatype_id), datatype, NULL);
117   datatype_id++;
118   return datatype_id-1;
119 }
120
121 static MPI_Datatype get_datatype(int datatype) {
122   char key[KEY_SIZE];
123   return datatype >= 0
124          ? (MPI_Datatype)xbt_dict_get_or_null(datatype_lookup, get_key(key, datatype))
125          : MPI_DATATYPE_NULL;
126 }
127
128 static void free_datatype(int datatype) {
129   char key[KEY_SIZE];
130   xbt_dict_remove(datatype_lookup, get_key(key, datatype));
131 }
132
133 static int new_op(MPI_Op op) {
134   static int op_id = 0;
135   char key[KEY_SIZE];
136   xbt_dict_set(op_lookup, get_key(key, op_id), op, NULL);
137   op_id++;
138   return op_id-1;
139 }
140
141 static MPI_Op get_op(int op) {
142   char key[KEY_SIZE];
143    return op >= 0
144           ? (MPI_Op)xbt_dict_get_or_null(op_lookup,  get_key(key, op))
145           : MPI_OP_NULL;
146 }
147
148 static void free_op(int op) {
149   char key[KEY_SIZE];
150   xbt_dict_remove(op_lookup, get_key(key, op));
151 }
152
153 void mpi_init_(int* ierr) {
154    if(!comm_lookup){
155      comm_lookup = xbt_dict_new_homogeneous(NULL);
156      new_comm(MPI_COMM_WORLD);
157      group_lookup = xbt_dict_new_homogeneous(NULL);
158
159      request_lookup = xbt_dict_new_homogeneous(NULL);
160
161      datatype_lookup = xbt_dict_new_homogeneous(NULL);
162      new_datatype(MPI_BYTE);
163      new_datatype(MPI_CHAR);
164      new_datatype(MPI_INT);
165      new_datatype(MPI_INT);
166      new_datatype(MPI_INT8_T);
167      new_datatype(MPI_INT16_T);
168      new_datatype(MPI_INT32_T);
169      new_datatype(MPI_INT64_T);
170      new_datatype(MPI_FLOAT);
171      new_datatype(MPI_FLOAT);
172      new_datatype(MPI_DOUBLE);
173      new_datatype(MPI_DOUBLE);
174      new_datatype(MPI_C_FLOAT_COMPLEX);
175      new_datatype(MPI_C_DOUBLE_COMPLEX);
176      new_datatype(MPI_2INT);
177      new_datatype(MPI_UINT8_T);
178      new_datatype(MPI_UINT16_T);
179      new_datatype(MPI_UINT32_T);
180      new_datatype(MPI_UINT64_T);
181      new_datatype(MPI_2FLOAT);
182      new_datatype(MPI_2DOUBLE);
183      new_datatype(MPI_DOUBLE);
184      new_datatype(MPI_DOUBLE);
185      new_datatype(MPI_INT);
186      new_datatype(MPI_DATATYPE_NULL);
187      new_datatype(MPI_DATATYPE_NULL);
188      new_datatype(MPI_DATATYPE_NULL);
189      new_datatype(MPI_DATATYPE_NULL);
190      op_lookup = xbt_dict_new_homogeneous(NULL);
191      new_op(MPI_MAX);
192      new_op(MPI_MIN);
193      new_op(MPI_MAXLOC);
194      new_op(MPI_MINLOC);
195      new_op(MPI_SUM);
196      new_op(MPI_PROD);
197      new_op(MPI_LAND);
198      new_op(MPI_LOR);
199      new_op(MPI_LXOR);
200      new_op(MPI_BAND);
201      new_op(MPI_BOR);
202      new_op(MPI_BXOR);
203    }
204    /* smpif2c is responsible for generating a call with the final arguments */
205    *ierr = MPI_Init(NULL, NULL);
206    running_processes++;
207 }
208
209 void mpi_finalize_(int* ierr) {
210    *ierr = MPI_Finalize();
211    running_processes--;
212    if(running_processes==0){
213      xbt_dict_free(&op_lookup);
214      op_lookup = NULL;
215      xbt_dict_free(&datatype_lookup);
216      datatype_lookup = NULL;
217      xbt_dict_free(&request_lookup);
218      request_lookup = NULL;
219      xbt_dict_free(&comm_lookup);
220      comm_lookup = NULL;
221    }
222 }
223
224 void mpi_abort_(int* comm, int* errorcode, int* ierr) {
225   *ierr = MPI_Abort(get_comm(*comm), *errorcode);
226 }
227
228 void mpi_comm_rank_(int* comm, int* rank, int* ierr) {
229    *ierr = MPI_Comm_rank(get_comm(*comm), rank);
230 }
231
232 void mpi_comm_size_(int* comm, int* size, int* ierr) {
233    *ierr = MPI_Comm_size(get_comm(*comm), size);
234 }
235
236 double mpi_wtime_(void) {
237    return MPI_Wtime();
238 }
239
240 double mpi_wtick_(void) {
241   return MPI_Wtick();
242 }
243
244 void mpi_comm_dup_(int* comm, int* newcomm, int* ierr) {
245   MPI_Comm tmp;
246
247   *ierr = MPI_Comm_dup(get_comm(*comm), &tmp);
248   if(*ierr == MPI_SUCCESS) {
249     *newcomm = new_comm(tmp);
250   }
251 }
252
253 void mpi_comm_create_(int* comm, int* group, int* newcomm, int* ierr) {
254   MPI_Comm tmp;
255
256   *ierr = MPI_Comm_create(get_comm(*comm),get_group(*group), &tmp);
257   if(*ierr == MPI_SUCCESS) {
258     *newcomm = new_comm(tmp);
259   }
260 }
261
262
263 void mpi_comm_free_(int* comm, int* ierr) {
264   MPI_Comm tmp = get_comm(*comm);
265
266   *ierr = MPI_Comm_free(&tmp);
267
268   if(*ierr == MPI_SUCCESS) {
269     free_comm(*comm);
270   }
271 }
272
273 void mpi_comm_split_(int* comm, int* color, int* key, int* comm_out, int* ierr) {
274   MPI_Comm tmp;
275
276   *ierr = MPI_Comm_split(get_comm(*comm), *color, *key, &tmp);
277   if(*ierr == MPI_SUCCESS) {
278     *comm_out = new_comm(tmp);
279   }
280 }
281
282 void mpi_group_incl_(int* group, int* n, int* ranks, int* group_out, int* ierr) {
283   MPI_Group tmp;
284
285   *ierr = MPI_Group_incl(get_group(*group), *n, ranks, &tmp);
286   if(*ierr == MPI_SUCCESS) {
287     *group_out = new_group(tmp);
288   }
289 }
290
291 void mpi_comm_group_(int* comm, int* group_out,  int* ierr) {
292   MPI_Group tmp;
293
294   *ierr = MPI_Comm_group(get_comm(*comm), &tmp);
295   if(*ierr == MPI_SUCCESS) {
296     *group_out = new_group(tmp);
297   }
298 }
299
300
301 void mpi_initialized_(int* flag, int* ierr){
302   *ierr = MPI_Initialized(flag);
303 }
304
305 void mpi_send_init_(void *buf, int* count, int* datatype, int* dst, int* tag,
306                      int* comm, int* request, int* ierr) {
307   MPI_Request req;
308
309   *ierr = MPI_Send_init(buf, *count, get_datatype(*datatype), *dst, *tag,
310                         get_comm(*comm), &req);
311   if(*ierr == MPI_SUCCESS) {
312     *request = new_request(req);
313   }
314 }
315
316 void mpi_isend_(void *buf, int* count, int* datatype, int* dst,
317                  int* tag, int* comm, int* request, int* ierr) {
318   MPI_Request req;
319   buf = (char *) F2C_BOTTOM(buf);
320   *ierr = MPI_Isend(buf, *count, get_datatype(*datatype), *dst, *tag,
321                     get_comm(*comm), &req);
322   if(*ierr == MPI_SUCCESS) {
323     *request = new_request(req);
324   }
325 }
326
327 void mpi_irsend_(void *buf, int* count, int* datatype, int* dst,
328                  int* tag, int* comm, int* request, int* ierr) {
329   MPI_Request req;
330   buf = (char *) F2C_BOTTOM(buf);
331   *ierr = MPI_Irsend(buf, *count, get_datatype(*datatype), *dst, *tag,
332                     get_comm(*comm), &req);
333   if(*ierr == MPI_SUCCESS) {
334     *request = new_request(req);
335   }
336 }
337
338 void mpi_send_(void* buf, int* count, int* datatype, int* dst,
339                 int* tag, int* comm, int* ierr) {
340    *ierr = MPI_Send(buf, *count, get_datatype(*datatype), *dst, *tag,
341                     get_comm(*comm));
342 }
343
344 void mpi_rsend_(void* buf, int* count, int* datatype, int* dst,
345                 int* tag, int* comm, int* ierr) {
346    *ierr = MPI_Rsend(buf, *count, get_datatype(*datatype), *dst, *tag,
347                     get_comm(*comm));
348 }
349
350 void mpi_sendrecv_(void* sendbuf, int* sendcount, int* sendtype, int* dst,
351                 int* sendtag, void *recvbuf, int* recvcount,
352                 int* recvtype, int* src, int* recvtag,
353                 int* comm, MPI_Status* status, int* ierr) {
354    *ierr = MPI_Sendrecv(sendbuf, *sendcount, get_datatype(*sendtype), *dst,
355        *sendtag, recvbuf, *recvcount,get_datatype(*recvtype), *src, *recvtag,
356        get_comm(*comm), status);
357 }
358
359 void mpi_recv_init_(void *buf, int* count, int* datatype, int* src, int* tag,
360                      int* comm, int* request, int* ierr) {
361   MPI_Request req;
362
363   *ierr = MPI_Recv_init(buf, *count, get_datatype(*datatype), *src, *tag,
364                         get_comm(*comm), &req);
365   if(*ierr == MPI_SUCCESS) {
366     *request = new_request(req);
367   }
368 }
369
370 void mpi_irecv_(void *buf, int* count, int* datatype, int* src, int* tag,
371                  int* comm, int* request, int* ierr) {
372   MPI_Request req;
373   buf = (char *) F2C_BOTTOM(buf);
374   *ierr = MPI_Irecv(buf, *count, get_datatype(*datatype), *src, *tag,
375                     get_comm(*comm), &req);
376   if(*ierr == MPI_SUCCESS) {
377     *request = new_request(req);
378   }
379 }
380
381 void mpi_recv_(void* buf, int* count, int* datatype, int* src,
382                 int* tag, int* comm, MPI_Status* status, int* ierr) {
383    *ierr = MPI_Recv(buf, *count, get_datatype(*datatype), *src, *tag,
384                     get_comm(*comm), status);
385 }
386
387 void mpi_start_(int* request, int* ierr) {
388   MPI_Request req = find_request(*request);
389
390   *ierr = MPI_Start(&req);
391 }
392
393 void mpi_startall_(int* count, int* requests, int* ierr) {
394   MPI_Request* reqs;
395   int i;
396
397   reqs = xbt_new(MPI_Request, *count);
398   for(i = 0; i < *count; i++) {
399     reqs[i] = find_request(requests[i]);
400   }
401   *ierr = MPI_Startall(*count, reqs);
402   free(reqs);
403 }
404
405 void mpi_wait_(int* request, MPI_Status* status, int* ierr) {
406    MPI_Request req = find_request(*request);
407    
408    *ierr = MPI_Wait(&req, status);
409    if(req==MPI_REQUEST_NULL){
410      free_request(*request);
411      *request=MPI_FORTRAN_REQUEST_NULL;
412    }
413 }
414
415 void mpi_waitany_(int* count, int* requests, int* index, MPI_Status* status, int* ierr) {
416   MPI_Request* reqs;
417   int i;
418
419   reqs = xbt_new(MPI_Request, *count);
420   for(i = 0; i < *count; i++) {
421     reqs[i] = find_request(requests[i]);
422   }
423   *ierr = MPI_Waitany(*count, reqs, index, status);
424   if(reqs[*index]==MPI_REQUEST_NULL){
425       free_request(requests[*index]);
426       requests[*index]=MPI_FORTRAN_REQUEST_NULL;
427   }
428   free(reqs);
429 }
430
431 void mpi_waitall_(int* count, int* requests, MPI_Status* status, int* ierr) {
432   MPI_Request* reqs;
433   int i;
434
435   reqs = xbt_new(MPI_Request, *count);
436   for(i = 0; i < *count; i++) {
437     reqs[i] = find_request(requests[i]);
438   }
439   *ierr = MPI_Waitall(*count, reqs, status);
440   for(i = 0; i < *count; i++) {
441       if(reqs[i]==MPI_REQUEST_NULL){
442           free_request(requests[i]);
443           requests[i]=MPI_FORTRAN_REQUEST_NULL;
444       }
445   }
446
447   free(reqs);
448 }
449
450 void mpi_barrier_(int* comm, int* ierr) {
451   *ierr = MPI_Barrier(get_comm(*comm));
452 }
453
454 void mpi_bcast_(void *buf, int* count, int* datatype, int* root, int* comm, int* ierr) {
455   *ierr = MPI_Bcast(buf, *count, get_datatype(*datatype), *root, get_comm(*comm));
456 }
457
458 void mpi_reduce_(void* sendbuf, void* recvbuf, int* count,
459                   int* datatype, int* op, int* root, int* comm, int* ierr) {
460   sendbuf = (char *) F2C_IN_PLACE(sendbuf);
461   sendbuf = (char *) F2C_BOTTOM(sendbuf);
462   recvbuf = (char *) F2C_BOTTOM(recvbuf);
463   *ierr = MPI_Reduce(sendbuf, recvbuf, *count,
464                      get_datatype(*datatype), get_op(*op), *root, get_comm(*comm));
465 }
466
467 void mpi_allreduce_(void* sendbuf, void* recvbuf, int* count, int* datatype,
468                      int* op, int* comm, int* ierr) {
469   sendbuf = (char *) F2C_IN_PLACE(sendbuf);
470   *ierr = MPI_Allreduce(sendbuf, recvbuf, *count, get_datatype(*datatype),
471                         get_op(*op), get_comm(*comm));
472 }
473
474 void mpi_reduce_scatter_(void* sendbuf, void* recvbuf, int* recvcounts, int* datatype,
475                      int* op, int* comm, int* ierr) {
476   sendbuf = (char *) F2C_IN_PLACE(sendbuf);
477   *ierr = MPI_Reduce_scatter(sendbuf, recvbuf, recvcounts, get_datatype(*datatype),
478                         get_op(*op), get_comm(*comm));
479 }
480
481 void mpi_scatter_(void* sendbuf, int* sendcount, int* sendtype,
482                    void* recvbuf, int* recvcount, int* recvtype, 
483                    int* root, int* comm, int* ierr) {
484   recvbuf = (char *) F2C_IN_PLACE(recvbuf);
485   *ierr = MPI_Scatter(sendbuf, *sendcount, get_datatype(*sendtype),
486                       recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
487 }
488
489
490 void mpi_scatterv_(void* sendbuf, int* sendcounts, int* displs, int* sendtype,
491                    void* recvbuf, int* recvcount, int* recvtype,
492                    int* root, int* comm, int* ierr) {
493   recvbuf = (char *) F2C_IN_PLACE(recvbuf);
494   *ierr = MPI_Scatterv(sendbuf, sendcounts, displs, get_datatype(*sendtype),
495                       recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
496 }
497
498 void mpi_gather_(void* sendbuf, int* sendcount, int* sendtype,
499                   void* recvbuf, int* recvcount, int* recvtype,
500                   int* root, int* comm, int* ierr) {
501   sendbuf = (char *) F2C_IN_PLACE(sendbuf);
502   sendbuf = (char *) F2C_BOTTOM(sendbuf);
503   recvbuf = (char *) F2C_BOTTOM(recvbuf);
504   *ierr = MPI_Gather(sendbuf, *sendcount, get_datatype(*sendtype),
505                      recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
506 }
507
508 void mpi_gatherv_(void* sendbuf, int* sendcount, int* sendtype,
509                   void* recvbuf, int* recvcounts, int* displs, int* recvtype,
510                   int* root, int* comm, int* ierr) {
511   sendbuf = (char *) F2C_IN_PLACE(sendbuf);
512   sendbuf = (char *) F2C_BOTTOM(sendbuf);
513   recvbuf = (char *) F2C_BOTTOM(recvbuf);
514   *ierr = MPI_Gatherv(sendbuf, *sendcount, get_datatype(*sendtype),
515                      recvbuf, recvcounts, displs, get_datatype(*recvtype), *root, get_comm(*comm));
516 }
517
518 void mpi_allgather_(void* sendbuf, int* sendcount, int* sendtype,
519                      void* recvbuf, int* recvcount, int* recvtype,
520                      int* comm, int* ierr) {
521   sendbuf = (char *) F2C_IN_PLACE(sendbuf);
522   *ierr = MPI_Allgather(sendbuf, *sendcount, get_datatype(*sendtype),
523                         recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
524 }
525
526 void mpi_allgatherv_(void* sendbuf, int* sendcount, int* sendtype,
527                      void* recvbuf, int* recvcounts,int* displs, int* recvtype,
528                      int* comm, int* ierr) {
529   sendbuf = (char *) F2C_IN_PLACE(sendbuf);
530   *ierr = MPI_Allgatherv(sendbuf, *sendcount, get_datatype(*sendtype),
531                         recvbuf, recvcounts, displs, get_datatype(*recvtype), get_comm(*comm));
532 }
533
534 void mpi_scan_(void* sendbuf, void* recvbuf, int* count, int* datatype,
535                 int* op, int* comm, int* ierr) {
536   *ierr = MPI_Scan(sendbuf, recvbuf, *count, get_datatype(*datatype),
537                    get_op(*op), get_comm(*comm));
538 }
539
540 void mpi_alltoall_(void* sendbuf, int* sendcount, int* sendtype,
541                     void* recvbuf, int* recvcount, int* recvtype, int* comm, int* ierr) {
542   *ierr = MPI_Alltoall(sendbuf, *sendcount, get_datatype(*sendtype),
543                        recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
544 }
545
546 void mpi_alltoallv_(void* sendbuf, int* sendcounts, int* senddisps, int* sendtype,
547                     void* recvbuf, int* recvcounts, int* recvdisps, int* recvtype, int* comm, int* ierr) {
548   *ierr = MPI_Alltoallv(sendbuf, sendcounts, senddisps, get_datatype(*sendtype),
549                        recvbuf, recvcounts, recvdisps, get_datatype(*recvtype), get_comm(*comm));
550 }
551
552 void mpi_test_ (int * request, int *flag, MPI_Status * status, int* ierr){
553   MPI_Request req = find_request(*request);
554   *ierr= MPI_Test(&req, flag, status);
555   if(req==MPI_REQUEST_NULL){
556       free_request(*request);
557       *request=MPI_FORTRAN_REQUEST_NULL;
558   }
559 }
560
561
562 void mpi_testall_ (int* count, int * requests,  int *flag, MPI_Status * statuses, int* ierr){
563   MPI_Request* reqs;
564   int i;
565   reqs = xbt_new(MPI_Request, *count);
566   for(i = 0; i < *count; i++) {
567     reqs[i] = find_request(requests[i]);
568   }
569   *ierr= MPI_Testall(*count, reqs, flag, statuses);
570   for(i = 0; i < *count; i++) {
571     if(reqs[i]==MPI_REQUEST_NULL){
572         free_request(requests[i]);
573         requests[i]=MPI_FORTRAN_REQUEST_NULL;
574     }
575   }
576 }
577
578
579 void mpi_get_processor_name_(char *name, int *resultlen, int* ierr){
580   *ierr = MPI_Get_processor_name(name, resultlen);
581 }
582
583 void mpi_get_count_(MPI_Status * status, int* datatype, int *count, int* ierr){
584   *ierr = MPI_Get_count(status, get_datatype(*datatype), count);
585 }
586
587 void mpi_attr_get_(int* comm, int* keyval, void* attr_value, int* flag, int* ierr ){
588   *ierr = MPI_Attr_get(get_comm(*comm), *keyval, attr_value, flag);
589 }
590
591 void mpi_type_extent_(int* datatype, MPI_Aint * extent, int* ierr){
592   *ierr= MPI_Type_extent(get_datatype(*datatype),  extent);
593 }
594
595 void mpi_type_commit_(int* datatype,  int* ierr){
596   MPI_Datatype tmp= get_datatype(*datatype);
597   *ierr= MPI_Type_commit(&tmp);
598 }
599
600 void mpi_type_vector_(int* count, int* blocklen, int* stride, int* old_type, int* newtype,  int* ierr){
601   MPI_Datatype tmp;
602   *ierr= MPI_Type_vector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
603   if(*ierr == MPI_SUCCESS) {
604     *newtype = new_datatype(tmp);
605   }
606 }
607
608 void mpi_type_create_vector_(int* count, int* blocklen, int* stride, int* old_type, int* newtype,  int* ierr){
609   MPI_Datatype tmp;
610   *ierr= MPI_Type_vector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
611   if(*ierr == MPI_SUCCESS) {
612     *newtype = new_datatype(tmp);
613   }
614 }
615
616 void mpi_type_hvector_(int* count, int* blocklen, MPI_Aint* stride, int* old_type, int* newtype,  int* ierr){
617   MPI_Datatype tmp;
618   *ierr= MPI_Type_hvector (*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
619   if(*ierr == MPI_SUCCESS) {
620     *newtype = new_datatype(tmp);
621   }
622 }
623
624 void mpi_type_create_hvector_(int* count, int* blocklen, MPI_Aint* stride, int* old_type, int* newtype,  int* ierr){
625   MPI_Datatype tmp;
626   *ierr= MPI_Type_hvector(*count, *blocklen, *stride, get_datatype(*old_type), &tmp);
627   if(*ierr == MPI_SUCCESS) {
628     *newtype = new_datatype(tmp);
629   }
630 }
631
632 void mpi_type_free_(int* datatype, int* ierr){
633   MPI_Datatype tmp= get_datatype(*datatype);
634   *ierr= MPI_Type_free (&tmp);
635   if(*ierr == MPI_SUCCESS) {
636     free_datatype(*datatype);
637   }
638 }
639
640 void mpi_type_ub_(int* datatype, MPI_Aint * disp, int* ierr){
641   *ierr= MPI_Type_ub(get_datatype(*datatype), disp);
642 }
643
644 void mpi_type_lb_(int* datatype, MPI_Aint * extent, int* ierr){
645   *ierr= MPI_Type_extent(get_datatype(*datatype), extent);
646 }
647
648 void mpi_type_size_(int* datatype, int *size, int* ierr)
649 {
650   *ierr = MPI_Type_size(get_datatype(*datatype), size);
651 }
652
653 void mpi_error_string_(int* errorcode, char* string, int* resultlen, int* ierr){
654   *ierr = MPI_Error_string(*errorcode, string, resultlen);
655 }
656
657 void mpi_win_fence_( int* assert,  int* win, int* ierr){
658   *ierr =  MPI_Win_fence(* assert, *(MPI_Win*)win);
659 }
660
661 void mpi_win_free_( int* win, int* ierr){
662   *ierr =  MPI_Win_free(  (MPI_Win*)win);
663 }
664
665 void mpi_win_create_( int *base, MPI_Aint* size, int* disp_unit, int* info, int* comm, int *win, int* ierr){
666   *ierr =  MPI_Win_create( (void*)base, *size, *disp_unit, *(MPI_Info*)info, get_comm(*comm),(MPI_Win*)win);
667 }
668
669 void mpi_info_create_( int *info, int* ierr){
670   *ierr =  MPI_Info_create( (MPI_Info *)info);
671 }
672
673 void mpi_info_set_( int *info, char *key, char *value, int* ierr){
674   *ierr =  MPI_Info_set( *(MPI_Info *)info, key, value);
675 }
676
677 void mpi_info_free_(int* info, int* ierr){
678   *ierr =  MPI_Info_free((MPI_Info *) info);
679 }
680
681 void mpi_get_( int *origin_addr, int* origin_count, int* origin_datatype, int *target_rank,
682     MPI_Aint* target_disp, int *target_count, int* target_datatype, int* win, int* ierr){
683   *ierr =  MPI_Get( (void*)origin_addr,*origin_count, get_datatype(*origin_datatype),*target_rank,
684       *target_disp, *target_count,get_datatype(*target_datatype), *(MPI_Win *)win);
685 }
686
687
688 //following are automatically generated, and have to be checked
689 void mpi_finalized_ (int * flag, int* ierr){
690
691  *ierr = MPI_Finalized(flag);
692 }
693
694 void mpi_init_thread_ (int* required, int *provided, int* ierr){
695   if(!comm_lookup){
696     comm_lookup = xbt_dict_new_homogeneous(NULL);
697     new_comm(MPI_COMM_WORLD);
698     group_lookup = xbt_dict_new_homogeneous(NULL);
699
700     request_lookup = xbt_dict_new_homogeneous(NULL);
701
702     datatype_lookup = xbt_dict_new_homogeneous(NULL);
703     new_datatype(MPI_BYTE);
704     new_datatype(MPI_CHAR);
705     new_datatype(MPI_INT);
706     new_datatype(MPI_INT);
707     new_datatype(MPI_INT8_T);
708     new_datatype(MPI_INT16_T);
709     new_datatype(MPI_INT32_T);
710     new_datatype(MPI_INT64_T);
711     new_datatype(MPI_FLOAT);
712     new_datatype(MPI_FLOAT);
713     new_datatype(MPI_DOUBLE);
714     new_datatype(MPI_DOUBLE);
715     new_datatype(MPI_C_FLOAT_COMPLEX);
716     new_datatype(MPI_C_DOUBLE_COMPLEX);
717     new_datatype(MPI_2INT);
718     new_datatype(MPI_UINT8_T);
719     new_datatype(MPI_UINT16_T);
720     new_datatype(MPI_UINT32_T);
721     new_datatype(MPI_UINT64_T);
722     new_datatype(MPI_2FLOAT);
723     new_datatype(MPI_2DOUBLE);
724
725     op_lookup = xbt_dict_new_homogeneous( NULL);
726     new_op(MPI_MAX);
727     new_op(MPI_MIN);
728     new_op(MPI_MAXLOC);
729     new_op(MPI_MINLOC);
730     new_op(MPI_SUM);
731     new_op(MPI_PROD);
732     new_op(MPI_LAND);
733     new_op(MPI_LOR);
734     new_op(MPI_LXOR);
735     new_op(MPI_BAND);
736     new_op(MPI_BOR);
737     new_op(MPI_BXOR);
738   }
739   /* smpif2c is responsible for generating a call with the final arguments */
740  *ierr = MPI_Init_thread(NULL, NULL,*required, provided);
741 }
742
743 void mpi_query_thread_ (int *provided, int* ierr){
744
745  *ierr = MPI_Query_thread(provided);
746 }
747
748 void mpi_is_thread_main_ (int *flag, int* ierr){
749
750  *ierr = MPI_Is_thread_main(flag);
751 }
752
753 void mpi_address_ (void *location, MPI_Aint * address, int* ierr){
754
755  *ierr = MPI_Address(location, address);
756 }
757
758 void mpi_get_address_ (void *location, MPI_Aint * address, int* ierr){
759
760  *ierr = MPI_Get_address(location, address);
761 }
762
763 void mpi_type_dup_ (int*  datatype, int* newdatatype, int* ierr){
764  MPI_Datatype tmp;
765  *ierr = MPI_Type_dup(get_datatype(*datatype), &tmp);
766  if(*ierr == MPI_SUCCESS) {
767    *newdatatype = new_datatype(tmp);
768  }
769 }
770
771 void mpi_type_set_name_ (int*  datatype, char * name, int* ierr){
772
773  *ierr = MPI_Type_set_name(get_datatype(*datatype), name);
774 }
775
776 void mpi_type_get_name_ (int*  datatype, char * name, int* len, int* ierr){
777
778  *ierr = MPI_Type_get_name(get_datatype(*datatype),name,len);
779 }
780
781 void mpi_type_get_attr_ (int* type, int* type_keyval, void *attribute_val, int* flag, int* ierr){
782
783  *ierr = MPI_Type_get_attr ( get_datatype(*type), *type_keyval, attribute_val,flag);
784 }
785
786 void mpi_type_set_attr_ (int* type, int* type_keyval, void *attribute_val, int* ierr){
787
788  *ierr = MPI_Type_set_attr ( get_datatype(*type), *type_keyval, attribute_val);
789 }
790
791 void mpi_type_delete_attr_ (int* type, int* type_keyval, int* ierr){
792
793  *ierr = MPI_Type_delete_attr ( get_datatype(*type),  *type_keyval);
794 }
795
796 void mpi_type_create_keyval_ (void* copy_fn, void*  delete_fn, int* keyval, void* extra_state, int* ierr){
797
798  *ierr = MPI_Type_create_keyval((MPI_Type_copy_attr_function*)copy_fn, (MPI_Type_delete_attr_function*) delete_fn,  keyval,  extra_state) ;
799 }
800
801 void mpi_type_free_keyval_ (int* keyval, int* ierr) {
802  *ierr = MPI_Type_free_keyval( keyval);
803 }
804
805 void mpi_pcontrol_ (int* level , int* ierr){
806  *ierr = MPI_Pcontrol(*(const int*)level);
807 }
808
809 void mpi_type_get_extent_ (int* datatype, MPI_Aint * lb, MPI_Aint * extent, int* ierr){
810
811  *ierr = MPI_Type_get_extent(get_datatype(*datatype), lb, extent);
812 }
813
814 void mpi_type_get_true_extent_ (int* datatype, MPI_Aint * lb, MPI_Aint * extent, int* ierr){
815
816  *ierr = MPI_Type_get_true_extent(get_datatype(*datatype), lb, extent);
817 }
818
819 void mpi_op_create_ (void * function, int* commute, int* op, int* ierr){
820   MPI_Op tmp;
821  *ierr = MPI_Op_create((MPI_User_function*)function,* commute, &tmp);
822  if(*ierr == MPI_SUCCESS) {
823    *op = new_op(tmp);
824  }
825 }
826
827 void mpi_op_free_ (int* op, int* ierr){
828   MPI_Op tmp=get_op(*op);
829   *ierr = MPI_Op_free(& tmp);
830   if(*ierr == MPI_SUCCESS) {
831     free_op(*op);
832   }
833 }
834
835 void mpi_group_free_ (int* group, int* ierr){
836  MPI_Group tmp=get_group(*group);
837  *ierr = MPI_Group_free(&tmp);
838  if(*ierr == MPI_SUCCESS) {
839    free_group(*group);
840  }
841 }
842
843 void mpi_group_size_ (int* group, int *size, int* ierr){
844
845  *ierr = MPI_Group_size(get_group(*group), size);
846 }
847
848 void mpi_group_rank_ (int* group, int *rank, int* ierr){
849
850  *ierr = MPI_Group_rank(get_group(*group), rank);
851 }
852
853 void mpi_group_translate_ranks_ (int* group1, int* n, int *ranks1, int* group2, int *ranks2, int* ierr)
854 {
855
856  *ierr = MPI_Group_translate_ranks(get_group(*group1), *n, ranks1, get_group(*group2), ranks2);
857 }
858
859 void mpi_group_compare_ (int* group1, int* group2, int *result, int* ierr){
860
861  *ierr = MPI_Group_compare(get_group(*group1), get_group(*group2), result);
862 }
863
864 void mpi_group_union_ (int* group1, int* group2, int* newgroup, int* ierr){
865  MPI_Group tmp;
866  *ierr = MPI_Group_union(get_group(*group1), get_group(*group2), &tmp);
867  if(*ierr == MPI_SUCCESS) {
868    *newgroup = new_group(tmp);
869  }
870 }
871
872 void mpi_group_intersection_ (int* group1, int* group2, int* newgroup, int* ierr){
873  MPI_Group tmp;
874  *ierr = MPI_Group_intersection(get_group(*group1), get_group(*group2), &tmp);
875  if(*ierr == MPI_SUCCESS) {
876    *newgroup = new_group(tmp);
877  }
878 }
879
880 void mpi_group_difference_ (int* group1, int* group2, int* newgroup, int* ierr){
881  MPI_Group tmp;
882  *ierr = MPI_Group_difference(get_group(*group1), get_group(*group2), &tmp);
883  if(*ierr == MPI_SUCCESS) {
884    *newgroup = new_group(tmp);
885  }
886 }
887
888 void mpi_group_excl_ (int* group, int* n, int *ranks, int* newgroup, int* ierr){
889   MPI_Group tmp;
890  *ierr = MPI_Group_excl(get_group(*group), *n, ranks, &tmp);
891  if(*ierr == MPI_SUCCESS) {
892    *newgroup = new_group(tmp);
893  }
894 }
895
896 void mpi_group_range_incl_ (int* group, int* n, int ranges[][3], int* newgroup, int* ierr)
897 {
898   MPI_Group tmp;
899  *ierr = MPI_Group_range_incl(get_group(*group), *n, ranges, &tmp);
900  if(*ierr == MPI_SUCCESS) {
901    *newgroup = new_group(tmp);
902  }
903 }
904
905 void mpi_group_range_excl_ (int* group, int* n, int ranges[][3], int* newgroup, int* ierr)
906 {
907  MPI_Group tmp;
908  *ierr = MPI_Group_range_excl(get_group(*group), *n, ranges, &tmp);
909  if(*ierr == MPI_SUCCESS) {
910    *newgroup = new_group(tmp);
911  }
912 }
913
914 void mpi_comm_get_attr_ (int* comm, int* comm_keyval, void *attribute_val, int *flag, int* ierr){
915
916  *ierr = MPI_Comm_get_attr (get_comm(*comm), *comm_keyval, attribute_val, flag);
917 }
918
919 void mpi_comm_set_attr_ (int* comm, int* comm_keyval, void *attribute_val, int* ierr){
920
921  *ierr = MPI_Comm_set_attr ( get_comm(*comm), *comm_keyval, attribute_val);
922 }
923
924 void mpi_comm_delete_attr_ (int* comm, int* comm_keyval, int* ierr){
925
926  *ierr = MPI_Comm_delete_attr (get_comm(*comm),  *comm_keyval);
927 }
928
929 void mpi_comm_create_keyval_ (void* copy_fn, void* delete_fn, int* keyval, void* extra_state, int* ierr){
930
931  *ierr = MPI_Comm_create_keyval((MPI_Comm_copy_attr_function*)copy_fn,  (MPI_Comm_delete_attr_function*)delete_fn,  keyval,  extra_state) ;
932 }
933
934 void mpi_comm_free_keyval_ (int* keyval, int* ierr) {
935  *ierr = MPI_Comm_free_keyval( keyval);
936 }
937
938 void mpi_comm_get_name_ (int* comm, char* name, int* len, int* ierr){
939
940  *ierr = MPI_Comm_get_name(get_comm(*comm), name, len);
941 }
942
943 void mpi_comm_compare_ (int* comm1, int* comm2, int *result, int* ierr){
944
945  *ierr = MPI_Comm_compare(get_comm(*comm1), get_comm(*comm2), result);
946 }
947
948 void mpi_comm_disconnect_ (int* comm, int* ierr){
949  MPI_Comm tmp=get_comm(*comm);
950  *ierr = MPI_Comm_disconnect(&tmp);
951  if(*ierr == MPI_SUCCESS) {
952    free_comm(*comm);
953  }
954 }
955
956 void mpi_request_free_ (int* request, int* ierr){
957   MPI_Request tmp=find_request(*request);
958  *ierr = MPI_Request_free(&tmp);
959  if(*ierr == MPI_SUCCESS) {
960    free_request(*request);
961  }
962 }
963
964 void mpi_sendrecv_replace_ (void *buf, int* count, int* datatype, int* dst, int* sendtag, int* src, int* recvtag,
965  int* comm, MPI_Status* status, int* ierr)
966 {
967
968  *ierr = MPI_Sendrecv_replace(buf, *count, get_datatype(*datatype), *dst, *sendtag, *src,
969  *recvtag, get_comm(*comm), status);
970 }
971
972 void mpi_testany_ (int* count, int* requests, int *index, int *flag, MPI_Status* status, int* ierr)
973 {
974   MPI_Request* reqs;
975   int i;
976
977   reqs = xbt_new(MPI_Request, *count);
978   for(i = 0; i < *count; i++) {
979     reqs[i] = find_request(requests[i]);
980   }
981   *ierr = MPI_Testany(*count, reqs, index, flag, status);
982   if(*index!=MPI_UNDEFINED)
983   if(reqs[*index]==MPI_REQUEST_NULL){
984     free_request(requests[*index]);
985     requests[*index]=MPI_FORTRAN_REQUEST_NULL;
986   }
987   free(reqs);
988 }
989
990 void mpi_waitsome_ (int* incount, int* requests, int *outcount, int *indices, MPI_Status* status, int* ierr)
991 {
992   MPI_Request* reqs;
993   int i;
994
995   reqs = xbt_new(MPI_Request, *incount);
996   for(i = 0; i < *incount; i++) {
997     reqs[i] = find_request(requests[i]);
998   }
999   *ierr = MPI_Waitsome(*incount, reqs, outcount, indices, status);
1000   for(i=0;i<*outcount;i++){
1001     if(reqs[indices[i]]==MPI_REQUEST_NULL){
1002         free_request(requests[indices[i]]);
1003         requests[indices[i]]=MPI_FORTRAN_REQUEST_NULL;
1004     }
1005   }
1006   free(reqs);
1007 }
1008
1009 void mpi_reduce_local_ (void *inbuf, void *inoutbuf, int* count, int* datatype, int* op, int* ierr){
1010
1011  *ierr = MPI_Reduce_local(inbuf, inoutbuf, *count, get_datatype(*datatype), get_op(*op));
1012 }
1013
1014 void mpi_reduce_scatter_block_ (void *sendbuf, void *recvbuf, int* recvcount, int* datatype, int* op, int* comm, int* ierr)
1015 {
1016   sendbuf = (char *) F2C_IN_PLACE(sendbuf);
1017  *ierr = MPI_Reduce_scatter_block(sendbuf, recvbuf, *recvcount, get_datatype(*datatype), get_op(*op), get_comm(*comm));
1018 }
1019
1020 void mpi_pack_size_ (int* incount, int* datatype, int* comm, int* size, int* ierr) {
1021  *ierr = MPI_Pack_size(*incount, get_datatype(*datatype), get_comm(*comm), size);
1022 }
1023
1024 void mpi_cart_coords_ (int* comm, int* rank, int* maxdims, int* coords, int* ierr) {
1025  *ierr = MPI_Cart_coords(get_comm(*comm), *rank, *maxdims, coords);
1026 }
1027
1028 void mpi_cart_create_ (int* comm_old, int* ndims, int* dims, int* periods, int* reorder, int*  comm_cart, int* ierr) {
1029   MPI_Comm tmp;
1030  *ierr = MPI_Cart_create(get_comm(*comm_old), *ndims, dims, periods, *reorder, &tmp);
1031  if(*ierr == MPI_SUCCESS) {
1032    *comm_cart = new_comm(tmp);
1033  }
1034 }
1035
1036 void mpi_cart_get_ (int* comm, int* maxdims, int* dims, int* periods, int* coords, int* ierr) {
1037  *ierr = MPI_Cart_get(get_comm(*comm), *maxdims, dims, periods, coords);
1038 }
1039
1040 void mpi_cart_map_ (int* comm_old, int* ndims, int* dims, int* periods, int* newrank, int* ierr) {
1041  *ierr = MPI_Cart_map(get_comm(*comm_old), *ndims, dims, periods, newrank);
1042 }
1043
1044 void mpi_cart_rank_ (int* comm, int* coords, int* rank, int* ierr) {
1045  *ierr = MPI_Cart_rank(get_comm(*comm), coords, rank);
1046 }
1047
1048 void mpi_cart_shift_ (int* comm, int* direction, int* displ, int* source, int* dest, int* ierr) {
1049  *ierr = MPI_Cart_shift(get_comm(*comm), *direction, *displ, source, dest);
1050 }
1051
1052 void mpi_cart_sub_ (int* comm, int* remain_dims, int*  comm_new, int* ierr) {
1053  MPI_Comm tmp;
1054  *ierr = MPI_Cart_sub(get_comm(*comm), remain_dims, &tmp);
1055  if(*ierr == MPI_SUCCESS) {
1056    *comm_new = new_comm(tmp);
1057  }
1058 }
1059
1060 void mpi_cartdim_get_ (int* comm, int* ndims, int* ierr) {
1061  *ierr = MPI_Cartdim_get(get_comm(*comm), ndims);
1062 }
1063
1064 void mpi_graph_create_ (int* comm_old, int* nnodes, int* index, int* edges, int* reorder, int*  comm_graph, int* ierr) {
1065   MPI_Comm tmp;
1066  *ierr = MPI_Graph_create(get_comm(*comm_old), *nnodes, index, edges, *reorder, &tmp);
1067  if(*ierr == MPI_SUCCESS) {
1068    *comm_graph = new_comm(tmp);
1069  }
1070 }
1071
1072 void mpi_graph_get_ (int* comm, int* maxindex, int* maxedges, int* index, int* edges, int* ierr) {
1073  *ierr = MPI_Graph_get(get_comm(*comm), *maxindex, *maxedges, index, edges);
1074 }
1075
1076 void mpi_graph_map_ (int* comm_old, int* nnodes, int* index, int* edges, int* newrank, int* ierr) {
1077  *ierr = MPI_Graph_map(get_comm(*comm_old), *nnodes, index, edges, newrank);
1078 }
1079
1080 void mpi_graph_neighbors_ (int* comm, int* rank, int* maxneighbors, int* neighbors, int* ierr) {
1081  *ierr = MPI_Graph_neighbors(get_comm(*comm), *rank, *maxneighbors, neighbors);
1082 }
1083
1084 void mpi_graph_neighbors_count_ (int* comm, int* rank, int* nneighbors, int* ierr) {
1085  *ierr = MPI_Graph_neighbors_count(get_comm(*comm), *rank, nneighbors);
1086 }
1087
1088 void mpi_graphdims_get_ (int* comm, int* nnodes, int* nedges, int* ierr) {
1089  *ierr = MPI_Graphdims_get(get_comm(*comm), nnodes, nedges);
1090 }
1091
1092 void mpi_topo_test_ (int* comm, int* top_type, int* ierr) {
1093  *ierr = MPI_Topo_test(get_comm(*comm), top_type);
1094 }
1095
1096 void mpi_error_class_ (int* errorcode, int* errorclass, int* ierr) {
1097  *ierr = MPI_Error_class(*errorcode, errorclass);
1098 }
1099
1100 void mpi_errhandler_create_ (void* function, void* errhandler, int* ierr) {
1101  *ierr = MPI_Errhandler_create((MPI_Handler_function*)function, (MPI_Errhandler*)errhandler);
1102 }
1103
1104 void mpi_errhandler_free_ (void* errhandler, int* ierr) {
1105  *ierr = MPI_Errhandler_free((MPI_Errhandler*)errhandler);
1106 }
1107
1108 void mpi_errhandler_get_ (int* comm, void* errhandler, int* ierr) {
1109  *ierr = MPI_Errhandler_get(get_comm(*comm), (MPI_Errhandler*) errhandler);
1110 }
1111
1112 void mpi_errhandler_set_ (int* comm, void* errhandler, int* ierr) {
1113  *ierr = MPI_Errhandler_set(get_comm(*comm), *(MPI_Errhandler*)errhandler);
1114 }
1115
1116 void mpi_comm_set_errhandler_ (int* comm, void* errhandler, int* ierr) {
1117  *ierr = MPI_Errhandler_set(get_comm(*comm), *(MPI_Errhandler*)errhandler);
1118 }
1119
1120 void mpi_type_contiguous_ (int* count, int* old_type, int*  newtype, int* ierr) {
1121   MPI_Datatype tmp;
1122   *ierr = MPI_Type_contiguous(*count, get_datatype(*old_type), &tmp);
1123   if(*ierr == MPI_SUCCESS) {
1124     *newtype = new_datatype(tmp);
1125   }
1126 }
1127
1128 void mpi_cancel_ (int* request, int* ierr) {
1129   MPI_Request tmp=find_request(*request);
1130  *ierr = MPI_Cancel(&tmp);
1131  if(*ierr == MPI_SUCCESS) {
1132    free_request(*request);
1133  }
1134 }
1135
1136 void mpi_buffer_attach_ (void* buffer, int* size, int* ierr) {
1137  *ierr = MPI_Buffer_attach(buffer, *size);
1138 }
1139
1140 void mpi_buffer_detach_ (void* buffer, int* size, int* ierr) {
1141  *ierr = MPI_Buffer_detach(buffer, size);
1142 }
1143
1144 void mpi_testsome_ (int* incount, int*  requests, int* outcount, int* indices, MPI_Status*  statuses, int* ierr) {
1145   MPI_Request* reqs;
1146   int i;
1147
1148   reqs = xbt_new(MPI_Request, *incount);
1149   for(i = 0; i < *incount; i++) {
1150     reqs[i] = find_request(requests[i]);
1151     indices[i]=0;
1152   }
1153   *ierr = MPI_Testsome(*incount, reqs, outcount, indices, statuses);
1154   for(i=0;i<*incount;i++){
1155     if(indices[i]){
1156       if(reqs[indices[i]]==MPI_REQUEST_NULL){
1157           free_request(requests[indices[i]]);
1158           requests[indices[i]]=MPI_FORTRAN_REQUEST_NULL;
1159       }
1160     }
1161   }
1162   free(reqs);
1163 }
1164
1165 void mpi_comm_test_inter_ (int* comm, int* flag, int* ierr) {
1166  *ierr = MPI_Comm_test_inter(get_comm(*comm), flag);
1167 }
1168
1169 void mpi_unpack_ (void* inbuf, int* insize, int* position, void* outbuf, int* outcount, int* type, int* comm, int* ierr) {
1170  *ierr = MPI_Unpack(inbuf, *insize, position, outbuf, *outcount, get_datatype(*type), get_comm(*comm));
1171 }
1172
1173 void mpi_pack_external_size_ (char *datarep, int* incount, int* datatype, MPI_Aint *size, int* ierr){
1174  *ierr = MPI_Pack_external_size(datarep, *incount, get_datatype(*datatype), size);
1175 }
1176
1177 void mpi_pack_external_ (char *datarep, void *inbuf, int* incount, int* datatype, void *outbuf, MPI_Aint* outcount, MPI_Aint *position, int* ierr){
1178  *ierr = MPI_Pack_external(datarep, inbuf, *incount, get_datatype(*datatype), outbuf, *outcount, position);
1179 }
1180
1181 void mpi_unpack_external_ ( char *datarep, void *inbuf, MPI_Aint* insize, MPI_Aint *position, void *outbuf, int* outcount, int* datatype, int* ierr){
1182  *ierr = MPI_Unpack_external( datarep, inbuf, *insize, position, outbuf, *outcount, get_datatype(*datatype));
1183 }
1184
1185 void mpi_type_hindexed_ (int* count, int* blocklens, MPI_Aint* indices, int* old_type, int*  newtype, int* ierr) {
1186   MPI_Datatype tmp;
1187   *ierr = MPI_Type_hindexed(*count, blocklens, indices, get_datatype(*old_type), &tmp);
1188   if(*ierr == MPI_SUCCESS) {
1189     *newtype = new_datatype(tmp);
1190   }
1191 }
1192
1193 void mpi_type_create_hindexed_ (int* count, int* blocklens, MPI_Aint* indices, int* old_type, int*  newtype, int* ierr) {
1194   MPI_Datatype tmp;
1195   *ierr = MPI_Type_create_hindexed(*count, blocklens, indices, get_datatype(*old_type), &tmp);
1196   if(*ierr == MPI_SUCCESS) {
1197     *newtype = new_datatype(tmp);
1198   }
1199 }
1200
1201 void mpi_type_create_hindexed_block_ (int* count, int* blocklength, MPI_Aint* indices, int* old_type, int*  newtype, int* ierr) {
1202   MPI_Datatype tmp;
1203   *ierr = MPI_Type_create_hindexed_block(*count, *blocklength, indices, get_datatype(*old_type), &tmp);
1204   if(*ierr == MPI_SUCCESS) {
1205     *newtype = new_datatype(tmp);
1206   }
1207 }
1208
1209 void mpi_type_indexed_ (int* count, int* blocklens, int* indices, int* old_type, int*  newtype, int* ierr) {
1210   MPI_Datatype tmp;
1211   *ierr = MPI_Type_indexed(*count, blocklens, indices, get_datatype(*old_type), &tmp);
1212   if(*ierr == MPI_SUCCESS) {
1213     *newtype = new_datatype(tmp);
1214   }
1215 }
1216
1217 void mpi_type_create_indexed_block_ (int* count, int* blocklength, int* indices,  int* old_type,  int*newtype, int* ierr){
1218   MPI_Datatype tmp;
1219   *ierr = MPI_Type_create_indexed_block(*count, *blocklength, indices, get_datatype(*old_type), &tmp);
1220   if(*ierr == MPI_SUCCESS) {
1221     *newtype = new_datatype(tmp);
1222   }
1223 }
1224
1225 void mpi_type_struct_ (int* count, int* blocklens, MPI_Aint* indices, int* old_types, int*  newtype, int* ierr) {
1226   MPI_Datatype tmp;
1227   *ierr = MPI_Type_struct(*count, blocklens, indices, (MPI_Datatype*)old_types, &tmp);
1228   if(*ierr == MPI_SUCCESS) {
1229     *newtype = new_datatype(tmp);
1230   }
1231 }
1232
1233 void mpi_type_create_struct_ (int* count, int* blocklens, MPI_Aint* indices, int*  old_types, int*  newtype, int* ierr) {
1234   MPI_Datatype tmp;
1235   *ierr = MPI_Type_create_struct(*count, blocklens, indices, (MPI_Datatype*)old_types, &tmp);
1236   if(*ierr == MPI_SUCCESS) {
1237     *newtype = new_datatype(tmp);
1238   }
1239 }
1240
1241 void mpi_ssend_ (void* buf, int* count, int* datatype, int* dest, int* tag, int* comm, int* ierr) {
1242  *ierr = MPI_Ssend(buf, *count, get_datatype(*datatype), *dest, *tag, get_comm(*comm));
1243 }
1244
1245 void mpi_ssend_init_ (void* buf, int* count, int* datatype, int* dest, int* tag, int* comm, int* request, int* ierr) {
1246   MPI_Request tmp;
1247  *ierr = MPI_Ssend_init(buf, *count, get_datatype(*datatype), *dest, *tag, get_comm(*comm), &tmp);
1248  if(*ierr == MPI_SUCCESS) {
1249    *request = new_request(tmp);
1250  }
1251 }
1252
1253 void mpi_intercomm_create_ (int* local_comm, int *local_leader, int* peer_comm, int* remote_leader, int* tag, int*  comm_out, int* ierr) {
1254   MPI_Comm tmp;
1255   *ierr = MPI_Intercomm_create(get_comm(*local_comm), *local_leader,get_comm(*peer_comm), *remote_leader, *tag, &tmp);
1256   if(*ierr == MPI_SUCCESS) {
1257     *comm_out = new_comm(tmp);
1258   }
1259 }
1260
1261 void mpi_intercomm_merge_ (int* comm, int* high, int*  comm_out, int* ierr) {
1262  MPI_Comm tmp;
1263  *ierr = MPI_Intercomm_merge(get_comm(*comm), *high, &tmp);
1264  if(*ierr == MPI_SUCCESS) {
1265    *comm_out = new_comm(tmp);
1266  }
1267 }
1268
1269 void mpi_bsend_ (void* buf, int* count, int* datatype, int *dest, int* tag, int* comm, int* ierr) {
1270  *ierr = MPI_Bsend(buf, *count, get_datatype(*datatype), *dest, *tag, get_comm(*comm));
1271 }
1272
1273 void mpi_bsend_init_ (void* buf, int* count, int* datatype, int *dest, int* tag, int* comm, int*  request, int* ierr) {
1274   MPI_Request tmp;
1275   *ierr = MPI_Bsend_init(buf, *count, get_datatype(*datatype), *dest, *tag, get_comm(*comm), &tmp);
1276  if(*ierr == MPI_SUCCESS) {
1277    *request = new_request(tmp);
1278  }
1279 }
1280
1281 void mpi_ibsend_ (void* buf, int* count, int* datatype, int *dest, int* tag, int* comm, int*  request, int* ierr) {
1282   MPI_Request tmp;
1283   *ierr = MPI_Ibsend(buf, *count, get_datatype(*datatype), *dest, *tag, get_comm(*comm), &tmp);
1284  if(*ierr == MPI_SUCCESS) {
1285    *request = new_request(tmp);
1286  }
1287 }
1288
1289 void mpi_comm_remote_group_ (int* comm, int*  group, int* ierr) {
1290   MPI_Group tmp;
1291  *ierr = MPI_Comm_remote_group(get_comm(*comm), &tmp);
1292  if(*ierr == MPI_SUCCESS) {
1293    *group = new_group(tmp);
1294  }
1295 }
1296
1297 void mpi_comm_remote_size_ (int* comm, int* size, int* ierr) {
1298  *ierr = MPI_Comm_remote_size(get_comm(*comm), size);
1299 }
1300
1301 void mpi_issend_ (void* buf, int* count, int* datatype, int *dest, int* tag, int* comm, int*  request, int* ierr) {
1302   MPI_Request tmp;
1303   *ierr = MPI_Issend(buf, *count, get_datatype(*datatype), *dest, *tag, get_comm(*comm), &tmp);
1304  if(*ierr == MPI_SUCCESS) {
1305    *request = new_request(tmp);
1306  }
1307 }
1308
1309 void mpi_probe_ (int* source, int* tag, int* comm, MPI_Status*  status, int* ierr) {
1310  *ierr = MPI_Probe(*source, *tag, get_comm(*comm), status);
1311 }
1312
1313 void mpi_attr_delete_ (int* comm, int* keyval, int* ierr) {
1314  *ierr = MPI_Attr_delete(get_comm(*comm), *keyval);
1315 }
1316
1317 void mpi_attr_put_ (int* comm, int* keyval, void* attr_value, int* ierr) {
1318  *ierr = MPI_Attr_put(get_comm(*comm), *keyval, attr_value);
1319 }
1320
1321 void mpi_rsend_init_ (void* buf, int* count, int* datatype, int *dest, int* tag, int* comm, int*  request, int* ierr) {
1322   MPI_Request tmp;
1323   *ierr = MPI_Rsend_init(buf, *count, get_datatype(*datatype), *dest, *tag, get_comm(*comm), &tmp);
1324  if(*ierr == MPI_SUCCESS) {
1325    *request = new_request(tmp);
1326  }
1327 }
1328
1329 void mpi_keyval_create_ (void* copy_fn, void* delete_fn, int* keyval, void* extra_state, int* ierr) {
1330  *ierr = MPI_Keyval_create((MPI_Copy_function*)copy_fn, (MPI_Delete_function*)delete_fn, keyval, extra_state);
1331 }
1332
1333 void mpi_keyval_free_ (int* keyval, int* ierr) {
1334  *ierr = MPI_Keyval_free(keyval);
1335 }
1336
1337 void mpi_test_cancelled_ (MPI_Status*  status, int* flag, int* ierr) {
1338  *ierr = MPI_Test_cancelled(status, flag);
1339 }
1340
1341 void mpi_pack_ (void* inbuf, int* incount, int* type, void* outbuf, int* outcount, int* position, int* comm, int* ierr) {
1342  *ierr = MPI_Pack(inbuf, *incount, get_datatype(*type), outbuf, *outcount, position, get_comm(*comm));
1343 }
1344
1345 void mpi_get_elements_ (MPI_Status*  status, int* datatype, int* elements, int* ierr) {
1346  *ierr = MPI_Get_elements(status, get_datatype(*datatype), elements);
1347 }
1348
1349 void mpi_dims_create_ (int* nnodes, int* ndims, int* dims, int* ierr) {
1350  *ierr = MPI_Dims_create(*nnodes, *ndims, dims);
1351 }
1352
1353 void mpi_iprobe_ (int* source, int* tag, int* comm, int* flag, MPI_Status*  status, int* ierr) {
1354  *ierr = MPI_Iprobe(*source, *tag, get_comm(*comm), flag, status);
1355 }
1356
1357 void mpi_type_get_envelope_ ( int* datatype, int *num_integers, int *num_addresses, int *num_datatypes, int *combiner, int* ierr){
1358
1359  *ierr = MPI_Type_get_envelope(  get_datatype(*datatype), num_integers,
1360  num_addresses, num_datatypes, combiner);
1361 }
1362
1363 void mpi_type_get_contents_ (int* datatype, int* max_integers, int* max_addresses, int* max_datatypes, int* array_of_integers, MPI_Aint* array_of_addresses,
1364  int* array_of_datatypes, int* ierr){
1365  *ierr = MPI_Type_get_contents(get_datatype(*datatype), *max_integers, *max_addresses,*max_datatypes, array_of_integers, array_of_addresses, (MPI_Datatype*)array_of_datatypes);
1366 }
1367
1368 void mpi_type_create_darray_ (int* size, int* rank, int* ndims, int* array_of_gsizes, int* array_of_distribs, int* array_of_dargs, int* array_of_psizes,
1369  int* order, int* oldtype, int*newtype, int* ierr) {
1370   MPI_Datatype tmp;
1371   *ierr = MPI_Type_create_darray(*size, *rank, *ndims,  array_of_gsizes,
1372   array_of_distribs,  array_of_dargs,  array_of_psizes,
1373   *order,  get_datatype(*oldtype), &tmp) ;
1374   if(*ierr == MPI_SUCCESS) {
1375     *newtype = new_datatype(tmp);
1376   }
1377 }
1378
1379 void mpi_type_create_resized_ (int* oldtype,MPI_Aint* lb, MPI_Aint* extent, int*newtype, int* ierr){
1380   MPI_Datatype tmp;
1381   *ierr = MPI_Type_create_resized(get_datatype(*oldtype),*lb, *extent, &tmp);
1382   if(*ierr == MPI_SUCCESS) {
1383     *newtype = new_datatype(tmp);
1384   }
1385 }
1386
1387 void mpi_type_create_subarray_ (int* ndims,int *array_of_sizes, int *array_of_subsizes, int *array_of_starts, int* order, int* oldtype, int*newtype, int* ierr){
1388   MPI_Datatype tmp;
1389   *ierr = MPI_Type_create_subarray(*ndims,array_of_sizes, array_of_subsizes, array_of_starts, *order, get_datatype(*oldtype), &tmp);
1390   if(*ierr == MPI_SUCCESS) {
1391     *newtype = new_datatype(tmp);
1392   }
1393 }
1394
1395 void mpi_type_match_size_ (int* typeclass,int* size,int* datatype, int* ierr){
1396   MPI_Datatype tmp;
1397   *ierr = MPI_Type_match_size(*typeclass,*size,&tmp);
1398   if(*ierr == MPI_SUCCESS) {
1399     *datatype = new_datatype(tmp);
1400   }
1401 }
1402
1403 void mpi_alltoallw_ ( void *sendbuf, int *sendcnts, int *sdispls, int* sendtypes, void *recvbuf, int *recvcnts, int *rdispls, int* recvtypes,
1404  int* comm, int* ierr){
1405  *ierr = MPI_Alltoallw( sendbuf, sendcnts, sdispls, (MPI_Datatype*) sendtypes, recvbuf, recvcnts, rdispls, (MPI_Datatype*)recvtypes, get_comm(*comm));
1406 }
1407
1408 void mpi_exscan_ (void *sendbuf, void *recvbuf, int* count, int* datatype, int* op, int* comm, int* ierr){
1409  *ierr = MPI_Exscan(sendbuf, recvbuf, *count, get_datatype(*datatype), get_op(*op), get_comm(*comm));
1410 }
1411
1412 void mpi_comm_set_name_ (int* comm, char* name, int* ierr){
1413  *ierr = MPI_Comm_set_name (get_comm(*comm), name);
1414 }
1415
1416 void mpi_comm_dup_with_info_ (int* comm, int* info, int* newcomm, int* ierr){
1417   MPI_Comm tmp;
1418   *ierr = MPI_Comm_dup_with_info(get_comm(*comm),*(MPI_Info*)info,&tmp);
1419   if(*ierr == MPI_SUCCESS) {
1420     *newcomm = new_comm(tmp);
1421   }
1422 }
1423
1424 void mpi_comm_split_type_ (int* comm, int* split_type, int* key, int* info, int* newcomm, int* ierr){
1425   MPI_Comm tmp;
1426   *ierr = MPI_Comm_split_type(get_comm(*comm), *split_type, *key, *(MPI_Info*)info, &tmp);
1427   if(*ierr == MPI_SUCCESS) {
1428     *newcomm = new_comm(tmp);
1429   }
1430 }
1431
1432 void mpi_comm_set_info_ (int* comm, int* info, int* ierr){
1433  *ierr = MPI_Comm_set_info (get_comm(*comm), *(MPI_Info*)info);
1434 }
1435
1436 void mpi_comm_get_info_ (int* comm, int* info, int* ierr){
1437  *ierr = MPI_Comm_get_info (get_comm(*comm), (MPI_Info*)info);
1438 }
1439
1440 void mpi_info_get_ (int* info,char *key,int* valuelen, char *value, int *flag, int* ierr){
1441  *ierr = MPI_Info_get(*(MPI_Info*)info,key,*valuelen, value, flag);
1442 }
1443
1444 void mpi_comm_create_errhandler_ ( void *function, void *errhandler, int* ierr){
1445  *ierr = MPI_Comm_create_errhandler( (MPI_Comm_errhandler_fn*) function, (MPI_Errhandler*)errhandler);
1446 }
1447
1448 void mpi_add_error_class_ ( int *errorclass, int* ierr){
1449  *ierr = MPI_Add_error_class( errorclass);
1450 }
1451
1452 void mpi_add_error_code_ (  int* errorclass, int *errorcode, int* ierr){
1453  *ierr = MPI_Add_error_code(*errorclass, errorcode);
1454 }
1455
1456 void mpi_add_error_string_ ( int* errorcode, char *string, int* ierr){
1457  *ierr = MPI_Add_error_string(*errorcode, string);
1458 }
1459
1460 void mpi_comm_call_errhandler_ (int* comm,int* errorcode, int* ierr){
1461  *ierr = MPI_Comm_call_errhandler(get_comm(*comm), *errorcode);
1462 }
1463
1464 void mpi_info_dup_ (int* info, int* newinfo, int* ierr){
1465  *ierr = MPI_Info_dup(*(MPI_Info*)info, (MPI_Info*)newinfo);
1466 }
1467
1468 void mpi_info_get_valuelen_ ( int* info, char *key, int *valuelen, int *flag, int* ierr){
1469  *ierr = MPI_Info_get_valuelen( *(MPI_Info*)info, key, valuelen, flag);
1470 }
1471
1472 void mpi_info_delete_ (int* info, char *key, int* ierr){
1473  *ierr = MPI_Info_delete(*(MPI_Info*)info, key);
1474 }
1475
1476 void mpi_info_get_nkeys_ ( int* info, int *nkeys, int* ierr){
1477  *ierr = MPI_Info_get_nkeys(  *(MPI_Info*)info, nkeys);
1478 }
1479
1480 void mpi_info_get_nthkey_ ( int* info, int* n, char *key, int* ierr){
1481  *ierr = MPI_Info_get_nthkey( *(MPI_Info*)info, *n, key);
1482 }
1483
1484 void mpi_get_version_ (int *version,int *subversion, int* ierr){
1485  *ierr = MPI_Get_version (version,subversion);
1486 }
1487
1488 void mpi_get_library_version_ (char *version,int *len, int* ierr){
1489  *ierr = MPI_Get_library_version (version,len);
1490 }
1491
1492 void mpi_request_get_status_ ( int* request, int *flag, MPI_Status* status, int* ierr){
1493  *ierr = MPI_Request_get_status( find_request(*request), flag, status);
1494 }
1495
1496 void mpi_grequest_start_ ( void *query_fn, void *free_fn, void *cancel_fn, void *extra_state, int*request, int* ierr){
1497   MPI_Request tmp;
1498   *ierr = MPI_Grequest_start( (MPI_Grequest_query_function*)query_fn, (MPI_Grequest_free_function*)free_fn, (MPI_Grequest_cancel_function*)cancel_fn, extra_state, &tmp);
1499  if(*ierr == MPI_SUCCESS) {
1500    *request = new_request(tmp);
1501  }
1502 }
1503
1504 void mpi_grequest_complete_ ( int* request, int* ierr){
1505  *ierr = MPI_Grequest_complete( find_request(*request));
1506 }
1507
1508 void mpi_status_set_cancelled_ (MPI_Status* status,int* flag, int* ierr){
1509  *ierr = MPI_Status_set_cancelled(status,*flag);
1510 }
1511
1512 void mpi_status_set_elements_ ( MPI_Status* status, int* datatype, int* count, int* ierr){
1513  *ierr = MPI_Status_set_elements( status, get_datatype(*datatype), *count);
1514 }
1515
1516 void mpi_comm_connect_ ( char *port_name, int* info, int* root, int* comm, int*newcomm, int* ierr){
1517   MPI_Comm tmp;
1518   *ierr = MPI_Comm_connect( port_name, *(MPI_Info*)info, *root, get_comm(*comm), &tmp);
1519   if(*ierr == MPI_SUCCESS) {
1520     *newcomm = new_comm(tmp);
1521   }
1522 }
1523
1524 void mpi_publish_name_ ( char *service_name, int* info, char *port_name, int* ierr){
1525  *ierr = MPI_Publish_name( service_name, *(MPI_Info*)info, port_name);
1526 }
1527
1528 void mpi_unpublish_name_ ( char *service_name, int* info, char *port_name, int* ierr){
1529  *ierr = MPI_Unpublish_name( service_name, *(MPI_Info*)info, port_name);
1530 }
1531
1532 void mpi_lookup_name_ ( char *service_name, int* info, char *port_name, int* ierr){
1533  *ierr = MPI_Lookup_name( service_name, *(MPI_Info*)info, port_name);
1534 }
1535
1536 void mpi_comm_join_ ( int* fd, int* intercomm, int* ierr){
1537   MPI_Comm tmp;
1538   *ierr = MPI_Comm_join( *fd, &tmp);
1539   if(*ierr == MPI_SUCCESS) {
1540     *intercomm = new_comm(tmp);
1541   }
1542 }
1543
1544 void mpi_open_port_ ( int* info, char *port_name, int* ierr){
1545  *ierr = MPI_Open_port( *(MPI_Info*)info,port_name);
1546 }
1547
1548 void mpi_close_port_ ( char *port_name, int* ierr){
1549  *ierr = MPI_Close_port( port_name);
1550 }
1551
1552 void mpi_comm_accept_ ( char *port_name, int* info, int* root, int* comm, int*newcomm, int* ierr){
1553   MPI_Comm tmp;
1554   *ierr = MPI_Comm_accept( port_name, *(MPI_Info*)info, *root, get_comm(*comm), &tmp);
1555   if(*ierr == MPI_SUCCESS) {
1556     *newcomm = new_comm(tmp);
1557   }
1558 }
1559
1560 void mpi_comm_spawn_ ( char *command, char *argv, int* maxprocs, int* info, int* root, int* comm, int* intercomm, int* array_of_errcodes, int* ierr){
1561   MPI_Comm tmp;
1562   *ierr = MPI_Comm_spawn( command, NULL, *maxprocs, *(MPI_Info*)info, *root, get_comm(*comm), &tmp, array_of_errcodes);
1563   if(*ierr == MPI_SUCCESS) {
1564     *intercomm = new_comm(tmp);
1565   }
1566 }
1567
1568 void mpi_comm_spawn_multiple_ ( int* count, char *array_of_commands, char** array_of_argv, int* array_of_maxprocs, int* array_of_info, int* root,
1569  int* comm, int* intercomm, int* array_of_errcodes, int* ierr){
1570  MPI_Comm tmp;
1571  *ierr = MPI_Comm_spawn_multiple(* count, &array_of_commands, &array_of_argv, array_of_maxprocs,
1572  (MPI_Info*)array_of_info, *root, get_comm(*comm), &tmp, array_of_errcodes);
1573  if(*ierr == MPI_SUCCESS) {
1574    *intercomm = new_comm(tmp);
1575  }
1576 }
1577
1578 void mpi_comm_get_parent_ ( int* parent, int* ierr){
1579   MPI_Comm tmp;
1580   *ierr = MPI_Comm_get_parent( &tmp);
1581   if(*ierr == MPI_SUCCESS) {
1582     *parent = new_comm(tmp);
1583   }
1584 }