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. */
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
11 "Logging specific to SMPI (base)");
12 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
13 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
14 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
15 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
16 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi_dt);
17 XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
18 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
19 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
20 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
22 static int match_recv(void* a, void* b) {
23 MPI_Request ref = (MPI_Request)a;
24 MPI_Request req = (MPI_Request)b;
26 xbt_assert0(ref, "Cannot match recv against null reference");
27 xbt_assert0(req, "Cannot match recv against null request");
28 return req->comm == ref->comm
29 && (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_assert0(ref, "Cannot match send against null reference");
38 xbt_assert0(req, "Cannot match send against null request");
39 return req->comm == ref->comm
40 && (req->src == MPI_ANY_SOURCE || req->src == ref->src)
41 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
44 static MPI_Request build_request(void *buf, int count,
45 MPI_Datatype datatype, int src, int dst,
46 int tag, MPI_Comm comm, unsigned flags)
50 request = xbt_new(s_smpi_mpi_request_t, 1);
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_assert0(!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 request->action = SIMIX_req_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
98 print_request("New send", request);
99 mailbox = smpi_process_remote_mailbox(request->dst);
100 request->action = SIMIX_req_comm_isend(mailbox, request->size, -1.0,
101 request->buf, request->size, &match_send, request);
103 SIMIX_req_set_category (request->action, TRACE_internal_smpi_get_category());
108 void smpi_mpi_startall(int count, MPI_Request * requests)
112 for(i = 0; i < count; i++) {
113 smpi_mpi_start(requests[i]);
117 void smpi_mpi_request_free(MPI_Request * request)
120 *request = MPI_REQUEST_NULL;
123 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
124 int dst, int tag, MPI_Comm comm)
126 MPI_Request request =
127 build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
128 comm, NON_PERSISTENT | SEND);
133 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
134 int dst, int tag, MPI_Comm comm)
136 MPI_Request request =
137 smpi_isend_init(buf, count, datatype, dst, tag, comm);
139 smpi_mpi_start(request);
143 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
144 int src, int tag, MPI_Comm comm)
146 MPI_Request request =
147 build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
148 comm, NON_PERSISTENT | RECV);
153 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
154 int src, int tag, MPI_Comm comm)
156 MPI_Request request =
157 smpi_irecv_init(buf, count, datatype, src, tag, comm);
159 smpi_mpi_start(request);
163 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
164 int tag, MPI_Comm comm, MPI_Status * status)
168 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
169 smpi_mpi_wait(&request, status);
172 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
173 int tag, MPI_Comm comm)
177 request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
178 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
181 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
182 int dst, int sendtag, void *recvbuf, int recvcount,
183 MPI_Datatype recvtype, int src, int recvtag,
184 MPI_Comm comm, MPI_Status * status)
186 MPI_Request requests[2];
190 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
192 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
193 smpi_mpi_startall(2, requests);
194 smpi_mpi_waitall(2, requests, stats);
195 if(status != MPI_STATUS_IGNORE) {
196 // Copy receive status
197 memcpy(status, &stats[1], sizeof(MPI_Status));
201 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
203 return status->count / smpi_datatype_size(datatype);
206 static void finish_wait(MPI_Request * request, MPI_Status * status)
208 MPI_Request req = *request;
210 if(status != MPI_STATUS_IGNORE) {
211 status->MPI_SOURCE = req->src;
212 status->MPI_TAG = req->tag;
213 status->MPI_ERROR = MPI_SUCCESS;
214 status->count = SIMIX_req_comm_get_dst_buff_size(req->action);
216 SIMIX_req_comm_destroy(req->action);
217 print_request("Finishing", req);
218 if(req->flags & NON_PERSISTENT) {
219 smpi_mpi_request_free(request);
225 int smpi_mpi_test(MPI_Request * request, MPI_Status * status)
227 int flag = SIMIX_req_comm_test((*request)->action);
230 smpi_mpi_wait(request, status);
235 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
242 *index = MPI_UNDEFINED;
245 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
246 map = xbt_new(int, count);
248 for(i = 0; i < count; i++) {
249 if(requests[i]->action) {
250 xbt_dynar_push(comms, &requests[i]->action);
256 *index = SIMIX_req_comm_testany(comms);
257 *index = map[*index];
258 if(*index != MPI_UNDEFINED) {
259 smpi_mpi_wait(&requests[*index], status);
264 xbt_dynar_free(&comms);
269 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
271 print_request("Waiting", *request);
272 SIMIX_req_comm_wait((*request)->action, -1.0);
273 finish_wait(request, status);
276 int smpi_mpi_waitany(int count, MPI_Request requests[],
283 index = MPI_UNDEFINED;
285 // Wait for a request to complete
286 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
287 map = xbt_new(int, count);
289 DEBUG0("Wait for one of");
290 for(i = 0; i < count; i++) {
291 if(requests[i] != MPI_REQUEST_NULL) {
292 print_request(" ", requests[i]);
293 xbt_dynar_push(comms, &requests[i]->action);
299 index = SIMIX_req_comm_waitany(comms);
301 finish_wait(&requests[index], status);
304 xbt_dynar_free(&comms);
309 void smpi_mpi_waitall(int count, MPI_Request requests[],
314 MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
318 index = smpi_mpi_waitany(count, requests, pstat);
319 if(index == MPI_UNDEFINED) {
322 if(status != MPI_STATUS_IGNORE) {
323 memcpy(&status[index], pstat, sizeof *pstat);
329 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
335 for(i = 0; i < incount; i++) {
336 if(smpi_mpi_testany(incount, requests, &index, status)) {
337 indices[count] = index;
344 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
347 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
348 nary_tree_bcast(buf, count, datatype, root, comm, 4);
351 void smpi_mpi_barrier(MPI_Comm comm)
353 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
354 nary_tree_barrier(comm, 4);
357 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
358 void *recvbuf, int recvcount, MPI_Datatype recvtype,
359 int root, MPI_Comm comm)
361 int system_tag = 666;
362 int rank, size, src, index, sendsize, recvsize;
363 MPI_Request *requests;
365 rank = smpi_comm_rank(comm);
366 size = smpi_comm_size(comm);
368 // Send buffer to root
369 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
371 sendsize = smpi_datatype_size(sendtype);
372 recvsize = smpi_datatype_size(recvtype);
373 // Local copy from root
374 memcpy(&((char *) recvbuf)[root * recvcount * recvsize], sendbuf,
375 sendcount * sendsize * sizeof(char));
376 // Receive buffers from senders
377 requests = xbt_new(MPI_Request, size - 1);
379 for(src = 0; src < size; src++) {
381 requests[index] = smpi_irecv_init(&((char *) recvbuf)
382 [src * recvcount * recvsize],
383 recvcount, recvtype, src,
388 // Wait for completion of irecv's.
389 smpi_mpi_startall(size - 1, requests);
390 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
395 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
396 void *recvbuf, int *recvcounts, int *displs,
397 MPI_Datatype recvtype, int root, MPI_Comm comm)
399 int system_tag = 666;
400 int rank, size, src, index, sendsize;
401 MPI_Request *requests;
403 rank = smpi_comm_rank(comm);
404 size = smpi_comm_size(comm);
406 // Send buffer to root
407 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
409 sendsize = smpi_datatype_size(sendtype);
410 // Local copy from root
411 memcpy(&((char *) recvbuf)[displs[root]], sendbuf,
412 sendcount * sendsize * sizeof(char));
413 // Receive buffers from senders
414 requests = xbt_new(MPI_Request, size - 1);
416 for(src = 0; src < size; src++) {
419 smpi_irecv_init(&((char *) recvbuf)[displs[src]],
420 recvcounts[src], recvtype, src, system_tag,
425 // Wait for completion of irecv's.
426 smpi_mpi_startall(size - 1, requests);
427 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
432 void smpi_mpi_allgather(void *sendbuf, int sendcount,
433 MPI_Datatype sendtype, void *recvbuf,
434 int recvcount, MPI_Datatype recvtype,
437 int system_tag = 666;
438 int rank, size, other, index, sendsize, recvsize;
439 MPI_Request *requests;
441 rank = smpi_comm_rank(comm);
442 size = smpi_comm_size(comm);
443 sendsize = smpi_datatype_size(sendtype);
444 recvsize = smpi_datatype_size(recvtype);
445 // Local copy from self
446 memcpy(&((char *) recvbuf)[rank * recvcount * recvsize], sendbuf,
447 sendcount * sendsize * sizeof(char));
448 // Send/Recv buffers to/from others;
449 requests = xbt_new(MPI_Request, 2 * (size - 1));
451 for(other = 0; other < size; other++) {
454 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
457 requests[index] = smpi_irecv_init(&((char *) recvbuf)
458 [other * recvcount * recvsize],
459 recvcount, recvtype, other,
464 // Wait for completion of all comms.
465 smpi_mpi_startall(2 * (size - 1), requests);
466 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
470 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
471 MPI_Datatype sendtype, void *recvbuf,
472 int *recvcounts, int *displs,
473 MPI_Datatype recvtype, MPI_Comm comm)
475 int system_tag = 666;
476 int rank, size, other, index, sendsize, recvsize;
477 MPI_Request *requests;
479 rank = smpi_comm_rank(comm);
480 size = smpi_comm_size(comm);
481 sendsize = smpi_datatype_size(sendtype);
482 recvsize = smpi_datatype_size(recvtype);
483 // Local copy from self
484 memcpy(&((char *) recvbuf)[displs[rank]], sendbuf,
485 sendcount * sendsize * sizeof(char));
486 // Send buffers to others;
487 requests = xbt_new(MPI_Request, 2 * (size - 1));
489 for(other = 0; other < size; other++) {
492 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
496 smpi_irecv_init(&((char *) recvbuf)[displs[other]],
497 recvcounts[other], recvtype, other, system_tag,
502 // Wait for completion of all comms.
503 smpi_mpi_startall(2 * (size - 1), requests);
504 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
508 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
509 void *recvbuf, int recvcount, MPI_Datatype recvtype,
510 int root, MPI_Comm comm)
512 int system_tag = 666;
513 int rank, size, dst, index, sendsize, recvsize;
514 MPI_Request *requests;
516 rank = smpi_comm_rank(comm);
517 size = smpi_comm_size(comm);
519 // Recv buffer from root
520 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
523 sendsize = smpi_datatype_size(sendtype);
524 recvsize = smpi_datatype_size(recvtype);
525 // Local copy from root
526 memcpy(recvbuf, &((char *) sendbuf)[root * sendcount * sendsize],
527 recvcount * recvsize * sizeof(char));
528 // Send buffers to receivers
529 requests = xbt_new(MPI_Request, size - 1);
531 for(dst = 0; dst < size; dst++) {
533 requests[index] = smpi_isend_init(&((char *) sendbuf)
534 [dst * sendcount * sendsize],
535 sendcount, sendtype, dst,
540 // Wait for completion of isend's.
541 smpi_mpi_startall(size - 1, requests);
542 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
547 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
548 MPI_Datatype sendtype, void *recvbuf, int recvcount,
549 MPI_Datatype recvtype, int root, MPI_Comm comm)
551 int system_tag = 666;
552 int rank, size, dst, index, sendsize, recvsize;
553 MPI_Request *requests;
555 rank = smpi_comm_rank(comm);
556 size = smpi_comm_size(comm);
558 // Recv buffer from root
559 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
562 sendsize = smpi_datatype_size(sendtype);
563 recvsize = smpi_datatype_size(recvtype);
564 // Local copy from root
565 memcpy(recvbuf, &((char *) sendbuf)[displs[root]],
566 recvcount * recvsize * sizeof(char));
567 // Send buffers to receivers
568 requests = xbt_new(MPI_Request, size - 1);
570 for(dst = 0; dst < size; dst++) {
573 smpi_isend_init(&((char *) sendbuf)[displs[dst]],
574 sendcounts[dst], sendtype, dst, system_tag,
579 // Wait for completion of isend's.
580 smpi_mpi_startall(size - 1, requests);
581 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
586 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
587 MPI_Datatype datatype, MPI_Op op, int root,
590 int system_tag = 666;
591 int rank, size, src, index, datasize;
592 MPI_Request *requests;
595 rank = smpi_comm_rank(comm);
596 size = smpi_comm_size(comm);
598 // Send buffer to root
599 smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
601 datasize = smpi_datatype_size(datatype);
602 // Local copy from root
603 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
604 // Receive buffers from senders
605 //TODO: make a MPI_barrier here ?
606 requests = xbt_new(MPI_Request, size - 1);
607 tmpbufs = xbt_new(void *, size - 1);
609 for(src = 0; src < size; src++) {
611 tmpbufs[index] = xbt_malloc(count * datasize);
613 smpi_irecv_init(tmpbufs[index], count, datatype, src,
618 // Wait for completion of irecv's.
619 smpi_mpi_startall(size - 1, requests);
620 for(src = 0; src < size - 1; src++) {
621 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
622 if(index == MPI_UNDEFINED) {
625 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
627 for(index = 0; index < size - 1; index++) {
628 xbt_free(tmpbufs[index]);
635 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
636 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
638 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
639 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
642 FIXME: buggy implementation
644 int system_tag = 666;
645 int rank, size, other, index, datasize;
646 MPI_Request* requests;
649 rank = smpi_comm_rank(comm);
650 size = smpi_comm_size(comm);
651 datasize = smpi_datatype_size(datatype);
652 // Local copy from self
653 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
654 // Send/Recv buffers to/from others;
655 //TODO: make a MPI_barrier here ?
656 requests = xbt_new(MPI_Request, 2 * (size - 1));
657 tmpbufs = xbt_new(void*, size - 1);
659 for(other = 0; other < size; other++) {
661 tmpbufs[index / 2] = xbt_malloc(count * datasize);
662 requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
663 requests[index + 1] = smpi_mpi_irecv(tmpbufs[index / 2], count, datatype, other, system_tag, comm);
667 // Wait for completion of all comms.
668 for(other = 0; other < 2 * (size - 1); other++) {
669 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
670 if(index == MPI_UNDEFINED) {
673 if((index & 1) == 1) {
674 // Request is odd: it's a irecv
675 smpi_op_apply(op, tmpbufs[index / 2], recvbuf, &count, &datatype);
678 for(index = 0; index < size - 1; index++) {
679 xbt_free(tmpbufs[index]);
686 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
687 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
689 int system_tag = 666;
690 int rank, size, other, index, datasize;
692 MPI_Request *requests;
695 rank = smpi_comm_rank(comm);
696 size = smpi_comm_size(comm);
697 datasize = smpi_datatype_size(datatype);
698 // Local copy from self
699 memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
700 // Send/Recv buffers to/from others;
701 total = rank + (size - (rank + 1));
702 requests = xbt_new(MPI_Request, total);
703 tmpbufs = xbt_new(void *, rank);
705 for(other = 0; other < rank; other++) {
706 tmpbufs[index] = xbt_malloc(count * datasize);
708 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
712 for(other = rank + 1; other < size; other++) {
714 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
717 // Wait for completion of all comms.
718 smpi_mpi_startall(size - 1, requests);
719 for(other = 0; other < total; other++) {
720 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
721 if(index == MPI_UNDEFINED) {
725 // #Request is below rank: it's a irecv
726 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
729 for(index = 0; index < size - 1; index++) {
730 xbt_free(tmpbufs[index]);