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);
113 SIMIX_req_comm_isend(mailbox, request->size, -1.0,
114 request->buf, request->size, &match_send, request,
115 // detach if msg size < eager/rdv switch limit
119 SIMIX_req_set_category (request->action, TRACE_internal_smpi_get_category());
124 void smpi_mpi_startall(int count, MPI_Request * requests)
128 for(i = 0; i < count; i++) {
129 smpi_mpi_start(requests[i]);
133 void smpi_mpi_request_free(MPI_Request * request)
136 *request = MPI_REQUEST_NULL;
139 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
140 int dst, int tag, MPI_Comm comm)
142 MPI_Request request =
143 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
144 comm, NON_PERSISTENT | SEND);
149 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
150 int dst, int tag, MPI_Comm comm)
152 MPI_Request request =
153 smpi_isend_init(buf, count, datatype, dst, tag, comm);
155 smpi_mpi_start(request);
159 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
160 int src, int tag, MPI_Comm comm)
162 MPI_Request request =
163 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
164 comm, NON_PERSISTENT | RECV);
169 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
170 int src, int tag, MPI_Comm comm)
172 MPI_Request request =
173 smpi_irecv_init(buf, count, datatype, src, tag, comm);
175 smpi_mpi_start(request);
179 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
180 int tag, MPI_Comm comm, MPI_Status * status)
184 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
185 smpi_mpi_wait(&request, status);
188 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
189 int tag, MPI_Comm comm)
193 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
194 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
197 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
198 int dst, int sendtag, void *recvbuf, int recvcount,
199 MPI_Datatype recvtype, int src, int recvtag,
200 MPI_Comm comm, MPI_Status * status)
202 MPI_Request requests[2];
206 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
208 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
209 smpi_mpi_startall(2, requests);
210 smpi_mpi_waitall(2, requests, stats);
211 if(status != MPI_STATUS_IGNORE) {
212 // Copy receive status
213 memcpy(status, &stats[1], sizeof(MPI_Status));
217 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
219 return status->count / smpi_datatype_size(datatype);
222 static void finish_wait(MPI_Request * request, MPI_Status * status)
224 MPI_Request req = *request;
226 if(status != MPI_STATUS_IGNORE) {
227 status->MPI_SOURCE = req->src;
228 status->MPI_TAG = req->tag;
229 status->MPI_ERROR = MPI_SUCCESS;
230 // FIXME: really this should just contain the count of receive-type blocks,
232 status->count = req->size;
234 print_request("Finishing", req);
235 if(req->flags & NON_PERSISTENT) {
236 smpi_mpi_request_free(request);
242 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
245 if ((*request)->action == NULL)
248 flag = SIMIX_req_comm_test((*request)->action);
250 smpi_mpi_wait(request, status);
255 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
262 *index = MPI_UNDEFINED;
265 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
266 map = xbt_new(int, count);
268 for(i = 0; i < count; i++) {
269 if(requests[i]->action) {
270 xbt_dynar_push(comms, &requests[i]->action);
276 i = SIMIX_req_comm_testany(comms);
277 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
278 if(i != MPI_UNDEFINED) {
280 smpi_mpi_wait(&requests[*index], status);
285 xbt_dynar_free(&comms);
290 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
292 print_request("Waiting", *request);
293 if ((*request)->action != NULL )
294 SIMIX_req_comm_wait((*request)->action, -1.0);
295 finish_wait(request, status);
298 int smpi_mpi_waitany(int count, MPI_Request requests[],
305 index = MPI_UNDEFINED;
307 // Wait for a request to complete
308 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
309 map = xbt_new(int, count);
311 XBT_DEBUG("Wait for one of");
312 for(i = 0; i < count; i++) {
313 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
314 print_request(" ", requests[i]);
315 xbt_dynar_push(comms, &requests[i]->action);
321 i = SIMIX_req_comm_waitany(comms);
322 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
323 if (i != MPI_UNDEFINED) {
325 finish_wait(&requests[index], status);
329 xbt_dynar_free(&comms);
334 void smpi_mpi_waitall(int count, MPI_Request requests[],
339 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
341 for(c = 0; c < count; c++) {
343 smpi_mpi_wait(&requests[c], pstat);
346 index = smpi_mpi_waitany(count, requests, pstat);
347 if(index == MPI_UNDEFINED) {
351 if(status != MPI_STATUS_IGNORE) {
352 memcpy(&status[index], pstat, sizeof(*pstat));
357 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
363 for(i = 0; i < incount; i++) {
364 if(smpi_mpi_testany(incount, requests, &index, status)) {
365 indices[count] = index;
372 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
375 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
376 nary_tree_bcast(buf, count, datatype, root, comm, 4);
379 void smpi_mpi_barrier(MPI_Comm comm)
381 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
382 nary_tree_barrier(comm, 4);
385 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
386 void *recvbuf, int recvcount, MPI_Datatype recvtype,
387 int root, MPI_Comm comm)
389 int system_tag = 666;
390 int rank, size, src, index;
391 MPI_Aint lb = 0, recvext = 0;
392 MPI_Request *requests;
394 rank = smpi_comm_rank(comm);
395 size = smpi_comm_size(comm);
397 // Send buffer to root
398 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
400 // FIXME: check for errors
401 smpi_datatype_extent(recvtype, &lb, &recvext);
402 // Local copy from root
403 smpi_datatype_copy(sendbuf, sendcount, sendtype,
404 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
405 // Receive buffers from senders
406 requests = xbt_new(MPI_Request, size - 1);
408 for(src = 0; src < size; src++) {
410 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
412 src, system_tag, comm);
416 // Wait for completion of irecv's.
417 smpi_mpi_startall(size - 1, requests);
418 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
423 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
424 void *recvbuf, int *recvcounts, int *displs,
425 MPI_Datatype recvtype, int root, MPI_Comm comm)
427 int system_tag = 666;
428 int rank, size, src, index;
429 MPI_Aint lb = 0, recvext = 0;
430 MPI_Request *requests;
432 rank = smpi_comm_rank(comm);
433 size = smpi_comm_size(comm);
435 // Send buffer to root
436 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
438 // FIXME: check for errors
439 smpi_datatype_extent(recvtype, &lb, &recvext);
440 // Local copy from root
441 smpi_datatype_copy(sendbuf, sendcount, sendtype,
442 (char *)recvbuf + displs[root] * recvext,
443 recvcounts[root], recvtype);
444 // Receive buffers from senders
445 requests = xbt_new(MPI_Request, size - 1);
447 for(src = 0; src < size; src++) {
450 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
451 recvcounts[src], recvtype, src, system_tag, comm);
455 // Wait for completion of irecv's.
456 smpi_mpi_startall(size - 1, requests);
457 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
462 void smpi_mpi_allgather(void *sendbuf, int sendcount,
463 MPI_Datatype sendtype, void *recvbuf,
464 int recvcount, MPI_Datatype recvtype,
467 int system_tag = 666;
468 int rank, size, other, index;
469 MPI_Aint lb = 0, recvext = 0;
470 MPI_Request *requests;
472 rank = smpi_comm_rank(comm);
473 size = smpi_comm_size(comm);
474 // FIXME: check for errors
475 smpi_datatype_extent(recvtype, &lb, &recvext);
476 // Local copy from self
477 smpi_datatype_copy(sendbuf, sendcount, sendtype,
478 (char *)recvbuf + rank * recvcount * recvext, recvcount,
480 // Send/Recv buffers to/from others;
481 requests = xbt_new(MPI_Request, 2 * (size - 1));
483 for(other = 0; other < size; other++) {
486 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
489 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
490 recvcount, recvtype, other,
495 // Wait for completion of all comms.
496 smpi_mpi_startall(2 * (size - 1), requests);
497 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
501 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
502 MPI_Datatype sendtype, void *recvbuf,
503 int *recvcounts, int *displs,
504 MPI_Datatype recvtype, MPI_Comm comm)
506 int system_tag = 666;
507 int rank, size, other, index;
508 MPI_Aint lb = 0, recvext = 0;
509 MPI_Request *requests;
511 rank = smpi_comm_rank(comm);
512 size = smpi_comm_size(comm);
513 // FIXME: check for errors
514 smpi_datatype_extent(recvtype, &lb, &recvext);
515 // Local copy from self
516 smpi_datatype_copy(sendbuf, sendcount, sendtype,
517 (char *)recvbuf + displs[rank] * recvext,
518 recvcounts[rank], recvtype);
519 // Send buffers to others;
520 requests = xbt_new(MPI_Request, 2 * (size - 1));
522 for(other = 0; other < size; other++) {
525 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
529 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
530 recvtype, other, system_tag, comm);
534 // Wait for completion of all comms.
535 smpi_mpi_startall(2 * (size - 1), requests);
536 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
540 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
541 void *recvbuf, int recvcount, MPI_Datatype recvtype,
542 int root, MPI_Comm comm)
544 int system_tag = 666;
545 int rank, size, dst, index;
546 MPI_Aint lb = 0, sendext = 0;
547 MPI_Request *requests;
549 rank = smpi_comm_rank(comm);
550 size = smpi_comm_size(comm);
552 // Recv buffer from root
553 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
556 // FIXME: check for errors
557 smpi_datatype_extent(sendtype, &lb, &sendext);
558 // Local copy from root
559 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
560 sendcount, sendtype, recvbuf, recvcount, recvtype);
561 // Send buffers to receivers
562 requests = xbt_new(MPI_Request, size - 1);
564 for(dst = 0; dst < size; dst++) {
566 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
567 sendcount, sendtype, dst,
572 // Wait for completion of isend's.
573 smpi_mpi_startall(size - 1, requests);
574 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
579 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
580 MPI_Datatype sendtype, void *recvbuf, int recvcount,
581 MPI_Datatype recvtype, int root, MPI_Comm comm)
583 int system_tag = 666;
584 int rank, size, dst, index;
585 MPI_Aint lb = 0, sendext = 0;
586 MPI_Request *requests;
588 rank = smpi_comm_rank(comm);
589 size = smpi_comm_size(comm);
591 // Recv buffer from root
592 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
595 // FIXME: check for errors
596 smpi_datatype_extent(sendtype, &lb, &sendext);
597 // Local copy from root
598 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
599 sendtype, recvbuf, recvcount, recvtype);
600 // Send buffers to receivers
601 requests = xbt_new(MPI_Request, size - 1);
603 for(dst = 0; dst < size; dst++) {
606 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
607 sendtype, dst, system_tag, comm);
611 // Wait for completion of isend's.
612 smpi_mpi_startall(size - 1, requests);
613 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
618 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
619 MPI_Datatype datatype, MPI_Op op, int root,
622 int system_tag = 666;
623 int rank, size, src, index;
624 MPI_Aint lb = 0, dataext = 0;
625 MPI_Request *requests;
628 rank = smpi_comm_rank(comm);
629 size = smpi_comm_size(comm);
631 // Send buffer to root
632 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
634 // FIXME: check for errors
635 smpi_datatype_extent(datatype, &lb, &dataext);
636 // Local copy from root
637 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
638 // Receive buffers from senders
639 //TODO: make a MPI_barrier here ?
640 requests = xbt_new(MPI_Request, size - 1);
641 tmpbufs = xbt_new(void *, size - 1);
643 for(src = 0; src < size; src++) {
645 // FIXME: possibly overkill we we have contiguous/noncontiguous data
647 tmpbufs[index] = xbt_malloc(count * dataext);
649 smpi_irecv_init(tmpbufs[index], count, datatype, src,
654 // Wait for completion of irecv's.
655 smpi_mpi_startall(size - 1, requests);
656 for(src = 0; src < size - 1; src++) {
657 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
658 if(index == MPI_UNDEFINED) {
661 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
663 for(index = 0; index < size - 1; index++) {
664 xbt_free(tmpbufs[index]);
671 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
672 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
674 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
675 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
678 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
679 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
681 int system_tag = 666;
682 int rank, size, other, index;
683 MPI_Aint lb = 0, dataext = 0;
684 MPI_Request *requests;
687 rank = smpi_comm_rank(comm);
688 size = smpi_comm_size(comm);
690 // FIXME: check for errors
691 smpi_datatype_extent(datatype, &lb, &dataext);
693 // Local copy from self
694 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
696 // Send/Recv buffers to/from others;
697 requests = xbt_new(MPI_Request, size - 1);
698 tmpbufs = xbt_new(void *, rank);
700 for(other = 0; other < rank; other++) {
701 // FIXME: possibly overkill we we have contiguous/noncontiguous data
703 tmpbufs[index] = xbt_malloc(count * dataext);
705 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
709 for(other = rank + 1; other < size; other++) {
711 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
714 // Wait for completion of all comms.
715 smpi_mpi_startall(size - 1, requests);
716 for(other = 0; other < size - 1; other++) {
717 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
718 if(index == MPI_UNDEFINED) {
722 // #Request is below rank: it's a irecv
723 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
726 for(index = 0; index < rank; index++) {
727 xbt_free(tmpbufs[index]);