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)
91 xbt_assert(!request->action,
92 "Cannot (re)start a non-finished communication");
93 if(request->flags & RECV) {
94 print_request("New recv", request);
95 mailbox = smpi_process_mailbox();
96 // FIXME: SIMIX does not yet support non-contiguous datatypes
97 request->action = SIMIX_req_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
99 print_request("New send", request);
100 mailbox = smpi_process_remote_mailbox(
101 smpi_group_index(smpi_comm_group(request->comm), request->dst));
102 // FIXME: SIMIX does not yet support non-contiguous datatypes
105 SIMIX_req_comm_isend(mailbox, request->size, -1.0,
106 request->buf, request->size, &match_send, request,
107 // detach if msg size < eager/rdv switch limit
108 request->size < 64*1024 ? 1:0);
109 // if detached request->action == NULL
111 SIMIX_req_set_category (request->action, TRACE_internal_smpi_get_category());
116 void smpi_mpi_startall(int count, MPI_Request * requests)
120 for(i = 0; i < count; i++) {
121 smpi_mpi_start(requests[i]);
125 void smpi_mpi_request_free(MPI_Request * request)
128 *request = MPI_REQUEST_NULL;
131 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
132 int dst, int tag, MPI_Comm comm)
134 MPI_Request request =
135 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
136 comm, NON_PERSISTENT | SEND);
141 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
142 int dst, int tag, MPI_Comm comm)
144 MPI_Request request =
145 smpi_isend_init(buf, count, datatype, dst, tag, comm);
147 smpi_mpi_start(request);
151 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
152 int src, int tag, MPI_Comm comm)
154 MPI_Request request =
155 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
156 comm, NON_PERSISTENT | RECV);
161 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
162 int src, int tag, MPI_Comm comm)
164 MPI_Request request =
165 smpi_irecv_init(buf, count, datatype, src, tag, comm);
167 smpi_mpi_start(request);
171 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
172 int tag, MPI_Comm comm, MPI_Status * status)
176 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
177 smpi_mpi_wait(&request, status);
180 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
181 int tag, MPI_Comm comm)
185 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
186 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
189 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
190 int dst, int sendtag, void *recvbuf, int recvcount,
191 MPI_Datatype recvtype, int src, int recvtag,
192 MPI_Comm comm, MPI_Status * status)
194 MPI_Request requests[2];
198 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
200 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
201 smpi_mpi_startall(2, requests);
202 smpi_mpi_waitall(2, requests, stats);
203 if(status != MPI_STATUS_IGNORE) {
204 // Copy receive status
205 memcpy(status, &stats[1], sizeof(MPI_Status));
209 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
211 return status->count / smpi_datatype_size(datatype);
214 static void finish_wait(MPI_Request * request, MPI_Status * status)
216 MPI_Request req = *request;
218 if(status != MPI_STATUS_IGNORE) {
219 status->MPI_SOURCE = req->src;
220 status->MPI_TAG = req->tag;
221 status->MPI_ERROR = MPI_SUCCESS;
222 // FIXME: really this should just contain the count of receive-type blocks,
224 status->count = req->size;
226 print_request("Finishing", req);
227 if(req->flags & NON_PERSISTENT) {
228 smpi_mpi_request_free(request);
234 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
237 if ((*request)->action == NULL)
240 flag = SIMIX_req_comm_test((*request)->action);
242 smpi_mpi_wait(request, status);
247 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
254 *index = MPI_UNDEFINED;
257 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
258 map = xbt_new(int, count);
260 for(i = 0; i < count; i++) {
261 if(requests[i]->action) {
262 xbt_dynar_push(comms, &requests[i]->action);
268 i = SIMIX_req_comm_testany(comms);
269 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
270 if(i != MPI_UNDEFINED) {
272 smpi_mpi_wait(&requests[*index], status);
277 xbt_dynar_free(&comms);
282 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
284 print_request("Waiting", *request);
285 if ((*request)->action != NULL )
286 SIMIX_req_comm_wait((*request)->action, -1.0);
287 finish_wait(request, status);
290 int smpi_mpi_waitany(int count, MPI_Request requests[],
297 index = MPI_UNDEFINED;
299 // Wait for a request to complete
300 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
301 map = xbt_new(int, count);
303 XBT_DEBUG("Wait for one of");
304 for(i = 0; i < count; i++) {
305 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
306 print_request(" ", requests[i]);
307 xbt_dynar_push(comms, &requests[i]->action);
313 i = SIMIX_req_comm_waitany(comms);
314 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
315 if (i != MPI_UNDEFINED) {
317 finish_wait(&requests[index], status);
321 xbt_dynar_free(&comms);
326 void smpi_mpi_waitall(int count, MPI_Request requests[],
331 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
333 for(c = 0; c < count; c++) {
335 smpi_mpi_wait(&requests[c], pstat);
338 index = smpi_mpi_waitany(count, requests, pstat);
339 if(index == MPI_UNDEFINED) {
343 if(status != MPI_STATUS_IGNORE) {
344 memcpy(&status[index], pstat, sizeof(*pstat));
349 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
355 for(i = 0; i < incount; i++) {
356 if(smpi_mpi_testany(incount, requests, &index, status)) {
357 indices[count] = index;
364 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
367 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
368 nary_tree_bcast(buf, count, datatype, root, comm, 4);
371 void smpi_mpi_barrier(MPI_Comm comm)
373 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
374 nary_tree_barrier(comm, 4);
377 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
378 void *recvbuf, int recvcount, MPI_Datatype recvtype,
379 int root, MPI_Comm comm)
381 int system_tag = 666;
382 int rank, size, src, index;
383 MPI_Aint lb = 0, recvext = 0;
384 MPI_Request *requests;
386 rank = smpi_comm_rank(comm);
387 size = smpi_comm_size(comm);
389 // Send buffer to root
390 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
392 // FIXME: check for errors
393 smpi_datatype_extent(recvtype, &lb, &recvext);
394 // Local copy from root
395 smpi_datatype_copy(sendbuf, sendcount, sendtype,
396 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
397 // Receive buffers from senders
398 requests = xbt_new(MPI_Request, size - 1);
400 for(src = 0; src < size; src++) {
402 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
404 src, system_tag, comm);
408 // Wait for completion of irecv's.
409 smpi_mpi_startall(size - 1, requests);
410 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
415 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
416 void *recvbuf, int *recvcounts, int *displs,
417 MPI_Datatype recvtype, int root, MPI_Comm comm)
419 int system_tag = 666;
420 int rank, size, src, index;
421 MPI_Aint lb = 0, recvext = 0;
422 MPI_Request *requests;
424 rank = smpi_comm_rank(comm);
425 size = smpi_comm_size(comm);
427 // Send buffer to root
428 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
430 // FIXME: check for errors
431 smpi_datatype_extent(recvtype, &lb, &recvext);
432 // Local copy from root
433 smpi_datatype_copy(sendbuf, sendcount, sendtype,
434 (char *)recvbuf + displs[root] * recvext,
435 recvcounts[root], recvtype);
436 // Receive buffers from senders
437 requests = xbt_new(MPI_Request, size - 1);
439 for(src = 0; src < size; src++) {
442 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
443 recvcounts[src], recvtype, src, system_tag, comm);
447 // Wait for completion of irecv's.
448 smpi_mpi_startall(size - 1, requests);
449 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
454 void smpi_mpi_allgather(void *sendbuf, int sendcount,
455 MPI_Datatype sendtype, void *recvbuf,
456 int recvcount, MPI_Datatype recvtype,
459 int system_tag = 666;
460 int rank, size, other, index;
461 MPI_Aint lb = 0, recvext = 0;
462 MPI_Request *requests;
464 rank = smpi_comm_rank(comm);
465 size = smpi_comm_size(comm);
466 // FIXME: check for errors
467 smpi_datatype_extent(recvtype, &lb, &recvext);
468 // Local copy from self
469 smpi_datatype_copy(sendbuf, sendcount, sendtype,
470 (char *)recvbuf + rank * recvcount * recvext, recvcount,
472 // Send/Recv buffers to/from others;
473 requests = xbt_new(MPI_Request, 2 * (size - 1));
475 for(other = 0; other < size; other++) {
478 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
481 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
482 recvcount, recvtype, other,
487 // Wait for completion of all comms.
488 smpi_mpi_startall(2 * (size - 1), requests);
489 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
493 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
494 MPI_Datatype sendtype, void *recvbuf,
495 int *recvcounts, int *displs,
496 MPI_Datatype recvtype, MPI_Comm comm)
498 int system_tag = 666;
499 int rank, size, other, index;
500 MPI_Aint lb = 0, recvext = 0;
501 MPI_Request *requests;
503 rank = smpi_comm_rank(comm);
504 size = smpi_comm_size(comm);
505 // FIXME: check for errors
506 smpi_datatype_extent(recvtype, &lb, &recvext);
507 // Local copy from self
508 smpi_datatype_copy(sendbuf, sendcount, sendtype,
509 (char *)recvbuf + displs[rank] * recvext,
510 recvcounts[rank], recvtype);
511 // Send buffers to others;
512 requests = xbt_new(MPI_Request, 2 * (size - 1));
514 for(other = 0; other < size; other++) {
517 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
521 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
522 recvtype, other, system_tag, comm);
526 // Wait for completion of all comms.
527 smpi_mpi_startall(2 * (size - 1), requests);
528 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
532 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
533 void *recvbuf, int recvcount, MPI_Datatype recvtype,
534 int root, MPI_Comm comm)
536 int system_tag = 666;
537 int rank, size, dst, index;
538 MPI_Aint lb = 0, sendext = 0;
539 MPI_Request *requests;
541 rank = smpi_comm_rank(comm);
542 size = smpi_comm_size(comm);
544 // Recv buffer from root
545 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
548 // FIXME: check for errors
549 smpi_datatype_extent(sendtype, &lb, &sendext);
550 // Local copy from root
551 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
552 sendcount, sendtype, recvbuf, recvcount, recvtype);
553 // Send buffers to receivers
554 requests = xbt_new(MPI_Request, size - 1);
556 for(dst = 0; dst < size; dst++) {
558 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
559 sendcount, sendtype, dst,
564 // Wait for completion of isend's.
565 smpi_mpi_startall(size - 1, requests);
566 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
571 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
572 MPI_Datatype sendtype, void *recvbuf, int recvcount,
573 MPI_Datatype recvtype, int root, MPI_Comm comm)
575 int system_tag = 666;
576 int rank, size, dst, index;
577 MPI_Aint lb = 0, sendext = 0;
578 MPI_Request *requests;
580 rank = smpi_comm_rank(comm);
581 size = smpi_comm_size(comm);
583 // Recv buffer from root
584 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
587 // FIXME: check for errors
588 smpi_datatype_extent(sendtype, &lb, &sendext);
589 // Local copy from root
590 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
591 sendtype, recvbuf, recvcount, recvtype);
592 // Send buffers to receivers
593 requests = xbt_new(MPI_Request, size - 1);
595 for(dst = 0; dst < size; dst++) {
598 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
599 sendtype, dst, system_tag, comm);
603 // Wait for completion of isend's.
604 smpi_mpi_startall(size - 1, requests);
605 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
610 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
611 MPI_Datatype datatype, MPI_Op op, int root,
614 int system_tag = 666;
615 int rank, size, src, index;
616 MPI_Aint lb = 0, dataext = 0;
617 MPI_Request *requests;
620 rank = smpi_comm_rank(comm);
621 size = smpi_comm_size(comm);
623 // Send buffer to root
624 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
626 // FIXME: check for errors
627 smpi_datatype_extent(datatype, &lb, &dataext);
628 // Local copy from root
629 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
630 // Receive buffers from senders
631 //TODO: make a MPI_barrier here ?
632 requests = xbt_new(MPI_Request, size - 1);
633 tmpbufs = xbt_new(void *, size - 1);
635 for(src = 0; src < size; src++) {
637 // FIXME: possibly overkill we we have contiguous/noncontiguous data
639 tmpbufs[index] = xbt_malloc(count * dataext);
641 smpi_irecv_init(tmpbufs[index], count, datatype, src,
646 // Wait for completion of irecv's.
647 smpi_mpi_startall(size - 1, requests);
648 for(src = 0; src < size - 1; src++) {
649 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
650 if(index == MPI_UNDEFINED) {
653 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
655 for(index = 0; index < size - 1; index++) {
656 xbt_free(tmpbufs[index]);
663 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
664 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
666 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
667 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
670 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
671 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
673 int system_tag = 666;
674 int rank, size, other, index;
675 MPI_Aint lb = 0, dataext = 0;
676 MPI_Request *requests;
679 rank = smpi_comm_rank(comm);
680 size = smpi_comm_size(comm);
682 // FIXME: check for errors
683 smpi_datatype_extent(datatype, &lb, &dataext);
685 // Local copy from self
686 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
688 // Send/Recv buffers to/from others;
689 requests = xbt_new(MPI_Request, size - 1);
690 tmpbufs = xbt_new(void *, rank);
692 for(other = 0; other < rank; other++) {
693 // FIXME: possibly overkill we we have contiguous/noncontiguous data
695 tmpbufs[index] = xbt_malloc(count * dataext);
697 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
701 for(other = rank + 1; other < size; other++) {
703 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
706 // Wait for completion of all comms.
707 smpi_mpi_startall(size - 1, requests);
708 for(other = 0; other < size - 1; other++) {
709 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
710 if(index == MPI_UNDEFINED) {
714 // #Request is below rank: it's a irecv
715 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
718 for(index = 0; index < rank; index++) {
719 xbt_free(tmpbufs[index]);