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 /* MPI Low level calls */
67 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
68 int dst, int tag, MPI_Comm comm)
71 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
72 comm, PERSISTENT | SEND);
77 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
78 int src, int tag, MPI_Comm comm)
81 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
82 comm, PERSISTENT | RECV);
87 void smpi_mpi_start(MPI_Request request)
92 xbt_assert(!request->action,
93 "Cannot (re)start a non-finished communication");
94 if(request->flags & RECV) {
95 print_request("New recv", request);
96 mailbox = smpi_process_mailbox();
97 // FIXME: SIMIX does not yet support non-contiguous datatypes
98 request->action = SIMIX_req_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
100 print_request("New send", request);
101 mailbox = smpi_process_remote_mailbox(
102 smpi_group_index(smpi_comm_group(request->comm), request->dst));
103 // FIXME: SIMIX does not yet support non-contiguous datatypes
105 if (request->size < 64*1024 ) { // eager mode => detached send (FIXME: this limit should be configurable)
106 void *oldbuf = request->buf;
108 request->buf = malloc(request->size);
109 memcpy(request->buf,oldbuf,request->size);
110 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
112 XBT_DEBUG("Send request %p is not detached (buf: %p)",request,request->buf);
115 SIMIX_req_comm_isend(mailbox, request->size, -1.0,
116 request->buf, request->size,
117 &match_send,free, // a simple free is used to free the buffer
119 // detach if msg size < eager/rdv switch limit
123 SIMIX_req_set_category (request->action, TRACE_internal_smpi_get_category());
128 void smpi_mpi_startall(int count, MPI_Request * requests)
132 for(i = 0; i < count; i++) {
133 smpi_mpi_start(requests[i]);
137 void smpi_mpi_request_free(MPI_Request * request)
140 *request = MPI_REQUEST_NULL;
143 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
144 int dst, int tag, MPI_Comm comm)
146 MPI_Request request =
147 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
148 comm, NON_PERSISTENT | SEND);
153 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
154 int dst, int tag, MPI_Comm comm)
156 MPI_Request request =
157 smpi_isend_init(buf, count, datatype, dst, tag, comm);
159 smpi_mpi_start(request);
163 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
164 int src, int tag, MPI_Comm comm)
166 MPI_Request request =
167 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
168 comm, NON_PERSISTENT | RECV);
173 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
174 int src, int tag, MPI_Comm comm)
176 MPI_Request request =
177 smpi_irecv_init(buf, count, datatype, src, tag, comm);
179 smpi_mpi_start(request);
183 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
184 int tag, MPI_Comm comm, MPI_Status * status)
188 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
189 smpi_mpi_wait(&request, status);
192 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
193 int tag, MPI_Comm comm)
197 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
198 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
201 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
202 int dst, int sendtag, void *recvbuf, int recvcount,
203 MPI_Datatype recvtype, int src, int recvtag,
204 MPI_Comm comm, MPI_Status * status)
206 MPI_Request requests[2];
210 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
212 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
213 smpi_mpi_startall(2, requests);
214 smpi_mpi_waitall(2, requests, stats);
215 if(status != MPI_STATUS_IGNORE) {
216 // Copy receive status
217 memcpy(status, &stats[1], sizeof(MPI_Status));
221 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
223 return status->count / smpi_datatype_size(datatype);
226 static void finish_wait(MPI_Request * request, MPI_Status * status)
228 MPI_Request req = *request;
230 if(status != MPI_STATUS_IGNORE) {
231 status->MPI_SOURCE = req->src;
232 status->MPI_TAG = req->tag;
233 status->MPI_ERROR = MPI_SUCCESS;
234 // FIXME: really this should just contain the count of receive-type blocks,
236 status->count = req->size;
238 print_request("Finishing", req);
239 if(req->flags & NON_PERSISTENT) {
240 smpi_mpi_request_free(request);
246 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
249 if ((*request)->action == NULL)
252 flag = SIMIX_req_comm_test((*request)->action);
254 smpi_mpi_wait(request, status);
259 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
266 *index = MPI_UNDEFINED;
269 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
270 map = xbt_new(int, count);
272 for(i = 0; i < count; i++) {
273 if(requests[i]->action) {
274 xbt_dynar_push(comms, &requests[i]->action);
280 i = SIMIX_req_comm_testany(comms);
281 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
282 if(i != MPI_UNDEFINED) {
284 smpi_mpi_wait(&requests[*index], status);
289 xbt_dynar_free(&comms);
294 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
296 print_request("Waiting", *request);
297 if ((*request)->action != NULL )
298 SIMIX_req_comm_wait((*request)->action, -1.0);
299 finish_wait(request, status);
302 int smpi_mpi_waitany(int count, MPI_Request requests[],
309 index = MPI_UNDEFINED;
311 // Wait for a request to complete
312 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
313 map = xbt_new(int, count);
315 XBT_DEBUG("Wait for one of");
316 for(i = 0; i < count; i++) {
317 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
318 print_request(" ", requests[i]);
319 xbt_dynar_push(comms, &requests[i]->action);
325 i = SIMIX_req_comm_waitany(comms);
326 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
327 if (i != MPI_UNDEFINED) {
329 finish_wait(&requests[index], status);
333 xbt_dynar_free(&comms);
338 void smpi_mpi_waitall(int count, MPI_Request requests[],
343 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
345 for(c = 0; c < count; c++) {
347 smpi_mpi_wait(&requests[c], pstat);
350 index = smpi_mpi_waitany(count, requests, pstat);
351 if(index == MPI_UNDEFINED) {
355 if(status != MPI_STATUS_IGNORE) {
356 memcpy(&status[index], pstat, sizeof(*pstat));
361 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
367 for(i = 0; i < incount; i++) {
368 if(smpi_mpi_testany(incount, requests, &index, status)) {
369 indices[count] = index;
376 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
379 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
380 nary_tree_bcast(buf, count, datatype, root, comm, 4);
383 void smpi_mpi_barrier(MPI_Comm comm)
385 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
386 nary_tree_barrier(comm, 4);
389 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
390 void *recvbuf, int recvcount, MPI_Datatype recvtype,
391 int root, MPI_Comm comm)
393 int system_tag = 666;
394 int rank, size, src, index;
395 MPI_Aint lb = 0, recvext = 0;
396 MPI_Request *requests;
398 rank = smpi_comm_rank(comm);
399 size = smpi_comm_size(comm);
401 // Send buffer to root
402 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
404 // FIXME: check for errors
405 smpi_datatype_extent(recvtype, &lb, &recvext);
406 // Local copy from root
407 smpi_datatype_copy(sendbuf, sendcount, sendtype,
408 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
409 // Receive buffers from senders
410 requests = xbt_new(MPI_Request, size - 1);
412 for(src = 0; src < size; src++) {
414 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
416 src, system_tag, comm);
420 // Wait for completion of irecv's.
421 smpi_mpi_startall(size - 1, requests);
422 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
427 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
428 void *recvbuf, int *recvcounts, int *displs,
429 MPI_Datatype recvtype, int root, MPI_Comm comm)
431 int system_tag = 666;
432 int rank, size, src, index;
433 MPI_Aint lb = 0, recvext = 0;
434 MPI_Request *requests;
436 rank = smpi_comm_rank(comm);
437 size = smpi_comm_size(comm);
439 // Send buffer to root
440 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
442 // FIXME: check for errors
443 smpi_datatype_extent(recvtype, &lb, &recvext);
444 // Local copy from root
445 smpi_datatype_copy(sendbuf, sendcount, sendtype,
446 (char *)recvbuf + displs[root] * recvext,
447 recvcounts[root], recvtype);
448 // Receive buffers from senders
449 requests = xbt_new(MPI_Request, size - 1);
451 for(src = 0; src < size; src++) {
454 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
455 recvcounts[src], recvtype, src, system_tag, comm);
459 // Wait for completion of irecv's.
460 smpi_mpi_startall(size - 1, requests);
461 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
466 void smpi_mpi_allgather(void *sendbuf, int sendcount,
467 MPI_Datatype sendtype, void *recvbuf,
468 int recvcount, MPI_Datatype recvtype,
471 int system_tag = 666;
472 int rank, size, other, index;
473 MPI_Aint lb = 0, recvext = 0;
474 MPI_Request *requests;
476 rank = smpi_comm_rank(comm);
477 size = smpi_comm_size(comm);
478 // FIXME: check for errors
479 smpi_datatype_extent(recvtype, &lb, &recvext);
480 // Local copy from self
481 smpi_datatype_copy(sendbuf, sendcount, sendtype,
482 (char *)recvbuf + rank * recvcount * recvext, recvcount,
484 // Send/Recv buffers to/from others;
485 requests = xbt_new(MPI_Request, 2 * (size - 1));
487 for(other = 0; other < size; other++) {
490 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
493 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
494 recvcount, recvtype, other,
499 // Wait for completion of all comms.
500 smpi_mpi_startall(2 * (size - 1), requests);
501 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
505 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
506 MPI_Datatype sendtype, void *recvbuf,
507 int *recvcounts, int *displs,
508 MPI_Datatype recvtype, MPI_Comm comm)
510 int system_tag = 666;
511 int rank, size, other, index;
512 MPI_Aint lb = 0, recvext = 0;
513 MPI_Request *requests;
515 rank = smpi_comm_rank(comm);
516 size = smpi_comm_size(comm);
517 // FIXME: check for errors
518 smpi_datatype_extent(recvtype, &lb, &recvext);
519 // Local copy from self
520 smpi_datatype_copy(sendbuf, sendcount, sendtype,
521 (char *)recvbuf + displs[rank] * recvext,
522 recvcounts[rank], recvtype);
523 // Send buffers to others;
524 requests = xbt_new(MPI_Request, 2 * (size - 1));
526 for(other = 0; other < size; other++) {
529 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
533 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
534 recvtype, other, system_tag, comm);
538 // Wait for completion of all comms.
539 smpi_mpi_startall(2 * (size - 1), requests);
540 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
544 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
545 void *recvbuf, int recvcount, MPI_Datatype recvtype,
546 int root, MPI_Comm comm)
548 int system_tag = 666;
549 int rank, size, dst, index;
550 MPI_Aint lb = 0, sendext = 0;
551 MPI_Request *requests;
553 rank = smpi_comm_rank(comm);
554 size = smpi_comm_size(comm);
556 // Recv buffer from root
557 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
560 // FIXME: check for errors
561 smpi_datatype_extent(sendtype, &lb, &sendext);
562 // Local copy from root
563 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
564 sendcount, sendtype, recvbuf, recvcount, recvtype);
565 // Send buffers to receivers
566 requests = xbt_new(MPI_Request, size - 1);
568 for(dst = 0; dst < size; dst++) {
570 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
571 sendcount, sendtype, dst,
576 // Wait for completion of isend's.
577 smpi_mpi_startall(size - 1, requests);
578 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
583 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
584 MPI_Datatype sendtype, void *recvbuf, int recvcount,
585 MPI_Datatype recvtype, int root, MPI_Comm comm)
587 int system_tag = 666;
588 int rank, size, dst, index;
589 MPI_Aint lb = 0, sendext = 0;
590 MPI_Request *requests;
592 rank = smpi_comm_rank(comm);
593 size = smpi_comm_size(comm);
595 // Recv buffer from root
596 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
599 // FIXME: check for errors
600 smpi_datatype_extent(sendtype, &lb, &sendext);
601 // Local copy from root
602 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
603 sendtype, recvbuf, recvcount, recvtype);
604 // Send buffers to receivers
605 requests = xbt_new(MPI_Request, size - 1);
607 for(dst = 0; dst < size; dst++) {
610 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
611 sendtype, dst, system_tag, comm);
615 // Wait for completion of isend's.
616 smpi_mpi_startall(size - 1, requests);
617 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
622 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
623 MPI_Datatype datatype, MPI_Op op, int root,
626 int system_tag = 666;
627 int rank, size, src, index;
628 MPI_Aint lb = 0, dataext = 0;
629 MPI_Request *requests;
632 rank = smpi_comm_rank(comm);
633 size = smpi_comm_size(comm);
635 // Send buffer to root
636 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
638 // FIXME: check for errors
639 smpi_datatype_extent(datatype, &lb, &dataext);
640 // Local copy from root
641 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
642 // Receive buffers from senders
643 //TODO: make a MPI_barrier here ?
644 requests = xbt_new(MPI_Request, size - 1);
645 tmpbufs = xbt_new(void *, size - 1);
647 for(src = 0; src < size; src++) {
649 // FIXME: possibly overkill we we have contiguous/noncontiguous data
651 tmpbufs[index] = xbt_malloc(count * dataext);
653 smpi_irecv_init(tmpbufs[index], count, datatype, src,
658 // Wait for completion of irecv's.
659 smpi_mpi_startall(size - 1, requests);
660 for(src = 0; src < size - 1; src++) {
661 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
662 if(index == MPI_UNDEFINED) {
665 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
667 for(index = 0; index < size - 1; index++) {
668 xbt_free(tmpbufs[index]);
675 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
676 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
678 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
679 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
682 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
683 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
685 int system_tag = 666;
686 int rank, size, other, index;
687 MPI_Aint lb = 0, dataext = 0;
688 MPI_Request *requests;
691 rank = smpi_comm_rank(comm);
692 size = smpi_comm_size(comm);
694 // FIXME: check for errors
695 smpi_datatype_extent(datatype, &lb, &dataext);
697 // Local copy from self
698 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
700 // Send/Recv buffers to/from others;
701 requests = xbt_new(MPI_Request, size - 1);
702 tmpbufs = xbt_new(void *, rank);
704 for(other = 0; other < rank; other++) {
705 // FIXME: possibly overkill we we have contiguous/noncontiguous data
707 tmpbufs[index] = xbt_malloc(count * dataext);
709 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
713 for(other = rank + 1; other < size; other++) {
715 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
718 // Wait for completion of all comms.
719 smpi_mpi_startall(size - 1, requests);
720 for(other = 0; other < size - 1; other++) {
721 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
722 if(index == MPI_UNDEFINED) {
726 // #Request is below rank: it's a irecv
727 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
730 for(index = 0; index < rank; index++) {
731 xbt_free(tmpbufs[index]);