1 /* Copyright (c) 2007-2014. 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) ? 1 :
81 (((s_smpi_factor_t*)pa)->factor < ((s_smpi_factor_t*)pb)->factor) ? -1 : 0;
85 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
88 unsigned int iter = 0;
92 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
94 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
95 radical_elements = xbt_str_split(smpi_coef_string, ";");
96 xbt_dynar_foreach(radical_elements, iter, value) {
97 memset(&fact, 0, sizeof(s_smpi_factor_t));
98 radical_elements2 = xbt_str_split(value, ":");
99 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
100 xbt_die("Malformed radical for smpi factor!");
101 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
103 fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
105 fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
110 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
111 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
112 xbt_dynar_free(&radical_elements2);
114 xbt_dynar_free(&radical_elements);
116 xbt_dynar_sort(smpi_factor, &factor_cmp);
117 xbt_dynar_foreach(smpi_factor, iter, fact) {
118 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
123 static double smpi_os(double size)
125 if (!smpi_os_values) {
126 smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
127 smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
129 unsigned int iter = 0;
130 s_smpi_factor_t fact;
132 xbt_dynar_foreach(smpi_os_values, iter, fact) {
133 if (size <= fact.factor) {
134 XBT_DEBUG("os : %f <= %ld return %f", size, fact.factor, current);
137 current=fact.values[0]+fact.values[1]*size;
140 XBT_DEBUG("os : %f > %ld return %f", size, fact.factor, current);
145 static double smpi_ois(double size)
147 if (!smpi_ois_values) {
148 smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
149 smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
151 unsigned int iter = 0;
152 s_smpi_factor_t fact;
154 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
155 if (size <= fact.factor) {
156 XBT_DEBUG("ois : %f <= %ld return %f", size, fact.factor, current);
159 current=fact.values[0]+fact.values[1]*size;
162 XBT_DEBUG("ois : %f > %ld return %f", size, fact.factor, current);
167 static double smpi_or(double size)
169 if (!smpi_or_values) {
170 smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
171 smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
173 unsigned int iter = 0;
174 s_smpi_factor_t fact;
176 xbt_dynar_foreach(smpi_or_values, iter, fact) {
177 if (size <= fact.factor) {
178 XBT_DEBUG("or : %f <= %ld return %f", size, fact.factor, current);
181 current=fact.values[0]+fact.values[1]*size;
183 XBT_DEBUG("or : %f > %ld return %f", size, fact.factor, current);
188 static MPI_Request build_request(void *buf, int count,
189 MPI_Datatype datatype, int src, int dst,
190 int tag, MPI_Comm comm, unsigned flags)
192 MPI_Request request = NULL;
194 void *old_buf = NULL;
196 request = xbt_new(s_smpi_mpi_request_t, 1);
198 s_smpi_subtype_t *subtype = datatype->substruct;
200 if(datatype->has_subtype == 1){
201 // This part handles the problem of non-contiguous memory
203 buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
205 subtype->serialize(old_buf, buf, count, datatype->substruct);
210 // This part handles the problem of non-contiguous memory (for the
211 // unserialisation at the reception)
212 request->old_buf = old_buf;
213 request->old_type = datatype;
215 request->size = smpi_datatype_size(datatype) * count;
219 request->comm = comm;
220 request->action = NULL;
221 request->flags = flags;
222 request->detached = 0;
223 request->detached_sender = NULL;
224 request->real_src = 0;
226 request->truncated = 0;
227 request->real_size = 0;
228 request->real_tag = 0;
235 if (flags & SEND) smpi_datatype_unuse(datatype);
241 void smpi_empty_status(MPI_Status * status)
243 if(status != MPI_STATUS_IGNORE) {
244 status->MPI_SOURCE = MPI_ANY_SOURCE;
245 status->MPI_TAG = MPI_ANY_TAG;
246 status->MPI_ERROR = MPI_SUCCESS;
251 void smpi_action_trace_run(char *path)
255 xbt_dict_cursor_t cursor;
259 action_fp = fopen(path, "r");
260 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
264 if (!xbt_dict_is_empty(action_queues)) {
266 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
269 xbt_dict_foreach(action_queues, cursor, name, todo) {
270 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
276 xbt_dict_free(&action_queues);
277 action_queues = xbt_dict_new_homogeneous(NULL);
280 static void smpi_mpi_request_free_voidp(void* request)
282 MPI_Request req = request;
283 smpi_mpi_request_free(&req);
286 /* MPI Low level calls */
287 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
288 int dst, int tag, MPI_Comm comm)
290 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
291 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
292 comm, PERSISTENT | SEND | PREPARED);
297 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
298 int dst, int tag, MPI_Comm comm)
300 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
301 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
302 comm, PERSISTENT | SSEND | SEND | PREPARED);
307 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
308 int src, int tag, MPI_Comm comm)
310 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
311 request = 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,
312 comm, PERSISTENT | RECV | PREPARED);
317 void smpi_mpi_start(MPI_Request request)
321 xbt_assert(!request->action, "Cannot (re)start a non-finished communication");
322 request->flags &= ~PREPARED;
323 if (request->flags & RECV) {
324 print_request("New recv", request);
325 //FIXME: if receive is posted with a large size, but send is smaller, mailboxes may not match !
326 if (request->size < sg_cfg_get_int("smpi/async_small_thres"))
327 mailbox = smpi_process_mailbox_small();
329 mailbox = smpi_process_mailbox();
330 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
331 request->real_size=request->size;
332 smpi_datatype_use(request->old_type);
333 smpi_comm_use(request->comm);
334 request->action = simcall_comm_irecv(mailbox, request->buf,
335 &request->real_size, &match_recv,
338 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
339 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
341 simcall_process_sleep(sleeptime);
342 XBT_DEBUG("receiving size of %zu : sleep %f ", request->size, smpi_or(request->size));
348 int receiver = request->dst;//smpi_group_index(smpi_comm_group(request->comm), request->dst);
351 int rank = smpi_process_index();
352 if (TRACE_smpi_view_internals()) {
353 TRACE_smpi_send(rank, rank, receiver,request->size);
356 /* if(receiver == MPI_UNDEFINED) {*/
357 /* XBT_WARN("Trying to send a message to a wrong rank");*/
360 print_request("New send", request);
361 if (request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
362 mailbox = smpi_process_remote_mailbox_small(receiver);
364 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
365 mailbox = smpi_process_remote_mailbox(receiver);
367 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
369 request->detached = 1;
371 if(request->old_type->has_subtype == 0){
372 oldbuf = request->buf;
373 if (oldbuf && request->size!=0){
374 request->buf = xbt_malloc(request->size);
375 memcpy(request->buf,oldbuf,request->size);
378 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
381 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
382 request->real_size=request->size;
383 smpi_datatype_use(request->old_type);
384 smpi_comm_use(request->comm);
386 //if we are giving back the control to the user without waiting for completion, we have to inject timings
387 double sleeptime =0.0;
388 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
389 //isend and send timings may be different
390 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
394 simcall_process_sleep(sleeptime);
395 XBT_DEBUG("sending size of %zu : sleep %f ", request->size, smpi_os(request->size));
399 simcall_comm_isend(mailbox, request->size, -1.0,
400 request->buf, request->real_size,
402 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
404 // detach if msg size < eager/rdv switch limit
408 /* FIXME: detached sends are not traceable (request->action == NULL) */
410 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
418 void smpi_mpi_startall(int count, MPI_Request * requests)
421 if(requests==NULL) return;
423 for(i = 0; i < count; i++) {
424 smpi_mpi_start(requests[i]);
428 void smpi_mpi_request_free(MPI_Request * request)
430 if((*request) != MPI_REQUEST_NULL){
431 (*request)->refcount--;
432 if((*request)->refcount<0) xbt_die("wrong refcount");
434 if((*request)->refcount==0){
435 print_request("Destroying", (*request));
437 *request = MPI_REQUEST_NULL;
439 print_request("Decrementing", (*request));
443 xbt_die("freeing an already free request");
447 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
448 int dst, int tag, MPI_Comm comm)
450 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
451 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
452 comm, NON_PERSISTENT | ISEND | SEND | PREPARED);
456 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
457 int dst, int tag, MPI_Comm comm)
459 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
460 request = build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
461 comm, NON_PERSISTENT | ISEND | SEND);
462 smpi_mpi_start(request);
466 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
467 int dst, int tag, MPI_Comm comm)
469 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
470 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
471 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
472 smpi_mpi_start(request);
478 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
479 int src, int tag, MPI_Comm comm)
481 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
482 request = 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,
483 comm, NON_PERSISTENT | RECV | PREPARED);
487 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
488 int src, int tag, MPI_Comm comm)
490 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
491 request = 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,
492 comm, NON_PERSISTENT | RECV);
493 smpi_mpi_start(request);
497 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
498 int tag, MPI_Comm comm, MPI_Status * status)
500 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
501 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
502 smpi_mpi_wait(&request, status);
508 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
509 int tag, MPI_Comm comm)
511 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
512 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
513 comm, NON_PERSISTENT | SEND);
515 smpi_mpi_start(request);
516 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
520 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
521 int dst, int tag, MPI_Comm comm)
523 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
524 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
525 comm, NON_PERSISTENT | SSEND | SEND);
527 smpi_mpi_start(request);
528 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
532 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
533 int dst, int sendtag, void *recvbuf, int recvcount,
534 MPI_Datatype recvtype, int src, int recvtag,
535 MPI_Comm comm, MPI_Status * status)
537 MPI_Request requests[2];
539 int myid=smpi_process_index();
540 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
541 smpi_datatype_copy(sendbuf, sendcount, sendtype,
542 recvbuf, recvcount, recvtype);
546 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
548 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
549 smpi_mpi_startall(2, requests);
550 smpi_mpi_waitall(2, requests, stats);
551 if(status != MPI_STATUS_IGNORE) {
552 // Copy receive status
557 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
559 return status->count / smpi_datatype_size(datatype);
562 static void finish_wait(MPI_Request * request, MPI_Status * status)
564 MPI_Request req = *request;
565 smpi_empty_status(status);
567 if(!(req->detached && req->flags & SEND) && !(req->flags & PREPARED)){
568 if(status != MPI_STATUS_IGNORE) {
569 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
570 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
571 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
572 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
573 // this handles the case were size in receive differs from size in send
574 // FIXME: really this should just contain the count of receive-type blocks,
576 status->count = req->real_size;
579 print_request("Finishing", req);
580 MPI_Datatype datatype = req->old_type;
582 if(datatype->has_subtype == 1){
583 // This part handles the problem of non-contignous memory
584 // the unserialization at the reception
585 s_smpi_subtype_t *subtype = datatype->substruct;
586 if(req->flags & RECV) {
587 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
589 if(req->detached == 0) free(req->buf);
591 smpi_comm_unuse(req->comm);
592 smpi_datatype_unuse(datatype);
597 if (TRACE_smpi_view_internals()) {
598 if(req->flags & RECV){
599 int rank = smpi_process_index();
600 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
601 TRACE_smpi_recv(rank, src_traced, rank);
606 if(req->detached_sender!=NULL){
607 smpi_mpi_request_free(&(req->detached_sender));
610 if(req->flags & NON_PERSISTENT) {
611 smpi_mpi_request_free(request);
617 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
620 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
621 smpi_empty_status(status);
623 if (!((*request)->flags & PREPARED)) {
624 if ((*request)->action != NULL)
625 flag = simcall_comm_test((*request)->action);
627 finish_wait(request, status);
628 *request = MPI_REQUEST_NULL;
634 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
641 *index = MPI_UNDEFINED;
643 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
644 map = xbt_new(int, count);
646 for(i = 0; i < count; i++) {
647 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action &&
648 !(requests[i]->flags & PREPARED)) {
649 xbt_dynar_push(comms, &requests[i]->action);
655 i = simcall_comm_testany(comms);
656 // not MPI_UNDEFINED, as this is a simix return code
659 finish_wait(&requests[*index], status);
660 requests[*index] = MPI_REQUEST_NULL;
664 //all requests are null or inactive, return true
666 smpi_empty_status(status);
669 xbt_dynar_free(&comms);
675 int smpi_mpi_testall(int count, MPI_Request requests[],
679 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
682 for(i=0; i<count; i++){
683 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
684 if (smpi_mpi_test(&requests[i], pstat)!=1){
687 requests[i]=MPI_REQUEST_NULL;
690 smpi_empty_status(pstat);
692 if(status != MPI_STATUSES_IGNORE) {
699 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
701 //FIXME find another wait to avoid busy waiting ?
702 // the issue here is that we have to wait on a nonexistent comm
704 smpi_mpi_iprobe(source, tag, comm, &flag, status);
705 XBT_DEBUG("Busy Waiting on probing : %d", flag);
709 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
711 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,
712 comm, NON_PERSISTENT | RECV);
714 //to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
715 double sleeptime= sg_cfg_get_double("smpi/iprobe");
716 //multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
717 static int nsleeps = 1;
719 simcall_process_sleep(sleeptime);
721 // behave like a receive, but don't do it
724 print_request("New iprobe", request);
725 // We have to test both mailboxes as we don't know if we will receive one one or another
726 if (sg_cfg_get_int("smpi/async_small_thres")>0){
727 mailbox = smpi_process_mailbox_small();
728 XBT_DEBUG("trying to probe the perm recv mailbox");
729 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
731 if (request->action==NULL){
732 mailbox = smpi_process_mailbox();
733 XBT_DEBUG("trying to probe the other mailbox");
734 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
738 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
740 if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
741 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
742 status->MPI_TAG = req->tag;
743 status->MPI_ERROR = MPI_SUCCESS;
744 status->count = req->real_size;
746 nsleeps=1;//reset the number of sleeps we will do next time
752 smpi_mpi_request_free(&request);
757 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
759 print_request("Waiting", *request);
760 if ((*request)->flags & PREPARED) {
761 smpi_empty_status(status);
765 if ((*request)->action != NULL) { // this is not a detached send
766 simcall_comm_wait((*request)->action, -1.0);
771 (*request)->action->comm.dst_data = NULL; // dangling pointer : dst_data is freed with a wait, need to set it to NULL for system state comparison
774 finish_wait(request, status);
775 *request = MPI_REQUEST_NULL;
776 // FIXME for a detached send, finish_wait is not called:
779 int smpi_mpi_waitany(int count, MPI_Request requests[],
786 index = MPI_UNDEFINED;
788 // Wait for a request to complete
789 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
790 map = xbt_new(int, count);
792 XBT_DEBUG("Wait for one of %d", count);
793 for(i = 0; i < count; i++) {
794 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
795 if (requests[i]->action != NULL) {
796 XBT_DEBUG("Waiting any %p ", requests[i]);
797 xbt_dynar_push(comms, &requests[i]->action);
801 //This is a finished detached request, let's return this one
802 size=0;//so we free the dynar but don't do the waitany call
804 finish_wait(&requests[i], status);//cleanup if refcount = 0
805 requests[i]=MPI_REQUEST_NULL;//set to null
811 i = simcall_comm_waitany(comms);
813 // not MPI_UNDEFINED, as this is a simix return code
816 finish_wait(&requests[index], status);
817 requests[index] = MPI_REQUEST_NULL;
821 xbt_dynar_free(&comms);
824 if (index==MPI_UNDEFINED)
825 smpi_empty_status(status);
830 int smpi_mpi_waitall(int count, MPI_Request requests[],
835 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
836 int retvalue = MPI_SUCCESS;
837 //tag invalid requests in the set
838 if (status != MPI_STATUSES_IGNORE) {
839 for (c = 0; c < count; c++) {
840 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ||
841 (requests[c]->flags & PREPARED)) {
842 smpi_empty_status(&status[c]);
843 } else if (requests[c]->src == MPI_PROC_NULL) {
844 smpi_empty_status(&status[c]);
845 status[c].MPI_SOURCE = MPI_PROC_NULL;
849 for(c = 0; c < count; c++) {
850 if (MC_is_active()) {
851 smpi_mpi_wait(&requests[c], pstat);
854 index = smpi_mpi_waitany(count, requests, pstat);
855 if (index == MPI_UNDEFINED)
857 requests[index]=MPI_REQUEST_NULL;
859 if (status != MPI_STATUSES_IGNORE) {
860 status[index] = *pstat;
861 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
862 retvalue = MPI_ERR_IN_STATUS;
869 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
874 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
877 for(i = 0; i < incount; i++)
879 index=smpi_mpi_waitany(incount, requests, pstat);
880 if(index!=MPI_UNDEFINED){
881 indices[count] = index;
883 if(status != MPI_STATUSES_IGNORE) {
884 status[index] = *pstat;
886 requests[index]=MPI_REQUEST_NULL;
888 return MPI_UNDEFINED;
894 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
897 int i, count, count_dead;
899 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
903 for(i = 0; i < incount; i++) {
904 if((requests[i] != MPI_REQUEST_NULL)) {
905 if(smpi_mpi_test(&requests[i], pstat)) {
908 if(status != MPI_STATUSES_IGNORE) {
911 requests[i]=MPI_REQUEST_NULL;
918 if(count_dead==incount)return MPI_UNDEFINED;
922 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
925 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
926 nary_tree_bcast(buf, count, datatype, root, comm, 4);
929 void smpi_mpi_barrier(MPI_Comm comm)
931 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
932 nary_tree_barrier(comm, 4);
935 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
936 void *recvbuf, int recvcount, MPI_Datatype recvtype,
937 int root, MPI_Comm comm)
939 int system_tag = COLL_TAG_GATHER;
940 int rank, size, src, index;
941 MPI_Aint lb = 0, recvext = 0;
942 MPI_Request *requests;
944 rank = smpi_comm_rank(comm);
945 size = smpi_comm_size(comm);
947 // Send buffer to root
948 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
950 // FIXME: check for errors
951 smpi_datatype_extent(recvtype, &lb, &recvext);
952 // Local copy from root
953 smpi_datatype_copy(sendbuf, sendcount, sendtype,
954 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
955 // Receive buffers from senders
956 requests = xbt_new(MPI_Request, size - 1);
958 for(src = 0; src < size; src++) {
960 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
962 src, system_tag, comm);
966 // Wait for completion of irecv's.
967 smpi_mpi_startall(size - 1, requests);
968 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
974 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
975 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
979 int rank = smpi_process_index();
982 /* arbitrarily choose root as rank 0 */
983 size = smpi_comm_size(comm);
985 displs = xbt_new(int, size);
986 for (i = 0; i < size; i++) {
988 count += recvcounts[i];
990 tmpbuf=(void*)xbt_malloc(count*smpi_datatype_get_extent(datatype));
991 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
992 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
993 recvcounts[rank], datatype, 0, comm);
998 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
999 void *recvbuf, int *recvcounts, int *displs,
1000 MPI_Datatype recvtype, int root, MPI_Comm comm)
1002 int system_tag = COLL_TAG_GATHERV;
1003 int rank, size, src, index;
1004 MPI_Aint lb = 0, recvext = 0;
1005 MPI_Request *requests;
1007 rank = smpi_comm_rank(comm);
1008 size = smpi_comm_size(comm);
1010 // Send buffer to root
1011 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1013 // FIXME: check for errors
1014 smpi_datatype_extent(recvtype, &lb, &recvext);
1015 // Local copy from root
1016 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1017 (char *)recvbuf + displs[root] * recvext,
1018 recvcounts[root], recvtype);
1019 // Receive buffers from senders
1020 requests = xbt_new(MPI_Request, size - 1);
1022 for(src = 0; src < size; src++) {
1025 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1026 recvcounts[src], recvtype, src, system_tag, comm);
1030 // Wait for completion of irecv's.
1031 smpi_mpi_startall(size - 1, requests);
1032 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1037 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1038 MPI_Datatype sendtype, void *recvbuf,
1039 int recvcount, MPI_Datatype recvtype,
1042 int system_tag = COLL_TAG_ALLGATHER;
1043 int rank, size, other, index;
1044 MPI_Aint lb = 0, recvext = 0;
1045 MPI_Request *requests;
1047 rank = smpi_comm_rank(comm);
1048 size = smpi_comm_size(comm);
1049 // FIXME: check for errors
1050 smpi_datatype_extent(recvtype, &lb, &recvext);
1051 // Local copy from self
1052 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1053 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1055 // Send/Recv buffers to/from others;
1056 requests = xbt_new(MPI_Request, 2 * (size - 1));
1058 for(other = 0; other < size; other++) {
1061 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1064 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1065 recvcount, recvtype, other,
1070 // Wait for completion of all comms.
1071 smpi_mpi_startall(2 * (size - 1), requests);
1072 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1076 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1077 MPI_Datatype sendtype, void *recvbuf,
1078 int *recvcounts, int *displs,
1079 MPI_Datatype recvtype, MPI_Comm comm)
1081 int system_tag = COLL_TAG_ALLGATHERV;
1082 int rank, size, other, index;
1083 MPI_Aint lb = 0, recvext = 0;
1084 MPI_Request *requests;
1086 rank = smpi_comm_rank(comm);
1087 size = smpi_comm_size(comm);
1088 // FIXME: check for errors
1089 smpi_datatype_extent(recvtype, &lb, &recvext);
1090 // Local copy from self
1091 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1092 (char *)recvbuf + displs[rank] * recvext,
1093 recvcounts[rank], recvtype);
1094 // Send buffers to others;
1095 requests = xbt_new(MPI_Request, 2 * (size - 1));
1097 for(other = 0; other < size; other++) {
1100 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1104 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1105 recvtype, other, system_tag, comm);
1109 // Wait for completion of all comms.
1110 smpi_mpi_startall(2 * (size - 1), requests);
1111 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1115 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1116 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1117 int root, MPI_Comm comm)
1119 int system_tag = COLL_TAG_SCATTER;
1120 int rank, size, dst, index;
1121 MPI_Aint lb = 0, sendext = 0;
1122 MPI_Request *requests;
1124 rank = smpi_comm_rank(comm);
1125 size = smpi_comm_size(comm);
1127 // Recv buffer from root
1128 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1131 // FIXME: check for errors
1132 smpi_datatype_extent(sendtype, &lb, &sendext);
1133 // Local copy from root
1134 if(recvbuf!=MPI_IN_PLACE){
1135 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1136 sendcount, sendtype, recvbuf, recvcount, recvtype);
1138 // Send buffers to receivers
1139 requests = xbt_new(MPI_Request, size - 1);
1141 for(dst = 0; dst < size; dst++) {
1143 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1144 sendcount, sendtype, dst,
1149 // Wait for completion of isend's.
1150 smpi_mpi_startall(size - 1, requests);
1151 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1156 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1157 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1158 MPI_Datatype recvtype, int root, MPI_Comm comm)
1160 int system_tag = COLL_TAG_SCATTERV;
1161 int rank, size, dst, index;
1162 MPI_Aint lb = 0, sendext = 0;
1163 MPI_Request *requests;
1165 rank = smpi_comm_rank(comm);
1166 size = smpi_comm_size(comm);
1168 // Recv buffer from root
1169 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1172 // FIXME: check for errors
1173 smpi_datatype_extent(sendtype, &lb, &sendext);
1174 // Local copy from root
1175 if(recvbuf!=MPI_IN_PLACE){
1176 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1177 sendtype, recvbuf, recvcount, recvtype);
1179 // Send buffers to receivers
1180 requests = xbt_new(MPI_Request, size - 1);
1182 for(dst = 0; dst < size; dst++) {
1185 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1186 sendtype, dst, system_tag, comm);
1190 // Wait for completion of isend's.
1191 smpi_mpi_startall(size - 1, requests);
1192 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1197 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1198 MPI_Datatype datatype, MPI_Op op, int root,
1201 int system_tag = COLL_TAG_REDUCE;
1202 int rank, size, src, index;
1203 MPI_Aint lb = 0, dataext = 0;
1204 MPI_Request *requests;
1208 char* sendtmpbuf = (char*) sendbuf;
1209 if( sendbuf == MPI_IN_PLACE ) {
1210 sendtmpbuf = (char *)xbt_malloc(count*smpi_datatype_get_extent(datatype));
1211 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1214 rank = smpi_comm_rank(comm);
1215 size = smpi_comm_size(comm);
1216 //non commutative case, use a working algo from openmpi
1217 if(!smpi_op_is_commute(op)){
1218 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1219 datatype, op, root, comm);
1224 // Send buffer to root
1225 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1227 // FIXME: check for errors
1228 smpi_datatype_extent(datatype, &lb, &dataext);
1229 // Local copy from root
1230 if (sendtmpbuf && recvbuf)
1231 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1232 // Receive buffers from senders
1233 //TODO: make a MPI_barrier here ?
1234 requests = xbt_new(MPI_Request, size - 1);
1235 tmpbufs = xbt_new(void *, size - 1);
1237 for(src = 0; src < size; src++) {
1239 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1241 tmpbufs[index] = xbt_malloc(count * dataext);
1243 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1248 // Wait for completion of irecv's.
1249 smpi_mpi_startall(size - 1, requests);
1250 for(src = 0; src < size - 1; src++) {
1251 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1252 XBT_DEBUG("finished waiting any request with index %d", index);
1253 if(index == MPI_UNDEFINED) {
1256 if(op) /* op can be MPI_OP_NULL that does nothing */
1257 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1259 for(index = 0; index < size - 1; index++) {
1260 xbt_free(tmpbufs[index]);
1265 if( sendbuf == MPI_IN_PLACE ) {
1266 xbt_free(sendtmpbuf);
1271 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1272 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1274 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1275 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1278 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1279 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1281 int system_tag = -888;
1282 int rank, size, other, index;
1283 MPI_Aint lb = 0, dataext = 0;
1284 MPI_Request *requests;
1287 rank = smpi_comm_rank(comm);
1288 size = smpi_comm_size(comm);
1290 // FIXME: check for errors
1291 smpi_datatype_extent(datatype, &lb, &dataext);
1293 // Local copy from self
1294 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1296 // Send/Recv buffers to/from others;
1297 requests = xbt_new(MPI_Request, size - 1);
1298 tmpbufs = xbt_new(void *, rank);
1300 for(other = 0; other < rank; other++) {
1301 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1303 tmpbufs[index] = xbt_malloc(count * dataext);
1305 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1309 for(other = rank + 1; other < size; other++) {
1311 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1314 // Wait for completion of all comms.
1315 smpi_mpi_startall(size - 1, requests);
1317 if(smpi_op_is_commute(op)){
1318 for(other = 0; other < size - 1; other++) {
1319 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1320 if(index == MPI_UNDEFINED) {
1324 // #Request is below rank: it's a irecv
1325 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1329 //non commutative case, wait in order
1330 for(other = 0; other < size - 1; other++) {
1331 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1333 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1337 for(index = 0; index < rank; index++) {
1338 xbt_free(tmpbufs[index]);
1344 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1345 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1347 int system_tag = -888;
1348 int rank, size, other, index;
1349 MPI_Aint lb = 0, dataext = 0;
1350 MPI_Request *requests;
1352 int recvbuf_is_empty=1;
1353 rank = smpi_comm_rank(comm);
1354 size = smpi_comm_size(comm);
1356 // FIXME: check for errors
1357 smpi_datatype_extent(datatype, &lb, &dataext);
1359 // Send/Recv buffers to/from others;
1360 requests = xbt_new(MPI_Request, size - 1);
1361 tmpbufs = xbt_new(void *, rank);
1363 for(other = 0; other < rank; other++) {
1364 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1366 tmpbufs[index] = xbt_malloc(count * dataext);
1368 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1372 for(other = rank + 1; other < size; other++) {
1374 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1377 // Wait for completion of all comms.
1378 smpi_mpi_startall(size - 1, requests);
1379 if(smpi_op_is_commute(op)){
1380 for(other = 0; other < size - 1; other++) {
1381 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1382 if(index == MPI_UNDEFINED) {
1386 if(recvbuf_is_empty){
1387 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1390 // #Request is below rank: it's a irecv
1391 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1395 //non commutative case, wait in order
1396 for(other = 0; other < size - 1; other++) {
1397 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1399 if(recvbuf_is_empty){
1400 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1402 }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1406 for(index = 0; index < rank; index++) {
1407 xbt_free(tmpbufs[index]);