Logo AND Algorithmique Numérique Distribuée

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