Logo AND Algorithmique Numérique Distribuée

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