Logo AND Algorithmique Numérique Distribuée

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