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 MPI_Request req = request;
69 smpi_mpi_request_free(&req);
72 /* MPI Low level calls */
73 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
74 int dst, int tag, MPI_Comm comm)
77 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
78 comm, PERSISTENT | SEND);
83 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
84 int src, int tag, MPI_Comm comm)
87 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
88 comm, PERSISTENT | RECV);
93 void smpi_mpi_start(MPI_Request request)
98 xbt_assert(!request->action,
99 "Cannot (re)start a non-finished communication");
100 if(request->flags & RECV) {
101 print_request("New recv", request);
102 mailbox = smpi_process_mailbox();
103 // FIXME: SIMIX does not yet support non-contiguous datatypes
104 request->action = simcall_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
106 print_request("New send", request);
107 mailbox = smpi_process_remote_mailbox(
108 smpi_group_index(smpi_comm_group(request->comm), request->dst));
109 // FIXME: SIMIX does not yet support non-contiguous datatypes
111 if (request->size < 64*1024 ) { // eager mode => detached send (FIXME: this limit should be configurable)
112 void *oldbuf = request->buf;
114 request->buf = malloc(request->size);
115 memcpy(request->buf,oldbuf,request->size);
116 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
118 XBT_DEBUG("Send request %p is not detached (buf: %p)",request,request->buf);
121 simcall_comm_isend(mailbox, request->size, -1.0,
122 request->buf, request->size,
124 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
126 // detach if msg size < eager/rdv switch limit
130 simcall_set_category (request->action, TRACE_internal_smpi_get_category());
135 void smpi_mpi_startall(int count, MPI_Request * requests)
139 for(i = 0; i < count; i++) {
140 smpi_mpi_start(requests[i]);
144 void smpi_mpi_request_free(MPI_Request * request)
147 *request = MPI_REQUEST_NULL;
150 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
151 int dst, int tag, MPI_Comm comm)
153 MPI_Request request =
154 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
155 comm, NON_PERSISTENT | SEND);
160 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
161 int dst, int tag, MPI_Comm comm)
163 MPI_Request request =
164 smpi_isend_init(buf, count, datatype, dst, tag, comm);
166 smpi_mpi_start(request);
170 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
171 int src, int tag, MPI_Comm comm)
173 MPI_Request request =
174 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
175 comm, NON_PERSISTENT | RECV);
180 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
181 int src, int tag, MPI_Comm comm)
183 MPI_Request request =
184 smpi_irecv_init(buf, count, datatype, src, tag, comm);
186 smpi_mpi_start(request);
190 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
191 int tag, MPI_Comm comm, MPI_Status * status)
195 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
196 smpi_mpi_wait(&request, status);
199 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
200 int tag, MPI_Comm comm)
204 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
205 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
208 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
209 int dst, int sendtag, void *recvbuf, int recvcount,
210 MPI_Datatype recvtype, int src, int recvtag,
211 MPI_Comm comm, MPI_Status * status)
213 MPI_Request requests[2];
217 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
219 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
220 smpi_mpi_startall(2, requests);
221 smpi_mpi_waitall(2, requests, stats);
222 if(status != MPI_STATUS_IGNORE) {
223 // Copy receive status
224 memcpy(status, &stats[1], sizeof(MPI_Status));
228 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
230 return status->count / smpi_datatype_size(datatype);
233 static void finish_wait(MPI_Request * request, MPI_Status * status)
235 MPI_Request req = *request;
237 if(status != MPI_STATUS_IGNORE) {
238 status->MPI_SOURCE = req->src;
239 status->MPI_TAG = req->tag;
240 status->MPI_ERROR = MPI_SUCCESS;
241 // FIXME: really this should just contain the count of receive-type blocks,
243 status->count = req->size;
245 print_request("Finishing", req);
246 if(req->flags & NON_PERSISTENT) {
247 smpi_mpi_request_free(request);
253 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
256 if ((*request)->action == NULL)
259 flag = simcall_comm_test((*request)->action);
261 smpi_mpi_wait(request, status);
266 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
273 *index = MPI_UNDEFINED;
276 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
277 map = xbt_new(int, count);
279 for(i = 0; i < count; i++) {
280 if(requests[i]->action) {
281 xbt_dynar_push(comms, &requests[i]->action);
287 i = simcall_comm_testany(comms);
288 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
289 if(i != MPI_UNDEFINED) {
291 smpi_mpi_wait(&requests[*index], status);
296 xbt_dynar_free(&comms);
301 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
303 print_request("Waiting", *request);
304 if ((*request)->action != NULL) { // this is not a detached send
305 simcall_comm_wait((*request)->action, -1.0);
306 finish_wait(request, status);
308 // FIXME for a detached send, finish_wait is not called:
311 int smpi_mpi_waitany(int count, MPI_Request requests[],
318 index = MPI_UNDEFINED;
320 // Wait for a request to complete
321 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
322 map = xbt_new(int, count);
324 XBT_DEBUG("Wait for one of");
325 for(i = 0; i < count; i++) {
326 if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
327 print_request(" ", requests[i]);
328 xbt_dynar_push(comms, &requests[i]->action);
334 i = simcall_comm_waitany(comms);
335 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
336 if (i != MPI_UNDEFINED) {
338 finish_wait(&requests[index], status);
342 xbt_dynar_free(&comms);
347 void smpi_mpi_waitall(int count, MPI_Request requests[],
352 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
354 for(c = 0; c < count; c++) {
356 smpi_mpi_wait(&requests[c], pstat);
359 index = smpi_mpi_waitany(count, requests, pstat);
360 if(index == MPI_UNDEFINED) {
364 if(status != MPI_STATUS_IGNORE) {
365 memcpy(&status[index], pstat, sizeof(*pstat));
370 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
376 for(i = 0; i < incount; i++) {
377 if(smpi_mpi_testany(incount, requests, &index, status)) {
378 indices[count] = index;
385 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
388 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
389 nary_tree_bcast(buf, count, datatype, root, comm, 4);
392 void smpi_mpi_barrier(MPI_Comm comm)
394 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
395 nary_tree_barrier(comm, 4);
398 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
399 void *recvbuf, int recvcount, MPI_Datatype recvtype,
400 int root, MPI_Comm comm)
402 int system_tag = 666;
403 int rank, size, src, index;
404 MPI_Aint lb = 0, recvext = 0;
405 MPI_Request *requests;
407 rank = smpi_comm_rank(comm);
408 size = smpi_comm_size(comm);
410 // Send buffer to root
411 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
413 // FIXME: check for errors
414 smpi_datatype_extent(recvtype, &lb, &recvext);
415 // Local copy from root
416 smpi_datatype_copy(sendbuf, sendcount, sendtype,
417 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
418 // Receive buffers from senders
419 requests = xbt_new(MPI_Request, size - 1);
421 for(src = 0; src < size; src++) {
423 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
425 src, system_tag, comm);
429 // Wait for completion of irecv's.
430 smpi_mpi_startall(size - 1, requests);
431 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
436 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
437 void *recvbuf, int *recvcounts, int *displs,
438 MPI_Datatype recvtype, int root, MPI_Comm comm)
440 int system_tag = 666;
441 int rank, size, src, index;
442 MPI_Aint lb = 0, recvext = 0;
443 MPI_Request *requests;
445 rank = smpi_comm_rank(comm);
446 size = smpi_comm_size(comm);
448 // Send buffer to root
449 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
451 // FIXME: check for errors
452 smpi_datatype_extent(recvtype, &lb, &recvext);
453 // Local copy from root
454 smpi_datatype_copy(sendbuf, sendcount, sendtype,
455 (char *)recvbuf + displs[root] * recvext,
456 recvcounts[root], recvtype);
457 // Receive buffers from senders
458 requests = xbt_new(MPI_Request, size - 1);
460 for(src = 0; src < size; src++) {
463 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
464 recvcounts[src], recvtype, src, system_tag, comm);
468 // Wait for completion of irecv's.
469 smpi_mpi_startall(size - 1, requests);
470 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
475 void smpi_mpi_allgather(void *sendbuf, int sendcount,
476 MPI_Datatype sendtype, void *recvbuf,
477 int recvcount, MPI_Datatype recvtype,
480 int system_tag = 666;
481 int rank, size, other, index;
482 MPI_Aint lb = 0, recvext = 0;
483 MPI_Request *requests;
485 rank = smpi_comm_rank(comm);
486 size = smpi_comm_size(comm);
487 // FIXME: check for errors
488 smpi_datatype_extent(recvtype, &lb, &recvext);
489 // Local copy from self
490 smpi_datatype_copy(sendbuf, sendcount, sendtype,
491 (char *)recvbuf + rank * recvcount * recvext, recvcount,
493 // Send/Recv buffers to/from others;
494 requests = xbt_new(MPI_Request, 2 * (size - 1));
496 for(other = 0; other < size; other++) {
499 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
502 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
503 recvcount, recvtype, other,
508 // Wait for completion of all comms.
509 smpi_mpi_startall(2 * (size - 1), requests);
510 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
514 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
515 MPI_Datatype sendtype, void *recvbuf,
516 int *recvcounts, int *displs,
517 MPI_Datatype recvtype, MPI_Comm comm)
519 int system_tag = 666;
520 int rank, size, other, index;
521 MPI_Aint lb = 0, recvext = 0;
522 MPI_Request *requests;
524 rank = smpi_comm_rank(comm);
525 size = smpi_comm_size(comm);
526 // FIXME: check for errors
527 smpi_datatype_extent(recvtype, &lb, &recvext);
528 // Local copy from self
529 smpi_datatype_copy(sendbuf, sendcount, sendtype,
530 (char *)recvbuf + displs[rank] * recvext,
531 recvcounts[rank], recvtype);
532 // Send buffers to others;
533 requests = xbt_new(MPI_Request, 2 * (size - 1));
535 for(other = 0; other < size; other++) {
538 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
542 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
543 recvtype, other, system_tag, comm);
547 // Wait for completion of all comms.
548 smpi_mpi_startall(2 * (size - 1), requests);
549 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
553 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
554 void *recvbuf, int recvcount, MPI_Datatype recvtype,
555 int root, MPI_Comm comm)
557 int system_tag = 666;
558 int rank, size, dst, index;
559 MPI_Aint lb = 0, sendext = 0;
560 MPI_Request *requests;
562 rank = smpi_comm_rank(comm);
563 size = smpi_comm_size(comm);
565 // Recv buffer from root
566 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
569 // FIXME: check for errors
570 smpi_datatype_extent(sendtype, &lb, &sendext);
571 // Local copy from root
572 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
573 sendcount, sendtype, recvbuf, recvcount, recvtype);
574 // Send buffers to receivers
575 requests = xbt_new(MPI_Request, size - 1);
577 for(dst = 0; dst < size; dst++) {
579 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
580 sendcount, sendtype, dst,
585 // Wait for completion of isend's.
586 smpi_mpi_startall(size - 1, requests);
587 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
592 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
593 MPI_Datatype sendtype, void *recvbuf, int recvcount,
594 MPI_Datatype recvtype, int root, MPI_Comm comm)
596 int system_tag = 666;
597 int rank, size, dst, index;
598 MPI_Aint lb = 0, sendext = 0;
599 MPI_Request *requests;
601 rank = smpi_comm_rank(comm);
602 size = smpi_comm_size(comm);
604 // Recv buffer from root
605 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
608 // FIXME: check for errors
609 smpi_datatype_extent(sendtype, &lb, &sendext);
610 // Local copy from root
611 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
612 sendtype, recvbuf, recvcount, recvtype);
613 // Send buffers to receivers
614 requests = xbt_new(MPI_Request, size - 1);
616 for(dst = 0; dst < size; dst++) {
619 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
620 sendtype, dst, system_tag, comm);
624 // Wait for completion of isend's.
625 smpi_mpi_startall(size - 1, requests);
626 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
631 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
632 MPI_Datatype datatype, MPI_Op op, int root,
635 int system_tag = 666;
636 int rank, size, src, index;
637 MPI_Aint lb = 0, dataext = 0;
638 MPI_Request *requests;
641 rank = smpi_comm_rank(comm);
642 size = smpi_comm_size(comm);
644 // Send buffer to root
645 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
647 // FIXME: check for errors
648 smpi_datatype_extent(datatype, &lb, &dataext);
649 // Local copy from root
650 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
651 // Receive buffers from senders
652 //TODO: make a MPI_barrier here ?
653 requests = xbt_new(MPI_Request, size - 1);
654 tmpbufs = xbt_new(void *, size - 1);
656 for(src = 0; src < size; src++) {
658 // FIXME: possibly overkill we we have contiguous/noncontiguous data
660 tmpbufs[index] = xbt_malloc(count * dataext);
662 smpi_irecv_init(tmpbufs[index], count, datatype, src,
667 // Wait for completion of irecv's.
668 smpi_mpi_startall(size - 1, requests);
669 for(src = 0; src < size - 1; src++) {
670 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
671 if(index == MPI_UNDEFINED) {
674 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
676 for(index = 0; index < size - 1; index++) {
677 xbt_free(tmpbufs[index]);
684 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
685 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
687 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
688 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
691 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
692 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
694 int system_tag = 666;
695 int rank, size, other, index;
696 MPI_Aint lb = 0, dataext = 0;
697 MPI_Request *requests;
700 rank = smpi_comm_rank(comm);
701 size = smpi_comm_size(comm);
703 // FIXME: check for errors
704 smpi_datatype_extent(datatype, &lb, &dataext);
706 // Local copy from self
707 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
709 // Send/Recv buffers to/from others;
710 requests = xbt_new(MPI_Request, size - 1);
711 tmpbufs = xbt_new(void *, rank);
713 for(other = 0; other < rank; other++) {
714 // FIXME: possibly overkill we we have contiguous/noncontiguous data
716 tmpbufs[index] = xbt_malloc(count * dataext);
718 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
722 for(other = rank + 1; other < size; other++) {
724 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
727 // Wait for completion of all comms.
728 smpi_mpi_startall(size - 1, requests);
729 for(other = 0; other < size - 1; other++) {
730 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
731 if(index == MPI_UNDEFINED) {
735 // #Request is below rank: it's a irecv
736 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
739 for(index = 0; index < rank; index++) {
740 xbt_free(tmpbufs[index]);