Logo AND Algorithmique Numérique Distribuée

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