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 smpi_mpi_request_free(request);
71 /* MPI Low level calls */
72 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
73 int dst, int tag, MPI_Comm comm)
76 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
77 comm, PERSISTENT | SEND);
82 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
83 int src, int tag, MPI_Comm comm)
86 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
87 comm, PERSISTENT | RECV);
92 void smpi_mpi_start(MPI_Request request)
97 xbt_assert(!request->action,
98 "Cannot (re)start a non-finished communication");
99 if(request->flags & RECV) {
100 print_request("New recv", request);
101 mailbox = smpi_process_mailbox();
102 // FIXME: SIMIX does not yet support non-contiguous datatypes
103 request->action = simcall_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
105 print_request("New send", request);
106 mailbox = smpi_process_remote_mailbox(
107 smpi_group_index(smpi_comm_group(request->comm), request->dst));
108 // FIXME: SIMIX does not yet support non-contiguous datatypes
110 if (request->size < 64*1024 ) { // eager mode => detached send (FIXME: this limit should be configurable)
111 void *oldbuf = request->buf;
113 request->buf = malloc(request->size);
114 memcpy(request->buf,oldbuf,request->size);
115 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
117 XBT_DEBUG("Send request %p is not detached (buf: %p)",request,request->buf);
120 simcall_comm_isend(mailbox, request->size, -1.0,
121 request->buf, request->size,
123 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
125 // detach if msg size < eager/rdv switch limit
129 simcall_set_category (request->action, TRACE_internal_smpi_get_category());
134 void smpi_mpi_startall(int count, MPI_Request * requests)
138 for(i = 0; i < count; i++) {
139 smpi_mpi_start(requests[i]);
143 void smpi_mpi_request_free(MPI_Request * request)
146 *request = MPI_REQUEST_NULL;
149 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
150 int dst, int tag, MPI_Comm comm)
152 MPI_Request request =
153 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
154 comm, NON_PERSISTENT | SEND);
159 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
160 int dst, int tag, MPI_Comm comm)
162 MPI_Request request =
163 smpi_isend_init(buf, count, datatype, dst, tag, comm);
165 smpi_mpi_start(request);
169 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
170 int src, int tag, MPI_Comm comm)
172 MPI_Request request =
173 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
174 comm, NON_PERSISTENT | RECV);
179 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
180 int src, int tag, MPI_Comm comm)
182 MPI_Request request =
183 smpi_irecv_init(buf, count, datatype, src, tag, comm);
185 smpi_mpi_start(request);
189 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
190 int tag, MPI_Comm comm, MPI_Status * status)
194 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
195 smpi_mpi_wait(&request, status);
198 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
199 int tag, MPI_Comm comm)
203 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
204 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
207 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
208 int dst, int sendtag, void *recvbuf, int recvcount,
209 MPI_Datatype recvtype, int src, int recvtag,
210 MPI_Comm comm, MPI_Status * status)
212 MPI_Request requests[2];
216 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
218 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
219 smpi_mpi_startall(2, requests);
220 smpi_mpi_waitall(2, requests, stats);
221 if(status != MPI_STATUS_IGNORE) {
222 // Copy receive status
223 memcpy(status, &stats[1], sizeof(MPI_Status));
227 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
229 return status->count / smpi_datatype_size(datatype);
232 static void finish_wait(MPI_Request * request, MPI_Status * status)
234 MPI_Request req = *request;
236 if(status != MPI_STATUS_IGNORE) {
237 status->MPI_SOURCE = req->src;
238 status->MPI_TAG = req->tag;
239 status->MPI_ERROR = MPI_SUCCESS;
240 // FIXME: really this should just contain the count of receive-type blocks,
242 status->count = req->size;
244 print_request("Finishing", req);
245 if(req->flags & NON_PERSISTENT) {
246 smpi_mpi_request_free(request);
252 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
255 if ((*request)->action == NULL)
258 flag = simcall_comm_test((*request)->action);
260 smpi_mpi_wait(request, status);
265 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
272 *index = MPI_UNDEFINED;
275 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
276 map = xbt_new(int, count);
278 for(i = 0; i < count; i++) {
279 if(requests[i]->action) {
280 xbt_dynar_push(comms, &requests[i]->action);
286 i = simcall_comm_testany(comms);
287 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
288 if(i != MPI_UNDEFINED) {
290 smpi_mpi_wait(&requests[*index], status);
295 xbt_dynar_free(&comms);
300 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
302 print_request("Waiting", *request);
303 if ((*request)->action != NULL) { // this is not a detached send
304 simcall_comm_wait((*request)->action, -1.0);
305 finish_wait(request, status);
307 // FIXME for a detached send, finish_wait is not called:
310 int smpi_mpi_waitany(int count, MPI_Request requests[],
317 index = MPI_UNDEFINED;
319 // Wait for a request to complete
320 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
321 map = xbt_new(int, count);
323 XBT_DEBUG("Wait for one of");
324 for(i = 0; i < count; i++) {
325 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
326 print_request(" ", requests[i]);
327 xbt_dynar_push(comms, &requests[i]->action);
333 i = simcall_comm_waitany(comms);
334 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
335 if (i != MPI_UNDEFINED) {
337 finish_wait(&requests[index], status);
341 xbt_dynar_free(&comms);
346 void smpi_mpi_waitall(int count, MPI_Request requests[],
351 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
353 for(c = 0; c < count; c++) {
355 smpi_mpi_wait(&requests[c], pstat);
358 index = smpi_mpi_waitany(count, requests, pstat);
359 if(index == MPI_UNDEFINED) {
363 if(status != MPI_STATUS_IGNORE) {
364 memcpy(&status[index], pstat, sizeof(*pstat));
369 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
375 for(i = 0; i < incount; i++) {
376 if(smpi_mpi_testany(incount, requests, &index, status)) {
377 indices[count] = index;
384 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
387 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
388 nary_tree_bcast(buf, count, datatype, root, comm, 4);
391 void smpi_mpi_barrier(MPI_Comm comm)
393 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
394 nary_tree_barrier(comm, 4);
397 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
398 void *recvbuf, int recvcount, MPI_Datatype recvtype,
399 int root, MPI_Comm comm)
401 int system_tag = 666;
402 int rank, size, src, index;
403 MPI_Aint lb = 0, recvext = 0;
404 MPI_Request *requests;
406 rank = smpi_comm_rank(comm);
407 size = smpi_comm_size(comm);
409 // Send buffer to root
410 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
412 // FIXME: check for errors
413 smpi_datatype_extent(recvtype, &lb, &recvext);
414 // Local copy from root
415 smpi_datatype_copy(sendbuf, sendcount, sendtype,
416 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
417 // Receive buffers from senders
418 requests = xbt_new(MPI_Request, size - 1);
420 for(src = 0; src < size; src++) {
422 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
424 src, system_tag, comm);
428 // Wait for completion of irecv's.
429 smpi_mpi_startall(size - 1, requests);
430 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
435 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
436 void *recvbuf, int *recvcounts, int *displs,
437 MPI_Datatype recvtype, int root, MPI_Comm comm)
439 int system_tag = 666;
440 int rank, size, src, index;
441 MPI_Aint lb = 0, recvext = 0;
442 MPI_Request *requests;
444 rank = smpi_comm_rank(comm);
445 size = smpi_comm_size(comm);
447 // Send buffer to root
448 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
450 // FIXME: check for errors
451 smpi_datatype_extent(recvtype, &lb, &recvext);
452 // Local copy from root
453 smpi_datatype_copy(sendbuf, sendcount, sendtype,
454 (char *)recvbuf + displs[root] * recvext,
455 recvcounts[root], recvtype);
456 // Receive buffers from senders
457 requests = xbt_new(MPI_Request, size - 1);
459 for(src = 0; src < size; src++) {
462 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
463 recvcounts[src], recvtype, src, system_tag, comm);
467 // Wait for completion of irecv's.
468 smpi_mpi_startall(size - 1, requests);
469 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
474 void smpi_mpi_allgather(void *sendbuf, int sendcount,
475 MPI_Datatype sendtype, void *recvbuf,
476 int recvcount, MPI_Datatype recvtype,
479 int system_tag = 666;
480 int rank, size, other, index;
481 MPI_Aint lb = 0, recvext = 0;
482 MPI_Request *requests;
484 rank = smpi_comm_rank(comm);
485 size = smpi_comm_size(comm);
486 // FIXME: check for errors
487 smpi_datatype_extent(recvtype, &lb, &recvext);
488 // Local copy from self
489 smpi_datatype_copy(sendbuf, sendcount, sendtype,
490 (char *)recvbuf + rank * recvcount * recvext, recvcount,
492 // Send/Recv buffers to/from others;
493 requests = xbt_new(MPI_Request, 2 * (size - 1));
495 for(other = 0; other < size; other++) {
498 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
501 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
502 recvcount, recvtype, other,
507 // Wait for completion of all comms.
508 smpi_mpi_startall(2 * (size - 1), requests);
509 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
513 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
514 MPI_Datatype sendtype, void *recvbuf,
515 int *recvcounts, int *displs,
516 MPI_Datatype recvtype, MPI_Comm comm)
518 int system_tag = 666;
519 int rank, size, other, index;
520 MPI_Aint lb = 0, recvext = 0;
521 MPI_Request *requests;
523 rank = smpi_comm_rank(comm);
524 size = smpi_comm_size(comm);
525 // FIXME: check for errors
526 smpi_datatype_extent(recvtype, &lb, &recvext);
527 // Local copy from self
528 smpi_datatype_copy(sendbuf, sendcount, sendtype,
529 (char *)recvbuf + displs[rank] * recvext,
530 recvcounts[rank], recvtype);
531 // Send buffers to others;
532 requests = xbt_new(MPI_Request, 2 * (size - 1));
534 for(other = 0; other < size; other++) {
537 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
541 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
542 recvtype, other, system_tag, comm);
546 // Wait for completion of all comms.
547 smpi_mpi_startall(2 * (size - 1), requests);
548 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
552 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
553 void *recvbuf, int recvcount, MPI_Datatype recvtype,
554 int root, MPI_Comm comm)
556 int system_tag = 666;
557 int rank, size, dst, index;
558 MPI_Aint lb = 0, sendext = 0;
559 MPI_Request *requests;
561 rank = smpi_comm_rank(comm);
562 size = smpi_comm_size(comm);
564 // Recv buffer from root
565 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
568 // FIXME: check for errors
569 smpi_datatype_extent(sendtype, &lb, &sendext);
570 // Local copy from root
571 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
572 sendcount, sendtype, recvbuf, recvcount, recvtype);
573 // Send buffers to receivers
574 requests = xbt_new(MPI_Request, size - 1);
576 for(dst = 0; dst < size; dst++) {
578 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
579 sendcount, sendtype, dst,
584 // Wait for completion of isend's.
585 smpi_mpi_startall(size - 1, requests);
586 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
591 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
592 MPI_Datatype sendtype, void *recvbuf, int recvcount,
593 MPI_Datatype recvtype, int root, MPI_Comm comm)
595 int system_tag = 666;
596 int rank, size, dst, index;
597 MPI_Aint lb = 0, sendext = 0;
598 MPI_Request *requests;
600 rank = smpi_comm_rank(comm);
601 size = smpi_comm_size(comm);
603 // Recv buffer from root
604 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
607 // FIXME: check for errors
608 smpi_datatype_extent(sendtype, &lb, &sendext);
609 // Local copy from root
610 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
611 sendtype, recvbuf, recvcount, recvtype);
612 // Send buffers to receivers
613 requests = xbt_new(MPI_Request, size - 1);
615 for(dst = 0; dst < size; dst++) {
618 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
619 sendtype, dst, system_tag, comm);
623 // Wait for completion of isend's.
624 smpi_mpi_startall(size - 1, requests);
625 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
630 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
631 MPI_Datatype datatype, MPI_Op op, int root,
634 int system_tag = 666;
635 int rank, size, src, index;
636 MPI_Aint lb = 0, dataext = 0;
637 MPI_Request *requests;
640 rank = smpi_comm_rank(comm);
641 size = smpi_comm_size(comm);
643 // Send buffer to root
644 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
646 // FIXME: check for errors
647 smpi_datatype_extent(datatype, &lb, &dataext);
648 // Local copy from root
649 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
650 // Receive buffers from senders
651 //TODO: make a MPI_barrier here ?
652 requests = xbt_new(MPI_Request, size - 1);
653 tmpbufs = xbt_new(void *, size - 1);
655 for(src = 0; src < size; src++) {
657 // FIXME: possibly overkill we we have contiguous/noncontiguous data
659 tmpbufs[index] = xbt_malloc(count * dataext);
661 smpi_irecv_init(tmpbufs[index], count, datatype, src,
666 // Wait for completion of irecv's.
667 smpi_mpi_startall(size - 1, requests);
668 for(src = 0; src < size - 1; src++) {
669 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
670 if(index == MPI_UNDEFINED) {
673 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
675 for(index = 0; index < size - 1; index++) {
676 xbt_free(tmpbufs[index]);
683 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
684 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
686 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
687 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
690 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
691 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
693 int system_tag = 666;
694 int rank, size, other, index;
695 MPI_Aint lb = 0, dataext = 0;
696 MPI_Request *requests;
699 rank = smpi_comm_rank(comm);
700 size = smpi_comm_size(comm);
702 // FIXME: check for errors
703 smpi_datatype_extent(datatype, &lb, &dataext);
705 // Local copy from self
706 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
708 // Send/Recv buffers to/from others;
709 requests = xbt_new(MPI_Request, size - 1);
710 tmpbufs = xbt_new(void *, rank);
712 for(other = 0; other < rank; other++) {
713 // FIXME: possibly overkill we we have contiguous/noncontiguous data
715 tmpbufs[index] = xbt_malloc(count * dataext);
717 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
721 for(other = rank + 1; other < size; other++) {
723 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
726 // Wait for completion of all comms.
727 smpi_mpi_startall(size - 1, requests);
728 for(other = 0; other < size - 1; other++) {
729 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
730 if(index == MPI_UNDEFINED) {
734 // #Request is below rank: it's a irecv
735 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
738 for(index = 0; index < rank; index++) {
739 xbt_free(tmpbufs[index]);