1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
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;
21 #define KEY_SIZE (sizeof(int) * 2 + 1)
23 static int new_comm(MPI_Comm comm) {
24 xbt_dynar_push(comm_lookup, &comm);
25 return (int)xbt_dynar_length(comm_lookup) - 1;
28 static MPI_Comm get_comm(int comm) {
31 } else if(comm >= 0) {
32 return *(MPI_Comm*)xbt_dynar_get_ptr(comm_lookup, comm);
37 static char* get_key(char* key, int id) {
38 snprintf(key, KEY_SIZE, "%x", id);
42 static int new_request(MPI_Request req) {
43 static int request_id = INT_MIN;
46 xbt_dict_set(request_lookup, get_key(key, request_id), req, NULL);
50 static MPI_Request find_request(int req) {
53 return (MPI_Request)xbt_dict_get(request_lookup, get_key(key, req));
56 static int new_datatype(MPI_Datatype datatype) {
57 xbt_dynar_push(datatype_lookup, &datatype);
58 return (int)xbt_dynar_length(datatype_lookup) - 1;
61 static MPI_Datatype get_datatype(int datatype) {
63 ? *(MPI_Datatype*)xbt_dynar_get_ptr(datatype_lookup, datatype)
67 static int new_op(MPI_Op op) {
68 xbt_dynar_push(op_lookup, &op);
69 return (int)xbt_dynar_length(op_lookup) - 1;
72 static MPI_Op get_op(int op) {
74 ? *(MPI_Op*)xbt_dynar_get_ptr(op_lookup, op)
78 void mpi_init__(int* ierr) {
79 comm_lookup = xbt_dynar_new(sizeof(MPI_Comm), NULL);
80 new_comm(MPI_COMM_WORLD);
82 request_lookup = xbt_dict_new_homogeneous(NULL);
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);
105 op_lookup = xbt_dynar_new(sizeof(MPI_Op), NULL);
119 /* smpif2c is responsible for generating a call with the final arguments */
120 *ierr = MPI_Init(NULL, NULL);
123 void mpi_finalize__(int* ierr) {
124 *ierr = MPI_Finalize();
125 xbt_dynar_free(&op_lookup);
126 xbt_dynar_free(&datatype_lookup);
127 xbt_dict_free(&request_lookup);
128 xbt_dynar_free(&comm_lookup);
131 void mpi_abort__(int* comm, int* errorcode, int* ierr) {
132 *ierr = MPI_Abort(get_comm(*comm), *errorcode);
135 void mpi_comm_rank__(int* comm, int* rank, int* ierr) {
136 *ierr = MPI_Comm_rank(get_comm(*comm), rank);
139 void mpi_comm_size__(int* comm, int* size, int* ierr) {
140 *ierr = MPI_Comm_size(get_comm(*comm), size);
143 double mpi_wtime__(void) {
147 void mpi_comm_dup__(int* comm, int* newcomm, int* ierr) {
150 *ierr = MPI_Comm_dup(get_comm(*comm), &tmp);
151 if(*ierr == MPI_SUCCESS) {
152 *newcomm = new_comm(tmp);
156 void mpi_comm_split__(int* comm, int* color, int* key, int* comm_out, int* ierr) {
159 *ierr = MPI_Comm_split(get_comm(*comm), *color, *key, &tmp);
160 if(*ierr == MPI_SUCCESS) {
161 *comm_out = new_comm(tmp);
165 void mpi_send_init__(void *buf, int* count, int* datatype, int* dst, int* tag,
166 int* comm, int* request, int* ierr) {
169 *ierr = MPI_Send_init(buf, *count, get_datatype(*datatype), *dst, *tag,
170 get_comm(*comm), &req);
171 if(*ierr == MPI_SUCCESS) {
172 *request = new_request(req);
176 void mpi_isend__(void *buf, int* count, int* datatype, int* dst,
177 int* tag, int* comm, int* request, int* ierr) {
180 *ierr = MPI_Isend(buf, *count, get_datatype(*datatype), *dst, *tag,
181 get_comm(*comm), &req);
182 if(*ierr == MPI_SUCCESS) {
183 *request = new_request(req);
187 void mpi_send__(void* buf, int* count, int* datatype, int* dst,
188 int* tag, int* comm, int* ierr) {
189 *ierr = MPI_Send(buf, *count, get_datatype(*datatype), *dst, *tag,
193 void mpi_recv_init__(void *buf, int* count, int* datatype, int* src, int* tag,
194 int* comm, int* request, int* ierr) {
197 *ierr = MPI_Recv_init(buf, *count, get_datatype(*datatype), *src, *tag,
198 get_comm(*comm), &req);
199 if(*ierr == MPI_SUCCESS) {
200 *request = new_request(req);
204 void mpi_irecv__(void *buf, int* count, int* datatype, int* src, int* tag,
205 int* comm, int* request, int* ierr) {
208 *ierr = MPI_Irecv(buf, *count, get_datatype(*datatype), *src, *tag,
209 get_comm(*comm), &req);
210 if(*ierr == MPI_SUCCESS) {
211 *request = new_request(req);
215 void mpi_recv__(void* buf, int* count, int* datatype, int* src,
216 int* tag, int* comm, MPI_Status* status, int* ierr) {
217 *ierr = MPI_Recv(buf, *count, get_datatype(*datatype), *src, *tag,
218 get_comm(*comm), status);
221 void mpi_start__(int* request, int* ierr) {
222 MPI_Request req = find_request(*request);
224 *ierr = MPI_Start(&req);
227 void mpi_startall__(int* count, int* requests, int* ierr) {
231 reqs = xbt_new(MPI_Request, *count);
232 for(i = 0; i < *count; i++) {
233 reqs[i] = find_request(requests[i]);
235 *ierr = MPI_Startall(*count, reqs);
239 void mpi_wait__(int* request, MPI_Status* status, int* ierr) {
240 MPI_Request req = find_request(*request);
242 *ierr = MPI_Wait(&req, status);
245 void mpi_waitany__(int* count, int* requests, int* index, MPI_Status* status, int* ierr) {
249 reqs = xbt_new(MPI_Request, *count);
250 for(i = 0; i < *count; i++) {
251 reqs[i] = find_request(requests[i]);
253 *ierr = MPI_Waitany(*count, reqs, index, status);
257 void mpi_waitall__(int* count, int* requests, MPI_Status* status, int* ierr) {
261 reqs = xbt_new(MPI_Request, *count);
262 for(i = 0; i < *count; i++) {
263 reqs[i] = find_request(requests[i]);
265 *ierr = MPI_Waitall(*count, reqs, status);
269 void mpi_barrier__(int* comm, int* ierr) {
270 *ierr = MPI_Barrier(get_comm(*comm));
273 void mpi_bcast__(void *buf, int* count, int* datatype, int* root, int* comm, int* ierr) {
274 *ierr = MPI_Bcast(buf, *count, get_datatype(*datatype), *root, get_comm(*comm));
277 void mpi_reduce__(void* sendbuf, void* recvbuf, int* count,
278 int* datatype, int* op, int* root, int* comm, int* ierr) {
279 *ierr = MPI_Reduce(sendbuf, recvbuf, *count,
280 get_datatype(*datatype), get_op(*op), *root, get_comm(*comm));
283 void mpi_allreduce__(void* sendbuf, void* recvbuf, int* count, int* datatype,
284 int* op, int* comm, int* ierr) {
285 *ierr = MPI_Allreduce(sendbuf, recvbuf, *count, get_datatype(*datatype),
286 get_op(*op), get_comm(*comm));
289 void mpi_scatter__(void* sendbuf, int* sendcount, int* sendtype,
290 void* recvbuf, int* recvcount, int* recvtype,
291 int* root, int* comm, int* ierr) {
292 *ierr = MPI_Scatter(sendbuf, *sendcount, get_datatype(*sendtype),
293 recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
296 void mpi_gather__(void* sendbuf, int* sendcount, int* sendtype,
297 void* recvbuf, int* recvcount, int* recvtype,
298 int* root, int* comm, int* ierr) {
299 *ierr = MPI_Gather(sendbuf, *sendcount, get_datatype(*sendtype),
300 recvbuf, *recvcount, get_datatype(*recvtype), *root, get_comm(*comm));
303 void mpi_allgather__(void* sendbuf, int* sendcount, int* sendtype,
304 void* recvbuf, int* recvcount, int* recvtype,
305 int* comm, int* ierr) {
306 *ierr = MPI_Allgather(sendbuf, *sendcount, get_datatype(*sendtype),
307 recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));
310 void mpi_scan__(void* sendbuf, void* recvbuf, int* count, int* datatype,
311 int* op, int* comm, int* ierr) {
312 *ierr = MPI_Scan(sendbuf, recvbuf, *count, get_datatype(*datatype),
313 get_op(*op), get_comm(*comm));
316 void mpi_alltoall__(void* sendbuf, int* sendcount, int* sendtype,
317 void* recvbuf, int* recvcount, int* recvtype, int* comm, int* ierr) {
318 *ierr = MPI_Alltoall(sendbuf, *sendcount, get_datatype(*sendtype),
319 recvbuf, *recvcount, get_datatype(*recvtype), get_comm(*comm));