Logo AND Algorithmique Numérique Distribuée

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