4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
5 "Logging specific to SMPI (base)");
6 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
7 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
8 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
9 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
10 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi_dt);
11 XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
12 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
13 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
14 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
16 void smpi_process_init(int* argc, char*** argv) {
18 smpi_process_data_t data;
21 proc = SIMIX_process_self();
22 index = atoi((*argv)[1]);
23 data = smpi_process_remote_data(index);
24 SIMIX_process_set_data(proc, data);
27 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
28 (*argv)[(*argc) - 1] = NULL;
31 DEBUG2("<%d> New process in the game: %p", index, proc);
34 void smpi_process_destroy(void) {
35 int index = smpi_process_index();
37 DEBUG1("<%d> Process left the game", index);
40 /* MPI Low level calls */
41 MPI_Request smpi_mpi_isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
44 request = xbt_new(s_smpi_mpi_request_t, 1);
46 request->src = smpi_comm_rank(comm);
49 request->size = smpi_datatype_size(datatype) * count;
50 request->complete = 0;
51 request->data = request;
52 smpi_process_post_send(comm, request);
53 request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, buf, request->size, NULL);
57 MPI_Request smpi_mpi_irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
60 request = xbt_new(s_smpi_mpi_request_t, 1);
63 request->dst = smpi_comm_rank(comm);
65 request->size = smpi_datatype_size(datatype) * count;
66 request->complete = 0;
67 request->data = MPI_REQUEST_NULL;
68 smpi_process_post_recv(request);
69 request->pair = SIMIX_network_irecv(request->rdv, buf, &request->size);
73 void smpi_mpi_recv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status* status) {
76 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
77 smpi_mpi_wait(&request, status);
80 void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
83 request = smpi_mpi_isend(buf, count, datatype, src, tag, comm);
84 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
87 void smpi_mpi_sendrecv(void* sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void* recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status* status) {
88 MPI_Request requests[2];
91 requests[0] = smpi_mpi_isend(sendbuf, sendcount, sendtype, dst, sendtag, comm);
92 requests[1] = smpi_mpi_irecv(recvbuf, recvcount, recvtype, src, recvtag, comm);
93 smpi_mpi_waitall(2, requests, stats);
94 if(status != MPI_STATUS_IGNORE) {
95 // Copy receive status
96 memcpy(status, &stats[1], sizeof(MPI_Status));
100 static void finish_wait(MPI_Request* request, MPI_Status* status) {
101 MPI_Request data = (*request)->data;
103 xbt_assert0(data != MPI_REQUEST_NULL, "Erroneous situation");
104 if(status != MPI_STATUS_IGNORE) {
105 status->MPI_SOURCE = (*request)->src;
106 status->MPI_TAG = (*request)->tag;
107 status->MPI_ERROR = MPI_SUCCESS;
109 DEBUG3("finishing wait for %p [data = %p, complete = %d]", *request, data, data->complete);
110 // data == *request if sender is first to finish its wait
111 // data != *request if receiver is first to finish its wait
112 if(data->complete == 0) {
113 // first arrives here
115 if(data != *request) {
116 // receveiver cleans its part
120 // second arrives here
121 if(data != *request) {
122 // receiver cleans everything
125 SIMIX_rdv_destroy((*request)->rdv);
128 *request = MPI_REQUEST_NULL;
131 int smpi_mpi_test(MPI_Request* request, MPI_Status* status) {
132 MPI_Request data = (*request)->data;
133 int flag = data && data->complete == 1;
136 SIMIX_communication_destroy((*request)->pair);
137 finish_wait(request, status);
142 int smpi_mpi_testany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
146 *index = MPI_UNDEFINED;
148 for(i = 0; i < count; i++) {
149 if(requests[i] != MPI_REQUEST_NULL) {
150 data = requests[i]->data;
151 if(data != MPI_REQUEST_NULL && data->complete == 1) {
152 SIMIX_communication_destroy(requests[i]->pair);
153 finish_wait(&requests[i], status);
163 void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
164 MPI_Request data = (*request)->data;
166 DEBUG6("wait for request %p (%p: %p) [src = %d, dst = %d, tag = %d]",
167 *request, (*request)->pair, data, (*request)->src, (*request)->dst, (*request)->tag);
168 // data is null if receiver waits before sender enters the rdv
169 if(data == MPI_REQUEST_NULL || data->complete == 0) {
170 SIMIX_network_wait((*request)->pair, -1.0);
172 SIMIX_communication_destroy((*request)->pair);
174 finish_wait(request, status);
177 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
183 index = MPI_UNDEFINED;
185 // First check for already completed requests
186 for(i = 0; i < count; i++) {
187 if(requests[i] != MPI_REQUEST_NULL) {
188 data = requests[i]->data;
189 if(data != MPI_REQUEST_NULL && data->complete == 1) {
191 SIMIX_communication_destroy(requests[index]->pair); // always succeeds (but cleans the simix layer)
196 if(index == MPI_UNDEFINED) {
197 // Otherwise, wait for a request to complete
198 comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
199 map = xbt_new(int, count);
201 DEBUG0("Wait for one of");
202 for(i = 0; i < count; i++) {
203 if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete == 0) {
204 DEBUG4(" request %p [src = %d, dst = %d, tag = %d]",
205 requests[i], requests[i]->src, requests[i]->dst, requests[i]->tag);
206 xbt_dynar_push(comms, &requests[i]->pair);
212 index = SIMIX_network_waitany(comms);
216 xbt_dynar_free_container(&comms);
218 if(index != MPI_UNDEFINED) {
219 finish_wait(&requests[index], status);
225 void smpi_mpi_waitall(int count, MPI_Request requests[], MPI_Status status[]) {
230 index = smpi_mpi_waitany(count, requests, &stat);
231 if(index == MPI_UNDEFINED) {
234 if(status != MPI_STATUS_IGNORE) {
235 memcpy(&status[index], &stat, sizeof(stat));
237 // Move the last request to the found position
238 requests[index] = requests[count - 1];
239 requests[count - 1] = MPI_REQUEST_NULL;
244 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int* indices, MPI_Status status[]) {
249 for(i = 0; i < incount; i++) {
250 if(requests[i] != MPI_REQUEST_NULL) {
251 data = requests[i]->data;
252 if(data != MPI_REQUEST_NULL && data->complete == 1) {
253 SIMIX_communication_destroy(requests[i]->pair);
254 finish_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
263 void smpi_mpi_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
264 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
265 nary_tree_bcast(buf, count, datatype, root, comm, 4);
268 void smpi_mpi_barrier(MPI_Comm comm) {
269 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
270 nary_tree_barrier(comm, 4);
273 void smpi_mpi_gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
274 int system_tag = 666;
275 int rank, size, src, index, sendsize, recvsize;
276 MPI_Request* requests;
278 rank = smpi_comm_rank(comm);
279 size = smpi_comm_size(comm);
281 // Send buffer to root
282 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
284 sendsize = smpi_datatype_size(sendtype);
285 recvsize = smpi_datatype_size(recvtype);
286 // Local copy from root
287 memcpy(&((char*)recvbuf)[root * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
288 // Receive buffers from senders
289 requests = xbt_new(MPI_Request, size - 1);
291 for(src = 0; src < size; src++) {
293 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[src * recvcount * recvsize], recvcount, recvtype, src, system_tag, comm);
297 // Wait for completion of irecv's.
298 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
303 void smpi_mpi_gatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, int root, MPI_Comm comm) {
304 int system_tag = 666;
305 int rank, size, src, index, sendsize;
306 MPI_Request* requests;
308 rank = smpi_comm_rank(comm);
309 size = smpi_comm_size(comm);
311 // Send buffer to root
312 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
314 sendsize = smpi_datatype_size(sendtype);
315 // Local copy from root
316 memcpy(&((char*)recvbuf)[displs[root]], sendbuf, sendcount * sendsize * sizeof(char));
317 // Receive buffers from senders
318 requests = xbt_new(MPI_Request, size - 1);
320 for(src = 0; src < size; src++) {
322 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[displs[src]], recvcounts[src], recvtype, src, system_tag, comm);
326 // Wait for completion of irecv's.
327 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
332 void smpi_mpi_allgather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
333 int system_tag = 666;
334 int rank, size, other, index, sendsize, recvsize;
335 MPI_Request* requests;
337 rank = smpi_comm_rank(comm);
338 size = smpi_comm_size(comm);
339 sendsize = smpi_datatype_size(sendtype);
340 recvsize = smpi_datatype_size(recvtype);
341 // Local copy from self
342 memcpy(&((char*)recvbuf)[rank * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
343 // Send/Recv buffers to/from others;
344 requests = xbt_new(MPI_Request, 2 * (size - 1));
346 for(other = 0; other < size; other++) {
348 requests[index] = smpi_mpi_isend(sendbuf, sendcount, sendtype, other, system_tag, comm);
350 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[other * recvcount * recvsize], recvcount, recvtype, other, system_tag, comm);
354 // Wait for completion of all comms.
355 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
359 void smpi_mpi_allgatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, MPI_Comm comm) {
360 int system_tag = 666;
361 int rank, size, other, index, sendsize, recvsize;
362 MPI_Request* requests;
364 rank = smpi_comm_rank(comm);
365 size = smpi_comm_size(comm);
366 sendsize = smpi_datatype_size(sendtype);
367 recvsize = smpi_datatype_size(recvtype);
368 // Local copy from self
369 memcpy(&((char*)recvbuf)[displs[rank]], sendbuf, sendcount * sendsize * sizeof(char));
370 // Send buffers to others;
371 requests = xbt_new(MPI_Request, 2 * (size - 1));
373 for(other = 0; other < size; other++) {
375 requests[index] = smpi_mpi_isend(sendbuf, sendcount, sendtype, other, system_tag, comm);
377 requests[index] = smpi_mpi_irecv(&((char*)recvbuf)[displs[other]], recvcounts[other], recvtype, other, system_tag, comm);
381 // Wait for completion of all comms.
382 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
386 void smpi_mpi_scatter(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
387 int system_tag = 666;
388 int rank, size, dst, index, sendsize, recvsize;
389 MPI_Request* requests;
391 rank = smpi_comm_rank(comm);
392 size = smpi_comm_size(comm);
394 // Recv buffer from root
395 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
397 sendsize = smpi_datatype_size(sendtype);
398 recvsize = smpi_datatype_size(recvtype);
399 // Local copy from root
400 memcpy(recvbuf, &((char*)sendbuf)[root * sendcount * sendsize], recvcount * recvsize * sizeof(char));
401 // Send buffers to receivers
402 requests = xbt_new(MPI_Request, size - 1);
404 for(dst = 0; dst < size; dst++) {
406 requests[index] = smpi_mpi_isend(&((char*)sendbuf)[dst * sendcount * sendsize], sendcount, sendtype, dst, system_tag, comm);
410 // Wait for completion of isend's.
411 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
416 void smpi_mpi_scatterv(void* sendbuf, int* sendcounts, int* displs, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
417 int system_tag = 666;
418 int rank, size, dst, index, sendsize, recvsize;
419 MPI_Request* requests;
421 rank = smpi_comm_rank(comm);
422 size = smpi_comm_size(comm);
424 // Recv buffer from root
425 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
427 sendsize = smpi_datatype_size(sendtype);
428 recvsize = smpi_datatype_size(recvtype);
429 // Local copy from root
430 memcpy(recvbuf, &((char*)sendbuf)[displs[root]], recvcount * recvsize * sizeof(char));
431 // Send buffers to receivers
432 requests = xbt_new(MPI_Request, size - 1);
434 for(dst = 0; dst < size; dst++) {
436 requests[index] = smpi_mpi_isend(&((char*)sendbuf)[displs[dst]], sendcounts[dst], sendtype, dst, system_tag, comm);
440 // Wait for completion of isend's.
441 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
446 void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
447 int system_tag = 666;
448 int rank, size, src, index, datasize;
449 MPI_Request* requests;
452 rank = smpi_comm_rank(comm);
453 size = smpi_comm_size(comm);
455 // Send buffer to root
456 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
458 datasize = smpi_datatype_size(datatype);
459 // Local copy from root
460 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
461 // Receive buffers from senders
462 //TODO: make a MPI_barrier here ?
463 requests = xbt_new(MPI_Request, size - 1);
464 tmpbufs = xbt_new(void*, size - 1);
466 for(src = 0; src < size; src++) {
468 tmpbufs[index] = xbt_malloc(count * datasize);
469 requests[index] = smpi_mpi_irecv(tmpbufs[index], count, datatype, src, system_tag, comm);
473 // Wait for completion of irecv's.
474 for(src = 0; src < size - 1; src++) {
475 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
476 if(index == MPI_UNDEFINED) {
479 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
481 for(index = 0; index < size - 1; index++) {
482 xbt_free(tmpbufs[index]);
489 void smpi_mpi_allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
490 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
491 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
494 FIXME: buggy implementation
496 int system_tag = 666;
497 int rank, size, other, index, datasize;
498 MPI_Request* requests;
501 rank = smpi_comm_rank(comm);
502 size = smpi_comm_size(comm);
503 datasize = smpi_datatype_size(datatype);
504 // Local copy from self
505 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
506 // Send/Recv buffers to/from others;
507 //TODO: make a MPI_barrier here ?
508 requests = xbt_new(MPI_Request, 2 * (size - 1));
509 tmpbufs = xbt_new(void*, size - 1);
511 for(other = 0; other < size; other++) {
513 tmpbufs[index / 2] = xbt_malloc(count * datasize);
514 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
515 requests[index + 1] = smpi_mpi_irecv(tmpbufs[index / 2], count, datatype, other, system_tag, comm);
519 // Wait for completion of all comms.
520 for(other = 0; other < 2 * (size - 1); other++) {
521 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
522 if(index == MPI_UNDEFINED) {
525 if((index & 1) == 1) {
526 // Request is odd: it's a irecv
527 smpi_op_apply(op, tmpbufs[index / 2], recvbuf, &count, &datatype);
530 for(index = 0; index < size - 1; index++) {
531 xbt_free(tmpbufs[index]);