Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add two functions to fortran bindings, needed by specfem
[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_dict_t request_lookup = NULL;
18 static xbt_dynar_t datatype_lookup = NULL;
19 static xbt_dynar_t op_lookup = NULL;
20
21 #define KEY_SIZE (sizeof(int) * 2 + 1)
22
23 static int new_comm(MPI_Comm comm) {
24   xbt_dynar_push(comm_lookup, &comm);
25   return (int)xbt_dynar_length(comm_lookup) - 1;
26 }
27
28 static MPI_Comm get_comm(int comm) {
29   if(comm == -2) {
30     return MPI_COMM_SELF;
31   } else if(comm_lookup && comm >= 0 && comm < (int)xbt_dynar_length(comm_lookup)) {
32     return *(MPI_Comm*)xbt_dynar_get_ptr(comm_lookup, comm);
33   }
34   return MPI_COMM_NULL;
35 }
36
37 static char* get_key(char* key, int id) {
38   snprintf(key, KEY_SIZE, "%x", id);
39   return key;
40 }
41
42 static int new_request(MPI_Request req) {
43   static int request_id = INT_MIN;
44   char key[KEY_SIZE];
45
46   xbt_dict_set(request_lookup, get_key(key, request_id), req, NULL);
47   return request_id++;
48 }
49
50 static MPI_Request find_request(int req) {
51   char key[KEY_SIZE];
52    
53   return (MPI_Request)xbt_dict_get(request_lookup, get_key(key, req));
54 }
55
56 static int new_datatype(MPI_Datatype datatype) {
57   xbt_dynar_push(datatype_lookup, &datatype);
58   return (int)xbt_dynar_length(datatype_lookup) - 1;
59 }
60
61 static MPI_Datatype get_datatype(int datatype) {
62   return datatype >= 0
63          ? *(MPI_Datatype*)xbt_dynar_get_ptr(datatype_lookup, datatype)
64          : MPI_DATATYPE_NULL;
65 }
66
67 static int new_op(MPI_Op op) {
68   xbt_dynar_push(op_lookup, &op);
69   return (int)xbt_dynar_length(op_lookup) - 1;
70 }
71
72 static MPI_Op get_op(int op) {
73    return op >= 0
74           ? *(MPI_Op*)xbt_dynar_get_ptr(op_lookup, op)
75           : MPI_OP_NULL;
76 }
77
78 void mpi_init__(int* ierr) {
79    comm_lookup = xbt_dynar_new(sizeof(MPI_Comm), NULL);
80    new_comm(MPI_COMM_WORLD);
81
82    request_lookup = xbt_dict_new_homogeneous(NULL);
83
84    datatype_lookup = xbt_dynar_new(sizeof(MPI_Datatype), NULL);
85    new_datatype(MPI_BYTE);
86    new_datatype(MPI_CHAR);
87    new_datatype(MPI_INT);
88    new_datatype(MPI_INT);
89    new_datatype(MPI_INT8_T);
90    new_datatype(MPI_INT16_T);
91    new_datatype(MPI_INT32_T);
92    new_datatype(MPI_INT64_T);
93    new_datatype(MPI_FLOAT);
94    new_datatype(MPI_FLOAT);
95    new_datatype(MPI_DOUBLE);
96    new_datatype(MPI_DOUBLE);
97    new_datatype(MPI_C_FLOAT_COMPLEX);
98    new_datatype(MPI_C_DOUBLE_COMPLEX);
99    new_datatype(MPI_2INT);
100    new_datatype(MPI_UINT8_T);
101    new_datatype(MPI_UINT16_T);
102    new_datatype(MPI_UINT32_T);
103    new_datatype(MPI_UINT64_T);
104    new_datatype(MPI_2FLOAT);
105    new_datatype(MPI_2DOUBLE);
106
107
108    op_lookup = xbt_dynar_new(sizeof(MPI_Op), NULL);
109    new_op(MPI_MAX);
110    new_op(MPI_MIN);
111    new_op(MPI_MAXLOC);
112    new_op(MPI_MINLOC);
113    new_op(MPI_SUM);
114    new_op(MPI_PROD);
115    new_op(MPI_LAND);
116    new_op(MPI_LOR);
117    new_op(MPI_LXOR);
118    new_op(MPI_BAND);
119    new_op(MPI_BOR);
120    new_op(MPI_BXOR);
121
122    /* smpif2c is responsible for generating a call with the final arguments */
123    *ierr = MPI_Init(NULL, NULL);
124 }
125
126 void mpi_finalize__(int* ierr) {
127    *ierr = MPI_Finalize();
128    xbt_dynar_free(&op_lookup);
129    op_lookup = NULL;
130    xbt_dynar_free(&datatype_lookup);
131    datatype_lookup = NULL;
132    xbt_dict_free(&request_lookup);
133    request_lookup = NULL;
134    xbt_dynar_free(&comm_lookup);
135    comm_lookup = NULL;
136 }
137
138 void mpi_abort__(int* comm, int* errorcode, int* ierr) {
139   *ierr = MPI_Abort(get_comm(*comm), *errorcode);
140 }
141
142 void mpi_comm_rank__(int* comm, int* rank, int* ierr) {
143    *ierr = MPI_Comm_rank(get_comm(*comm), rank);
144 }
145
146 void mpi_comm_size__(int* comm, int* size, int* ierr) {
147    *ierr = MPI_Comm_size(get_comm(*comm), size);
148 }
149
150 double mpi_wtime__(void) {
151    return MPI_Wtime();
152 }
153
154 double mpi_wtick__(void) {
155   return MPI_Wtick();
156 }
157
158 void mpi_comm_dup__(int* comm, int* newcomm, int* ierr) {
159   MPI_Comm tmp;
160
161   *ierr = MPI_Comm_dup(get_comm(*comm), &tmp);
162   if(*ierr == MPI_SUCCESS) {
163     *newcomm = new_comm(tmp);
164   }
165 }
166
167 void mpi_comm_split__(int* comm, int* color, int* key, int* comm_out, int* ierr) {
168   MPI_Comm tmp;
169
170   *ierr = MPI_Comm_split(get_comm(*comm), *color, *key, &tmp);
171   if(*ierr == MPI_SUCCESS) {
172     *comm_out = new_comm(tmp);
173   }
174 }
175
176 void mpi_send_init__(void *buf, int* count, int* datatype, int* dst, int* tag,
177                      int* comm, int* request, int* ierr) {
178   MPI_Request req;
179
180   *ierr = MPI_Send_init(buf, *count, get_datatype(*datatype), *dst, *tag,
181                         get_comm(*comm), &req);
182   if(*ierr == MPI_SUCCESS) {
183     *request = new_request(req);
184   }
185 }
186
187 void mpi_isend__(void *buf, int* count, int* datatype, int* dst,
188                  int* tag, int* comm, int* request, int* ierr) {
189   MPI_Request req;
190
191   *ierr = MPI_Isend(buf, *count, get_datatype(*datatype), *dst, *tag,
192                     get_comm(*comm), &req);
193   if(*ierr == MPI_SUCCESS) {
194     *request = new_request(req);
195   }
196 }
197
198 void mpi_send__(void* buf, int* count, int* datatype, int* dst,
199                 int* tag, int* comm, int* ierr) {
200    *ierr = MPI_Send(buf, *count, get_datatype(*datatype), *dst, *tag,
201                     get_comm(*comm));
202 }
203
204 void mpi_sendrecv__(void* sendbuf, int* sendcount, int* sendtype, int* dst,
205                 int* sendtag, void *recvbuf, int* recvcount,
206                 int* recvtype, int* src, int* recvtag,
207                 int* comm, MPI_Status* status, int* ierr) {
208    *ierr = MPI_Sendrecv(sendbuf, *sendcount, get_datatype(*sendtype), *dst,
209        *sendtag, recvbuf, *recvcount,get_datatype(*recvtype), *src, *recvtag,
210        get_comm(*comm), status);
211 }
212
213 void mpi_recv_init__(void *buf, int* count, int* datatype, int* src, int* tag,
214                      int* comm, int* request, int* ierr) {
215   MPI_Request req;
216
217   *ierr = MPI_Recv_init(buf, *count, get_datatype(*datatype), *src, *tag,
218                         get_comm(*comm), &req);
219   if(*ierr == MPI_SUCCESS) {
220     *request = new_request(req);
221   }
222 }
223
224 void mpi_irecv__(void *buf, int* count, int* datatype, int* src, int* tag,
225                  int* comm, int* request, int* ierr) {
226   MPI_Request req;
227
228   *ierr = MPI_Irecv(buf, *count, get_datatype(*datatype), *src, *tag,
229                     get_comm(*comm), &req);
230   if(*ierr == MPI_SUCCESS) {
231     *request = new_request(req);
232   }
233 }
234
235 void mpi_recv__(void* buf, int* count, int* datatype, int* src,
236                 int* tag, int* comm, MPI_Status* status, int* ierr) {
237    *ierr = MPI_Recv(buf, *count, get_datatype(*datatype), *src, *tag,
238                     get_comm(*comm), status);
239 }
240
241 void mpi_start__(int* request, int* ierr) {
242   MPI_Request req = find_request(*request);
243
244   *ierr = MPI_Start(&req);
245 }
246
247 void mpi_startall__(int* count, int* requests, int* ierr) {
248   MPI_Request* reqs;
249   int i;
250
251   reqs = xbt_new(MPI_Request, *count);
252   for(i = 0; i < *count; i++) {
253     reqs[i] = find_request(requests[i]);
254   }
255   *ierr = MPI_Startall(*count, reqs);
256   free(reqs);
257 }
258
259 void mpi_wait__(int* request, MPI_Status* status, int* ierr) {
260    MPI_Request req = find_request(*request);
261    
262    *ierr = MPI_Wait(&req, status);
263 }
264
265 void mpi_waitany__(int* count, int* requests, int* index, MPI_Status* status, int* ierr) {
266   MPI_Request* reqs;
267   int i;
268
269   reqs = xbt_new(MPI_Request, *count);
270   for(i = 0; i < *count; i++) {
271     reqs[i] = find_request(requests[i]);
272   }
273   *ierr = MPI_Waitany(*count, reqs, index, status);
274   free(reqs);
275 }
276
277 void mpi_waitall__(int* count, int* requests, MPI_Status* status, int* ierr) {
278   MPI_Request* reqs;
279   int i;
280
281   reqs = xbt_new(MPI_Request, *count);
282   for(i = 0; i < *count; i++) {
283     reqs[i] = find_request(requests[i]);
284   }
285   *ierr = MPI_Waitall(*count, reqs, status);
286   free(reqs);
287 }
288
289 void mpi_barrier__(int* comm, int* ierr) {
290   *ierr = MPI_Barrier(get_comm(*comm));
291 }
292
293 void mpi_bcast__(void *buf, int* count, int* datatype, int* root, int* comm, int* ierr) {
294   *ierr = MPI_Bcast(buf, *count, get_datatype(*datatype), *root, get_comm(*comm));
295 }
296
297 void mpi_reduce__(void* sendbuf, void* recvbuf, int* count,
298                   int* datatype, int* op, int* root, int* comm, int* ierr) {
299   *ierr = MPI_Reduce(sendbuf, recvbuf, *count,
300                      get_datatype(*datatype), get_op(*op), *root, get_comm(*comm));
301 }
302
303 void mpi_allreduce__(void* sendbuf, void* recvbuf, int* count, int* datatype,
304                      int* op, int* comm, int* ierr) {
305   *ierr = MPI_Allreduce(sendbuf, recvbuf, *count, get_datatype(*datatype),
306                         get_op(*op), get_comm(*comm));
307 }
308
309 void mpi_scatter__(void* sendbuf, int* sendcount, int* sendtype,
310                    void* recvbuf, int* recvcount, int* recvtype, 
311                    int* root, int* comm, int* ierr) {
312   *ierr = MPI_Scatter(sendbuf, *sendcount, get_datatype(*sendtype),
313                       recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
314 }
315
316 void mpi_gather__(void* sendbuf, int* sendcount, int* sendtype,
317                   void* recvbuf, int* recvcount, int* recvtype,
318                   int* root, int* comm, int* ierr) {
319   *ierr = MPI_Gather(sendbuf, *sendcount, get_datatype(*sendtype),
320                      recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
321 }
322
323 void mpi_gatherv__(void* sendbuf, int* sendcount, int* sendtype,
324                   void* recvbuf, int* recvcounts, int* displs, int* recvtype,
325                   int* root, int* comm, int* ierr) {
326   *ierr = MPI_Gatherv(sendbuf, *sendcount, get_datatype(*sendtype),
327                      recvbuf, recvcounts, displs, get_datatype(*recvtype), *root, get_comm(*comm));
328 }
329
330 void mpi_allgather__(void* sendbuf, int* sendcount, int* sendtype,
331                      void* recvbuf, int* recvcount, int* recvtype,
332                      int* comm, int* ierr) {
333   *ierr = MPI_Allgather(sendbuf, *sendcount, get_datatype(*sendtype),
334                         recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
335 }
336
337 void mpi_allgatherv__(void* sendbuf, int* sendcount, int* sendtype,
338                      void* recvbuf, int* recvcount,int* displs, int* recvtype,
339                      int* comm, int* ierr) {
340   *ierr = MPI_Allgatherv(sendbuf, *sendcount, get_datatype(*sendtype),
341                         recvbuf, recvcount, displs, get_datatype(*recvtype), get_comm(*comm));
342 }
343
344 void mpi_scan__(void* sendbuf, void* recvbuf, int* count, int* datatype,
345                 int* op, int* comm, int* ierr) {
346   *ierr = MPI_Scan(sendbuf, recvbuf, *count, get_datatype(*datatype),
347                    get_op(*op), get_comm(*comm));
348 }
349
350 void mpi_alltoall__(void* sendbuf, int* sendcount, int* sendtype,
351                     void* recvbuf, int* recvcount, int* recvtype, int* comm, int* ierr) {
352   *ierr = MPI_Alltoall(sendbuf, *sendcount, get_datatype(*sendtype),
353                        recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
354 }
355
356 void mpi_test__ (int * request, int *flag, MPI_Status * status, int* ierr){
357   MPI_Request req = find_request(*request);
358   *ierr= MPI_Test(&req, flag, status);
359 }
360 void mpi_get_processor_name__(char *name, int *resultlen, int* ierr){
361   *ierr = MPI_Get_processor_name(name, resultlen);
362 }
363
364 void mpi_get_count__(MPI_Status * status, int* datatype, int *count, int* ierr){
365   *ierr = MPI_Get_count(status, get_datatype(*datatype), count);
366 }
367
368 void mpi_type_extent__(int* datatype, MPI_Aint * extent, int* ierr){
369   *ierr= MPI_Type_extent(get_datatype(*datatype),  extent);
370 }
371
372 void mpi_type_ub__(int* datatype, MPI_Aint * disp, int* ierr){
373   *ierr= MPI_Type_ub(get_datatype(*datatype), disp);
374 }
375
376 void mpi_type_lb__(int* datatype, MPI_Aint * extent, int* ierr){
377   *ierr= MPI_Type_extent(get_datatype(*datatype), extent);
378 }
379
380 void mpi_type_size__(int* datatype, int *size, int* ierr)
381 {
382   *ierr = MPI_Type_size(get_datatype(*datatype), size);
383 }