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,
118 (void (*)(void *))&smpi_mpi_request_free, // how to free the userdata if a detached send fails
120 // detach if msg size < eager/rdv switch limit
124 SIMIX_req_set_category (request->action, TRACE_internal_smpi_get_category());
129 void smpi_mpi_startall(int count, MPI_Request * requests)
133 for(i = 0; i < count; i++) {
134 smpi_mpi_start(requests[i]);
138 void smpi_mpi_request_free(MPI_Request * request)
141 *request = MPI_REQUEST_NULL;
144 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
145 int dst, int tag, MPI_Comm comm)
147 MPI_Request request =
148 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
149 comm, NON_PERSISTENT | SEND);
154 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
155 int dst, int tag, MPI_Comm comm)
157 MPI_Request request =
158 smpi_isend_init(buf, count, datatype, dst, tag, comm);
160 smpi_mpi_start(request);
164 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
165 int src, int tag, MPI_Comm comm)
167 MPI_Request request =
168 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
169 comm, NON_PERSISTENT | RECV);
174 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
175 int src, int tag, MPI_Comm comm)
177 MPI_Request request =
178 smpi_irecv_init(buf, count, datatype, src, tag, comm);
180 smpi_mpi_start(request);
184 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
185 int tag, MPI_Comm comm, MPI_Status * status)
189 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
190 smpi_mpi_wait(&request, status);
193 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
194 int tag, MPI_Comm comm)
198 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
199 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
202 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
203 int dst, int sendtag, void *recvbuf, int recvcount,
204 MPI_Datatype recvtype, int src, int recvtag,
205 MPI_Comm comm, MPI_Status * status)
207 MPI_Request requests[2];
211 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
213 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
214 smpi_mpi_startall(2, requests);
215 smpi_mpi_waitall(2, requests, stats);
216 if(status != MPI_STATUS_IGNORE) {
217 // Copy receive status
218 memcpy(status, &stats[1], sizeof(MPI_Status));
222 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
224 return status->count / smpi_datatype_size(datatype);
227 static void finish_wait(MPI_Request * request, MPI_Status * status)
229 MPI_Request req = *request;
231 if(status != MPI_STATUS_IGNORE) {
232 status->MPI_SOURCE = req->src;
233 status->MPI_TAG = req->tag;
234 status->MPI_ERROR = MPI_SUCCESS;
235 // FIXME: really this should just contain the count of receive-type blocks,
237 status->count = req->size;
239 print_request("Finishing", req);
240 if(req->flags & NON_PERSISTENT) {
241 smpi_mpi_request_free(request);
247 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
250 if ((*request)->action == NULL)
253 flag = SIMIX_req_comm_test((*request)->action);
255 smpi_mpi_wait(request, status);
260 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
267 *index = MPI_UNDEFINED;
270 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
271 map = xbt_new(int, count);
273 for(i = 0; i < count; i++) {
274 if(requests[i]->action) {
275 xbt_dynar_push(comms, &requests[i]->action);
281 i = SIMIX_req_comm_testany(comms);
282 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
283 if(i != MPI_UNDEFINED) {
285 smpi_mpi_wait(&requests[*index], status);
290 xbt_dynar_free(&comms);
295 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
297 print_request("Waiting", *request);
298 if ((*request)->action != NULL) { // this is not a detached send
299 SIMIX_req_comm_wait((*request)->action, -1.0);
300 finish_wait(request, status);
302 // FIXME for a detached send, finish_wait is not called:
305 int smpi_mpi_waitany(int count, MPI_Request requests[],
312 index = MPI_UNDEFINED;
314 // Wait for a request to complete
315 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
316 map = xbt_new(int, count);
318 XBT_DEBUG("Wait for one of");
319 for(i = 0; i < count; i++) {
320 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
321 print_request(" ", requests[i]);
322 xbt_dynar_push(comms, &requests[i]->action);
328 i = SIMIX_req_comm_waitany(comms);
329 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
330 if (i != MPI_UNDEFINED) {
332 finish_wait(&requests[index], status);
336 xbt_dynar_free(&comms);
341 void smpi_mpi_waitall(int count, MPI_Request requests[],
346 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
348 for(c = 0; c < count; c++) {
350 smpi_mpi_wait(&requests[c], pstat);
353 index = smpi_mpi_waitany(count, requests, pstat);
354 if(index == MPI_UNDEFINED) {
358 if(status != MPI_STATUS_IGNORE) {
359 memcpy(&status[index], pstat, sizeof(*pstat));
364 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
370 for(i = 0; i < incount; i++) {
371 if(smpi_mpi_testany(incount, requests, &index, status)) {
372 indices[count] = index;
379 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
382 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
383 nary_tree_bcast(buf, count, datatype, root, comm, 4);
386 void smpi_mpi_barrier(MPI_Comm comm)
388 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
389 nary_tree_barrier(comm, 4);
392 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
393 void *recvbuf, int recvcount, MPI_Datatype recvtype,
394 int root, MPI_Comm comm)
396 int system_tag = 666;
397 int rank, size, src, index;
398 MPI_Aint lb = 0, recvext = 0;
399 MPI_Request *requests;
401 rank = smpi_comm_rank(comm);
402 size = smpi_comm_size(comm);
404 // Send buffer to root
405 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
407 // FIXME: check for errors
408 smpi_datatype_extent(recvtype, &lb, &recvext);
409 // Local copy from root
410 smpi_datatype_copy(sendbuf, sendcount, sendtype,
411 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
412 // Receive buffers from senders
413 requests = xbt_new(MPI_Request, size - 1);
415 for(src = 0; src < size; src++) {
417 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
419 src, system_tag, comm);
423 // Wait for completion of irecv's.
424 smpi_mpi_startall(size - 1, requests);
425 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
430 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
431 void *recvbuf, int *recvcounts, int *displs,
432 MPI_Datatype recvtype, int root, MPI_Comm comm)
434 int system_tag = 666;
435 int rank, size, src, index;
436 MPI_Aint lb = 0, recvext = 0;
437 MPI_Request *requests;
439 rank = smpi_comm_rank(comm);
440 size = smpi_comm_size(comm);
442 // Send buffer to root
443 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
445 // FIXME: check for errors
446 smpi_datatype_extent(recvtype, &lb, &recvext);
447 // Local copy from root
448 smpi_datatype_copy(sendbuf, sendcount, sendtype,
449 (char *)recvbuf + displs[root] * recvext,
450 recvcounts[root], recvtype);
451 // Receive buffers from senders
452 requests = xbt_new(MPI_Request, size - 1);
454 for(src = 0; src < size; src++) {
457 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
458 recvcounts[src], recvtype, src, system_tag, comm);
462 // Wait for completion of irecv's.
463 smpi_mpi_startall(size - 1, requests);
464 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
469 void smpi_mpi_allgather(void *sendbuf, int sendcount,
470 MPI_Datatype sendtype, void *recvbuf,
471 int recvcount, MPI_Datatype recvtype,
474 int system_tag = 666;
475 int rank, size, other, index;
476 MPI_Aint lb = 0, recvext = 0;
477 MPI_Request *requests;
479 rank = smpi_comm_rank(comm);
480 size = smpi_comm_size(comm);
481 // FIXME: check for errors
482 smpi_datatype_extent(recvtype, &lb, &recvext);
483 // Local copy from self
484 smpi_datatype_copy(sendbuf, sendcount, sendtype,
485 (char *)recvbuf + rank * recvcount * recvext, recvcount,
487 // Send/Recv buffers to/from others;
488 requests = xbt_new(MPI_Request, 2 * (size - 1));
490 for(other = 0; other < size; other++) {
493 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
496 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
497 recvcount, recvtype, other,
502 // Wait for completion of all comms.
503 smpi_mpi_startall(2 * (size - 1), requests);
504 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
508 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
509 MPI_Datatype sendtype, void *recvbuf,
510 int *recvcounts, int *displs,
511 MPI_Datatype recvtype, MPI_Comm comm)
513 int system_tag = 666;
514 int rank, size, other, index;
515 MPI_Aint lb = 0, recvext = 0;
516 MPI_Request *requests;
518 rank = smpi_comm_rank(comm);
519 size = smpi_comm_size(comm);
520 // FIXME: check for errors
521 smpi_datatype_extent(recvtype, &lb, &recvext);
522 // Local copy from self
523 smpi_datatype_copy(sendbuf, sendcount, sendtype,
524 (char *)recvbuf + displs[rank] * recvext,
525 recvcounts[rank], recvtype);
526 // Send buffers to others;
527 requests = xbt_new(MPI_Request, 2 * (size - 1));
529 for(other = 0; other < size; other++) {
532 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
536 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
537 recvtype, other, system_tag, comm);
541 // Wait for completion of all comms.
542 smpi_mpi_startall(2 * (size - 1), requests);
543 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
547 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
548 void *recvbuf, int recvcount, MPI_Datatype recvtype,
549 int root, MPI_Comm comm)
551 int system_tag = 666;
552 int rank, size, dst, index;
553 MPI_Aint lb = 0, sendext = 0;
554 MPI_Request *requests;
556 rank = smpi_comm_rank(comm);
557 size = smpi_comm_size(comm);
559 // Recv buffer from root
560 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
563 // FIXME: check for errors
564 smpi_datatype_extent(sendtype, &lb, &sendext);
565 // Local copy from root
566 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
567 sendcount, sendtype, recvbuf, recvcount, recvtype);
568 // Send buffers to receivers
569 requests = xbt_new(MPI_Request, size - 1);
571 for(dst = 0; dst < size; dst++) {
573 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
574 sendcount, sendtype, dst,
579 // Wait for completion of isend's.
580 smpi_mpi_startall(size - 1, requests);
581 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
586 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
587 MPI_Datatype sendtype, void *recvbuf, int recvcount,
588 MPI_Datatype recvtype, int root, MPI_Comm comm)
590 int system_tag = 666;
591 int rank, size, dst, index;
592 MPI_Aint lb = 0, sendext = 0;
593 MPI_Request *requests;
595 rank = smpi_comm_rank(comm);
596 size = smpi_comm_size(comm);
598 // Recv buffer from root
599 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
602 // FIXME: check for errors
603 smpi_datatype_extent(sendtype, &lb, &sendext);
604 // Local copy from root
605 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
606 sendtype, recvbuf, recvcount, recvtype);
607 // Send buffers to receivers
608 requests = xbt_new(MPI_Request, size - 1);
610 for(dst = 0; dst < size; dst++) {
613 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
614 sendtype, dst, system_tag, comm);
618 // Wait for completion of isend's.
619 smpi_mpi_startall(size - 1, requests);
620 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
625 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
626 MPI_Datatype datatype, MPI_Op op, int root,
629 int system_tag = 666;
630 int rank, size, src, index;
631 MPI_Aint lb = 0, dataext = 0;
632 MPI_Request *requests;
635 rank = smpi_comm_rank(comm);
636 size = smpi_comm_size(comm);
638 // Send buffer to root
639 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
641 // FIXME: check for errors
642 smpi_datatype_extent(datatype, &lb, &dataext);
643 // Local copy from root
644 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
645 // Receive buffers from senders
646 //TODO: make a MPI_barrier here ?
647 requests = xbt_new(MPI_Request, size - 1);
648 tmpbufs = xbt_new(void *, size - 1);
650 for(src = 0; src < size; src++) {
652 // FIXME: possibly overkill we we have contiguous/noncontiguous data
654 tmpbufs[index] = xbt_malloc(count * dataext);
656 smpi_irecv_init(tmpbufs[index], count, datatype, src,
661 // Wait for completion of irecv's.
662 smpi_mpi_startall(size - 1, requests);
663 for(src = 0; src < size - 1; src++) {
664 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
665 if(index == MPI_UNDEFINED) {
668 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
670 for(index = 0; index < size - 1; index++) {
671 xbt_free(tmpbufs[index]);
678 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
679 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
681 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
682 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
685 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
686 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
688 int system_tag = 666;
689 int rank, size, other, index;
690 MPI_Aint lb = 0, dataext = 0;
691 MPI_Request *requests;
694 rank = smpi_comm_rank(comm);
695 size = smpi_comm_size(comm);
697 // FIXME: check for errors
698 smpi_datatype_extent(datatype, &lb, &dataext);
700 // Local copy from self
701 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
703 // Send/Recv buffers to/from others;
704 requests = xbt_new(MPI_Request, size - 1);
705 tmpbufs = xbt_new(void *, rank);
707 for(other = 0; other < rank; other++) {
708 // FIXME: possibly overkill we we have contiguous/noncontiguous data
710 tmpbufs[index] = xbt_malloc(count * dataext);
712 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
716 for(other = rank + 1; other < size; other++) {
718 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
721 // Wait for completion of all comms.
722 smpi_mpi_startall(size - 1, requests);
723 for(other = 0; other < size - 1; other++) {
724 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
725 if(index == MPI_UNDEFINED) {
729 // #Request is below rank: it's a irecv
730 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
733 for(index = 0; index < rank; index++) {
734 xbt_free(tmpbufs[index]);