Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / smpi / bindings / smpi_f77.cpp
1 /* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "private.hpp"
7 #include "smpi_comm.hpp"
8 #include "smpi_datatype.hpp"
9 #include "smpi_errhandler.hpp"
10 #include "smpi_op.hpp"
11 #include "smpi_request.hpp"
12 #include "smpi_win.hpp"
13 #include "src/smpi/include/smpi_actor.hpp"
14
15 static int running_processes = 0;
16
17 void smpi_init_fortran_types()
18 {
19   if (simgrid::smpi::F2C::lookup() == nullptr) {
20     MPI_COMM_WORLD->add_f();
21     MPI_BYTE->add_f(); // MPI_BYTE
22     MPI_CHAR->add_f(); // MPI_CHARACTER
23     if (sizeof(void*) == 8) {
24       MPI_C_BOOL->add_f(); // MPI_LOGICAL
25       MPI_INT->add_f();    // MPI_INTEGER
26     } else {
27       MPI_C_BOOL->add_f(); // MPI_LOGICAL
28       MPI_LONG->add_f();   // MPI_INTEGER
29     }
30     MPI_INT8_T->add_f();    // MPI_INTEGER1
31     MPI_INT16_T->add_f();   // MPI_INTEGER2
32     MPI_INT32_T->add_f();   // MPI_INTEGER4
33     MPI_INT64_T->add_f();   // MPI_INTEGER8
34     MPI_REAL->add_f();      // MPI_REAL
35     MPI_REAL4->add_f();     // MPI_REAL4
36     MPI_REAL8->add_f();     // MPI_REAL8
37     MPI_DOUBLE->add_f();    // MPI_DOUBLE_PRECISION
38     MPI_COMPLEX8->add_f();  // MPI_COMPLEX
39     MPI_COMPLEX16->add_f(); // MPI_DOUBLE_COMPLEX
40     if (sizeof(void*) == 8)
41       MPI_2INT->add_f(); // MPI_2INTEGER
42     else
43       MPI_2LONG->add_f();   // MPI_2INTEGER
44     MPI_UINT8_T->add_f();   // MPI_LOGICAL1
45     MPI_UINT16_T->add_f();  // MPI_LOGICAL2
46     MPI_UINT32_T->add_f();  // MPI_LOGICAL4
47     MPI_UINT64_T->add_f();  // MPI_LOGICAL8
48     MPI_2FLOAT->add_f();    // MPI_2REAL
49     MPI_2DOUBLE->add_f();   // MPI_2DOUBLE_PRECISION
50     MPI_PTR->add_f();       // MPI_AINT
51     MPI_OFFSET->add_f();    // MPI_OFFSET
52     MPI_AINT->add_f();      // MPI_COUNT
53     MPI_REAL16->add_f();    // MPI_REAL16
54     MPI_PACKED->add_f();    // MPI_PACKED
55     MPI_COMPLEX8->add_f();  // MPI_COMPLEX8
56     MPI_COMPLEX16->add_f(); // MPI_COMPLEX16
57     MPI_COMPLEX32->add_f(); // MPI_COMPLEX32
58
59     MPI_MAX->add_f();
60     MPI_MIN->add_f();
61     MPI_MAXLOC->add_f();
62     MPI_MINLOC->add_f();
63     MPI_SUM->add_f();
64     MPI_PROD->add_f();
65     MPI_LAND->add_f();
66     MPI_LOR->add_f();
67     MPI_LXOR->add_f();
68     MPI_BAND->add_f();
69     MPI_BOR->add_f();
70     MPI_BXOR->add_f();
71
72     MPI_ERRORS_RETURN->add_f();
73     MPI_ERRORS_ARE_FATAL->add_f();
74   }
75 }
76
77 extern "C" { // This should really use the C linkage to be usable from Fortran
78
79 void mpi_init_(int* ierr)
80 {
81   smpi_init_fortran_types();
82   *ierr = MPI_Init(nullptr, nullptr);
83   running_processes++;
84 }
85
86 void mpi_finalize_(int* ierr)
87 {
88   *ierr = MPI_Finalize();
89   running_processes--;
90 }
91
92 void mpi_abort_(int* comm, int* errorcode, int* ierr)
93 {
94   *ierr = MPI_Abort(simgrid::smpi::Comm::f2c(*comm), *errorcode);
95 }
96
97 double mpi_wtime_()
98 {
99   return MPI_Wtime();
100 }
101
102 double mpi_wtick_()
103 {
104   return MPI_Wtick();
105 }
106
107 void mpi_group_incl_(int* group, int* n, int* ranks, int* group_out, int* ierr)
108 {
109   MPI_Group tmp;
110
111   *ierr = MPI_Group_incl(simgrid::smpi::Group::f2c(*group), *n, ranks, &tmp);
112   if(*ierr == MPI_SUCCESS) {
113     *group_out = tmp->c2f();
114   }
115 }
116
117 void mpi_initialized_(int* flag, int* ierr)
118 {
119   *ierr = MPI_Initialized(flag);
120 }
121
122 void mpi_get_processor_name_(char* name, int* resultlen, int* ierr)
123 {
124   //fortran does not handle string endings cleanly, so initialize everything before
125   memset(name, 0, MPI_MAX_PROCESSOR_NAME);
126   *ierr = MPI_Get_processor_name(name, resultlen);
127 }
128
129 void mpi_get_count_(MPI_Status* status, int* datatype, int* count, int* ierr)
130 {
131   *ierr = MPI_Get_count(FORT_STATUS_IGNORE(status), simgrid::smpi::Datatype::f2c(*datatype), count);
132 }
133
134 void mpi_attr_get_(int* comm, int* keyval, int* attr_value, int* flag, int* ierr)
135 {
136   int* value = nullptr;
137   *ierr = MPI_Attr_get(simgrid::smpi::Comm::f2c(*comm), *keyval, &value, flag);
138   if(*flag == 1)
139     *attr_value=*value;
140 }
141
142 void mpi_error_string_(int* errorcode, char* string, int* resultlen, int* ierr)
143 {
144   *ierr = MPI_Error_string(*errorcode, string, resultlen);
145 }
146
147 void mpi_win_fence_(int* assert, int* win, int* ierr)
148 {
149   *ierr =  MPI_Win_fence(* assert, simgrid::smpi::Win::f2c(*win));
150 }
151
152 void mpi_win_free_(int* win, int* ierr)
153 {
154   MPI_Win tmp = simgrid::smpi::Win::f2c(*win);
155   *ierr =  MPI_Win_free(&tmp);
156   if(*ierr == MPI_SUCCESS) {
157     simgrid::smpi::F2C::free_f(*win);
158   }
159 }
160
161 void mpi_win_create_(int* base, MPI_Aint* size, int* disp_unit, int* info, int* comm, int* win, int* ierr)
162 {
163   MPI_Win tmp;
164   *ierr =  MPI_Win_create( static_cast<void*>(base), *size, *disp_unit, simgrid::smpi::Info::f2c(*info), simgrid::smpi::Comm::f2c(*comm),&tmp);
165   if (*ierr == MPI_SUCCESS) {
166     *win = tmp->add_f();
167   }
168 }
169
170 void mpi_win_post_(int* group, int assert, int* win, int* ierr)
171 {
172   *ierr =  MPI_Win_post(simgrid::smpi::Group::f2c(*group), assert, simgrid::smpi::Win::f2c(*win));
173 }
174
175 void mpi_win_start_(int* group, int assert, int* win, int* ierr)
176 {
177   *ierr =  MPI_Win_start(simgrid::smpi::Group::f2c(*group), assert, simgrid::smpi::Win::f2c(*win));
178 }
179
180 void mpi_win_complete_(int* win, int* ierr)
181 {
182   *ierr =  MPI_Win_complete(simgrid::smpi::Win::f2c(*win));
183 }
184
185 void mpi_win_wait_(int* win, int* ierr)
186 {
187   *ierr =  MPI_Win_wait(simgrid::smpi::Win::f2c(*win));
188 }
189
190 void mpi_win_set_name_(int* win, char* name, int* ierr, int size)
191 {
192   //handle trailing blanks
193   while(name[size-1]==' ')
194     size--;
195   while(*name==' '){//handle leading blanks
196     size--;
197     name++;
198   }
199   char* tname = xbt_new(char,size+1);
200   strncpy(tname, name, size);
201   tname[size]='\0';
202   *ierr = MPI_Win_set_name(simgrid::smpi::Win::f2c(*win), tname);
203   xbt_free(tname);
204 }
205
206 void mpi_win_get_name_(int* win, char* name, int* len, int* ierr)
207 {
208   *ierr = MPI_Win_get_name(simgrid::smpi::Win::f2c(*win),name,len);
209   if(*len>0)
210     name[*len]=' ';//blank padding, not \0
211 }
212
213 void mpi_win_allocate_(MPI_Aint* size, int* disp_unit, int* info, int* comm, void* base, int* win, int* ierr)
214 {
215   MPI_Win tmp;
216   *ierr =  MPI_Win_allocate( *size, *disp_unit, simgrid::smpi::Info::f2c(*info), simgrid::smpi::Comm::f2c(*comm),static_cast<void*>(base),&tmp);
217  if(*ierr == MPI_SUCCESS) {
218    *win = tmp->add_f();
219  }
220 }
221
222 void mpi_win_attach_(int* win, int* base, MPI_Aint* size, int* ierr)
223 {
224   *ierr =  MPI_Win_attach(simgrid::smpi::Win::f2c(*win), static_cast<void*>(base), *size);
225 }
226
227 void mpi_win_create_dynamic_(int* info, int* comm, int* win, int* ierr)
228 {
229   MPI_Win tmp;
230   *ierr =  MPI_Win_create_dynamic( simgrid::smpi::Info::f2c(*info), simgrid::smpi::Comm::f2c(*comm),&tmp);
231  if(*ierr == MPI_SUCCESS) {
232    *win = tmp->add_f();
233  }
234 }
235
236 void mpi_win_detach_(int* win, int* base, int* ierr)
237 {
238   *ierr =  MPI_Win_detach(simgrid::smpi::Win::f2c(*win), static_cast<void*>(base));
239 }
240
241 void mpi_win_set_info_(int* win, int* info, int* ierr)
242 {
243   *ierr =  MPI_Win_set_info(simgrid::smpi::Win::f2c(*win), simgrid::smpi::Info::f2c(*info));
244 }
245
246 void mpi_win_get_info_(int* win, int* info, int* ierr)
247 {
248   MPI_Info tmp;
249   *ierr =  MPI_Win_get_info(simgrid::smpi::Win::f2c(*win), &tmp);
250   if (*ierr == MPI_SUCCESS) {
251     *info = tmp->add_f();
252   }
253 }
254
255 void mpi_win_get_group_(int* win, int* group, int* ierr)
256 {
257   MPI_Group tmp;
258   *ierr =  MPI_Win_get_group(simgrid::smpi::Win::f2c(*win), &tmp);
259   if (*ierr == MPI_SUCCESS) {
260     *group = tmp->add_f();
261   }
262 }
263
264 void mpi_win_get_attr_(int* win, int* type_keyval, MPI_Aint* attribute_val, int* flag, int* ierr)
265 {
266   MPI_Aint* value = nullptr;
267   *ierr = MPI_Win_get_attr(simgrid::smpi::Win::f2c(*win), *type_keyval, &value, flag);
268   if (*flag == 1)
269     *attribute_val = *value;
270 }
271
272 void mpi_win_set_attr_(int* win, int* type_keyval, MPI_Aint* att, int* ierr)
273 {
274   MPI_Aint* val = (MPI_Aint*)xbt_malloc(sizeof(MPI_Aint));
275   *val          = *att;
276   *ierr = MPI_Win_set_attr(simgrid::smpi::Win::f2c(*win), *type_keyval, val);
277 }
278
279 void mpi_win_delete_attr_(int* win, int* comm_keyval, int* ierr)
280 {
281   *ierr = MPI_Win_delete_attr (simgrid::smpi::Win::f2c(*win),  *comm_keyval);
282 }
283
284 void mpi_win_create_keyval_(void* copy_fn, void* delete_fn, int* keyval, void* extra_state, int* ierr)
285 {
286   smpi_copy_fn _copy_fn={nullptr,nullptr,nullptr,nullptr,nullptr,(*(int*)copy_fn) == 0 ? nullptr : reinterpret_cast<MPI_Win_copy_attr_function_fort*>(copy_fn)};
287   smpi_delete_fn _delete_fn={nullptr,nullptr,nullptr,nullptr,nullptr,(*(int*)delete_fn) == 0 ? nullptr : reinterpret_cast<MPI_Win_delete_attr_function_fort*>(delete_fn)};
288   *ierr = simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Win>(_copy_fn, _delete_fn, keyval, extra_state);
289 }
290
291 void mpi_win_free_keyval_(int* keyval, int* ierr)
292 {
293   *ierr = MPI_Win_free_keyval( keyval);
294 }
295
296 void mpi_win_lock_(int* lock_type, int* rank, int* assert, int* win, int* ierr)
297 {
298   *ierr = MPI_Win_lock(*lock_type, *rank, *assert, simgrid::smpi::Win::f2c(*win));
299 }
300
301 void mpi_win_lock_all_(int* assert, int* win, int* ierr)
302 {
303   *ierr = MPI_Win_lock_all(*assert, simgrid::smpi::Win::f2c(*win));
304 }
305
306 void mpi_win_unlock_(int* rank, int* win, int* ierr)
307 {
308   *ierr = MPI_Win_unlock(*rank, simgrid::smpi::Win::f2c(*win));
309 }
310
311 void mpi_win_unlock_all_(int* win, int* ierr)
312 {
313   *ierr = MPI_Win_unlock_all(simgrid::smpi::Win::f2c(*win));
314 }
315
316 void mpi_win_flush_(int* rank, int* win, int* ierr)
317 {
318   *ierr = MPI_Win_flush(*rank, simgrid::smpi::Win::f2c(*win));
319 }
320
321 void mpi_win_flush_local_(int* rank, int* win, int* ierr)
322 {
323   *ierr = MPI_Win_flush_local(*rank, simgrid::smpi::Win::f2c(*win));
324 }
325
326 void mpi_win_flush_all_(int* win, int* ierr)
327 {
328   *ierr = MPI_Win_flush_all(simgrid::smpi::Win::f2c(*win));
329 }
330
331 void mpi_win_flush_local_all_(int* win, int* ierr)
332 {
333   *ierr = MPI_Win_flush_local_all(simgrid::smpi::Win::f2c(*win));
334 }
335
336 void mpi_win_null_copy_fn_(int* /*win*/, int* /*keyval*/, int* /*extrastate*/, MPI_Aint* /*valin*/,
337                            MPI_Aint* /*valout*/, int* flag, int* ierr)
338 {
339   *flag=0;
340   *ierr=MPI_SUCCESS;
341 }
342
343 void mpi_win_dup_fn_(int* /*win*/, int* /*keyval*/, int* /*extrastate*/, MPI_Aint* valin, MPI_Aint* valout, int* flag,
344                      int* ierr)
345 {
346   *flag=1;
347   *valout=*valin;
348   *ierr=MPI_SUCCESS;
349 }
350
351 void mpi_info_create_(int* info, int* ierr)
352 {
353   MPI_Info tmp;
354   *ierr =  MPI_Info_create(&tmp);
355   if(*ierr == MPI_SUCCESS) {
356     *info = tmp->add_f();
357   }
358 }
359
360 void mpi_info_set_(int* info, char* key, char* value, int* ierr, unsigned int keylen, unsigned int valuelen)
361 {
362   //handle trailing blanks
363   while(key[keylen-1]==' ')
364     keylen--;
365   while(*key==' '){//handle leading blanks
366     keylen--;
367     key++;
368   }
369   char* tkey = xbt_new(char,keylen+1);
370   strncpy(tkey, key, keylen);
371   tkey[keylen]='\0';
372
373   while(value[valuelen-1]==' ')
374     valuelen--;
375   while(*value==' '){//handle leading blanks
376     valuelen--;
377     value++;
378   }
379   char* tvalue = xbt_new(char,valuelen+1);
380   strncpy(tvalue, value, valuelen);
381   tvalue[valuelen]='\0';
382
383   *ierr =  MPI_Info_set( simgrid::smpi::Info::f2c(*info), tkey, tvalue);
384   xbt_free(tkey);
385   xbt_free(tvalue);
386 }
387
388 void mpi_info_get_(int* info, char* key, int* valuelen, char* value, int* flag, int* ierr, unsigned int keylen)
389 {
390   while(key[keylen-1]==' ')
391     keylen--;
392   while(*key==' '){//handle leading blanks
393     keylen--;
394     key++;
395   }
396   char* tkey = xbt_new(char,keylen+1);
397   strncpy(tkey, key, keylen);
398   tkey[keylen]='\0';
399   *ierr = MPI_Info_get(simgrid::smpi::Info::f2c(*info),tkey,*valuelen, value, flag);
400   xbt_free(tkey);
401   if(*flag!=0){
402     int replace=0;
403     int i=0;
404     for (i=0; i<*valuelen; i++){
405       if(value[i]=='\0')
406         replace=1;
407       if(replace)
408         value[i]=' ';
409     }
410   }
411 }
412
413 void mpi_info_free_(int* info, int* ierr)
414 {
415   MPI_Info tmp = simgrid::smpi::Info::f2c(*info);
416   *ierr =  MPI_Info_free(&tmp);
417   if(*ierr == MPI_SUCCESS) {
418     simgrid::smpi::F2C::free_f(*info);
419   }
420 }
421
422 void mpi_get_(int* origin_addr, int* origin_count, int* origin_datatype, int* target_rank, MPI_Aint* target_disp,
423               int* target_count, int* tarsmpi_type_f2c, int* win, int* ierr)
424 {
425   *ierr =  MPI_Get( static_cast<void*>(origin_addr),*origin_count, simgrid::smpi::Datatype::f2c(*origin_datatype),*target_rank,
426       *target_disp, *target_count, simgrid::smpi::Datatype::f2c(*tarsmpi_type_f2c), simgrid::smpi::Win::f2c(*win));
427 }
428
429 void mpi_rget_(int* origin_addr, int* origin_count, int* origin_datatype, int* target_rank, MPI_Aint* target_disp,
430                int* target_count, int* tarsmpi_type_f2c, int* win, int* request, int* ierr)
431 {
432   MPI_Request req;
433   *ierr =  MPI_Rget( static_cast<void*>(origin_addr),*origin_count, simgrid::smpi::Datatype::f2c(*origin_datatype),*target_rank,
434       *target_disp, *target_count, simgrid::smpi::Datatype::f2c(*tarsmpi_type_f2c), simgrid::smpi::Win::f2c(*win), &req);
435   if(*ierr == MPI_SUCCESS) {
436     *request = req->add_f();
437   }
438 }
439
440 void mpi_accumulate_(int* origin_addr, int* origin_count, int* origin_datatype, int* target_rank, MPI_Aint* target_disp,
441                      int* target_count, int* tarsmpi_type_f2c, int* op, int* win, int* ierr)
442 {
443   *ierr =  MPI_Accumulate( static_cast<void *>(origin_addr),*origin_count, simgrid::smpi::Datatype::f2c(*origin_datatype),*target_rank,
444       *target_disp, *target_count, simgrid::smpi::Datatype::f2c(*tarsmpi_type_f2c), simgrid::smpi::Op::f2c(*op), simgrid::smpi::Win::f2c(*win));
445 }
446
447 void mpi_raccumulate_(int* origin_addr, int* origin_count, int* origin_datatype, int* target_rank,
448                       MPI_Aint* target_disp, int* target_count, int* tarsmpi_type_f2c, int* op, int* win, int* request,
449                       int* ierr)
450 {
451   MPI_Request req;
452   *ierr =  MPI_Raccumulate( static_cast<void *>(origin_addr),*origin_count, simgrid::smpi::Datatype::f2c(*origin_datatype),*target_rank,
453       *target_disp, *target_count, simgrid::smpi::Datatype::f2c(*tarsmpi_type_f2c), simgrid::smpi::Op::f2c(*op), simgrid::smpi::Win::f2c(*win),&req);
454   if(*ierr == MPI_SUCCESS) {
455     *request = req->add_f();
456   }
457 }
458
459 void mpi_put_(int* origin_addr, int* origin_count, int* origin_datatype, int* target_rank, MPI_Aint* target_disp,
460               int* target_count, int* tarsmpi_type_f2c, int* win, int* ierr)
461 {
462   *ierr =  MPI_Put( static_cast<void *>(origin_addr),*origin_count, simgrid::smpi::Datatype::f2c(*origin_datatype),*target_rank,
463       *target_disp, *target_count, simgrid::smpi::Datatype::f2c(*tarsmpi_type_f2c), simgrid::smpi::Win::f2c(*win));
464 }
465
466 void mpi_rput_(int* origin_addr, int* origin_count, int* origin_datatype, int* target_rank, MPI_Aint* target_disp,
467                int* target_count, int* tarsmpi_type_f2c, int* win, int* request, int* ierr)
468 {
469   MPI_Request req;
470   *ierr =  MPI_Rput( static_cast<void *>(origin_addr),*origin_count, simgrid::smpi::Datatype::f2c(*origin_datatype),*target_rank,
471       *target_disp, *target_count, simgrid::smpi::Datatype::f2c(*tarsmpi_type_f2c), simgrid::smpi::Win::f2c(*win),&req);
472   if(*ierr == MPI_SUCCESS) {
473     *request = req->add_f();
474   }
475 }
476
477 void mpi_fetch_and_op_(int* origin_addr, int* result_addr, int* datatype, int* target_rank, MPI_Aint* target_disp,
478                        int* op, int* win, int* ierr)
479 {
480   *ierr =  MPI_Fetch_and_op( static_cast<void *>(origin_addr),
481               static_cast<void *>(result_addr), simgrid::smpi::Datatype::f2c(*datatype),*target_rank,
482               *target_disp, simgrid::smpi::Op::f2c(*op), simgrid::smpi::Win::f2c(*win));
483 }
484
485 void mpi_compare_and_swap_(int* origin_addr, int* compare_addr, int* result_addr, int* datatype, int* target_rank,
486                            MPI_Aint* target_disp, int* win, int* ierr)
487 {
488   *ierr =  MPI_Compare_and_swap( static_cast<void *>(origin_addr),static_cast<void *>(compare_addr),
489               static_cast<void *>(result_addr), simgrid::smpi::Datatype::f2c(*datatype),*target_rank,
490               *target_disp, simgrid::smpi::Win::f2c(*win));
491 }
492
493 void mpi_get_accumulate_(int* origin_addr, int* origin_count, int* origin_datatype, int* result_addr, int* result_count,
494                          int* result_datatype, int* target_rank, MPI_Aint* target_disp, int* target_count,
495                          int* target_datatype, int* op, int* win, int* ierr)
496 {
497   *ierr =
498       MPI_Get_accumulate(static_cast<void*>(origin_addr), *origin_count, simgrid::smpi::Datatype::f2c(*origin_datatype),
499                          static_cast<void*>(result_addr), *result_count, simgrid::smpi::Datatype::f2c(*result_datatype),
500                          *target_rank, *target_disp, *target_count, simgrid::smpi::Datatype::f2c(*target_datatype),
501                          simgrid::smpi::Op::f2c(*op), simgrid::smpi::Win::f2c(*win));
502 }
503
504 void mpi_rget_accumulate_(int* origin_addr, int* origin_count, int* origin_datatype, int* result_addr,
505                           int* result_count, int* result_datatype, int* target_rank, MPI_Aint* target_disp,
506                           int* target_count, int* target_datatype, int* op, int* win, int* request, int* ierr)
507 {
508   MPI_Request req;
509   *ierr = MPI_Rget_accumulate(static_cast<void*>(origin_addr), *origin_count,
510                               simgrid::smpi::Datatype::f2c(*origin_datatype), static_cast<void*>(result_addr),
511                               *result_count, simgrid::smpi::Datatype::f2c(*result_datatype), *target_rank, *target_disp,
512                               *target_count, simgrid::smpi::Datatype::f2c(*target_datatype),
513                               simgrid::smpi::Op::f2c(*op), simgrid::smpi::Win::f2c(*win), &req);
514   if(*ierr == MPI_SUCCESS) {
515     *request = req->add_f();
516   }
517 }
518
519 //following are automatically generated, and have to be checked
520 void mpi_finalized_(int* flag, int* ierr)
521 {
522   *ierr = MPI_Finalized(flag);
523 }
524
525 void mpi_init_thread_(int* required, int* provided, int* ierr)
526 {
527   smpi_init_fortran_types();
528   *ierr = MPI_Init_thread(nullptr, nullptr,*required, provided);
529   running_processes++;
530 }
531
532 void mpi_query_thread_(int* provided, int* ierr)
533 {
534   *ierr = MPI_Query_thread(provided);
535 }
536
537 void mpi_is_thread_main_(int* flag, int* ierr)
538 {
539   *ierr = MPI_Is_thread_main(flag);
540 }
541
542 void mpi_address_(void* location, MPI_Aint* address, int* ierr)
543 {
544   *ierr = MPI_Address(location, address);
545 }
546
547 void mpi_get_address_(void* location, MPI_Aint* address, int* ierr)
548 {
549   *ierr = MPI_Get_address(location, address);
550 }
551
552 void mpi_pcontrol_(int* level, int* ierr)
553 {
554   *ierr = MPI_Pcontrol(*static_cast<const int*>(level));
555 }
556
557 void mpi_op_create_(void* function, int* commute, int* op, int* ierr)
558 {
559   MPI_Op tmp;
560   *ierr = MPI_Op_create(reinterpret_cast<MPI_User_function*>(function), *commute, &tmp);
561   if (*ierr == MPI_SUCCESS) {
562     tmp->set_fortran_op();
563     *op = tmp->add_f();
564   }
565 }
566
567 void mpi_op_free_(int* op, int* ierr)
568 {
569   MPI_Op tmp= simgrid::smpi::Op::f2c(*op);
570   *ierr = MPI_Op_free(& tmp);
571   if(*ierr == MPI_SUCCESS) {
572     simgrid::smpi::F2C::free_f(*op);
573   }
574 }
575
576 void mpi_op_commutative_(int* op, int* commute, int* ierr)
577 {
578   *ierr = MPI_Op_commutative(simgrid::smpi::Op::f2c(*op), commute);
579 }
580
581 void mpi_group_free_(int* group, int* ierr)
582 {
583   MPI_Group tmp = simgrid::smpi::Group::f2c(*group);
584   if(tmp != MPI_COMM_WORLD->group() && tmp != MPI_GROUP_EMPTY){
585     simgrid::smpi::Group::unref(tmp);
586     simgrid::smpi::F2C::free_f(*group);
587   }
588   *ierr = MPI_SUCCESS;
589 }
590
591 void mpi_group_size_(int* group, int* size, int* ierr)
592 {
593   *ierr = MPI_Group_size(simgrid::smpi::Group::f2c(*group), size);
594 }
595
596 void mpi_group_rank_(int* group, int* rank, int* ierr)
597 {
598   *ierr = MPI_Group_rank(simgrid::smpi::Group::f2c(*group), rank);
599 }
600
601 void mpi_group_translate_ranks_ (int* group1, int* n, int *ranks1, int* group2, int *ranks2, int* ierr)
602 {
603  *ierr = MPI_Group_translate_ranks(simgrid::smpi::Group::f2c(*group1), *n, ranks1, simgrid::smpi::Group::f2c(*group2), ranks2);
604 }
605
606 void mpi_group_compare_(int* group1, int* group2, int* result, int* ierr)
607 {
608   *ierr = MPI_Group_compare(simgrid::smpi::Group::f2c(*group1), simgrid::smpi::Group::f2c(*group2), result);
609 }
610
611 void mpi_group_union_(int* group1, int* group2, int* newgroup, int* ierr)
612 {
613   MPI_Group tmp;
614   *ierr = MPI_Group_union(simgrid::smpi::Group::f2c(*group1), simgrid::smpi::Group::f2c(*group2), &tmp);
615   if (*ierr == MPI_SUCCESS) {
616     *newgroup = tmp->add_f();
617   }
618 }
619
620 void mpi_group_intersection_(int* group1, int* group2, int* newgroup, int* ierr)
621 {
622   MPI_Group tmp;
623   *ierr = MPI_Group_intersection(simgrid::smpi::Group::f2c(*group1), simgrid::smpi::Group::f2c(*group2), &tmp);
624   if (*ierr == MPI_SUCCESS) {
625     *newgroup = tmp->add_f();
626   }
627 }
628
629 void mpi_group_difference_(int* group1, int* group2, int* newgroup, int* ierr)
630 {
631   MPI_Group tmp;
632   *ierr = MPI_Group_difference(simgrid::smpi::Group::f2c(*group1), simgrid::smpi::Group::f2c(*group2), &tmp);
633   if (*ierr == MPI_SUCCESS) {
634     *newgroup = tmp->add_f();
635   }
636 }
637
638 void mpi_group_excl_(int* group, int* n, int* ranks, int* newgroup, int* ierr)
639 {
640   MPI_Group tmp;
641   *ierr = MPI_Group_excl(simgrid::smpi::Group::f2c(*group), *n, ranks, &tmp);
642   if (*ierr == MPI_SUCCESS) {
643     *newgroup = tmp->add_f();
644   }
645 }
646
647 void mpi_group_range_incl_ (int* group, int* n, int ranges[][3], int* newgroup, int* ierr)
648 {
649   MPI_Group tmp;
650   *ierr = MPI_Group_range_incl(simgrid::smpi::Group::f2c(*group), *n, ranges, &tmp);
651   if (*ierr == MPI_SUCCESS) {
652     *newgroup = tmp->add_f();
653   }
654 }
655
656 void mpi_group_range_excl_ (int* group, int* n, int ranges[][3], int* newgroup, int* ierr)
657 {
658  MPI_Group tmp;
659  *ierr = MPI_Group_range_excl(simgrid::smpi::Group::f2c(*group), *n, ranges, &tmp);
660  if(*ierr == MPI_SUCCESS) {
661    *newgroup = tmp->add_f();
662  }
663 }
664
665 void mpi_request_free_ (int* request, int* ierr){
666   MPI_Request tmp=simgrid::smpi::Request::f2c(*request);
667  *ierr = MPI_Request_free(&tmp);
668  if(*ierr == MPI_SUCCESS) {
669    simgrid::smpi::Request::free_f(*request);
670  }
671 }
672
673 void mpi_pack_size_ (int* incount, int* datatype, int* comm, int* size, int* ierr) {
674  *ierr = MPI_Pack_size(*incount, simgrid::smpi::Datatype::f2c(*datatype), simgrid::smpi::Comm::f2c(*comm), size);
675 }
676
677 void mpi_cart_coords_ (int* comm, int* rank, int* maxdims, int* coords, int* ierr) {
678  *ierr = MPI_Cart_coords(simgrid::smpi::Comm::f2c(*comm), *rank, *maxdims, coords);
679 }
680
681 void mpi_cart_create_ (int* comm_old, int* ndims, int* dims, int* periods, int* reorder, int*  comm_cart, int* ierr) {
682   MPI_Comm tmp;
683  *ierr = MPI_Cart_create(simgrid::smpi::Comm::f2c(*comm_old), *ndims, dims, periods, *reorder, &tmp);
684  if(*ierr == MPI_SUCCESS) {
685    *comm_cart = tmp->add_f();
686  }
687 }
688
689 void mpi_cart_get_ (int* comm, int* maxdims, int* dims, int* periods, int* coords, int* ierr) {
690  *ierr = MPI_Cart_get(simgrid::smpi::Comm::f2c(*comm), *maxdims, dims, periods, coords);
691 }
692
693 void mpi_cart_map_ (int* comm_old, int* ndims, int* dims, int* periods, int* newrank, int* ierr) {
694  *ierr = MPI_Cart_map(simgrid::smpi::Comm::f2c(*comm_old), *ndims, dims, periods, newrank);
695 }
696
697 void mpi_cart_rank_ (int* comm, int* coords, int* rank, int* ierr) {
698  *ierr = MPI_Cart_rank(simgrid::smpi::Comm::f2c(*comm), coords, rank);
699 }
700
701 void mpi_cart_shift_ (int* comm, int* direction, int* displ, int* source, int* dest, int* ierr) {
702  *ierr = MPI_Cart_shift(simgrid::smpi::Comm::f2c(*comm), *direction, *displ, source, dest);
703 }
704
705 void mpi_cart_sub_ (int* comm, int* remain_dims, int*  comm_new, int* ierr) {
706  MPI_Comm tmp;
707  *ierr = MPI_Cart_sub(simgrid::smpi::Comm::f2c(*comm), remain_dims, &tmp);
708  if(*ierr == MPI_SUCCESS) {
709    *comm_new = tmp->add_f();
710  }
711 }
712
713 void mpi_cartdim_get_ (int* comm, int* ndims, int* ierr) {
714  *ierr = MPI_Cartdim_get(simgrid::smpi::Comm::f2c(*comm), ndims);
715 }
716
717 void mpi_graph_create_ (int* comm_old, int* nnodes, int* index, int* edges, int* reorder, int*  comm_graph, int* ierr) {
718   MPI_Comm tmp;
719  *ierr = MPI_Graph_create(simgrid::smpi::Comm::f2c(*comm_old), *nnodes, index, edges, *reorder, &tmp);
720  if(*ierr == MPI_SUCCESS) {
721    *comm_graph = tmp->add_f();
722  }
723 }
724
725 void mpi_graph_get_ (int* comm, int* maxindex, int* maxedges, int* index, int* edges, int* ierr) {
726  *ierr = MPI_Graph_get(simgrid::smpi::Comm::f2c(*comm), *maxindex, *maxedges, index, edges);
727 }
728
729 void mpi_graph_map_ (int* comm_old, int* nnodes, int* index, int* edges, int* newrank, int* ierr) {
730  *ierr = MPI_Graph_map(simgrid::smpi::Comm::f2c(*comm_old), *nnodes, index, edges, newrank);
731 }
732
733 void mpi_graph_neighbors_ (int* comm, int* rank, int* maxneighbors, int* neighbors, int* ierr) {
734  *ierr = MPI_Graph_neighbors(simgrid::smpi::Comm::f2c(*comm), *rank, *maxneighbors, neighbors);
735 }
736
737 void mpi_graph_neighbors_count_ (int* comm, int* rank, int* nneighbors, int* ierr) {
738  *ierr = MPI_Graph_neighbors_count(simgrid::smpi::Comm::f2c(*comm), *rank, nneighbors);
739 }
740
741 void mpi_graphdims_get_ (int* comm, int* nnodes, int* nedges, int* ierr) {
742  *ierr = MPI_Graphdims_get(simgrid::smpi::Comm::f2c(*comm), nnodes, nedges);
743 }
744
745 void mpi_topo_test_ (int* comm, int* top_type, int* ierr) {
746  *ierr = MPI_Topo_test(simgrid::smpi::Comm::f2c(*comm), top_type);
747 }
748
749 void mpi_error_class_ (int* errorcode, int* errorclass, int* ierr) {
750  *ierr = MPI_Error_class(*errorcode, errorclass);
751 }
752
753 void mpi_errhandler_create_ (void* function, int* errhandler, int* ierr) {
754  MPI_Errhandler tmp;
755  *ierr = MPI_Errhandler_create( reinterpret_cast<MPI_Handler_function*>(function), &tmp);
756  if(*ierr==MPI_SUCCESS){
757    *errhandler = tmp->c2f();
758  }
759 }
760
761 void mpi_errhandler_free_ (int* errhandler, int* ierr) {
762   MPI_Errhandler tmp = simgrid::smpi::Errhandler::f2c(*errhandler);
763   *ierr =  MPI_Errhandler_free(&tmp);
764   if(*ierr == MPI_SUCCESS) {
765     simgrid::smpi::F2C::free_f(*errhandler);
766   }
767 }
768
769 void mpi_errhandler_get_ (int* comm, int* errhandler, int* ierr) {
770  MPI_Errhandler tmp;
771  *ierr = MPI_Errhandler_get(simgrid::smpi::Comm::f2c(*comm), &tmp);
772  if(*ierr == MPI_SUCCESS) {
773    *errhandler = tmp->c2f();
774  }
775 }
776
777 void mpi_errhandler_set_ (int* comm, int* errhandler, int* ierr) {
778  *ierr = MPI_Errhandler_set(simgrid::smpi::Comm::f2c(*comm), simgrid::smpi::Errhandler::f2c(*errhandler));
779 }
780
781 void mpi_cancel_ (int* request, int* ierr) {
782   MPI_Request tmp=simgrid::smpi::Request::f2c(*request);
783  *ierr = MPI_Cancel(&tmp);
784 }
785
786 void mpi_buffer_attach_ (void* buffer, int* size, int* ierr) {
787  *ierr = MPI_Buffer_attach(buffer, *size);
788 }
789
790 void mpi_buffer_detach_ (void* buffer, int* size, int* ierr) {
791  *ierr = MPI_Buffer_detach(buffer, size);
792 }
793
794
795
796 void mpi_intercomm_create_ (int* local_comm, int *local_leader, int* peer_comm, int* remote_leader, int* tag,
797                             int* comm_out, int* ierr) {
798   MPI_Comm tmp;
799   *ierr = MPI_Intercomm_create(simgrid::smpi::Comm::f2c(*local_comm), *local_leader, simgrid::smpi::Comm::f2c(*peer_comm), *remote_leader,
800                                *tag, &tmp);
801   if(*ierr == MPI_SUCCESS) {
802     *comm_out = tmp->add_f();
803   }
804 }
805
806 void mpi_intercomm_merge_ (int* comm, int* high, int*  comm_out, int* ierr) {
807  MPI_Comm tmp;
808  *ierr = MPI_Intercomm_merge(simgrid::smpi::Comm::f2c(*comm), *high, &tmp);
809  if(*ierr == MPI_SUCCESS) {
810    *comm_out = tmp->add_f();
811  }
812 }
813
814 void mpi_attr_delete_ (int* comm, int* keyval, int* ierr) {
815  *ierr = MPI_Attr_delete(simgrid::smpi::Comm::f2c(*comm), *keyval);
816 }
817
818 void mpi_attr_put_ (int* comm, int* keyval, int* attr_value, int* ierr) {
819  int* val = (int*)xbt_malloc(sizeof(int));
820  *val=*attr_value;
821  *ierr = MPI_Attr_put(simgrid::smpi::Comm::f2c(*comm), *keyval, val);
822 }
823
824 void mpi_keyval_create_ (void* copy_fn, void* delete_fn, int* keyval, void* extra_state, int* ierr) {
825   smpi_copy_fn _copy_fn={nullptr,nullptr,nullptr,(*(int*)copy_fn) == 0 ? nullptr : reinterpret_cast<MPI_Copy_function_fort*>(copy_fn),nullptr,nullptr};
826   smpi_delete_fn _delete_fn={nullptr,nullptr,nullptr,(*(int*)delete_fn) == 0 ? nullptr : reinterpret_cast<MPI_Delete_function_fort*>(delete_fn),nullptr,nullptr};
827   *ierr = simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Comm>(_copy_fn, _delete_fn, keyval, extra_state);
828 }
829
830 void mpi_keyval_free_ (int* keyval, int* ierr) {
831  *ierr = MPI_Keyval_free(keyval);
832 }
833
834 void mpi_test_cancelled_ (MPI_Status*  status, int* flag, int* ierr) {
835  *ierr = MPI_Test_cancelled(status, flag);
836 }
837
838 void mpi_get_elements_ (MPI_Status*  status, int* datatype, int* elements, int* ierr) {
839  *ierr = MPI_Get_elements(status, simgrid::smpi::Datatype::f2c(*datatype), elements);
840 }
841
842 void mpi_dims_create_ (int* nnodes, int* ndims, int* dims, int* ierr) {
843  *ierr = MPI_Dims_create(*nnodes, *ndims, dims);
844 }
845
846 void mpi_add_error_class_ ( int *errorclass, int* ierr){
847  *ierr = MPI_Add_error_class( errorclass);
848 }
849
850 void mpi_add_error_code_ (  int* errorclass, int *errorcode, int* ierr){
851  *ierr = MPI_Add_error_code(*errorclass, errorcode);
852 }
853
854 void mpi_add_error_string_ ( int* errorcode, char *string, int* ierr){
855  *ierr = MPI_Add_error_string(*errorcode, string);
856 }
857
858 void mpi_info_dup_ (int* info, int* newinfo, int* ierr){
859  MPI_Info tmp;
860  *ierr = MPI_Info_dup(simgrid::smpi::Info::f2c(*info), &tmp);
861  if(*ierr==MPI_SUCCESS){
862    *newinfo= tmp->add_f();
863  }
864 }
865
866 void mpi_info_get_valuelen_ ( int* info, char *key, int *valuelen, int *flag, int* ierr, unsigned int keylen){
867   while(key[keylen-1]==' ')
868     keylen--;
869   while(*key==' '){//handle leading blanks
870     keylen--;
871     key++;
872   }
873   char* tkey = xbt_new(char, keylen+1);
874   strncpy(tkey, key, keylen);
875   tkey[keylen]='\0';
876   *ierr = MPI_Info_get_valuelen( simgrid::smpi::Info::f2c(*info), tkey, valuelen, flag);
877   xbt_free(tkey);
878 }
879
880 void mpi_info_delete_ (int* info, char *key, int* ierr, unsigned int keylen){
881   while(key[keylen-1]==' ')
882     keylen--;
883   while(*key==' '){//handle leading blanks
884     keylen--;
885     key++;
886   }
887   char* tkey = xbt_new(char, keylen+1);
888   strncpy(tkey, key, keylen);
889   tkey[keylen]='\0';
890   *ierr = MPI_Info_delete(simgrid::smpi::Info::f2c(*info), tkey);
891   xbt_free(tkey);
892 }
893
894 void mpi_info_get_nkeys_ ( int* info, int *nkeys, int* ierr){
895  *ierr = MPI_Info_get_nkeys(  simgrid::smpi::Info::f2c(*info), nkeys);
896 }
897
898 void mpi_info_get_nthkey_ ( int* info, int* n, char *key, int* ierr, unsigned int keylen){
899   *ierr = MPI_Info_get_nthkey( simgrid::smpi::Info::f2c(*info), *n, key);
900   unsigned int i = 0;
901   for (i=strlen(key); i<keylen; i++)
902     key[i]=' ';
903 }
904
905 void mpi_get_version_ (int *version,int *subversion, int* ierr){
906  *ierr = MPI_Get_version (version,subversion);
907 }
908
909 void mpi_get_library_version_ (char *version,int *len, int* ierr){
910  *ierr = MPI_Get_library_version (version,len);
911 }
912
913 void mpi_request_get_status_ ( int* request, int *flag, MPI_Status* status, int* ierr){
914  *ierr = MPI_Request_get_status( simgrid::smpi::Request::f2c(*request), flag, status);
915 }
916
917 void mpi_grequest_start_ ( void *query_fn, void *free_fn, void *cancel_fn, void *extra_state, int*request, int* ierr){
918   MPI_Request tmp;
919   *ierr = MPI_Grequest_start( reinterpret_cast<MPI_Grequest_query_function*>(query_fn), reinterpret_cast<MPI_Grequest_free_function*>(free_fn),
920                               reinterpret_cast<MPI_Grequest_cancel_function*>(cancel_fn), extra_state, &tmp);
921  if(*ierr == MPI_SUCCESS) {
922    *request = tmp->add_f();
923  }
924 }
925
926 void mpi_grequest_complete_ ( int* request, int* ierr){
927  *ierr = MPI_Grequest_complete( simgrid::smpi::Request::f2c(*request));
928 }
929
930 void mpi_status_set_cancelled_ (MPI_Status* status,int* flag, int* ierr){
931  *ierr = MPI_Status_set_cancelled(status,*flag);
932 }
933
934 void mpi_status_set_elements_ ( MPI_Status* status, int* datatype, int* count, int* ierr){
935  *ierr = MPI_Status_set_elements( status, simgrid::smpi::Datatype::f2c(*datatype), *count);
936 }
937
938 void mpi_publish_name_ ( char *service_name, int* info, char *port_name, int* ierr){
939  *ierr = MPI_Publish_name( service_name, simgrid::smpi::Info::f2c(*info), port_name);
940 }
941
942 void mpi_unpublish_name_ ( char *service_name, int* info, char *port_name, int* ierr){
943  *ierr = MPI_Unpublish_name( service_name, simgrid::smpi::Info::f2c(*info), port_name);
944 }
945
946 void mpi_lookup_name_ ( char *service_name, int* info, char *port_name, int* ierr){
947  *ierr = MPI_Lookup_name( service_name, simgrid::smpi::Info::f2c(*info), port_name);
948 }
949
950 void mpi_open_port_ ( int* info, char *port_name, int* ierr){
951  *ierr = MPI_Open_port( simgrid::smpi::Info::f2c(*info),port_name);
952 }
953
954 void mpi_close_port_ ( char *port_name, int* ierr){
955  *ierr = MPI_Close_port( port_name);
956 }
957
958 void mpi_file_close_ ( int* file, int* ierr){
959   *ierr= MPI_File_close(reinterpret_cast<MPI_File*>(*file));
960 }
961
962 void mpi_file_delete_ ( char* filename, int* info, int* ierr){
963   *ierr= MPI_File_delete(filename, simgrid::smpi::Info::f2c(*info));
964 }
965
966 void mpi_file_open_ ( int* comm, char* filename, int* amode, int* info, int* fh, int* ierr){
967   *ierr= MPI_File_open(simgrid::smpi::Comm::f2c(*comm), filename, *amode, simgrid::smpi::Info::f2c(*info), reinterpret_cast<MPI_File*>(*fh));
968 }
969
970 void mpi_file_set_view_ ( int* fh, long long int* offset, int* etype, int* filetype, char* datarep, int* info, int* ierr){
971   *ierr= MPI_File_set_view(reinterpret_cast<MPI_File>(*fh) , reinterpret_cast<MPI_Offset>(*offset), simgrid::smpi::Datatype::f2c(*etype), simgrid::smpi::Datatype::f2c(*filetype), datarep, simgrid::smpi::Info::f2c(*info));
972 }
973
974 void mpi_file_read_ ( int* fh, void* buf, int* count, int* datatype, MPI_Status* status, int* ierr){
975   *ierr=  MPI_File_read(reinterpret_cast<MPI_File>(*fh), buf, *count, simgrid::smpi::Datatype::f2c(*datatype), status);
976 }
977
978 void mpi_file_write_ ( int* fh, void* buf, int* count, int* datatype, MPI_Status* status, int* ierr){
979   *ierr=  MPI_File_write(reinterpret_cast<MPI_File>(*fh), buf, *count, simgrid::smpi::Datatype::f2c(*datatype), status);
980 }
981
982 } // extern "C"