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 #include "xbt/replay.h"
12 #include "simix/smx_private.h"
13 #include "surf/surf.h"
14 #include "simgrid/sg_config.h"
15 #include "colls/colls.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
20 static int match_recv(void* a, void* b, smx_action_t ignored) {
21 MPI_Request ref = (MPI_Request)a;
22 MPI_Request req = (MPI_Request)b;
23 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
25 xbt_assert(ref, "Cannot match recv against null reference");
26 xbt_assert(req, "Cannot match recv against null request");
27 if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
28 && ((ref->tag == MPI_ANY_TAG && req->tag >=0) || req->tag == ref->tag)){
29 //we match, we can transfer some values
30 // FIXME : move this to the copy function ?
31 if(ref->src == MPI_ANY_SOURCE)ref->real_src = req->src;
32 if(ref->tag == MPI_ANY_TAG)ref->real_tag = req->tag;
33 if(ref->real_size < req->real_size) ref->truncated = 1;
35 ref->detached_sender=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
37 XBT_DEBUG("match succeeded");
42 static int match_send(void* a, void* b,smx_action_t ignored) {
43 MPI_Request ref = (MPI_Request)a;
44 MPI_Request req = (MPI_Request)b;
45 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
46 xbt_assert(ref, "Cannot match send against null reference");
47 xbt_assert(req, "Cannot match send against null request");
49 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
50 && ((req->tag == MPI_ANY_TAG && ref->tag >=0)|| req->tag == ref->tag))
52 if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
53 if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
54 if(req->real_size < ref->real_size) req->truncated = 1;
56 req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
58 XBT_DEBUG("match succeeded");
64 typedef struct s_smpi_factor *smpi_factor_t;
65 typedef struct s_smpi_factor {
68 double values[4];//arbitrary set to 4
70 xbt_dynar_t smpi_os_values = NULL;
71 xbt_dynar_t smpi_or_values = NULL;
72 xbt_dynar_t smpi_ois_values = NULL;
74 // Methods used to parse and store the values for timing injections in smpi
75 // These are taken from surf/network.c and generalized to have more factors
76 // These methods should be merged with those in surf/network.c (moved somewhere in xbt ?)
78 static int factor_cmp(const void *pa, const void *pb)
80 return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor);
84 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
87 unsigned int iter = 0;
90 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
92 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
93 radical_elements = xbt_str_split(smpi_coef_string, ";");
94 xbt_dynar_foreach(radical_elements, iter, value) {
96 radical_elements2 = xbt_str_split(value, ":");
97 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
98 xbt_die("Malformed radical for smpi factor!");
99 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
101 fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
103 fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
108 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
109 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
110 xbt_dynar_free(&radical_elements2);
112 xbt_dynar_free(&radical_elements);
114 xbt_dynar_sort(smpi_factor, &factor_cmp);
115 xbt_dynar_foreach(smpi_factor, iter, fact) {
116 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
121 static double smpi_os(double size)
123 if (!smpi_os_values) {
124 smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
125 smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
127 unsigned int iter = 0;
128 s_smpi_factor_t fact;
130 xbt_dynar_foreach(smpi_os_values, iter, fact) {
131 if (size <= fact.factor) {
132 XBT_DEBUG("os : %lf <= %ld return %f", size, fact.factor, current);
135 current=fact.values[0]+fact.values[1]*size;
138 XBT_DEBUG("os : %lf > %ld return %f", size, fact.factor, current);
143 static double smpi_ois(double size)
145 if (!smpi_ois_values) {
146 smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
147 smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
149 unsigned int iter = 0;
150 s_smpi_factor_t fact;
152 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
153 if (size <= fact.factor) {
154 XBT_DEBUG("ois : %lf <= %ld return %f", size, fact.factor, current);
157 current=fact.values[0]+fact.values[1]*size;
160 XBT_DEBUG("ois : %lf > %ld return %f", size, fact.factor, current);
165 static double smpi_or(double size)
167 if (!smpi_or_values) {
168 smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
169 smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
171 unsigned int iter = 0;
172 s_smpi_factor_t fact;
174 xbt_dynar_foreach(smpi_or_values, iter, fact) {
175 if (size <= fact.factor) {
176 XBT_DEBUG("or : %lf <= %ld return %f", size, fact.factor, current);
179 current=fact.values[0]+fact.values[1]*size;
181 XBT_DEBUG("or : %lf > %ld return %f", size, fact.factor, current);
186 static MPI_Request build_request(void *buf, int count,
187 MPI_Datatype datatype, int src, int dst,
188 int tag, MPI_Comm comm, unsigned flags)
192 void *old_buf = NULL;
194 request = xbt_new(s_smpi_mpi_request_t, 1);
196 s_smpi_subtype_t *subtype = datatype->substruct;
198 if(datatype->has_subtype == 1){
199 // This part handles the problem of non-contiguous memory
201 buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
203 subtype->serialize(old_buf, buf, count, datatype->substruct);
208 // This part handles the problem of non-contiguous memory (for the
209 // unserialisation at the reception)
210 request->old_buf = old_buf;
211 request->old_type = datatype;
213 request->size = smpi_datatype_size(datatype) * count;
217 request->comm = comm;
218 request->action = NULL;
219 request->flags = flags;
220 request->detached = 0;
221 request->detached_sender = NULL;
223 request->truncated = 0;
224 request->real_size = 0;
225 request->real_tag = 0;
232 if (flags & SEND) smpi_datatype_unuse(datatype);
238 void smpi_empty_status(MPI_Status * status)
240 if(status != MPI_STATUS_IGNORE) {
241 status->MPI_SOURCE = MPI_ANY_SOURCE;
242 status->MPI_TAG = MPI_ANY_TAG;
243 status->MPI_ERROR = MPI_SUCCESS;
248 void smpi_action_trace_run(char *path)
252 xbt_dict_cursor_t cursor;
256 action_fp = fopen(path, "r");
257 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
261 if (!xbt_dict_is_empty(action_queues)) {
263 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
266 xbt_dict_foreach(action_queues, cursor, name, todo) {
267 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
273 xbt_dict_free(&action_queues);
274 action_queues = xbt_dict_new_homogeneous(NULL);
277 static void smpi_mpi_request_free_voidp(void* request)
279 MPI_Request req = request;
280 smpi_mpi_request_free(&req);
283 /* MPI Low level calls */
284 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
285 int dst, int tag, MPI_Comm comm)
287 MPI_Request request =
288 build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
289 comm, PERSISTENT | SEND | PREPARED);
294 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
295 int dst, int tag, MPI_Comm comm)
297 MPI_Request request =
298 build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
299 comm, PERSISTENT | SSEND | SEND | PREPARED);
304 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
305 int src, int tag, MPI_Comm comm)
307 MPI_Request request =
308 build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
309 comm, PERSISTENT | RECV | PREPARED);
314 void smpi_mpi_start(MPI_Request request)
318 xbt_assert(!request->action,
319 "Cannot (re)start a non-finished communication");
320 if(request->flags & PREPARED)request->flags &= ~PREPARED;
321 if(request->flags & RECV) {
322 print_request("New recv", request);
323 if (request->size < sg_cfg_get_int("smpi/async_small_thres"))
324 mailbox = smpi_process_mailbox_small();
326 mailbox = smpi_process_mailbox();
327 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
328 request->real_size=request->size;
329 smpi_datatype_use(request->old_type);
330 smpi_comm_use(request->comm);
331 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
333 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
334 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
336 simcall_process_sleep(sleeptime);
337 XBT_DEBUG("receiving size of %zu : sleep %lf ", request->size, smpi_or(request->size));
343 int receiver = request->dst;//smpi_group_index(smpi_comm_group(request->comm), request->dst);
346 int rank = smpi_process_index();
347 if (TRACE_smpi_view_internals()) {
348 TRACE_smpi_send(rank, rank, receiver);
351 /* if(receiver == MPI_UNDEFINED) {*/
352 /* XBT_WARN("Trying to send a message to a wrong rank");*/
355 print_request("New send", request);
356 if (request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
357 mailbox = smpi_process_remote_mailbox_small(receiver);
359 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
360 mailbox = smpi_process_remote_mailbox(receiver);
362 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
364 request->detached = 1;
366 if(request->old_type->has_subtype == 0){
367 oldbuf = request->buf;
368 if (oldbuf && request->size!=0){
369 request->buf = xbt_malloc(request->size);
370 memcpy(request->buf,oldbuf,request->size);
373 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
376 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
377 request->real_size=request->size;
378 smpi_datatype_use(request->old_type);
379 smpi_comm_use(request->comm);
381 //if we are giving back the control to the user without waiting for completion, we have to inject timings
382 double sleeptime =0.0;
383 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
384 //isend and send timings may be different
385 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
389 simcall_process_sleep(sleeptime);
390 XBT_DEBUG("sending size of %zu : sleep %lf ", request->size, smpi_os(request->size));
394 simcall_comm_isend(mailbox, request->size, -1.0,
395 request->buf, request->real_size,
397 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
399 // detach if msg size < eager/rdv switch limit
403 /* FIXME: detached sends are not traceable (request->action == NULL) */
405 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
413 void smpi_mpi_startall(int count, MPI_Request * requests)
417 for(i = 0; i < count; i++) {
418 smpi_mpi_start(requests[i]);
422 void smpi_mpi_request_free(MPI_Request * request)
424 if((*request) != MPI_REQUEST_NULL){
425 (*request)->refcount--;
426 if((*request)->refcount<0) xbt_die("wrong refcount");
428 if((*request)->refcount==0){
429 print_request("Destroying", (*request));
431 *request = MPI_REQUEST_NULL;
433 print_request("Decrementing", (*request));
437 xbt_die("freeing an already free request");
441 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
442 int dst, int tag, MPI_Comm comm)
444 MPI_Request request =
445 build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
446 comm, NON_PERSISTENT | SEND | PREPARED);
451 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
452 int dst, int tag, MPI_Comm comm)
454 MPI_Request request =
455 build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
456 comm, NON_PERSISTENT | ISEND | SEND);
458 smpi_mpi_start(request);
462 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
463 int dst, int tag, MPI_Comm comm)
465 MPI_Request request =
466 build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
467 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
468 smpi_mpi_start(request);
474 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
475 int src, int tag, MPI_Comm comm)
477 MPI_Request request =
478 build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
479 comm, NON_PERSISTENT | RECV | PREPARED);
483 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
484 int src, int tag, MPI_Comm comm)
486 MPI_Request request =
487 build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
488 comm, NON_PERSISTENT | RECV);
490 smpi_mpi_start(request);
494 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
495 int tag, MPI_Comm comm, MPI_Status * status)
498 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
499 smpi_mpi_wait(&request, status);
504 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
505 int tag, MPI_Comm comm)
507 MPI_Request request =
508 build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
509 comm, NON_PERSISTENT | SEND);
510 smpi_mpi_start(request);
511 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
515 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
516 int dst, int tag, MPI_Comm comm)
518 MPI_Request request =
519 build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
520 comm, NON_PERSISTENT | SSEND | SEND);
522 smpi_mpi_start(request);
523 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
526 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
527 int dst, int sendtag, void *recvbuf, int recvcount,
528 MPI_Datatype recvtype, int src, int recvtag,
529 MPI_Comm comm, MPI_Status * status)
531 MPI_Request requests[2];
533 int myid=smpi_process_index();
534 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
535 smpi_datatype_copy(sendbuf, sendcount, sendtype,
536 recvbuf, recvcount, recvtype);
540 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
542 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
543 smpi_mpi_startall(2, requests);
544 smpi_mpi_waitall(2, requests, stats);
545 if(status != MPI_STATUS_IGNORE) {
546 // Copy receive status
551 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
553 return status->count / smpi_datatype_size(datatype);
556 static void finish_wait(MPI_Request * request, MPI_Status * status)
558 MPI_Request req = *request;
559 if(status != MPI_STATUS_IGNORE)
560 smpi_empty_status(status);
562 if(!(req->detached && req->flags & SEND) && !(req->flags & PREPARED)){
563 if(status != MPI_STATUS_IGNORE) {
564 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
565 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
566 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
567 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
568 // this handles the case were size in receive differs from size in send
569 // FIXME: really this should just contain the count of receive-type blocks,
571 status->count = req->real_size;
574 print_request("Finishing", req);
575 MPI_Datatype datatype = req->old_type;
577 if(datatype->has_subtype == 1){
578 // This part handles the problem of non-contignous memory
579 // the unserialization at the reception
580 s_smpi_subtype_t *subtype = datatype->substruct;
581 if(req->flags & RECV) {
582 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
584 if(req->detached == 0) free(req->buf);
586 smpi_comm_unuse(req->comm);
587 smpi_datatype_unuse(datatype);
592 if (TRACE_smpi_view_internals()) {
593 if(req->flags & RECV){
594 int rank = smpi_process_index();
595 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
596 TRACE_smpi_recv(rank, src_traced, rank);
601 if(req->detached_sender!=NULL){
602 smpi_mpi_request_free(&(req->detached_sender));
605 if(req->flags & NON_PERSISTENT) {
606 smpi_mpi_request_free(request);
612 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
615 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
616 if ((*request)->action == NULL)
619 flag = simcall_comm_test((*request)->action);
621 finish_wait(request, status);
622 request=MPI_REQUEST_NULL;
624 smpi_empty_status(status);
629 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
636 *index = MPI_UNDEFINED;
638 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
639 map = xbt_new(int, count);
641 for(i = 0; i < count; i++) {
642 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
643 xbt_dynar_push(comms, &requests[i]->action);
649 i = simcall_comm_testany(comms);
650 // not MPI_UNDEFINED, as this is a simix return code
653 finish_wait(&requests[*index], status);
657 //all requests are null or inactive, return true
659 smpi_empty_status(status);
662 xbt_dynar_free(&comms);
668 int smpi_mpi_testall(int count, MPI_Request requests[],
672 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
675 for(i=0; i<count; i++){
676 if(requests[i]!= MPI_REQUEST_NULL){
677 if (smpi_mpi_test(&requests[i], pstat)!=1){
681 smpi_empty_status(pstat);
683 if(status != MPI_STATUSES_IGNORE) {
690 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
692 //FIXME find another wait to avoid busy waiting ?
693 // the issue here is that we have to wait on a nonexistent comm
695 smpi_mpi_iprobe(source, tag, comm, &flag, status);
696 XBT_DEBUG("Busy Waiting on probing : %d", flag);
700 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
702 MPI_Request request =build_request(NULL, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), source), smpi_comm_rank(comm), tag,
703 comm, NON_PERSISTENT | RECV);
705 //to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
706 double sleeptime= sg_cfg_get_double("smpi/iprobe");
707 //multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
708 static int nsleeps = 1;
710 simcall_process_sleep(sleeptime);
712 // behave like a receive, but don't do it
715 print_request("New iprobe", request);
716 // We have to test both mailboxes as we don't know if we will receive one one or another
717 if (sg_cfg_get_int("smpi/async_small_thres")>0){
718 mailbox = smpi_process_mailbox_small();
719 XBT_DEBUG("trying to probe the perm recv mailbox");
720 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
722 if (request->action==NULL){
723 mailbox = smpi_process_mailbox();
724 XBT_DEBUG("trying to probe the other mailbox");
725 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
729 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
731 if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
732 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
733 status->MPI_TAG = req->tag;
734 status->MPI_ERROR = MPI_SUCCESS;
735 status->count = req->real_size;
737 nsleeps=1;//reset the number of sleeps we will do next time
743 smpi_mpi_request_free(&request);
748 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
750 print_request("Waiting", *request);
751 if ((*request)->action != NULL) { // this is not a detached send
752 simcall_comm_wait((*request)->action, -1.0);
754 finish_wait(request, status);
756 // FIXME for a detached send, finish_wait is not called:
759 int smpi_mpi_waitany(int count, MPI_Request requests[],
766 index = MPI_UNDEFINED;
768 // Wait for a request to complete
769 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
770 map = xbt_new(int, count);
772 XBT_DEBUG("Wait for one of %d", count);
773 for(i = 0; i < count; i++) {
774 if(requests[i] != MPI_REQUEST_NULL) {
775 if (requests[i]->action != NULL) {
776 XBT_DEBUG("Waiting any %p ", requests[i]);
777 xbt_dynar_push(comms, &requests[i]->action);
781 //This is a finished detached request, let's return this one
782 size=0;//so we free the dynar but don't do the waitany call
784 finish_wait(&requests[i], status);//cleanup if refcount = 0
785 requests[i]=MPI_REQUEST_NULL;//set to null
791 i = simcall_comm_waitany(comms);
793 // not MPI_UNDEFINED, as this is a simix return code
796 finish_wait(&requests[index], status);
800 xbt_dynar_free(&comms);
803 if (index==MPI_UNDEFINED)
804 smpi_empty_status(status);
809 int smpi_mpi_waitall(int count, MPI_Request requests[],
814 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
815 int retvalue = MPI_SUCCESS;
816 //tag invalid requests in the set
817 if (status != MPI_STATUSES_IGNORE) {
818 for (c = 0; c < count; c++) {
819 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL) {
820 smpi_empty_status(&status[c]);
821 } else if (requests[c]->src == MPI_PROC_NULL) {
822 smpi_empty_status(&status[c]);
823 status[c].MPI_SOURCE = MPI_PROC_NULL;
827 for(c = 0; c < count; c++) {
828 if (MC_is_active()) {
829 smpi_mpi_wait(&requests[c], pstat);
832 index = smpi_mpi_waitany(count, requests, pstat);
833 if (index == MPI_UNDEFINED)
835 requests[index]=MPI_REQUEST_NULL;
837 if (status != MPI_STATUSES_IGNORE) {
838 status[index] = *pstat;
839 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
840 retvalue = MPI_ERR_IN_STATUS;
847 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
852 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
855 for(i = 0; i < incount; i++)
857 index=smpi_mpi_waitany(incount, requests, pstat);
858 if(index!=MPI_UNDEFINED){
859 indices[count] = index;
861 if(status != MPI_STATUSES_IGNORE) {
862 status[index] = *pstat;
864 requests[index]=MPI_REQUEST_NULL;
866 return MPI_UNDEFINED;
872 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
875 int i, count, count_dead;
877 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
881 for(i = 0; i < incount; i++) {
882 if((requests[i] != MPI_REQUEST_NULL)) {
883 if(smpi_mpi_test(&requests[i], pstat)) {
886 if(status != MPI_STATUSES_IGNORE) {
889 requests[i]=MPI_REQUEST_NULL;
896 if(count_dead==incount)return MPI_UNDEFINED;
900 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
903 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
904 nary_tree_bcast(buf, count, datatype, root, comm, 4);
907 void smpi_mpi_barrier(MPI_Comm comm)
909 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
910 nary_tree_barrier(comm, 4);
913 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
914 void *recvbuf, int recvcount, MPI_Datatype recvtype,
915 int root, MPI_Comm comm)
917 int system_tag = COLL_TAG_GATHER;
918 int rank, size, src, index;
919 MPI_Aint lb = 0, recvext = 0;
920 MPI_Request *requests;
922 rank = smpi_comm_rank(comm);
923 size = smpi_comm_size(comm);
925 // Send buffer to root
926 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
928 // FIXME: check for errors
929 smpi_datatype_extent(recvtype, &lb, &recvext);
930 // Local copy from root
931 smpi_datatype_copy(sendbuf, sendcount, sendtype,
932 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
933 // Receive buffers from senders
934 requests = xbt_new(MPI_Request, size - 1);
936 for(src = 0; src < size; src++) {
938 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
940 src, system_tag, comm);
944 // Wait for completion of irecv's.
945 smpi_mpi_startall(size - 1, requests);
946 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
952 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
953 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
957 int rank = smpi_process_index();
960 /* arbitrarily choose root as rank 0 */
961 size = smpi_comm_size(comm);
963 displs = xbt_new(int, size);
964 for (i = 0; i < size; i++) {
966 count += recvcounts[i];
968 tmpbuf=(void*)xbt_malloc(count*smpi_datatype_get_extent(datatype));
969 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
970 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
971 recvcounts[rank], datatype, 0, comm);
976 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
977 void *recvbuf, int *recvcounts, int *displs,
978 MPI_Datatype recvtype, int root, MPI_Comm comm)
980 int system_tag = COLL_TAG_GATHERV;
981 int rank, size, src, index;
982 MPI_Aint lb = 0, recvext = 0;
983 MPI_Request *requests;
985 rank = smpi_comm_rank(comm);
986 size = smpi_comm_size(comm);
988 // Send buffer to root
989 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
991 // FIXME: check for errors
992 smpi_datatype_extent(recvtype, &lb, &recvext);
993 // Local copy from root
994 smpi_datatype_copy(sendbuf, sendcount, sendtype,
995 (char *)recvbuf + displs[root] * recvext,
996 recvcounts[root], recvtype);
997 // Receive buffers from senders
998 requests = xbt_new(MPI_Request, size - 1);
1000 for(src = 0; src < size; src++) {
1003 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1004 recvcounts[src], recvtype, src, system_tag, comm);
1008 // Wait for completion of irecv's.
1009 smpi_mpi_startall(size - 1, requests);
1010 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1015 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1016 MPI_Datatype sendtype, void *recvbuf,
1017 int recvcount, MPI_Datatype recvtype,
1020 int system_tag = COLL_TAG_ALLGATHER;
1021 int rank, size, other, index;
1022 MPI_Aint lb = 0, recvext = 0;
1023 MPI_Request *requests;
1025 rank = smpi_comm_rank(comm);
1026 size = smpi_comm_size(comm);
1027 // FIXME: check for errors
1028 smpi_datatype_extent(recvtype, &lb, &recvext);
1029 // Local copy from self
1030 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1031 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1033 // Send/Recv buffers to/from others;
1034 requests = xbt_new(MPI_Request, 2 * (size - 1));
1036 for(other = 0; other < size; other++) {
1039 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1042 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1043 recvcount, recvtype, other,
1048 // Wait for completion of all comms.
1049 smpi_mpi_startall(2 * (size - 1), requests);
1050 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1054 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1055 MPI_Datatype sendtype, void *recvbuf,
1056 int *recvcounts, int *displs,
1057 MPI_Datatype recvtype, MPI_Comm comm)
1059 int system_tag = COLL_TAG_ALLGATHERV;
1060 int rank, size, other, index;
1061 MPI_Aint lb = 0, recvext = 0;
1062 MPI_Request *requests;
1064 rank = smpi_comm_rank(comm);
1065 size = smpi_comm_size(comm);
1066 // FIXME: check for errors
1067 smpi_datatype_extent(recvtype, &lb, &recvext);
1068 // Local copy from self
1069 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1070 (char *)recvbuf + displs[rank] * recvext,
1071 recvcounts[rank], recvtype);
1072 // Send buffers to others;
1073 requests = xbt_new(MPI_Request, 2 * (size - 1));
1075 for(other = 0; other < size; other++) {
1078 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1082 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1083 recvtype, other, system_tag, comm);
1087 // Wait for completion of all comms.
1088 smpi_mpi_startall(2 * (size - 1), requests);
1089 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1093 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1094 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1095 int root, MPI_Comm comm)
1097 int system_tag = COLL_TAG_SCATTER;
1098 int rank, size, dst, index;
1099 MPI_Aint lb = 0, sendext = 0;
1100 MPI_Request *requests;
1102 rank = smpi_comm_rank(comm);
1103 size = smpi_comm_size(comm);
1105 // Recv buffer from root
1106 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1109 // FIXME: check for errors
1110 smpi_datatype_extent(sendtype, &lb, &sendext);
1111 // Local copy from root
1112 if(recvbuf!=MPI_IN_PLACE){
1113 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1114 sendcount, sendtype, recvbuf, recvcount, recvtype);
1116 // Send buffers to receivers
1117 requests = xbt_new(MPI_Request, size - 1);
1119 for(dst = 0; dst < size; dst++) {
1121 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1122 sendcount, sendtype, dst,
1127 // Wait for completion of isend's.
1128 smpi_mpi_startall(size - 1, requests);
1129 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1134 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1135 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1136 MPI_Datatype recvtype, int root, MPI_Comm comm)
1138 int system_tag = COLL_TAG_SCATTERV;
1139 int rank, size, dst, index;
1140 MPI_Aint lb = 0, sendext = 0;
1141 MPI_Request *requests;
1143 rank = smpi_comm_rank(comm);
1144 size = smpi_comm_size(comm);
1146 // Recv buffer from root
1147 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1150 // FIXME: check for errors
1151 smpi_datatype_extent(sendtype, &lb, &sendext);
1152 // Local copy from root
1153 if(recvbuf!=MPI_IN_PLACE){
1154 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1155 sendtype, recvbuf, recvcount, recvtype);
1157 // Send buffers to receivers
1158 requests = xbt_new(MPI_Request, size - 1);
1160 for(dst = 0; dst < size; dst++) {
1163 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1164 sendtype, dst, system_tag, comm);
1168 // Wait for completion of isend's.
1169 smpi_mpi_startall(size - 1, requests);
1170 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1175 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1176 MPI_Datatype datatype, MPI_Op op, int root,
1179 int system_tag = COLL_TAG_REDUCE;
1180 int rank, size, src, index;
1181 MPI_Aint lb = 0, dataext = 0;
1182 MPI_Request *requests;
1186 char* sendtmpbuf = (char*) sendbuf;
1187 if( sendbuf == MPI_IN_PLACE ) {
1188 sendtmpbuf = (char *)xbt_malloc(count*smpi_datatype_get_extent(datatype));
1189 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1192 rank = smpi_comm_rank(comm);
1193 size = smpi_comm_size(comm);
1194 //non commutative case, use a working algo from openmpi
1195 if(!smpi_op_is_commute(op)){
1196 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1197 datatype, op, root, comm);
1202 // Send buffer to root
1203 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1205 // FIXME: check for errors
1206 smpi_datatype_extent(datatype, &lb, &dataext);
1207 // Local copy from root
1208 if (sendtmpbuf && recvbuf)
1209 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1210 // Receive buffers from senders
1211 //TODO: make a MPI_barrier here ?
1212 requests = xbt_new(MPI_Request, size - 1);
1213 tmpbufs = xbt_new(void *, size - 1);
1215 for(src = 0; src < size; src++) {
1217 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1219 tmpbufs[index] = xbt_malloc(count * dataext);
1221 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1226 // Wait for completion of irecv's.
1227 smpi_mpi_startall(size - 1, requests);
1228 for(src = 0; src < size - 1; src++) {
1229 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1230 XBT_DEBUG("finished waiting any request with index %d", index);
1231 if(index == MPI_UNDEFINED) {
1234 if(op) /* op can be MPI_OP_NULL that does nothing */
1235 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1237 for(index = 0; index < size - 1; index++) {
1238 xbt_free(tmpbufs[index]);
1243 if( sendbuf == MPI_IN_PLACE ) {
1244 xbt_free(sendtmpbuf);
1249 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1250 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1252 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1253 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1256 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1257 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1259 int system_tag = -888;
1260 int rank, size, other, index;
1261 MPI_Aint lb = 0, dataext = 0;
1262 MPI_Request *requests;
1265 rank = smpi_comm_rank(comm);
1266 size = smpi_comm_size(comm);
1268 // FIXME: check for errors
1269 smpi_datatype_extent(datatype, &lb, &dataext);
1271 // Local copy from self
1272 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1274 // Send/Recv buffers to/from others;
1275 requests = xbt_new(MPI_Request, size - 1);
1276 tmpbufs = xbt_new(void *, rank);
1278 for(other = 0; other < rank; other++) {
1279 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1281 tmpbufs[index] = xbt_malloc(count * dataext);
1283 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1287 for(other = rank + 1; other < size; other++) {
1289 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1292 // Wait for completion of all comms.
1293 smpi_mpi_startall(size - 1, requests);
1295 if(smpi_op_is_commute(op)){
1296 for(other = 0; other < size - 1; other++) {
1297 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1298 if(index == MPI_UNDEFINED) {
1302 // #Request is below rank: it's a irecv
1303 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1307 //non commutative case, wait in order
1308 for(other = 0; other < size - 1; other++) {
1309 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1311 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1315 for(index = 0; index < rank; index++) {
1316 xbt_free(tmpbufs[index]);
1322 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1323 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1325 int system_tag = -888;
1326 int rank, size, other, index;
1327 MPI_Aint lb = 0, dataext = 0;
1328 MPI_Request *requests;
1330 int recvbuf_is_empty=1;
1331 rank = smpi_comm_rank(comm);
1332 size = smpi_comm_size(comm);
1334 // FIXME: check for errors
1335 smpi_datatype_extent(datatype, &lb, &dataext);
1337 // Send/Recv buffers to/from others;
1338 requests = xbt_new(MPI_Request, size - 1);
1339 tmpbufs = xbt_new(void *, rank);
1341 for(other = 0; other < rank; other++) {
1342 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1344 tmpbufs[index] = xbt_malloc(count * dataext);
1346 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1350 for(other = rank + 1; other < size; other++) {
1352 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1355 // Wait for completion of all comms.
1356 smpi_mpi_startall(size - 1, requests);
1357 if(smpi_op_is_commute(op)){
1358 for(other = 0; other < size - 1; other++) {
1359 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1360 if(index == MPI_UNDEFINED) {
1364 if(recvbuf_is_empty){
1365 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1368 // #Request is below rank: it's a irecv
1369 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1373 //non commutative case, wait in order
1374 for(other = 0; other < size - 1; other++) {
1375 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1377 if(recvbuf_is_empty){
1378 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1380 }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1384 for(index = 0; index < rank; index++) {
1385 xbt_free(tmpbufs[index]);