Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branches 'MC_LTL' and 'MC_LTL' of 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_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
105    op_lookup = xbt_dynar_new(sizeof(MPI_Op), NULL);
106    new_op(MPI_MAX);
107    new_op(MPI_MIN);
108    new_op(MPI_MAXLOC);
109    new_op(MPI_MINLOC);
110    new_op(MPI_SUM);
111    new_op(MPI_PROD);
112    new_op(MPI_LAND);
113    new_op(MPI_LOR);
114    new_op(MPI_LXOR);
115    new_op(MPI_BAND);
116    new_op(MPI_BOR);
117    new_op(MPI_BXOR);
118
119    /* smpif2c is responsible for generating a call with the final arguments */
120    *ierr = MPI_Init(NULL, NULL);
121 }
122
123 void mpi_finalize__(int* ierr) {
124    *ierr = MPI_Finalize();
125    xbt_dynar_free(&op_lookup);
126    op_lookup = NULL;
127    xbt_dynar_free(&datatype_lookup);
128    datatype_lookup = NULL;
129    xbt_dict_free(&request_lookup);
130    request_lookup = NULL;
131    xbt_dynar_free(&comm_lookup);
132    comm_lookup = NULL;
133 }
134
135 void mpi_abort__(int* comm, int* errorcode, int* ierr) {
136   *ierr = MPI_Abort(get_comm(*comm), *errorcode);
137 }
138
139 void mpi_comm_rank__(int* comm, int* rank, int* ierr) {
140    *ierr = MPI_Comm_rank(get_comm(*comm), rank);
141 }
142
143 void mpi_comm_size__(int* comm, int* size, int* ierr) {
144    *ierr = MPI_Comm_size(get_comm(*comm), size);
145 }
146
147 double mpi_wtime__(void) {
148    return MPI_Wtime();
149 }
150
151 void mpi_comm_dup__(int* comm, int* newcomm, int* ierr) {
152   MPI_Comm tmp;
153
154   *ierr = MPI_Comm_dup(get_comm(*comm), &tmp);
155   if(*ierr == MPI_SUCCESS) {
156     *newcomm = new_comm(tmp);
157   }
158 }
159
160 void mpi_comm_split__(int* comm, int* color, int* key, int* comm_out, int* ierr) {
161   MPI_Comm tmp;
162
163   *ierr = MPI_Comm_split(get_comm(*comm), *color, *key, &tmp);
164   if(*ierr == MPI_SUCCESS) {
165     *comm_out = new_comm(tmp);
166   }
167 }
168
169 void mpi_send_init__(void *buf, int* count, int* datatype, int* dst, int* tag,
170                      int* comm, int* request, int* ierr) {
171   MPI_Request req;
172
173   *ierr = MPI_Send_init(buf, *count, get_datatype(*datatype), *dst, *tag,
174                         get_comm(*comm), &req);
175   if(*ierr == MPI_SUCCESS) {
176     *request = new_request(req);
177   }
178 }
179
180 void mpi_isend__(void *buf, int* count, int* datatype, int* dst,
181                  int* tag, int* comm, int* request, int* ierr) {
182   MPI_Request req;
183
184   *ierr = MPI_Isend(buf, *count, get_datatype(*datatype), *dst, *tag,
185                     get_comm(*comm), &req);
186   if(*ierr == MPI_SUCCESS) {
187     *request = new_request(req);
188   }
189 }
190
191 void mpi_send__(void* buf, int* count, int* datatype, int* dst,
192                 int* tag, int* comm, int* ierr) {
193    *ierr = MPI_Send(buf, *count, get_datatype(*datatype), *dst, *tag,
194                     get_comm(*comm));
195 }
196
197 void mpi_recv_init__(void *buf, int* count, int* datatype, int* src, int* tag,
198                      int* comm, int* request, int* ierr) {
199   MPI_Request req;
200
201   *ierr = MPI_Recv_init(buf, *count, get_datatype(*datatype), *src, *tag,
202                         get_comm(*comm), &req);
203   if(*ierr == MPI_SUCCESS) {
204     *request = new_request(req);
205   }
206 }
207
208 void mpi_irecv__(void *buf, int* count, int* datatype, int* src, int* tag,
209                  int* comm, int* request, int* ierr) {
210   MPI_Request req;
211
212   *ierr = MPI_Irecv(buf, *count, get_datatype(*datatype), *src, *tag,
213                     get_comm(*comm), &req);
214   if(*ierr == MPI_SUCCESS) {
215     *request = new_request(req);
216   }
217 }
218
219 void mpi_recv__(void* buf, int* count, int* datatype, int* src,
220                 int* tag, int* comm, MPI_Status* status, int* ierr) {
221    *ierr = MPI_Recv(buf, *count, get_datatype(*datatype), *src, *tag,
222                     get_comm(*comm), status);
223 }
224
225 void mpi_start__(int* request, int* ierr) {
226   MPI_Request req = find_request(*request);
227
228   *ierr = MPI_Start(&req);
229 }
230
231 void mpi_startall__(int* count, int* requests, int* ierr) {
232   MPI_Request* reqs;
233   int i;
234
235   reqs = xbt_new(MPI_Request, *count);
236   for(i = 0; i < *count; i++) {
237     reqs[i] = find_request(requests[i]);
238   }
239   *ierr = MPI_Startall(*count, reqs);
240   free(reqs);
241 }
242
243 void mpi_wait__(int* request, MPI_Status* status, int* ierr) {
244    MPI_Request req = find_request(*request);
245    
246    *ierr = MPI_Wait(&req, status);
247 }
248
249 void mpi_waitany__(int* count, int* requests, int* index, MPI_Status* status, int* ierr) {
250   MPI_Request* reqs;
251   int i;
252
253   reqs = xbt_new(MPI_Request, *count);
254   for(i = 0; i < *count; i++) {
255     reqs[i] = find_request(requests[i]);
256   }
257   *ierr = MPI_Waitany(*count, reqs, index, status);
258   free(reqs);
259 }
260
261 void mpi_waitall__(int* count, int* requests, MPI_Status* status, int* ierr) {
262   MPI_Request* reqs;
263   int i;
264
265   reqs = xbt_new(MPI_Request, *count);
266   for(i = 0; i < *count; i++) {
267     reqs[i] = find_request(requests[i]);
268   }
269   *ierr = MPI_Waitall(*count, reqs, status);
270   free(reqs);
271 }
272
273 void mpi_barrier__(int* comm, int* ierr) {
274   *ierr = MPI_Barrier(get_comm(*comm));
275 }
276
277 void mpi_bcast__(void *buf, int* count, int* datatype, int* root, int* comm, int* ierr) {
278   *ierr = MPI_Bcast(buf, *count, get_datatype(*datatype), *root, get_comm(*comm));
279 }
280
281 void mpi_reduce__(void* sendbuf, void* recvbuf, int* count,
282                   int* datatype, int* op, int* root, int* comm, int* ierr) {
283   *ierr = MPI_Reduce(sendbuf, recvbuf, *count,
284                      get_datatype(*datatype), get_op(*op), *root, get_comm(*comm));
285 }
286
287 void mpi_allreduce__(void* sendbuf, void* recvbuf, int* count, int* datatype,
288                      int* op, int* comm, int* ierr) {
289   *ierr = MPI_Allreduce(sendbuf, recvbuf, *count, get_datatype(*datatype),
290                         get_op(*op), get_comm(*comm));
291 }
292
293 void mpi_scatter__(void* sendbuf, int* sendcount, int* sendtype,
294                    void* recvbuf, int* recvcount, int* recvtype, 
295                    int* root, int* comm, int* ierr) {
296   *ierr = MPI_Scatter(sendbuf, *sendcount, get_datatype(*sendtype),
297                       recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
298 }
299
300 void mpi_gather__(void* sendbuf, int* sendcount, int* sendtype,
301                   void* recvbuf, int* recvcount, int* recvtype,
302                   int* root, int* comm, int* ierr) {
303   *ierr = MPI_Gather(sendbuf, *sendcount, get_datatype(*sendtype),
304                      recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
305 }
306
307 void mpi_allgather__(void* sendbuf, int* sendcount, int* sendtype,
308                      void* recvbuf, int* recvcount, int* recvtype,
309                      int* comm, int* ierr) {
310   *ierr = MPI_Allgather(sendbuf, *sendcount, get_datatype(*sendtype),
311                         recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
312 }
313
314 void mpi_scan__(void* sendbuf, void* recvbuf, int* count, int* datatype,
315                 int* op, int* comm, int* ierr) {
316   *ierr = MPI_Scan(sendbuf, recvbuf, *count, get_datatype(*datatype),
317                    get_op(*op), get_comm(*comm));
318 }
319
320 void mpi_alltoall__(void* sendbuf, int* sendcount, int* sendtype,
321                     void* recvbuf, int* recvcount, int* recvtype, int* comm, int* ierr) {
322   *ierr = MPI_Alltoall(sendbuf, *sendcount, get_datatype(*sendtype),
323                        recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
324 }