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 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(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 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(recvbuf + displs[src] * recvext, recvcounts[src],
434 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 recvbuf + rank * recvcount * recvext, recvcount, recvtype);
462 // Send/Recv buffers to/from others;
463 requests = xbt_new(MPI_Request, 2 * (size - 1));
465 for(other = 0; other < size; other++) {
468 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
471 requests[index] = smpi_irecv_init(recvbuf + other * recvcount * recvext,
472 recvcount, recvtype, other,
477 // Wait for completion of all comms.
478 smpi_mpi_startall(2 * (size - 1), requests);
479 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
483 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
484 MPI_Datatype sendtype, void *recvbuf,
485 int *recvcounts, int *displs,
486 MPI_Datatype recvtype, MPI_Comm comm)
488 int system_tag = 666;
489 int rank, size, other, index;
490 MPI_Aint lb = 0, recvext = 0;
491 MPI_Request *requests;
493 rank = smpi_comm_rank(comm);
494 size = smpi_comm_size(comm);
495 // FIXME: check for errors
496 smpi_datatype_extent(recvtype, &lb, &recvext);
497 // Local copy from self
498 smpi_datatype_copy(sendbuf, sendcount, sendtype,
499 recvbuf + displs[rank] * recvext,
500 recvcounts[rank], recvtype);
501 // Send buffers to others;
502 requests = xbt_new(MPI_Request, 2 * (size - 1));
504 for(other = 0; other < size; other++) {
507 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
511 smpi_irecv_init(recvbuf + displs[other] * recvext, recvcounts[other],
512 recvtype, other, system_tag, comm);
516 // Wait for completion of all comms.
517 smpi_mpi_startall(2 * (size - 1), requests);
518 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
522 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
523 void *recvbuf, int recvcount, MPI_Datatype recvtype,
524 int root, MPI_Comm comm)
526 int system_tag = 666;
527 int rank, size, dst, index;
528 MPI_Aint lb = 0, sendext = 0;
529 MPI_Request *requests;
531 rank = smpi_comm_rank(comm);
532 size = smpi_comm_size(comm);
534 // Recv buffer from root
535 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
538 // FIXME: check for errors
539 smpi_datatype_extent(sendtype, &lb, &sendext);
540 // Local copy from root
541 smpi_datatype_copy(sendbuf + root * sendcount * sendext,
542 sendcount, sendtype, recvbuf, recvcount, recvtype);
543 // Send buffers to receivers
544 requests = xbt_new(MPI_Request, size - 1);
546 for(dst = 0; dst < size; dst++) {
548 requests[index] = smpi_isend_init(sendbuf + dst * sendcount * sendext,
549 sendcount, sendtype, dst,
554 // Wait for completion of isend's.
555 smpi_mpi_startall(size - 1, requests);
556 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
561 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
562 MPI_Datatype sendtype, void *recvbuf, int recvcount,
563 MPI_Datatype recvtype, int root, MPI_Comm comm)
565 int system_tag = 666;
566 int rank, size, dst, index;
567 MPI_Aint lb = 0, sendext = 0;
568 MPI_Request *requests;
570 rank = smpi_comm_rank(comm);
571 size = smpi_comm_size(comm);
573 // Recv buffer from root
574 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
577 // FIXME: check for errors
578 smpi_datatype_extent(sendtype, &lb, &sendext);
579 // Local copy from root
580 smpi_datatype_copy(sendbuf + displs[root] * sendext, sendcounts[root],
581 sendtype, recvbuf, recvcount, recvtype);
582 // Send buffers to receivers
583 requests = xbt_new(MPI_Request, size - 1);
585 for(dst = 0; dst < size; dst++) {
588 smpi_isend_init(sendbuf + displs[dst] * sendext, sendcounts[dst],
589 sendtype, dst, system_tag, comm);
593 // Wait for completion of isend's.
594 smpi_mpi_startall(size - 1, requests);
595 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
600 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
601 MPI_Datatype datatype, MPI_Op op, int root,
604 int system_tag = 666;
605 int rank, size, src, index;
606 MPI_Aint lb = 0, dataext = 0;
607 MPI_Request *requests;
610 rank = smpi_comm_rank(comm);
611 size = smpi_comm_size(comm);
613 // Send buffer to root
614 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
616 // FIXME: check for errors
617 smpi_datatype_extent(datatype, &lb, &dataext);
618 // Local copy from root
619 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
620 // Receive buffers from senders
621 //TODO: make a MPI_barrier here ?
622 requests = xbt_new(MPI_Request, size - 1);
623 tmpbufs = xbt_new(void *, size - 1);
625 for(src = 0; src < size; src++) {
627 // FIXME: possibly overkill we we have contiguous/noncontiguous data
629 tmpbufs[index] = xbt_malloc(count * dataext);
631 smpi_irecv_init(tmpbufs[index], count, datatype, src,
636 // Wait for completion of irecv's.
637 smpi_mpi_startall(size - 1, requests);
638 for(src = 0; src < size - 1; src++) {
639 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
640 if(index == MPI_UNDEFINED) {
643 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
645 for(index = 0; index < size - 1; index++) {
646 xbt_free(tmpbufs[index]);
653 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
654 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
656 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
657 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
660 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
661 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
663 int system_tag = 666;
664 int rank, size, other, index;
665 MPI_Aint lb = 0, dataext = 0;
666 MPI_Request *requests;
669 rank = smpi_comm_rank(comm);
670 size = smpi_comm_size(comm);
672 // FIXME: check for errors
673 smpi_datatype_extent(datatype, &lb, &dataext);
675 // Local copy from self
676 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
678 // Send/Recv buffers to/from others;
679 requests = xbt_new(MPI_Request, size - 1);
680 tmpbufs = xbt_new(void *, rank);
682 for(other = 0; other < rank; other++) {
683 // FIXME: possibly overkill we we have contiguous/noncontiguous data
685 tmpbufs[index] = xbt_malloc(count * dataext);
687 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
691 for(other = rank + 1; other < size; other++) {
693 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
696 // Wait for completion of all comms.
697 smpi_mpi_startall(size - 1, requests);
698 for(other = 0; other < size - 1; other++) {
699 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
700 if(index == MPI_UNDEFINED) {
704 // #Request is below rank: it's a irecv
705 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
708 for(index = 0; index < rank; index++) {
709 xbt_free(tmpbufs[index]);