Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / smpi_f77.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <limits.h>
8 #include <stdio.h>
9
10 #include "private.h"
11 #include "xbt.h"
12
13 extern int xargc;
14 extern char** xargv;
15
16 static xbt_dynar_t comm_lookup = NULL;
17 static xbt_dynar_t group_lookup = NULL;
18 static xbt_dict_t request_lookup = NULL;
19 static xbt_dynar_t datatype_lookup = NULL;
20 static xbt_dynar_t op_lookup = NULL;
21
22 #define KEY_SIZE (sizeof(int) * 2 + 1)
23
24 static int new_comm(MPI_Comm comm) {
25   xbt_dynar_push(comm_lookup, &comm);
26   return (int)xbt_dynar_length(comm_lookup) - 1;
27 }
28
29 static void free_comm(int comm) {
30   xbt_dynar_remove_at(comm_lookup, comm, NULL);
31 }
32
33 static MPI_Comm get_comm(int comm) {
34   if(comm == -2) {
35     return MPI_COMM_SELF;
36   } else if(comm_lookup && comm >= 0 && comm < (int)xbt_dynar_length(comm_lookup)) {
37     return *(MPI_Comm*)xbt_dynar_get_ptr(comm_lookup, comm);
38   }
39   return MPI_COMM_NULL;
40 }
41
42 static int new_group(MPI_Group group) {
43   xbt_dynar_push(group_lookup, &group);
44   return (int)xbt_dynar_length(group_lookup) - 1;
45 }
46
47 static MPI_Group get_group(int group) {
48   if(group == -2) {
49     return MPI_GROUP_EMPTY;
50   } else if(group_lookup && group >= 0 && group < (int)xbt_dynar_length(group_lookup)) {
51     return *(MPI_Group*)xbt_dynar_get_ptr(group_lookup, group);
52   }
53   return MPI_COMM_NULL;
54 }
55
56 static char* get_key(char* key, int id) {
57   snprintf(key, KEY_SIZE, "%x", id);
58   return key;
59 }
60
61 static int new_request(MPI_Request req) {
62   static int request_id = INT_MIN;
63   char key[KEY_SIZE];
64
65   xbt_dict_set(request_lookup, get_key(key, request_id), req, NULL);
66   return request_id++;
67 }
68
69 static MPI_Request find_request(int req) {
70   char key[KEY_SIZE];
71    
72   return (MPI_Request)xbt_dict_get(request_lookup, get_key(key, req));
73 }
74
75 static int new_datatype(MPI_Datatype datatype) {
76   xbt_dynar_push(datatype_lookup, &datatype);
77   return (int)xbt_dynar_length(datatype_lookup) - 1;
78 }
79
80 static MPI_Datatype get_datatype(int datatype) {
81   return datatype >= 0
82          ? *(MPI_Datatype*)xbt_dynar_get_ptr(datatype_lookup, datatype)
83          : MPI_DATATYPE_NULL;
84 }
85
86 static int new_op(MPI_Op op) {
87   xbt_dynar_push(op_lookup, &op);
88   return (int)xbt_dynar_length(op_lookup) - 1;
89 }
90
91 static MPI_Op get_op(int op) {
92    return op >= 0
93           ? *(MPI_Op*)xbt_dynar_get_ptr(op_lookup, op)
94           : MPI_OP_NULL;
95 }
96
97 void mpi_init__(int* ierr) {
98    comm_lookup = xbt_dynar_new(sizeof(MPI_Comm), NULL);
99    new_comm(MPI_COMM_WORLD);
100    group_lookup = xbt_dynar_new(sizeof(MPI_Group), NULL);
101
102    request_lookup = xbt_dict_new_homogeneous(NULL);
103
104    datatype_lookup = xbt_dynar_new(sizeof(MPI_Datatype), NULL);
105    new_datatype(MPI_BYTE);
106    new_datatype(MPI_CHAR);
107    new_datatype(MPI_INT);
108    new_datatype(MPI_INT);
109    new_datatype(MPI_INT8_T);
110    new_datatype(MPI_INT16_T);
111    new_datatype(MPI_INT32_T);
112    new_datatype(MPI_INT64_T);
113    new_datatype(MPI_FLOAT);
114    new_datatype(MPI_FLOAT);
115    new_datatype(MPI_DOUBLE);
116    new_datatype(MPI_DOUBLE);
117    new_datatype(MPI_C_FLOAT_COMPLEX);
118    new_datatype(MPI_C_DOUBLE_COMPLEX);
119    new_datatype(MPI_2INT);
120    new_datatype(MPI_UINT8_T);
121    new_datatype(MPI_UINT16_T);
122    new_datatype(MPI_UINT32_T);
123    new_datatype(MPI_UINT64_T);
124    new_datatype(MPI_2FLOAT);
125    new_datatype(MPI_2DOUBLE);
126
127
128    op_lookup = xbt_dynar_new(sizeof(MPI_Op), NULL);
129    new_op(MPI_MAX);
130    new_op(MPI_MIN);
131    new_op(MPI_MAXLOC);
132    new_op(MPI_MINLOC);
133    new_op(MPI_SUM);
134    new_op(MPI_PROD);
135    new_op(MPI_LAND);
136    new_op(MPI_LOR);
137    new_op(MPI_LXOR);
138    new_op(MPI_BAND);
139    new_op(MPI_BOR);
140    new_op(MPI_BXOR);
141
142    /* smpif2c is responsible for generating a call with the final arguments */
143    *ierr = MPI_Init(NULL, NULL);
144 }
145
146 void mpi_finalize__(int* ierr) {
147    *ierr = MPI_Finalize();
148    xbt_dynar_free(&op_lookup);
149    op_lookup = NULL;
150    xbt_dynar_free(&datatype_lookup);
151    datatype_lookup = NULL;
152    xbt_dict_free(&request_lookup);
153    request_lookup = NULL;
154    xbt_dynar_free(&comm_lookup);
155    comm_lookup = NULL;
156 }
157
158 void mpi_abort__(int* comm, int* errorcode, int* ierr) {
159   *ierr = MPI_Abort(get_comm(*comm), *errorcode);
160 }
161
162 void mpi_comm_rank__(int* comm, int* rank, int* ierr) {
163    *ierr = MPI_Comm_rank(get_comm(*comm), rank);
164 }
165
166 void mpi_comm_size__(int* comm, int* size, int* ierr) {
167    *ierr = MPI_Comm_size(get_comm(*comm), size);
168 }
169
170 double mpi_wtime__(void) {
171    return MPI_Wtime();
172 }
173
174 double mpi_wtick__(void) {
175   return MPI_Wtick();
176 }
177
178 void mpi_comm_dup__(int* comm, int* newcomm, int* ierr) {
179   MPI_Comm tmp;
180
181   *ierr = MPI_Comm_dup(get_comm(*comm), &tmp);
182   if(*ierr == MPI_SUCCESS) {
183     *newcomm = new_comm(tmp);
184   }
185 }
186
187 void mpi_comm_create__(int* comm, int* group, int* newcomm, int* ierr) {
188   MPI_Comm tmp;
189
190   *ierr = MPI_Comm_create(get_comm(*comm),get_group(*group), &tmp);
191   if(*ierr == MPI_SUCCESS) {
192     *newcomm = new_comm(tmp);
193   }
194 }
195
196
197 void mpi_comm_free__(int* comm, int* ierr) {
198   MPI_Comm tmp = get_comm(*comm);
199
200   *ierr = MPI_Comm_free(&tmp);
201
202   if(*ierr == MPI_SUCCESS) {
203     free_comm(*comm);
204   }
205 }
206
207 void mpi_comm_split__(int* comm, int* color, int* key, int* comm_out, int* ierr) {
208   MPI_Comm tmp;
209
210   *ierr = MPI_Comm_split(get_comm(*comm), *color, *key, &tmp);
211   if(*ierr == MPI_SUCCESS) {
212     *comm_out = new_comm(tmp);
213   }
214 }
215
216 void mpi_group_incl__(int* group, int* n, int* ranks, int* group_out, int* ierr) {
217   MPI_Group tmp;
218
219   *ierr = MPI_Group_incl(get_group(*group), *n, ranks, &tmp);
220   if(*ierr == MPI_SUCCESS) {
221     *group_out = new_group(tmp);
222   }
223 }
224
225 void mpi_comm_group__(int* comm, int* group_out,  int* ierr) {
226   MPI_Group tmp;
227
228   *ierr = MPI_Comm_group(get_comm(*comm), &tmp);
229   if(*ierr == MPI_SUCCESS) {
230     *group_out = new_group(tmp);
231   }
232 }
233
234
235 void mpi_initialized__(int* flag, int* ierr){
236   *ierr = MPI_Initialized(flag);
237 }
238
239 void mpi_send_init__(void *buf, int* count, int* datatype, int* dst, int* tag,
240                      int* comm, int* request, int* ierr) {
241   MPI_Request req;
242
243   *ierr = MPI_Send_init(buf, *count, get_datatype(*datatype), *dst, *tag,
244                         get_comm(*comm), &req);
245   if(*ierr == MPI_SUCCESS) {
246     *request = new_request(req);
247   }
248 }
249
250 void mpi_isend__(void *buf, int* count, int* datatype, int* dst,
251                  int* tag, int* comm, int* request, int* ierr) {
252   MPI_Request req;
253
254   *ierr = MPI_Isend(buf, *count, get_datatype(*datatype), *dst, *tag,
255                     get_comm(*comm), &req);
256   if(*ierr == MPI_SUCCESS) {
257     *request = new_request(req);
258   }
259 }
260
261 void mpi_irsend__(void *buf, int* count, int* datatype, int* dst,
262                  int* tag, int* comm, int* request, int* ierr) {
263   MPI_Request req;
264
265   *ierr = MPI_Irsend(buf, *count, get_datatype(*datatype), *dst, *tag,
266                     get_comm(*comm), &req);
267   if(*ierr == MPI_SUCCESS) {
268     *request = new_request(req);
269   }
270 }
271
272 void mpi_send__(void* buf, int* count, int* datatype, int* dst,
273                 int* tag, int* comm, int* ierr) {
274    *ierr = MPI_Send(buf, *count, get_datatype(*datatype), *dst, *tag,
275                     get_comm(*comm));
276 }
277
278 void mpi_rsend__(void* buf, int* count, int* datatype, int* dst,
279                 int* tag, int* comm, int* ierr) {
280    *ierr = MPI_Rsend(buf, *count, get_datatype(*datatype), *dst, *tag,
281                     get_comm(*comm));
282 }
283
284 void mpi_sendrecv__(void* sendbuf, int* sendcount, int* sendtype, int* dst,
285                 int* sendtag, void *recvbuf, int* recvcount,
286                 int* recvtype, int* src, int* recvtag,
287                 int* comm, MPI_Status* status, int* ierr) {
288    *ierr = MPI_Sendrecv(sendbuf, *sendcount, get_datatype(*sendtype), *dst,
289        *sendtag, recvbuf, *recvcount,get_datatype(*recvtype), *src, *recvtag,
290        get_comm(*comm), status);
291 }
292
293 void mpi_recv_init__(void *buf, int* count, int* datatype, int* src, int* tag,
294                      int* comm, int* request, int* ierr) {
295   MPI_Request req;
296
297   *ierr = MPI_Recv_init(buf, *count, get_datatype(*datatype), *src, *tag,
298                         get_comm(*comm), &req);
299   if(*ierr == MPI_SUCCESS) {
300     *request = new_request(req);
301   }
302 }
303
304 void mpi_irecv__(void *buf, int* count, int* datatype, int* src, int* tag,
305                  int* comm, int* request, int* ierr) {
306   MPI_Request req;
307
308   *ierr = MPI_Irecv(buf, *count, get_datatype(*datatype), *src, *tag,
309                     get_comm(*comm), &req);
310   if(*ierr == MPI_SUCCESS) {
311     *request = new_request(req);
312   }
313 }
314
315 void mpi_recv__(void* buf, int* count, int* datatype, int* src,
316                 int* tag, int* comm, MPI_Status* status, int* ierr) {
317    *ierr = MPI_Recv(buf, *count, get_datatype(*datatype), *src, *tag,
318                     get_comm(*comm), status);
319 }
320
321 void mpi_start__(int* request, int* ierr) {
322   MPI_Request req = find_request(*request);
323
324   *ierr = MPI_Start(&req);
325 }
326
327 void mpi_startall__(int* count, int* requests, int* ierr) {
328   MPI_Request* reqs;
329   int i;
330
331   reqs = xbt_new(MPI_Request, *count);
332   for(i = 0; i < *count; i++) {
333     reqs[i] = find_request(requests[i]);
334   }
335   *ierr = MPI_Startall(*count, reqs);
336   free(reqs);
337 }
338
339 void mpi_wait__(int* request, MPI_Status* status, int* ierr) {
340    MPI_Request req = find_request(*request);
341    
342    *ierr = MPI_Wait(&req, status);
343 }
344
345 void mpi_waitany__(int* count, int* requests, int* index, MPI_Status* status, int* ierr) {
346   MPI_Request* reqs;
347   int i;
348
349   reqs = xbt_new(MPI_Request, *count);
350   for(i = 0; i < *count; i++) {
351     reqs[i] = find_request(requests[i]);
352   }
353   *ierr = MPI_Waitany(*count, reqs, index, status);
354   free(reqs);
355 }
356
357 void mpi_waitall__(int* count, int* requests, MPI_Status* status, int* ierr) {
358   MPI_Request* reqs;
359   int i;
360
361   reqs = xbt_new(MPI_Request, *count);
362   for(i = 0; i < *count; i++) {
363     reqs[i] = find_request(requests[i]);
364   }
365   *ierr = MPI_Waitall(*count, reqs, status);
366   free(reqs);
367 }
368
369 void mpi_barrier__(int* comm, int* ierr) {
370   *ierr = MPI_Barrier(get_comm(*comm));
371 }
372
373 void mpi_bcast__(void *buf, int* count, int* datatype, int* root, int* comm, int* ierr) {
374   *ierr = MPI_Bcast(buf, *count, get_datatype(*datatype), *root, get_comm(*comm));
375 }
376
377 void mpi_reduce__(void* sendbuf, void* recvbuf, int* count,
378                   int* datatype, int* op, int* root, int* comm, int* ierr) {
379   *ierr = MPI_Reduce(sendbuf, recvbuf, *count,
380                      get_datatype(*datatype), get_op(*op), *root, get_comm(*comm));
381 }
382
383 void mpi_allreduce__(void* sendbuf, void* recvbuf, int* count, int* datatype,
384                      int* op, int* comm, int* ierr) {
385   *ierr = MPI_Allreduce(sendbuf, recvbuf, *count, get_datatype(*datatype),
386                         get_op(*op), get_comm(*comm));
387 }
388
389 void mpi_reduce_scatter__(void* sendbuf, void* recvbuf, int* recvcounts, int* datatype,
390                      int* op, int* comm, int* ierr) {
391   *ierr = MPI_Reduce_scatter(sendbuf, recvbuf, recvcounts, get_datatype(*datatype),
392                         get_op(*op), get_comm(*comm));
393 }
394
395 void mpi_scatter__(void* sendbuf, int* sendcount, int* sendtype,
396                    void* recvbuf, int* recvcount, int* recvtype, 
397                    int* root, int* comm, int* ierr) {
398   *ierr = MPI_Scatter(sendbuf, *sendcount, get_datatype(*sendtype),
399                       recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
400 }
401
402
403 void mpi_scatterv__(void* sendbuf, int* sendcounts, int* displs, int* sendtype,
404                    void* recvbuf, int* recvcount, int* recvtype,
405                    int* root, int* comm, int* ierr) {
406   *ierr = MPI_Scatterv(sendbuf, sendcounts, displs, get_datatype(*sendtype),
407                       recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
408 }
409
410 void mpi_gather__(void* sendbuf, int* sendcount, int* sendtype,
411                   void* recvbuf, int* recvcount, int* recvtype,
412                   int* root, int* comm, int* ierr) {
413   *ierr = MPI_Gather(sendbuf, *sendcount, get_datatype(*sendtype),
414                      recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
415 }
416
417 void mpi_gatherv__(void* sendbuf, int* sendcount, int* sendtype,
418                   void* recvbuf, int* recvcounts, int* displs, int* recvtype,
419                   int* root, int* comm, int* ierr) {
420   *ierr = MPI_Gatherv(sendbuf, *sendcount, get_datatype(*sendtype),
421                      recvbuf, recvcounts, displs, get_datatype(*recvtype), *root, get_comm(*comm));
422 }
423
424 void mpi_allgather__(void* sendbuf, int* sendcount, int* sendtype,
425                      void* recvbuf, int* recvcount, int* recvtype,
426                      int* comm, int* ierr) {
427   *ierr = MPI_Allgather(sendbuf, *sendcount, get_datatype(*sendtype),
428                         recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
429 }
430
431 void mpi_allgatherv__(void* sendbuf, int* sendcount, int* sendtype,
432                      void* recvbuf, int* recvcounts,int* displs, int* recvtype,
433                      int* comm, int* ierr) {
434   *ierr = MPI_Allgatherv(sendbuf, *sendcount, get_datatype(*sendtype),
435                         recvbuf, recvcounts, displs, get_datatype(*recvtype), get_comm(*comm));
436 }
437
438 void mpi_scan__(void* sendbuf, void* recvbuf, int* count, int* datatype,
439                 int* op, int* comm, int* ierr) {
440   *ierr = MPI_Scan(sendbuf, recvbuf, *count, get_datatype(*datatype),
441                    get_op(*op), get_comm(*comm));
442 }
443
444 void mpi_alltoall__(void* sendbuf, int* sendcount, int* sendtype,
445                     void* recvbuf, int* recvcount, int* recvtype, int* comm, int* ierr) {
446   *ierr = MPI_Alltoall(sendbuf, *sendcount, get_datatype(*sendtype),
447                        recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
448 }
449
450 void mpi_alltoallv__(void* sendbuf, int* sendcounts, int* senddisps, int* sendtype,
451                     void* recvbuf, int* recvcounts, int* recvdisps, int* recvtype, int* comm, int* ierr) {
452   *ierr = MPI_Alltoallv(sendbuf, sendcounts, senddisps, get_datatype(*sendtype),
453                        recvbuf, recvcounts, recvdisps, get_datatype(*recvtype), get_comm(*comm));
454 }
455
456 void mpi_test__ (int * request, int *flag, MPI_Status * status, int* ierr){
457   MPI_Request req = find_request(*request);
458   *ierr= MPI_Test(&req, flag, status);
459 }
460
461
462 void mpi_testall__ (int* count, int * requests,  int *flag, MPI_Status * statuses, int* ierr){
463   MPI_Request* reqs;
464   int i;
465   reqs = xbt_new(MPI_Request, *count);
466   for(i = 0; i < *count; i++) {
467     reqs[i] = find_request(requests[i]);
468   }
469   *ierr= MPI_Testall(*count, reqs, flag, statuses);
470 }
471
472
473 void mpi_get_processor_name__(char *name, int *resultlen, int* ierr){
474   *ierr = MPI_Get_processor_name(name, resultlen);
475 }
476
477 void mpi_get_count__(MPI_Status * status, int* datatype, int *count, int* ierr){
478   *ierr = MPI_Get_count(status, get_datatype(*datatype), count);
479 }
480
481 void mpi_attr_get__(int* comm, int* keyval, void* attr_value, int* flag, int* ierr ){
482   *ierr = MPI_Attr_get(get_comm(*comm), *keyval, attr_value, flag);
483 }
484
485 void mpi_type_extent__(int* datatype, MPI_Aint * extent, int* ierr){
486   *ierr= MPI_Type_extent(get_datatype(*datatype),  extent);
487 }
488
489 void mpi_type_ub__(int* datatype, MPI_Aint * disp, int* ierr){
490   *ierr= MPI_Type_ub(get_datatype(*datatype), disp);
491 }
492
493 void mpi_type_lb__(int* datatype, MPI_Aint * extent, int* ierr){
494   *ierr= MPI_Type_extent(get_datatype(*datatype), extent);
495 }
496
497 void mpi_type_size__(int* datatype, int *size, int* ierr)
498 {
499   *ierr = MPI_Type_size(get_datatype(*datatype), size);
500 }
501
502 void mpi_error_string__(int* errorcode, char* string, int* resultlen, int* ierr){
503   *ierr = MPI_Error_string(*errorcode, string, resultlen);
504 }