1 /* Copyright (c) 2007-2013. 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;
91 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
93 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
94 radical_elements = xbt_str_split(smpi_coef_string, ";");
95 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 : %f <= %ld return %f", size, fact.factor, current);
135 current=fact.values[0]+fact.values[1]*size;
138 XBT_DEBUG("os : %f > %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 : %f <= %ld return %f", size, fact.factor, current);
157 current=fact.values[0]+fact.values[1]*size;
160 XBT_DEBUG("ois : %f > %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 : %f <= %ld return %f", size, fact.factor, current);
179 current=fact.values[0]+fact.values[1]*size;
181 XBT_DEBUG("or : %f > %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)
190 MPI_Request request = NULL;
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;
222 request->real_src = 0;
224 request->truncated = 0;
225 request->real_size = 0;
226 request->real_tag = 0;
233 if (flags & SEND) smpi_datatype_unuse(datatype);
239 void smpi_empty_status(MPI_Status * status)
241 if(status != MPI_STATUS_IGNORE) {
242 status->MPI_SOURCE = MPI_ANY_SOURCE;
243 status->MPI_TAG = MPI_ANY_TAG;
244 status->MPI_ERROR = MPI_SUCCESS;
249 void smpi_action_trace_run(char *path)
253 xbt_dict_cursor_t cursor;
257 action_fp = fopen(path, "r");
258 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
262 if (!xbt_dict_is_empty(action_queues)) {
264 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
267 xbt_dict_foreach(action_queues, cursor, name, todo) {
268 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
274 xbt_dict_free(&action_queues);
275 action_queues = xbt_dict_new_homogeneous(NULL);
278 static void smpi_mpi_request_free_voidp(void* request)
280 MPI_Request req = request;
281 smpi_mpi_request_free(&req);
284 /* MPI Low level calls */
285 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
286 int dst, int tag, MPI_Comm comm)
288 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
289 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
290 comm, PERSISTENT | SEND | PREPARED);
295 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
296 int dst, int tag, MPI_Comm comm)
298 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
299 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
300 comm, PERSISTENT | SSEND | SEND | PREPARED);
305 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
306 int src, int tag, MPI_Comm comm)
308 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
309 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,
310 comm, PERSISTENT | RECV | PREPARED);
315 void smpi_mpi_start(MPI_Request request)
319 xbt_assert(!request->action, "Cannot (re)start a non-finished communication");
320 request->flags &= ~PREPARED;
321 if (request->flags & RECV) {
322 print_request("New recv", request);
323 //FIXME: if receive is posted with a large size, but send is smaller, mailboxes may not match !
324 if (request->size < sg_cfg_get_int("smpi/async_small_thres"))
325 mailbox = smpi_process_mailbox_small();
327 mailbox = smpi_process_mailbox();
328 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
329 request->real_size=request->size;
330 smpi_datatype_use(request->old_type);
331 smpi_comm_use(request->comm);
332 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
334 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
335 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
337 simcall_process_sleep(sleeptime);
338 XBT_DEBUG("receiving size of %zu : sleep %f ", request->size, smpi_or(request->size));
344 int receiver = request->dst;//smpi_group_index(smpi_comm_group(request->comm), request->dst);
347 int rank = smpi_process_index();
348 if (TRACE_smpi_view_internals()) {
349 TRACE_smpi_send(rank, rank, receiver,request->size);
352 /* if(receiver == MPI_UNDEFINED) {*/
353 /* XBT_WARN("Trying to send a message to a wrong rank");*/
356 print_request("New send", request);
357 if (request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
358 mailbox = smpi_process_remote_mailbox_small(receiver);
360 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
361 mailbox = smpi_process_remote_mailbox(receiver);
363 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
365 request->detached = 1;
367 if(request->old_type->has_subtype == 0){
368 oldbuf = request->buf;
369 if (oldbuf && request->size!=0){
370 request->buf = xbt_malloc(request->size);
371 memcpy(request->buf,oldbuf,request->size);
374 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
377 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
378 request->real_size=request->size;
379 smpi_datatype_use(request->old_type);
380 smpi_comm_use(request->comm);
382 //if we are giving back the control to the user without waiting for completion, we have to inject timings
383 double sleeptime =0.0;
384 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
385 //isend and send timings may be different
386 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
390 simcall_process_sleep(sleeptime);
391 XBT_DEBUG("sending size of %zu : sleep %f ", request->size, smpi_os(request->size));
395 simcall_comm_isend(mailbox, request->size, -1.0,
396 request->buf, request->real_size,
398 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
400 // detach if msg size < eager/rdv switch limit
404 /* FIXME: detached sends are not traceable (request->action == NULL) */
406 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
414 void smpi_mpi_startall(int count, MPI_Request * requests)
417 if(requests==NULL) return;
419 for(i = 0; i < count; i++) {
420 smpi_mpi_start(requests[i]);
424 void smpi_mpi_request_free(MPI_Request * request)
426 if((*request) != MPI_REQUEST_NULL){
427 (*request)->refcount--;
428 if((*request)->refcount<0) xbt_die("wrong refcount");
430 if((*request)->refcount==0){
431 print_request("Destroying", (*request));
433 *request = MPI_REQUEST_NULL;
435 print_request("Decrementing", (*request));
439 xbt_die("freeing an already free request");
443 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
444 int dst, int tag, MPI_Comm comm)
446 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
447 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
448 comm, NON_PERSISTENT | ISEND | SEND | PREPARED);
452 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
453 int dst, int tag, MPI_Comm comm)
455 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
456 request = build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
457 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 = NULL; /* MC needs the comm to be set to NULL during the call */
466 request = 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 = NULL; /* MC needs the comm to be set to NULL during the call */
478 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,
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 = NULL; /* MC needs the comm to be set to NULL during the call */
487 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,
488 comm, NON_PERSISTENT | RECV);
489 smpi_mpi_start(request);
493 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
494 int tag, MPI_Comm comm, MPI_Status * status)
496 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
497 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
498 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 = NULL; /* MC needs the comm to be set to NULL during the call */
508 request = 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);
511 smpi_mpi_start(request);
512 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
516 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
517 int dst, int tag, MPI_Comm comm)
519 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
520 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
521 comm, NON_PERSISTENT | SSEND | SEND);
523 smpi_mpi_start(request);
524 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
528 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
529 int dst, int sendtag, void *recvbuf, int recvcount,
530 MPI_Datatype recvtype, int src, int recvtag,
531 MPI_Comm comm, MPI_Status * status)
533 MPI_Request requests[2];
535 int myid=smpi_process_index();
536 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
537 smpi_datatype_copy(sendbuf, sendcount, sendtype,
538 recvbuf, recvcount, recvtype);
542 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
544 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
545 smpi_mpi_startall(2, requests);
546 smpi_mpi_waitall(2, requests, stats);
547 if(status != MPI_STATUS_IGNORE) {
548 // Copy receive status
553 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
555 return status->count / smpi_datatype_size(datatype);
558 static void finish_wait(MPI_Request * request, MPI_Status * status)
560 MPI_Request req = *request;
561 smpi_empty_status(status);
563 if(!(req->detached && req->flags & SEND) && !(req->flags & PREPARED)){
564 if(status != MPI_STATUS_IGNORE) {
565 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
566 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
567 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
568 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
569 // this handles the case were size in receive differs from size in send
570 // FIXME: really this should just contain the count of receive-type blocks,
572 status->count = req->real_size;
575 print_request("Finishing", req);
576 MPI_Datatype datatype = req->old_type;
578 if(datatype->has_subtype == 1){
579 // This part handles the problem of non-contignous memory
580 // the unserialization at the reception
581 s_smpi_subtype_t *subtype = datatype->substruct;
582 if(req->flags & RECV) {
583 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
585 if(req->detached == 0) free(req->buf);
587 smpi_comm_unuse(req->comm);
588 smpi_datatype_unuse(datatype);
593 if (TRACE_smpi_view_internals()) {
594 if(req->flags & RECV){
595 int rank = smpi_process_index();
596 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
597 TRACE_smpi_recv(rank, src_traced, rank);
602 if(req->detached_sender!=NULL){
603 smpi_mpi_request_free(&(req->detached_sender));
606 if(req->flags & NON_PERSISTENT) {
607 smpi_mpi_request_free(request);
613 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
616 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
617 smpi_empty_status(status);
619 if (!((*request)->flags & PREPARED)) {
620 if ((*request)->action != NULL)
621 flag = simcall_comm_test((*request)->action);
623 finish_wait(request, status);
624 *request = MPI_REQUEST_NULL;
630 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
637 *index = MPI_UNDEFINED;
639 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
640 map = xbt_new(int, count);
642 for(i = 0; i < count; i++) {
643 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action &&
644 !(requests[i]->flags & PREPARED)) {
645 xbt_dynar_push(comms, &requests[i]->action);
651 i = simcall_comm_testany(comms);
652 // not MPI_UNDEFINED, as this is a simix return code
655 finish_wait(&requests[*index], status);
656 requests[*index] = MPI_REQUEST_NULL;
660 //all requests are null or inactive, return true
662 smpi_empty_status(status);
665 xbt_dynar_free(&comms);
671 int smpi_mpi_testall(int count, MPI_Request requests[],
675 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
678 for(i=0; i<count; i++){
679 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
680 if (smpi_mpi_test(&requests[i], pstat)!=1){
683 requests[i]=MPI_REQUEST_NULL;
686 smpi_empty_status(pstat);
688 if(status != MPI_STATUSES_IGNORE) {
695 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
697 //FIXME find another wait to avoid busy waiting ?
698 // the issue here is that we have to wait on a nonexistent comm
700 smpi_mpi_iprobe(source, tag, comm, &flag, status);
701 XBT_DEBUG("Busy Waiting on probing : %d", flag);
705 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
707 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,
708 comm, NON_PERSISTENT | RECV);
710 //to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
711 double sleeptime= sg_cfg_get_double("smpi/iprobe");
712 //multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
713 static int nsleeps = 1;
715 simcall_process_sleep(sleeptime);
717 // behave like a receive, but don't do it
720 print_request("New iprobe", request);
721 // We have to test both mailboxes as we don't know if we will receive one one or another
722 if (sg_cfg_get_int("smpi/async_small_thres")>0){
723 mailbox = smpi_process_mailbox_small();
724 XBT_DEBUG("trying to probe the perm recv mailbox");
725 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
727 if (request->action==NULL){
728 mailbox = smpi_process_mailbox();
729 XBT_DEBUG("trying to probe the other mailbox");
730 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
734 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
736 if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
737 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
738 status->MPI_TAG = req->tag;
739 status->MPI_ERROR = MPI_SUCCESS;
740 status->count = req->real_size;
742 nsleeps=1;//reset the number of sleeps we will do next time
748 smpi_mpi_request_free(&request);
753 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
755 print_request("Waiting", *request);
756 if ((*request)->flags & PREPARED) {
757 smpi_empty_status(status);
761 if ((*request)->action != NULL) { // this is not a detached send
762 simcall_comm_wait((*request)->action, -1.0);
766 if(MC_is_active() && (*request)->action)
767 (*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
770 finish_wait(request, status);
771 *request = MPI_REQUEST_NULL;
772 // FIXME for a detached send, finish_wait is not called:
775 int smpi_mpi_waitany(int count, MPI_Request requests[],
782 index = MPI_UNDEFINED;
784 // Wait for a request to complete
785 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
786 map = xbt_new(int, count);
788 XBT_DEBUG("Wait for one of %d", count);
789 for(i = 0; i < count; i++) {
790 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
791 if (requests[i]->action != NULL) {
792 XBT_DEBUG("Waiting any %p ", requests[i]);
793 xbt_dynar_push(comms, &requests[i]->action);
797 //This is a finished detached request, let's return this one
798 size=0;//so we free the dynar but don't do the waitany call
800 finish_wait(&requests[i], status);//cleanup if refcount = 0
801 requests[i]=MPI_REQUEST_NULL;//set to null
807 i = simcall_comm_waitany(comms);
809 // not MPI_UNDEFINED, as this is a simix return code
812 finish_wait(&requests[index], status);
813 requests[index] = MPI_REQUEST_NULL;
817 xbt_dynar_free(&comms);
820 if (index==MPI_UNDEFINED)
821 smpi_empty_status(status);
826 int smpi_mpi_waitall(int count, MPI_Request requests[],
831 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
832 int retvalue = MPI_SUCCESS;
833 //tag invalid requests in the set
834 if (status != MPI_STATUSES_IGNORE) {
835 for (c = 0; c < count; c++) {
836 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ||
837 (requests[c]->flags & PREPARED)) {
838 smpi_empty_status(&status[c]);
839 } else if (requests[c]->src == MPI_PROC_NULL) {
840 smpi_empty_status(&status[c]);
841 status[c].MPI_SOURCE = MPI_PROC_NULL;
845 for(c = 0; c < count; c++) {
846 if (MC_is_active()) {
847 smpi_mpi_wait(&requests[c], pstat);
850 index = smpi_mpi_waitany(count, requests, pstat);
851 if (index == MPI_UNDEFINED)
853 requests[index]=MPI_REQUEST_NULL;
855 if (status != MPI_STATUSES_IGNORE) {
856 status[index] = *pstat;
857 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
858 retvalue = MPI_ERR_IN_STATUS;
865 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
870 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
873 for(i = 0; i < incount; i++)
875 index=smpi_mpi_waitany(incount, requests, pstat);
876 if(index!=MPI_UNDEFINED){
877 indices[count] = index;
879 if(status != MPI_STATUSES_IGNORE) {
880 status[index] = *pstat;
882 requests[index]=MPI_REQUEST_NULL;
884 return MPI_UNDEFINED;
890 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
893 int i, count, count_dead;
895 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
899 for(i = 0; i < incount; i++) {
900 if((requests[i] != MPI_REQUEST_NULL)) {
901 if(smpi_mpi_test(&requests[i], pstat)) {
904 if(status != MPI_STATUSES_IGNORE) {
907 requests[i]=MPI_REQUEST_NULL;
914 if(count_dead==incount)return MPI_UNDEFINED;
918 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
921 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
922 nary_tree_bcast(buf, count, datatype, root, comm, 4);
925 void smpi_mpi_barrier(MPI_Comm comm)
927 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
928 nary_tree_barrier(comm, 4);
931 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
932 void *recvbuf, int recvcount, MPI_Datatype recvtype,
933 int root, MPI_Comm comm)
935 int system_tag = COLL_TAG_GATHER;
936 int rank, size, src, index;
937 MPI_Aint lb = 0, recvext = 0;
938 MPI_Request *requests;
940 rank = smpi_comm_rank(comm);
941 size = smpi_comm_size(comm);
943 // Send buffer to root
944 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
946 // FIXME: check for errors
947 smpi_datatype_extent(recvtype, &lb, &recvext);
948 // Local copy from root
949 smpi_datatype_copy(sendbuf, sendcount, sendtype,
950 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
951 // Receive buffers from senders
952 requests = xbt_new(MPI_Request, size - 1);
954 for(src = 0; src < size; src++) {
956 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
958 src, system_tag, comm);
962 // Wait for completion of irecv's.
963 smpi_mpi_startall(size - 1, requests);
964 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
970 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
971 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
975 int rank = smpi_process_index();
978 /* arbitrarily choose root as rank 0 */
979 size = smpi_comm_size(comm);
981 displs = xbt_new(int, size);
982 for (i = 0; i < size; i++) {
984 count += recvcounts[i];
986 tmpbuf=(void*)xbt_malloc(count*smpi_datatype_get_extent(datatype));
987 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
988 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
989 recvcounts[rank], datatype, 0, comm);
994 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
995 void *recvbuf, int *recvcounts, int *displs,
996 MPI_Datatype recvtype, int root, MPI_Comm comm)
998 int system_tag = COLL_TAG_GATHERV;
999 int rank, size, src, index;
1000 MPI_Aint lb = 0, recvext = 0;
1001 MPI_Request *requests;
1003 rank = smpi_comm_rank(comm);
1004 size = smpi_comm_size(comm);
1006 // Send buffer to root
1007 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1009 // FIXME: check for errors
1010 smpi_datatype_extent(recvtype, &lb, &recvext);
1011 // Local copy from root
1012 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1013 (char *)recvbuf + displs[root] * recvext,
1014 recvcounts[root], recvtype);
1015 // Receive buffers from senders
1016 requests = xbt_new(MPI_Request, size - 1);
1018 for(src = 0; src < size; src++) {
1021 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1022 recvcounts[src], recvtype, src, system_tag, comm);
1026 // Wait for completion of irecv's.
1027 smpi_mpi_startall(size - 1, requests);
1028 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1033 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1034 MPI_Datatype sendtype, void *recvbuf,
1035 int recvcount, MPI_Datatype recvtype,
1038 int system_tag = COLL_TAG_ALLGATHER;
1039 int rank, size, other, index;
1040 MPI_Aint lb = 0, recvext = 0;
1041 MPI_Request *requests;
1043 rank = smpi_comm_rank(comm);
1044 size = smpi_comm_size(comm);
1045 // FIXME: check for errors
1046 smpi_datatype_extent(recvtype, &lb, &recvext);
1047 // Local copy from self
1048 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1049 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1051 // Send/Recv buffers to/from others;
1052 requests = xbt_new(MPI_Request, 2 * (size - 1));
1054 for(other = 0; other < size; other++) {
1057 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1060 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1061 recvcount, recvtype, other,
1066 // Wait for completion of all comms.
1067 smpi_mpi_startall(2 * (size - 1), requests);
1068 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1072 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1073 MPI_Datatype sendtype, void *recvbuf,
1074 int *recvcounts, int *displs,
1075 MPI_Datatype recvtype, MPI_Comm comm)
1077 int system_tag = COLL_TAG_ALLGATHERV;
1078 int rank, size, other, index;
1079 MPI_Aint lb = 0, recvext = 0;
1080 MPI_Request *requests;
1082 rank = smpi_comm_rank(comm);
1083 size = smpi_comm_size(comm);
1084 // FIXME: check for errors
1085 smpi_datatype_extent(recvtype, &lb, &recvext);
1086 // Local copy from self
1087 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1088 (char *)recvbuf + displs[rank] * recvext,
1089 recvcounts[rank], recvtype);
1090 // Send buffers to others;
1091 requests = xbt_new(MPI_Request, 2 * (size - 1));
1093 for(other = 0; other < size; other++) {
1096 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1100 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1101 recvtype, other, system_tag, comm);
1105 // Wait for completion of all comms.
1106 smpi_mpi_startall(2 * (size - 1), requests);
1107 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1111 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1112 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1113 int root, MPI_Comm comm)
1115 int system_tag = COLL_TAG_SCATTER;
1116 int rank, size, dst, index;
1117 MPI_Aint lb = 0, sendext = 0;
1118 MPI_Request *requests;
1120 rank = smpi_comm_rank(comm);
1121 size = smpi_comm_size(comm);
1123 // Recv buffer from root
1124 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1127 // FIXME: check for errors
1128 smpi_datatype_extent(sendtype, &lb, &sendext);
1129 // Local copy from root
1130 if(recvbuf!=MPI_IN_PLACE){
1131 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1132 sendcount, sendtype, recvbuf, recvcount, recvtype);
1134 // Send buffers to receivers
1135 requests = xbt_new(MPI_Request, size - 1);
1137 for(dst = 0; dst < size; dst++) {
1139 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1140 sendcount, sendtype, dst,
1145 // Wait for completion of isend's.
1146 smpi_mpi_startall(size - 1, requests);
1147 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1152 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1153 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1154 MPI_Datatype recvtype, int root, MPI_Comm comm)
1156 int system_tag = COLL_TAG_SCATTERV;
1157 int rank, size, dst, index;
1158 MPI_Aint lb = 0, sendext = 0;
1159 MPI_Request *requests;
1161 rank = smpi_comm_rank(comm);
1162 size = smpi_comm_size(comm);
1164 // Recv buffer from root
1165 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1168 // FIXME: check for errors
1169 smpi_datatype_extent(sendtype, &lb, &sendext);
1170 // Local copy from root
1171 if(recvbuf!=MPI_IN_PLACE){
1172 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1173 sendtype, recvbuf, recvcount, recvtype);
1175 // Send buffers to receivers
1176 requests = xbt_new(MPI_Request, size - 1);
1178 for(dst = 0; dst < size; dst++) {
1181 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1182 sendtype, dst, system_tag, comm);
1186 // Wait for completion of isend's.
1187 smpi_mpi_startall(size - 1, requests);
1188 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1193 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1194 MPI_Datatype datatype, MPI_Op op, int root,
1197 int system_tag = COLL_TAG_REDUCE;
1198 int rank, size, src, index;
1199 MPI_Aint lb = 0, dataext = 0;
1200 MPI_Request *requests;
1204 char* sendtmpbuf = (char*) sendbuf;
1205 if( sendbuf == MPI_IN_PLACE ) {
1206 sendtmpbuf = (char *)xbt_malloc(count*smpi_datatype_get_extent(datatype));
1207 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1210 rank = smpi_comm_rank(comm);
1211 size = smpi_comm_size(comm);
1212 //non commutative case, use a working algo from openmpi
1213 if(!smpi_op_is_commute(op)){
1214 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1215 datatype, op, root, comm);
1220 // Send buffer to root
1221 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1223 // FIXME: check for errors
1224 smpi_datatype_extent(datatype, &lb, &dataext);
1225 // Local copy from root
1226 if (sendtmpbuf && recvbuf)
1227 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1228 // Receive buffers from senders
1229 //TODO: make a MPI_barrier here ?
1230 requests = xbt_new(MPI_Request, size - 1);
1231 tmpbufs = xbt_new(void *, size - 1);
1233 for(src = 0; src < size; src++) {
1235 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1237 tmpbufs[index] = xbt_malloc(count * dataext);
1239 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1244 // Wait for completion of irecv's.
1245 smpi_mpi_startall(size - 1, requests);
1246 for(src = 0; src < size - 1; src++) {
1247 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1248 XBT_DEBUG("finished waiting any request with index %d", index);
1249 if(index == MPI_UNDEFINED) {
1252 if(op) /* op can be MPI_OP_NULL that does nothing */
1253 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1255 for(index = 0; index < size - 1; index++) {
1256 xbt_free(tmpbufs[index]);
1261 if( sendbuf == MPI_IN_PLACE ) {
1262 xbt_free(sendtmpbuf);
1267 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1268 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1270 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1271 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1274 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1275 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1277 int system_tag = -888;
1278 int rank, size, other, index;
1279 MPI_Aint lb = 0, dataext = 0;
1280 MPI_Request *requests;
1283 rank = smpi_comm_rank(comm);
1284 size = smpi_comm_size(comm);
1286 // FIXME: check for errors
1287 smpi_datatype_extent(datatype, &lb, &dataext);
1289 // Local copy from self
1290 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1292 // Send/Recv buffers to/from others;
1293 requests = xbt_new(MPI_Request, size - 1);
1294 tmpbufs = xbt_new(void *, rank);
1296 for(other = 0; other < rank; other++) {
1297 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1299 tmpbufs[index] = xbt_malloc(count * dataext);
1301 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1305 for(other = rank + 1; other < size; other++) {
1307 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1310 // Wait for completion of all comms.
1311 smpi_mpi_startall(size - 1, requests);
1313 if(smpi_op_is_commute(op)){
1314 for(other = 0; other < size - 1; other++) {
1315 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1316 if(index == MPI_UNDEFINED) {
1320 // #Request is below rank: it's a irecv
1321 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1325 //non commutative case, wait in order
1326 for(other = 0; other < size - 1; other++) {
1327 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1329 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1333 for(index = 0; index < rank; index++) {
1334 xbt_free(tmpbufs[index]);
1340 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1341 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1343 int system_tag = -888;
1344 int rank, size, other, index;
1345 MPI_Aint lb = 0, dataext = 0;
1346 MPI_Request *requests;
1348 int recvbuf_is_empty=1;
1349 rank = smpi_comm_rank(comm);
1350 size = smpi_comm_size(comm);
1352 // FIXME: check for errors
1353 smpi_datatype_extent(datatype, &lb, &dataext);
1355 // Send/Recv buffers to/from others;
1356 requests = xbt_new(MPI_Request, size - 1);
1357 tmpbufs = xbt_new(void *, rank);
1359 for(other = 0; other < rank; other++) {
1360 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1362 tmpbufs[index] = xbt_malloc(count * dataext);
1364 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1368 for(other = rank + 1; other < size; other++) {
1370 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1373 // Wait for completion of all comms.
1374 smpi_mpi_startall(size - 1, requests);
1375 if(smpi_op_is_commute(op)){
1376 for(other = 0; other < size - 1; other++) {
1377 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1378 if(index == MPI_UNDEFINED) {
1382 if(recvbuf_is_empty){
1383 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1386 // #Request is below rank: it's a irecv
1387 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1391 //non commutative case, wait in order
1392 for(other = 0; other < size - 1; other++) {
1393 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1395 if(recvbuf_is_empty){
1396 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1398 }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1402 for(index = 0; index < rank; index++) {
1403 xbt_free(tmpbufs[index]);