1 /* Copyright (c) 2007, 2008, 2009, 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. */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
12 "Logging specific to SMPI (base)");
13 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
14 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
15 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
16 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
17 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi_dt);
18 XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
19 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
20 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
21 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
23 static int match_recv(void* a, void* b) {
24 MPI_Request ref = (MPI_Request)a;
25 MPI_Request req = (MPI_Request)b;
27 xbt_assert(ref, "Cannot match recv against null reference");
28 xbt_assert(req, "Cannot match recv against null request");
29 return (ref->src == MPI_ANY_SOURCE || req->src == ref->src)
30 && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag);
33 static int match_send(void* a, void* b) {
34 MPI_Request ref = (MPI_Request)a;
35 MPI_Request req = (MPI_Request)b;
37 xbt_assert(ref, "Cannot match send against null reference");
38 xbt_assert(req, "Cannot match send against null request");
39 return (req->src == MPI_ANY_SOURCE || req->src == ref->src)
40 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
43 static MPI_Request build_request(void *buf, int count,
44 MPI_Datatype datatype, int src, int dst,
45 int tag, MPI_Comm comm, unsigned flags)
49 request = xbt_new(s_smpi_mpi_request_t, 1);
51 // FIXME: this will have to be changed to support non-contiguous datatypes
52 request->size = smpi_datatype_size(datatype) * count;
57 request->action = NULL;
58 request->flags = flags;
66 static void smpi_mpi_request_free_voidp(void* request)
68 MPI_Request req = request;
69 smpi_mpi_request_free(&req);
72 /* MPI Low level calls */
73 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
74 int dst, int tag, MPI_Comm comm)
77 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
78 comm, PERSISTENT | SEND);
83 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
84 int src, int tag, MPI_Comm comm)
87 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
88 comm, PERSISTENT | RECV);
93 void smpi_mpi_start(MPI_Request request)
98 xbt_assert(!request->action,
99 "Cannot (re)start a non-finished communication");
100 if(request->flags & RECV) {
101 print_request("New recv", request);
102 mailbox = smpi_process_mailbox();
103 // FIXME: SIMIX does not yet support non-contiguous datatypes
104 request->action = simcall_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
106 print_request("New send", request);
107 mailbox = smpi_process_remote_mailbox(
108 smpi_group_index(smpi_comm_group(request->comm), request->dst));
109 // FIXME: SIMIX does not yet support non-contiguous datatypes
111 if (request->size < 64*1024 ) { // eager mode => detached send (FIXME: this limit should be configurable)
112 void *oldbuf = request->buf;
114 request->buf = malloc(request->size);
115 memcpy(request->buf,oldbuf,request->size);
116 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
118 XBT_DEBUG("Send request %p is not detached (buf: %p)",request,request->buf);
121 simcall_comm_isend(mailbox, request->size, -1.0,
122 request->buf, request->size,
124 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
126 // detach if msg size < eager/rdv switch limit
130 /* FIXME: detached sends are not traceable (request->action == NULL) */
132 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
137 void smpi_mpi_startall(int count, MPI_Request * requests)
141 for(i = 0; i < count; i++) {
142 smpi_mpi_start(requests[i]);
146 void smpi_mpi_request_free(MPI_Request * request)
149 *request = MPI_REQUEST_NULL;
152 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
153 int dst, int tag, MPI_Comm comm)
155 MPI_Request request =
156 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
157 comm, NON_PERSISTENT | SEND);
162 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
163 int dst, int tag, MPI_Comm comm)
165 MPI_Request request =
166 smpi_isend_init(buf, count, datatype, dst, tag, comm);
168 smpi_mpi_start(request);
172 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
173 int src, int tag, MPI_Comm comm)
175 MPI_Request request =
176 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
177 comm, NON_PERSISTENT | RECV);
182 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
183 int src, int tag, MPI_Comm comm)
185 MPI_Request request =
186 smpi_irecv_init(buf, count, datatype, src, tag, comm);
188 smpi_mpi_start(request);
192 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
193 int tag, MPI_Comm comm, MPI_Status * status)
197 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
198 smpi_mpi_wait(&request, status);
201 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
202 int tag, MPI_Comm comm)
206 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
207 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
210 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
211 int dst, int sendtag, void *recvbuf, int recvcount,
212 MPI_Datatype recvtype, int src, int recvtag,
213 MPI_Comm comm, MPI_Status * status)
215 MPI_Request requests[2];
219 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
221 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
222 smpi_mpi_startall(2, requests);
223 smpi_mpi_waitall(2, requests, stats);
224 if(status != MPI_STATUS_IGNORE) {
225 // Copy receive status
226 memcpy(status, &stats[1], sizeof(MPI_Status));
230 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
232 return status->count / smpi_datatype_size(datatype);
235 static void finish_wait(MPI_Request * request, MPI_Status * status)
237 MPI_Request req = *request;
239 if(status != MPI_STATUS_IGNORE) {
240 status->MPI_SOURCE = req->src;
241 status->MPI_TAG = req->tag;
242 status->MPI_ERROR = MPI_SUCCESS;
243 // FIXME: really this should just contain the count of receive-type blocks,
245 status->count = req->size;
247 print_request("Finishing", req);
248 if(req->flags & NON_PERSISTENT) {
249 smpi_mpi_request_free(request);
255 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
258 if ((*request)->action == NULL)
261 flag = simcall_comm_test((*request)->action);
263 smpi_mpi_wait(request, status);
268 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
275 *index = MPI_UNDEFINED;
278 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
279 map = xbt_new(int, count);
281 for(i = 0; i < count; i++) {
282 if(requests[i]->action) {
283 xbt_dynar_push(comms, &requests[i]->action);
289 i = simcall_comm_testany(comms);
290 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
291 if(i != MPI_UNDEFINED) {
293 smpi_mpi_wait(&requests[*index], status);
298 xbt_dynar_free(&comms);
303 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
305 print_request("Waiting", *request);
306 if ((*request)->action != NULL) { // this is not a detached send
307 simcall_comm_wait((*request)->action, -1.0);
308 finish_wait(request, status);
310 // FIXME for a detached send, finish_wait is not called:
313 int smpi_mpi_waitany(int count, MPI_Request requests[],
320 index = MPI_UNDEFINED;
322 // Wait for a request to complete
323 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
324 map = xbt_new(int, count);
326 XBT_DEBUG("Wait for one of");
327 for(i = 0; i < count; i++) {
328 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
329 print_request(" ", requests[i]);
330 xbt_dynar_push(comms, &requests[i]->action);
336 i = simcall_comm_waitany(comms);
337 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
338 if (i != MPI_UNDEFINED) {
340 finish_wait(&requests[index], status);
344 xbt_dynar_free(&comms);
349 void smpi_mpi_waitall(int count, MPI_Request requests[],
354 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
356 for(c = 0; c < count; c++) {
358 smpi_mpi_wait(&requests[c], pstat);
361 index = smpi_mpi_waitany(count, requests, pstat);
362 if(index == MPI_UNDEFINED) {
366 if(status != MPI_STATUS_IGNORE) {
367 memcpy(&status[index], pstat, sizeof(*pstat));
372 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
378 for(i = 0; i < incount; i++) {
379 if(smpi_mpi_testany(incount, requests, &index, status)) {
380 indices[count] = index;
387 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
390 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
391 nary_tree_bcast(buf, count, datatype, root, comm, 4);
394 void smpi_mpi_barrier(MPI_Comm comm)
396 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
397 nary_tree_barrier(comm, 4);
400 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
401 void *recvbuf, int recvcount, MPI_Datatype recvtype,
402 int root, MPI_Comm comm)
404 int system_tag = 666;
405 int rank, size, src, index;
406 MPI_Aint lb = 0, recvext = 0;
407 MPI_Request *requests;
409 rank = smpi_comm_rank(comm);
410 size = smpi_comm_size(comm);
412 // Send buffer to root
413 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
415 // FIXME: check for errors
416 smpi_datatype_extent(recvtype, &lb, &recvext);
417 // Local copy from root
418 smpi_datatype_copy(sendbuf, sendcount, sendtype,
419 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
420 // Receive buffers from senders
421 requests = xbt_new(MPI_Request, size - 1);
423 for(src = 0; src < size; src++) {
425 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
427 src, system_tag, comm);
431 // Wait for completion of irecv's.
432 smpi_mpi_startall(size - 1, requests);
433 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
438 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
439 void *recvbuf, int *recvcounts, int *displs,
440 MPI_Datatype recvtype, int root, MPI_Comm comm)
442 int system_tag = 666;
443 int rank, size, src, index;
444 MPI_Aint lb = 0, recvext = 0;
445 MPI_Request *requests;
447 rank = smpi_comm_rank(comm);
448 size = smpi_comm_size(comm);
450 // Send buffer to root
451 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
453 // FIXME: check for errors
454 smpi_datatype_extent(recvtype, &lb, &recvext);
455 // Local copy from root
456 smpi_datatype_copy(sendbuf, sendcount, sendtype,
457 (char *)recvbuf + displs[root] * recvext,
458 recvcounts[root], recvtype);
459 // Receive buffers from senders
460 requests = xbt_new(MPI_Request, size - 1);
462 for(src = 0; src < size; src++) {
465 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
466 recvcounts[src], recvtype, src, system_tag, comm);
470 // Wait for completion of irecv's.
471 smpi_mpi_startall(size - 1, requests);
472 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
477 void smpi_mpi_allgather(void *sendbuf, int sendcount,
478 MPI_Datatype sendtype, void *recvbuf,
479 int recvcount, MPI_Datatype recvtype,
482 int system_tag = 666;
483 int rank, size, other, index;
484 MPI_Aint lb = 0, recvext = 0;
485 MPI_Request *requests;
487 rank = smpi_comm_rank(comm);
488 size = smpi_comm_size(comm);
489 // FIXME: check for errors
490 smpi_datatype_extent(recvtype, &lb, &recvext);
491 // Local copy from self
492 smpi_datatype_copy(sendbuf, sendcount, sendtype,
493 (char *)recvbuf + rank * recvcount * recvext, recvcount,
495 // Send/Recv buffers to/from others;
496 requests = xbt_new(MPI_Request, 2 * (size - 1));
498 for(other = 0; other < size; other++) {
501 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
504 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
505 recvcount, recvtype, other,
510 // Wait for completion of all comms.
511 smpi_mpi_startall(2 * (size - 1), requests);
512 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
516 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
517 MPI_Datatype sendtype, void *recvbuf,
518 int *recvcounts, int *displs,
519 MPI_Datatype recvtype, MPI_Comm comm)
521 int system_tag = 666;
522 int rank, size, other, index;
523 MPI_Aint lb = 0, recvext = 0;
524 MPI_Request *requests;
526 rank = smpi_comm_rank(comm);
527 size = smpi_comm_size(comm);
528 // FIXME: check for errors
529 smpi_datatype_extent(recvtype, &lb, &recvext);
530 // Local copy from self
531 smpi_datatype_copy(sendbuf, sendcount, sendtype,
532 (char *)recvbuf + displs[rank] * recvext,
533 recvcounts[rank], recvtype);
534 // Send buffers to others;
535 requests = xbt_new(MPI_Request, 2 * (size - 1));
537 for(other = 0; other < size; other++) {
540 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
544 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
545 recvtype, other, system_tag, comm);
549 // Wait for completion of all comms.
550 smpi_mpi_startall(2 * (size - 1), requests);
551 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
555 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
556 void *recvbuf, int recvcount, MPI_Datatype recvtype,
557 int root, MPI_Comm comm)
559 int system_tag = 666;
560 int rank, size, dst, index;
561 MPI_Aint lb = 0, sendext = 0;
562 MPI_Request *requests;
564 rank = smpi_comm_rank(comm);
565 size = smpi_comm_size(comm);
567 // Recv buffer from root
568 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
571 // FIXME: check for errors
572 smpi_datatype_extent(sendtype, &lb, &sendext);
573 // Local copy from root
574 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
575 sendcount, sendtype, recvbuf, recvcount, recvtype);
576 // Send buffers to receivers
577 requests = xbt_new(MPI_Request, size - 1);
579 for(dst = 0; dst < size; dst++) {
581 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
582 sendcount, sendtype, dst,
587 // Wait for completion of isend's.
588 smpi_mpi_startall(size - 1, requests);
589 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
594 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
595 MPI_Datatype sendtype, void *recvbuf, int recvcount,
596 MPI_Datatype recvtype, int root, MPI_Comm comm)
598 int system_tag = 666;
599 int rank, size, dst, index;
600 MPI_Aint lb = 0, sendext = 0;
601 MPI_Request *requests;
603 rank = smpi_comm_rank(comm);
604 size = smpi_comm_size(comm);
606 // Recv buffer from root
607 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
610 // FIXME: check for errors
611 smpi_datatype_extent(sendtype, &lb, &sendext);
612 // Local copy from root
613 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
614 sendtype, recvbuf, recvcount, recvtype);
615 // Send buffers to receivers
616 requests = xbt_new(MPI_Request, size - 1);
618 for(dst = 0; dst < size; dst++) {
621 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
622 sendtype, dst, system_tag, comm);
626 // Wait for completion of isend's.
627 smpi_mpi_startall(size - 1, requests);
628 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
633 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
634 MPI_Datatype datatype, MPI_Op op, int root,
637 int system_tag = 666;
638 int rank, size, src, index;
639 MPI_Aint lb = 0, dataext = 0;
640 MPI_Request *requests;
643 rank = smpi_comm_rank(comm);
644 size = smpi_comm_size(comm);
646 // Send buffer to root
647 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
649 // FIXME: check for errors
650 smpi_datatype_extent(datatype, &lb, &dataext);
651 // Local copy from root
652 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
653 // Receive buffers from senders
654 //TODO: make a MPI_barrier here ?
655 requests = xbt_new(MPI_Request, size - 1);
656 tmpbufs = xbt_new(void *, size - 1);
658 for(src = 0; src < size; src++) {
660 // FIXME: possibly overkill we we have contiguous/noncontiguous data
662 tmpbufs[index] = xbt_malloc(count * dataext);
664 smpi_irecv_init(tmpbufs[index], count, datatype, src,
669 // Wait for completion of irecv's.
670 smpi_mpi_startall(size - 1, requests);
671 for(src = 0; src < size - 1; src++) {
672 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
673 if(index == MPI_UNDEFINED) {
676 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
678 for(index = 0; index < size - 1; index++) {
679 xbt_free(tmpbufs[index]);
686 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
687 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
689 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
690 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
693 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
694 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
696 int system_tag = 666;
697 int rank, size, other, index;
698 MPI_Aint lb = 0, dataext = 0;
699 MPI_Request *requests;
702 rank = smpi_comm_rank(comm);
703 size = smpi_comm_size(comm);
705 // FIXME: check for errors
706 smpi_datatype_extent(datatype, &lb, &dataext);
708 // Local copy from self
709 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
711 // Send/Recv buffers to/from others;
712 requests = xbt_new(MPI_Request, size - 1);
713 tmpbufs = xbt_new(void *, rank);
715 for(other = 0; other < rank; other++) {
716 // FIXME: possibly overkill we we have contiguous/noncontiguous data
718 tmpbufs[index] = xbt_malloc(count * dataext);
720 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
724 for(other = rank + 1; other < size; other++) {
726 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
729 // Wait for completion of all comms.
730 smpi_mpi_startall(size - 1, requests);
731 for(other = 0; other < size - 1; other++) {
732 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
733 if(index == MPI_UNDEFINED) {
737 // #Request is below rank: it's a irecv
738 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
741 for(index = 0; index < rank; index++) {
742 xbt_free(tmpbufs[index]);