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 don-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 don-contiguous datatypes
103 request->action = SIMIX_req_comm_isend(mailbox, request->size, -1.0,
104 request->buf, request->size, &match_send, request, 0);
106 SIMIX_req_set_category (request->action, TRACE_internal_smpi_get_category());
111 void smpi_mpi_startall(int count, MPI_Request * requests)
115 for(i = 0; i < count; i++) {
116 smpi_mpi_start(requests[i]);
120 void smpi_mpi_request_free(MPI_Request * request)
123 *request = MPI_REQUEST_NULL;
126 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
127 int dst, int tag, MPI_Comm comm)
129 MPI_Request request =
130 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
131 comm, NON_PERSISTENT | SEND);
136 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
137 int dst, int tag, MPI_Comm comm)
139 MPI_Request request =
140 smpi_isend_init(buf, count, datatype, dst, tag, comm);
142 smpi_mpi_start(request);
146 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
147 int src, int tag, MPI_Comm comm)
149 MPI_Request request =
150 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
151 comm, NON_PERSISTENT | RECV);
156 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
157 int src, int tag, MPI_Comm comm)
159 MPI_Request request =
160 smpi_irecv_init(buf, count, datatype, src, tag, comm);
162 smpi_mpi_start(request);
166 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
167 int tag, MPI_Comm comm, MPI_Status * status)
171 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
172 smpi_mpi_wait(&request, status);
175 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
176 int tag, MPI_Comm comm)
180 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
181 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
184 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
185 int dst, int sendtag, void *recvbuf, int recvcount,
186 MPI_Datatype recvtype, int src, int recvtag,
187 MPI_Comm comm, MPI_Status * status)
189 MPI_Request requests[2];
193 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
195 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
196 smpi_mpi_startall(2, requests);
197 smpi_mpi_waitall(2, requests, stats);
198 if(status != MPI_STATUS_IGNORE) {
199 // Copy receive status
200 memcpy(status, &stats[1], sizeof(MPI_Status));
204 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
206 return status->count / smpi_datatype_size(datatype);
209 static void finish_wait(MPI_Request * request, MPI_Status * status)
211 MPI_Request req = *request;
213 if(status != MPI_STATUS_IGNORE) {
214 status->MPI_SOURCE = req->src;
215 status->MPI_TAG = req->tag;
216 status->MPI_ERROR = MPI_SUCCESS;
217 // FIXME: really this should just contain the count of receive-type blocks,
219 status->count = req->size;
221 print_request("Finishing", req);
222 if(req->flags & NON_PERSISTENT) {
223 smpi_mpi_request_free(request);
229 int smpi_mpi_test(MPI_Request * request, MPI_Status * status)
231 int flag = SIMIX_req_comm_test((*request)->action);
234 smpi_mpi_wait(request, status);
239 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
246 *index = MPI_UNDEFINED;
249 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
250 map = xbt_new(int, count);
252 for(i = 0; i < count; i++) {
253 if(requests[i]->action) {
254 xbt_dynar_push(comms, &requests[i]->action);
260 i = SIMIX_req_comm_testany(comms);
261 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
262 if(i != MPI_UNDEFINED) {
264 smpi_mpi_wait(&requests[*index], status);
269 xbt_dynar_free(&comms);
274 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
276 print_request("Waiting", *request);
277 SIMIX_req_comm_wait((*request)->action, -1.0);
278 finish_wait(request, status);
281 int smpi_mpi_waitany(int count, MPI_Request requests[],
288 index = MPI_UNDEFINED;
290 // Wait for a request to complete
291 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
292 map = xbt_new(int, count);
294 XBT_DEBUG("Wait for one of");
295 for(i = 0; i < count; i++) {
296 if(requests[i] != MPI_REQUEST_NULL) {
297 print_request(" ", requests[i]);
298 xbt_dynar_push(comms, &requests[i]->action);
304 i = SIMIX_req_comm_waitany(comms);
305 // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
306 if (i != MPI_UNDEFINED) {
308 finish_wait(&requests[index], status);
312 xbt_dynar_free(&comms);
317 void smpi_mpi_waitall(int count, MPI_Request requests[],
322 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
324 for(c = 0; c < count; c++) {
326 smpi_mpi_wait(&requests[c], pstat);
329 index = smpi_mpi_waitany(count, requests, pstat);
330 if(index == MPI_UNDEFINED) {
334 if(status != MPI_STATUS_IGNORE) {
335 memcpy(&status[index], pstat, sizeof(*pstat));
340 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
346 for(i = 0; i < incount; i++) {
347 if(smpi_mpi_testany(incount, requests, &index, status)) {
348 indices[count] = index;
355 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
358 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
359 nary_tree_bcast(buf, count, datatype, root, comm, 4);
362 void smpi_mpi_barrier(MPI_Comm comm)
364 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
365 nary_tree_barrier(comm, 4);
368 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
369 void *recvbuf, int recvcount, MPI_Datatype recvtype,
370 int root, MPI_Comm comm)
372 int system_tag = 666;
373 int rank, size, src, index;
374 MPI_Aint lb = 0, recvext = 0;
375 MPI_Request *requests;
377 rank = smpi_comm_rank(comm);
378 size = smpi_comm_size(comm);
380 // Send buffer to root
381 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
383 // FIXME: check for errors
384 smpi_datatype_extent(recvtype, &lb, &recvext);
385 // Local copy from root
386 smpi_datatype_copy(sendbuf, sendcount, sendtype,
387 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
388 // Receive buffers from senders
389 requests = xbt_new(MPI_Request, size - 1);
391 for(src = 0; src < size; src++) {
393 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
395 src, system_tag, comm);
399 // Wait for completion of irecv's.
400 smpi_mpi_startall(size - 1, requests);
401 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
406 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
407 void *recvbuf, int *recvcounts, int *displs,
408 MPI_Datatype recvtype, int root, MPI_Comm comm)
410 int system_tag = 666;
411 int rank, size, src, index;
412 MPI_Aint lb = 0, recvext = 0;
413 MPI_Request *requests;
415 rank = smpi_comm_rank(comm);
416 size = smpi_comm_size(comm);
418 // Send buffer to root
419 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
421 // FIXME: check for errors
422 smpi_datatype_extent(recvtype, &lb, &recvext);
423 // Local copy from root
424 smpi_datatype_copy(sendbuf, sendcount, sendtype,
425 (char *)recvbuf + displs[root] * recvext,
426 recvcounts[root], recvtype);
427 // Receive buffers from senders
428 requests = xbt_new(MPI_Request, size - 1);
430 for(src = 0; src < size; src++) {
433 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
434 recvcounts[src], recvtype, src, system_tag, comm);
438 // Wait for completion of irecv's.
439 smpi_mpi_startall(size - 1, requests);
440 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
445 void smpi_mpi_allgather(void *sendbuf, int sendcount,
446 MPI_Datatype sendtype, void *recvbuf,
447 int recvcount, MPI_Datatype recvtype,
450 int system_tag = 666;
451 int rank, size, other, index;
452 MPI_Aint lb = 0, recvext = 0;
453 MPI_Request *requests;
455 rank = smpi_comm_rank(comm);
456 size = smpi_comm_size(comm);
457 // FIXME: check for errors
458 smpi_datatype_extent(recvtype, &lb, &recvext);
459 // Local copy from self
460 smpi_datatype_copy(sendbuf, sendcount, sendtype,
461 (char *)recvbuf + rank * recvcount * recvext, recvcount,
463 // Send/Recv buffers to/from others;
464 requests = xbt_new(MPI_Request, 2 * (size - 1));
466 for(other = 0; other < size; other++) {
469 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
472 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
473 recvcount, recvtype, other,
478 // Wait for completion of all comms.
479 smpi_mpi_startall(2 * (size - 1), requests);
480 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
484 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
485 MPI_Datatype sendtype, void *recvbuf,
486 int *recvcounts, int *displs,
487 MPI_Datatype recvtype, MPI_Comm comm)
489 int system_tag = 666;
490 int rank, size, other, index;
491 MPI_Aint lb = 0, recvext = 0;
492 MPI_Request *requests;
494 rank = smpi_comm_rank(comm);
495 size = smpi_comm_size(comm);
496 // FIXME: check for errors
497 smpi_datatype_extent(recvtype, &lb, &recvext);
498 // Local copy from self
499 smpi_datatype_copy(sendbuf, sendcount, sendtype,
500 (char *)recvbuf + displs[rank] * recvext,
501 recvcounts[rank], recvtype);
502 // Send buffers to others;
503 requests = xbt_new(MPI_Request, 2 * (size - 1));
505 for(other = 0; other < size; other++) {
508 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
512 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
513 recvtype, other, system_tag, comm);
517 // Wait for completion of all comms.
518 smpi_mpi_startall(2 * (size - 1), requests);
519 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
523 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
524 void *recvbuf, int recvcount, MPI_Datatype recvtype,
525 int root, MPI_Comm comm)
527 int system_tag = 666;
528 int rank, size, dst, index;
529 MPI_Aint lb = 0, sendext = 0;
530 MPI_Request *requests;
532 rank = smpi_comm_rank(comm);
533 size = smpi_comm_size(comm);
535 // Recv buffer from root
536 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
539 // FIXME: check for errors
540 smpi_datatype_extent(sendtype, &lb, &sendext);
541 // Local copy from root
542 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
543 sendcount, sendtype, recvbuf, recvcount, recvtype);
544 // Send buffers to receivers
545 requests = xbt_new(MPI_Request, size - 1);
547 for(dst = 0; dst < size; dst++) {
549 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
550 sendcount, sendtype, dst,
555 // Wait for completion of isend's.
556 smpi_mpi_startall(size - 1, requests);
557 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
562 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
563 MPI_Datatype sendtype, void *recvbuf, int recvcount,
564 MPI_Datatype recvtype, int root, MPI_Comm comm)
566 int system_tag = 666;
567 int rank, size, dst, index;
568 MPI_Aint lb = 0, sendext = 0;
569 MPI_Request *requests;
571 rank = smpi_comm_rank(comm);
572 size = smpi_comm_size(comm);
574 // Recv buffer from root
575 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
578 // FIXME: check for errors
579 smpi_datatype_extent(sendtype, &lb, &sendext);
580 // Local copy from root
581 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
582 sendtype, recvbuf, recvcount, recvtype);
583 // Send buffers to receivers
584 requests = xbt_new(MPI_Request, size - 1);
586 for(dst = 0; dst < size; dst++) {
589 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
590 sendtype, dst, system_tag, comm);
594 // Wait for completion of isend's.
595 smpi_mpi_startall(size - 1, requests);
596 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
601 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
602 MPI_Datatype datatype, MPI_Op op, int root,
605 int system_tag = 666;
606 int rank, size, src, index;
607 MPI_Aint lb = 0, dataext = 0;
608 MPI_Request *requests;
611 rank = smpi_comm_rank(comm);
612 size = smpi_comm_size(comm);
614 // Send buffer to root
615 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
617 // FIXME: check for errors
618 smpi_datatype_extent(datatype, &lb, &dataext);
619 // Local copy from root
620 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
621 // Receive buffers from senders
622 //TODO: make a MPI_barrier here ?
623 requests = xbt_new(MPI_Request, size - 1);
624 tmpbufs = xbt_new(void *, size - 1);
626 for(src = 0; src < size; src++) {
628 // FIXME: possibly overkill we we have contiguous/noncontiguous data
630 tmpbufs[index] = xbt_malloc(count * dataext);
632 smpi_irecv_init(tmpbufs[index], count, datatype, src,
637 // Wait for completion of irecv's.
638 smpi_mpi_startall(size - 1, requests);
639 for(src = 0; src < size - 1; src++) {
640 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
641 if(index == MPI_UNDEFINED) {
644 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
646 for(index = 0; index < size - 1; index++) {
647 xbt_free(tmpbufs[index]);
654 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
655 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
657 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
658 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
661 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
662 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
664 int system_tag = 666;
665 int rank, size, other, index;
666 MPI_Aint lb = 0, dataext = 0;
667 MPI_Request *requests;
670 rank = smpi_comm_rank(comm);
671 size = smpi_comm_size(comm);
673 // FIXME: check for errors
674 smpi_datatype_extent(datatype, &lb, &dataext);
676 // Local copy from self
677 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
679 // Send/Recv buffers to/from others;
680 requests = xbt_new(MPI_Request, size - 1);
681 tmpbufs = xbt_new(void *, rank);
683 for(other = 0; other < rank; other++) {
684 // FIXME: possibly overkill we we have contiguous/noncontiguous data
686 tmpbufs[index] = xbt_malloc(count * dataext);
688 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
692 for(other = rank + 1; other < size; other++) {
694 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
697 // Wait for completion of all comms.
698 smpi_mpi_startall(size - 1, requests);
699 for(other = 0; other < size - 1; other++) {
700 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
701 if(index == MPI_UNDEFINED) {
705 // #Request is below rank: it's a irecv
706 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
709 for(index = 0; index < rank; index++) {
710 xbt_free(tmpbufs[index]);