Logo AND Algorithmique Numérique Distribuée

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