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 memset(&fact, 0, sizeof(s_smpi_factor_t));
97 radical_elements2 = xbt_str_split(value, ":");
98 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
99 xbt_die("Malformed radical for smpi factor!");
100 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
102 fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
104 fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
109 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
110 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
111 xbt_dynar_free(&radical_elements2);
113 xbt_dynar_free(&radical_elements);
115 xbt_dynar_sort(smpi_factor, &factor_cmp);
116 xbt_dynar_foreach(smpi_factor, iter, fact) {
117 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
122 static double smpi_os(double size)
124 if (!smpi_os_values) {
125 smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
126 smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
128 unsigned int iter = 0;
129 s_smpi_factor_t fact;
131 xbt_dynar_foreach(smpi_os_values, iter, fact) {
132 if (size <= fact.factor) {
133 XBT_DEBUG("os : %f <= %ld return %f", size, fact.factor, current);
136 current=fact.values[0]+fact.values[1]*size;
139 XBT_DEBUG("os : %f > %ld return %f", size, fact.factor, current);
144 static double smpi_ois(double size)
146 if (!smpi_ois_values) {
147 smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
148 smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
150 unsigned int iter = 0;
151 s_smpi_factor_t fact;
153 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
154 if (size <= fact.factor) {
155 XBT_DEBUG("ois : %f <= %ld return %f", size, fact.factor, current);
158 current=fact.values[0]+fact.values[1]*size;
161 XBT_DEBUG("ois : %f > %ld return %f", size, fact.factor, current);
166 static double smpi_or(double size)
168 if (!smpi_or_values) {
169 smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
170 smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
172 unsigned int iter = 0;
173 s_smpi_factor_t fact;
175 xbt_dynar_foreach(smpi_or_values, iter, fact) {
176 if (size <= fact.factor) {
177 XBT_DEBUG("or : %f <= %ld return %f", size, fact.factor, current);
180 current=fact.values[0]+fact.values[1]*size;
182 XBT_DEBUG("or : %f > %ld return %f", size, fact.factor, current);
187 static MPI_Request build_request(void *buf, int count,
188 MPI_Datatype datatype, int src, int dst,
189 int tag, MPI_Comm comm, unsigned flags)
191 MPI_Request request = NULL;
193 void *old_buf = NULL;
195 request = xbt_new(s_smpi_mpi_request_t, 1);
197 s_smpi_subtype_t *subtype = datatype->substruct;
199 if(datatype->has_subtype == 1){
200 // This part handles the problem of non-contiguous memory
202 buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
204 subtype->serialize(old_buf, buf, count, datatype->substruct);
209 // This part handles the problem of non-contiguous memory (for the
210 // unserialisation at the reception)
211 request->old_buf = old_buf;
212 request->old_type = datatype;
214 request->size = smpi_datatype_size(datatype) * count;
218 request->comm = comm;
219 request->action = NULL;
220 request->flags = flags;
221 request->detached = 0;
222 request->detached_sender = NULL;
223 request->real_src = 0;
225 request->truncated = 0;
226 request->real_size = 0;
227 request->real_tag = 0;
234 if (flags & SEND) smpi_datatype_unuse(datatype);
240 void smpi_empty_status(MPI_Status * status)
242 if(status != MPI_STATUS_IGNORE) {
243 status->MPI_SOURCE = MPI_ANY_SOURCE;
244 status->MPI_TAG = MPI_ANY_TAG;
245 status->MPI_ERROR = MPI_SUCCESS;
250 void smpi_action_trace_run(char *path)
254 xbt_dict_cursor_t cursor;
258 action_fp = fopen(path, "r");
259 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
263 if (!xbt_dict_is_empty(action_queues)) {
265 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
268 xbt_dict_foreach(action_queues, cursor, name, todo) {
269 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
275 xbt_dict_free(&action_queues);
276 action_queues = xbt_dict_new_homogeneous(NULL);
279 static void smpi_mpi_request_free_voidp(void* request)
281 MPI_Request req = request;
282 smpi_mpi_request_free(&req);
285 /* MPI Low level calls */
286 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
287 int dst, int tag, MPI_Comm comm)
289 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
290 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
291 comm, PERSISTENT | SEND | PREPARED);
296 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
297 int dst, int tag, MPI_Comm comm)
299 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
300 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
301 comm, PERSISTENT | SSEND | SEND | PREPARED);
306 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
307 int src, int tag, MPI_Comm comm)
309 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
310 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,
311 comm, PERSISTENT | RECV | PREPARED);
316 void smpi_mpi_start(MPI_Request request)
320 xbt_assert(!request->action, "Cannot (re)start a non-finished communication");
321 request->flags &= ~PREPARED;
322 if (request->flags & RECV) {
323 print_request("New recv", request);
324 //FIXME: if receive is posted with a large size, but send is smaller, mailboxes may not match !
325 if (request->size < sg_cfg_get_int("smpi/async_small_thres"))
326 mailbox = smpi_process_mailbox_small();
328 mailbox = smpi_process_mailbox();
329 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
330 request->real_size=request->size;
331 smpi_datatype_use(request->old_type);
332 smpi_comm_use(request->comm);
333 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
335 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
336 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
338 simcall_process_sleep(sleeptime);
339 XBT_DEBUG("receiving size of %zu : sleep %f ", request->size, smpi_or(request->size));
345 int receiver = request->dst;//smpi_group_index(smpi_comm_group(request->comm), request->dst);
348 int rank = smpi_process_index();
349 if (TRACE_smpi_view_internals()) {
350 TRACE_smpi_send(rank, rank, receiver,request->size);
353 /* if(receiver == MPI_UNDEFINED) {*/
354 /* XBT_WARN("Trying to send a message to a wrong rank");*/
357 print_request("New send", request);
358 if (request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
359 mailbox = smpi_process_remote_mailbox_small(receiver);
361 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
362 mailbox = smpi_process_remote_mailbox(receiver);
364 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
366 request->detached = 1;
368 if(request->old_type->has_subtype == 0){
369 oldbuf = request->buf;
370 if (oldbuf && request->size!=0){
371 request->buf = xbt_malloc(request->size);
372 memcpy(request->buf,oldbuf,request->size);
375 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
378 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
379 request->real_size=request->size;
380 smpi_datatype_use(request->old_type);
381 smpi_comm_use(request->comm);
383 //if we are giving back the control to the user without waiting for completion, we have to inject timings
384 double sleeptime =0.0;
385 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
386 //isend and send timings may be different
387 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
391 simcall_process_sleep(sleeptime);
392 XBT_DEBUG("sending size of %zu : sleep %f ", request->size, smpi_os(request->size));
396 simcall_comm_isend(mailbox, request->size, -1.0,
397 request->buf, request->real_size,
399 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
401 // detach if msg size < eager/rdv switch limit
405 /* FIXME: detached sends are not traceable (request->action == NULL) */
407 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
415 void smpi_mpi_startall(int count, MPI_Request * requests)
418 if(requests==NULL) return;
420 for(i = 0; i < count; i++) {
421 smpi_mpi_start(requests[i]);
425 void smpi_mpi_request_free(MPI_Request * request)
427 if((*request) != MPI_REQUEST_NULL){
428 (*request)->refcount--;
429 if((*request)->refcount<0) xbt_die("wrong refcount");
431 if((*request)->refcount==0){
432 print_request("Destroying", (*request));
434 *request = MPI_REQUEST_NULL;
436 print_request("Decrementing", (*request));
440 xbt_die("freeing an already free request");
444 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
445 int dst, int tag, MPI_Comm comm)
447 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
448 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
449 comm, NON_PERSISTENT | ISEND | SEND | PREPARED);
453 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
454 int dst, int tag, MPI_Comm comm)
456 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
457 request = build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
458 comm, NON_PERSISTENT | ISEND | SEND);
459 smpi_mpi_start(request);
463 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
464 int dst, int tag, MPI_Comm comm)
466 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
467 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
468 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
469 smpi_mpi_start(request);
475 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
476 int src, int tag, MPI_Comm comm)
478 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
479 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,
480 comm, NON_PERSISTENT | RECV | PREPARED);
484 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
485 int src, int tag, MPI_Comm comm)
487 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
488 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,
489 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)
497 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
498 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
499 smpi_mpi_wait(&request, status);
505 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
506 int tag, MPI_Comm comm)
508 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
509 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
510 comm, NON_PERSISTENT | SEND);
512 smpi_mpi_start(request);
513 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
517 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
518 int dst, int tag, MPI_Comm comm)
520 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
521 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
522 comm, NON_PERSISTENT | SSEND | SEND);
524 smpi_mpi_start(request);
525 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
529 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
530 int dst, int sendtag, void *recvbuf, int recvcount,
531 MPI_Datatype recvtype, int src, int recvtag,
532 MPI_Comm comm, MPI_Status * status)
534 MPI_Request requests[2];
536 int myid=smpi_process_index();
537 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
538 smpi_datatype_copy(sendbuf, sendcount, sendtype,
539 recvbuf, recvcount, recvtype);
543 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
545 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
546 smpi_mpi_startall(2, requests);
547 smpi_mpi_waitall(2, requests, stats);
548 if(status != MPI_STATUS_IGNORE) {
549 // Copy receive status
554 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
556 return status->count / smpi_datatype_size(datatype);
559 static void finish_wait(MPI_Request * request, MPI_Status * status)
561 MPI_Request req = *request;
562 smpi_empty_status(status);
564 if(!(req->detached && req->flags & SEND) && !(req->flags & PREPARED)){
565 if(status != MPI_STATUS_IGNORE) {
566 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
567 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
568 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
569 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
570 // this handles the case were size in receive differs from size in send
571 // FIXME: really this should just contain the count of receive-type blocks,
573 status->count = req->real_size;
576 print_request("Finishing", req);
577 MPI_Datatype datatype = req->old_type;
579 if(datatype->has_subtype == 1){
580 // This part handles the problem of non-contignous memory
581 // the unserialization at the reception
582 s_smpi_subtype_t *subtype = datatype->substruct;
583 if(req->flags & RECV) {
584 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
586 if(req->detached == 0) free(req->buf);
588 smpi_comm_unuse(req->comm);
589 smpi_datatype_unuse(datatype);
594 if (TRACE_smpi_view_internals()) {
595 if(req->flags & RECV){
596 int rank = smpi_process_index();
597 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
598 TRACE_smpi_recv(rank, src_traced, rank);
603 if(req->detached_sender!=NULL){
604 smpi_mpi_request_free(&(req->detached_sender));
607 if(req->flags & NON_PERSISTENT) {
608 smpi_mpi_request_free(request);
614 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
617 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
618 smpi_empty_status(status);
620 if (!((*request)->flags & PREPARED)) {
621 if ((*request)->action != NULL)
622 flag = simcall_comm_test((*request)->action);
624 finish_wait(request, status);
625 *request = MPI_REQUEST_NULL;
631 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
638 *index = MPI_UNDEFINED;
640 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
641 map = xbt_new(int, count);
643 for(i = 0; i < count; i++) {
644 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action &&
645 !(requests[i]->flags & PREPARED)) {
646 xbt_dynar_push(comms, &requests[i]->action);
652 i = simcall_comm_testany(comms);
653 // not MPI_UNDEFINED, as this is a simix return code
656 finish_wait(&requests[*index], status);
657 requests[*index] = MPI_REQUEST_NULL;
661 //all requests are null or inactive, return true
663 smpi_empty_status(status);
666 xbt_dynar_free(&comms);
672 int smpi_mpi_testall(int count, MPI_Request requests[],
676 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
679 for(i=0; i<count; i++){
680 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
681 if (smpi_mpi_test(&requests[i], pstat)!=1){
684 requests[i]=MPI_REQUEST_NULL;
687 smpi_empty_status(pstat);
689 if(status != MPI_STATUSES_IGNORE) {
696 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
698 //FIXME find another wait to avoid busy waiting ?
699 // the issue here is that we have to wait on a nonexistent comm
701 smpi_mpi_iprobe(source, tag, comm, &flag, status);
702 XBT_DEBUG("Busy Waiting on probing : %d", flag);
706 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
708 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,
709 comm, NON_PERSISTENT | RECV);
711 //to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
712 double sleeptime= sg_cfg_get_double("smpi/iprobe");
713 //multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
714 static int nsleeps = 1;
716 simcall_process_sleep(sleeptime);
718 // behave like a receive, but don't do it
721 print_request("New iprobe", request);
722 // We have to test both mailboxes as we don't know if we will receive one one or another
723 if (sg_cfg_get_int("smpi/async_small_thres")>0){
724 mailbox = smpi_process_mailbox_small();
725 XBT_DEBUG("trying to probe the perm recv mailbox");
726 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
728 if (request->action==NULL){
729 mailbox = smpi_process_mailbox();
730 XBT_DEBUG("trying to probe the other mailbox");
731 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
735 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
737 if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
738 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
739 status->MPI_TAG = req->tag;
740 status->MPI_ERROR = MPI_SUCCESS;
741 status->count = req->real_size;
743 nsleeps=1;//reset the number of sleeps we will do next time
749 smpi_mpi_request_free(&request);
754 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
756 print_request("Waiting", *request);
757 if ((*request)->flags & PREPARED) {
758 smpi_empty_status(status);
762 if ((*request)->action != NULL) { // this is not a detached send
763 simcall_comm_wait((*request)->action, -1.0);
768 (*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
771 finish_wait(request, status);
772 *request = MPI_REQUEST_NULL;
773 // FIXME for a detached send, finish_wait is not called:
776 int smpi_mpi_waitany(int count, MPI_Request requests[],
783 index = MPI_UNDEFINED;
785 // Wait for a request to complete
786 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
787 map = xbt_new(int, count);
789 XBT_DEBUG("Wait for one of %d", count);
790 for(i = 0; i < count; i++) {
791 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
792 if (requests[i]->action != NULL) {
793 XBT_DEBUG("Waiting any %p ", requests[i]);
794 xbt_dynar_push(comms, &requests[i]->action);
798 //This is a finished detached request, let's return this one
799 size=0;//so we free the dynar but don't do the waitany call
801 finish_wait(&requests[i], status);//cleanup if refcount = 0
802 requests[i]=MPI_REQUEST_NULL;//set to null
808 i = simcall_comm_waitany(comms);
810 // not MPI_UNDEFINED, as this is a simix return code
813 finish_wait(&requests[index], status);
814 requests[index] = MPI_REQUEST_NULL;
818 xbt_dynar_free(&comms);
821 if (index==MPI_UNDEFINED)
822 smpi_empty_status(status);
827 int smpi_mpi_waitall(int count, MPI_Request requests[],
832 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
833 int retvalue = MPI_SUCCESS;
834 //tag invalid requests in the set
835 if (status != MPI_STATUSES_IGNORE) {
836 for (c = 0; c < count; c++) {
837 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ||
838 (requests[c]->flags & PREPARED)) {
839 smpi_empty_status(&status[c]);
840 } else if (requests[c]->src == MPI_PROC_NULL) {
841 smpi_empty_status(&status[c]);
842 status[c].MPI_SOURCE = MPI_PROC_NULL;
846 for(c = 0; c < count; c++) {
847 if (MC_is_active()) {
848 smpi_mpi_wait(&requests[c], pstat);
851 index = smpi_mpi_waitany(count, requests, pstat);
852 if (index == MPI_UNDEFINED)
854 requests[index]=MPI_REQUEST_NULL;
856 if (status != MPI_STATUSES_IGNORE) {
857 status[index] = *pstat;
858 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
859 retvalue = MPI_ERR_IN_STATUS;
866 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
871 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
874 for(i = 0; i < incount; i++)
876 index=smpi_mpi_waitany(incount, requests, pstat);
877 if(index!=MPI_UNDEFINED){
878 indices[count] = index;
880 if(status != MPI_STATUSES_IGNORE) {
881 status[index] = *pstat;
883 requests[index]=MPI_REQUEST_NULL;
885 return MPI_UNDEFINED;
891 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
894 int i, count, count_dead;
896 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
900 for(i = 0; i < incount; i++) {
901 if((requests[i] != MPI_REQUEST_NULL)) {
902 if(smpi_mpi_test(&requests[i], pstat)) {
905 if(status != MPI_STATUSES_IGNORE) {
908 requests[i]=MPI_REQUEST_NULL;
915 if(count_dead==incount)return MPI_UNDEFINED;
919 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
922 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
923 nary_tree_bcast(buf, count, datatype, root, comm, 4);
926 void smpi_mpi_barrier(MPI_Comm comm)
928 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
929 nary_tree_barrier(comm, 4);
932 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
933 void *recvbuf, int recvcount, MPI_Datatype recvtype,
934 int root, MPI_Comm comm)
936 int system_tag = COLL_TAG_GATHER;
937 int rank, size, src, index;
938 MPI_Aint lb = 0, recvext = 0;
939 MPI_Request *requests;
941 rank = smpi_comm_rank(comm);
942 size = smpi_comm_size(comm);
944 // Send buffer to root
945 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
947 // FIXME: check for errors
948 smpi_datatype_extent(recvtype, &lb, &recvext);
949 // Local copy from root
950 smpi_datatype_copy(sendbuf, sendcount, sendtype,
951 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
952 // Receive buffers from senders
953 requests = xbt_new(MPI_Request, size - 1);
955 for(src = 0; src < size; src++) {
957 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
959 src, system_tag, comm);
963 // Wait for completion of irecv's.
964 smpi_mpi_startall(size - 1, requests);
965 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
971 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
972 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
976 int rank = smpi_process_index();
979 /* arbitrarily choose root as rank 0 */
980 size = smpi_comm_size(comm);
982 displs = xbt_new(int, size);
983 for (i = 0; i < size; i++) {
985 count += recvcounts[i];
987 tmpbuf=(void*)xbt_malloc(count*smpi_datatype_get_extent(datatype));
988 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
989 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
990 recvcounts[rank], datatype, 0, comm);
995 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
996 void *recvbuf, int *recvcounts, int *displs,
997 MPI_Datatype recvtype, int root, MPI_Comm comm)
999 int system_tag = COLL_TAG_GATHERV;
1000 int rank, size, src, index;
1001 MPI_Aint lb = 0, recvext = 0;
1002 MPI_Request *requests;
1004 rank = smpi_comm_rank(comm);
1005 size = smpi_comm_size(comm);
1007 // Send buffer to root
1008 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1010 // FIXME: check for errors
1011 smpi_datatype_extent(recvtype, &lb, &recvext);
1012 // Local copy from root
1013 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1014 (char *)recvbuf + displs[root] * recvext,
1015 recvcounts[root], recvtype);
1016 // Receive buffers from senders
1017 requests = xbt_new(MPI_Request, size - 1);
1019 for(src = 0; src < size; src++) {
1022 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1023 recvcounts[src], recvtype, src, system_tag, comm);
1027 // Wait for completion of irecv's.
1028 smpi_mpi_startall(size - 1, requests);
1029 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1034 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1035 MPI_Datatype sendtype, void *recvbuf,
1036 int recvcount, MPI_Datatype recvtype,
1039 int system_tag = COLL_TAG_ALLGATHER;
1040 int rank, size, other, index;
1041 MPI_Aint lb = 0, recvext = 0;
1042 MPI_Request *requests;
1044 rank = smpi_comm_rank(comm);
1045 size = smpi_comm_size(comm);
1046 // FIXME: check for errors
1047 smpi_datatype_extent(recvtype, &lb, &recvext);
1048 // Local copy from self
1049 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1050 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1052 // Send/Recv buffers to/from others;
1053 requests = xbt_new(MPI_Request, 2 * (size - 1));
1055 for(other = 0; other < size; other++) {
1058 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1061 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1062 recvcount, recvtype, other,
1067 // Wait for completion of all comms.
1068 smpi_mpi_startall(2 * (size - 1), requests);
1069 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1073 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1074 MPI_Datatype sendtype, void *recvbuf,
1075 int *recvcounts, int *displs,
1076 MPI_Datatype recvtype, MPI_Comm comm)
1078 int system_tag = COLL_TAG_ALLGATHERV;
1079 int rank, size, other, index;
1080 MPI_Aint lb = 0, recvext = 0;
1081 MPI_Request *requests;
1083 rank = smpi_comm_rank(comm);
1084 size = smpi_comm_size(comm);
1085 // FIXME: check for errors
1086 smpi_datatype_extent(recvtype, &lb, &recvext);
1087 // Local copy from self
1088 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1089 (char *)recvbuf + displs[rank] * recvext,
1090 recvcounts[rank], recvtype);
1091 // Send buffers to others;
1092 requests = xbt_new(MPI_Request, 2 * (size - 1));
1094 for(other = 0; other < size; other++) {
1097 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1101 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1102 recvtype, other, system_tag, comm);
1106 // Wait for completion of all comms.
1107 smpi_mpi_startall(2 * (size - 1), requests);
1108 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1112 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1113 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1114 int root, MPI_Comm comm)
1116 int system_tag = COLL_TAG_SCATTER;
1117 int rank, size, dst, index;
1118 MPI_Aint lb = 0, sendext = 0;
1119 MPI_Request *requests;
1121 rank = smpi_comm_rank(comm);
1122 size = smpi_comm_size(comm);
1124 // Recv buffer from root
1125 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1128 // FIXME: check for errors
1129 smpi_datatype_extent(sendtype, &lb, &sendext);
1130 // Local copy from root
1131 if(recvbuf!=MPI_IN_PLACE){
1132 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1133 sendcount, sendtype, recvbuf, recvcount, recvtype);
1135 // Send buffers to receivers
1136 requests = xbt_new(MPI_Request, size - 1);
1138 for(dst = 0; dst < size; dst++) {
1140 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1141 sendcount, sendtype, dst,
1146 // Wait for completion of isend's.
1147 smpi_mpi_startall(size - 1, requests);
1148 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1153 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1154 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1155 MPI_Datatype recvtype, int root, MPI_Comm comm)
1157 int system_tag = COLL_TAG_SCATTERV;
1158 int rank, size, dst, index;
1159 MPI_Aint lb = 0, sendext = 0;
1160 MPI_Request *requests;
1162 rank = smpi_comm_rank(comm);
1163 size = smpi_comm_size(comm);
1165 // Recv buffer from root
1166 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1169 // FIXME: check for errors
1170 smpi_datatype_extent(sendtype, &lb, &sendext);
1171 // Local copy from root
1172 if(recvbuf!=MPI_IN_PLACE){
1173 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1174 sendtype, recvbuf, recvcount, recvtype);
1176 // Send buffers to receivers
1177 requests = xbt_new(MPI_Request, size - 1);
1179 for(dst = 0; dst < size; dst++) {
1182 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1183 sendtype, dst, system_tag, comm);
1187 // Wait for completion of isend's.
1188 smpi_mpi_startall(size - 1, requests);
1189 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1194 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1195 MPI_Datatype datatype, MPI_Op op, int root,
1198 int system_tag = COLL_TAG_REDUCE;
1199 int rank, size, src, index;
1200 MPI_Aint lb = 0, dataext = 0;
1201 MPI_Request *requests;
1205 char* sendtmpbuf = (char*) sendbuf;
1206 if( sendbuf == MPI_IN_PLACE ) {
1207 sendtmpbuf = (char *)xbt_malloc(count*smpi_datatype_get_extent(datatype));
1208 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1211 rank = smpi_comm_rank(comm);
1212 size = smpi_comm_size(comm);
1213 //non commutative case, use a working algo from openmpi
1214 if(!smpi_op_is_commute(op)){
1215 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1216 datatype, op, root, comm);
1221 // Send buffer to root
1222 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1224 // FIXME: check for errors
1225 smpi_datatype_extent(datatype, &lb, &dataext);
1226 // Local copy from root
1227 if (sendtmpbuf && recvbuf)
1228 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1229 // Receive buffers from senders
1230 //TODO: make a MPI_barrier here ?
1231 requests = xbt_new(MPI_Request, size - 1);
1232 tmpbufs = xbt_new(void *, size - 1);
1234 for(src = 0; src < size; src++) {
1236 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1238 tmpbufs[index] = xbt_malloc(count * dataext);
1240 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1245 // Wait for completion of irecv's.
1246 smpi_mpi_startall(size - 1, requests);
1247 for(src = 0; src < size - 1; src++) {
1248 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1249 XBT_DEBUG("finished waiting any request with index %d", index);
1250 if(index == MPI_UNDEFINED) {
1253 if(op) /* op can be MPI_OP_NULL that does nothing */
1254 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1256 for(index = 0; index < size - 1; index++) {
1257 xbt_free(tmpbufs[index]);
1262 if( sendbuf == MPI_IN_PLACE ) {
1263 xbt_free(sendtmpbuf);
1268 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1269 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1271 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1272 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1275 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1276 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1278 int system_tag = -888;
1279 int rank, size, other, index;
1280 MPI_Aint lb = 0, dataext = 0;
1281 MPI_Request *requests;
1284 rank = smpi_comm_rank(comm);
1285 size = smpi_comm_size(comm);
1287 // FIXME: check for errors
1288 smpi_datatype_extent(datatype, &lb, &dataext);
1290 // Local copy from self
1291 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1293 // Send/Recv buffers to/from others;
1294 requests = xbt_new(MPI_Request, size - 1);
1295 tmpbufs = xbt_new(void *, rank);
1297 for(other = 0; other < rank; other++) {
1298 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1300 tmpbufs[index] = xbt_malloc(count * dataext);
1302 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1306 for(other = rank + 1; other < size; other++) {
1308 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1311 // Wait for completion of all comms.
1312 smpi_mpi_startall(size - 1, requests);
1314 if(smpi_op_is_commute(op)){
1315 for(other = 0; other < size - 1; other++) {
1316 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1317 if(index == MPI_UNDEFINED) {
1321 // #Request is below rank: it's a irecv
1322 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1326 //non commutative case, wait in order
1327 for(other = 0; other < size - 1; other++) {
1328 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1330 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1334 for(index = 0; index < rank; index++) {
1335 xbt_free(tmpbufs[index]);
1341 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1342 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1344 int system_tag = -888;
1345 int rank, size, other, index;
1346 MPI_Aint lb = 0, dataext = 0;
1347 MPI_Request *requests;
1349 int recvbuf_is_empty=1;
1350 rank = smpi_comm_rank(comm);
1351 size = smpi_comm_size(comm);
1353 // FIXME: check for errors
1354 smpi_datatype_extent(datatype, &lb, &dataext);
1356 // Send/Recv buffers to/from others;
1357 requests = xbt_new(MPI_Request, size - 1);
1358 tmpbufs = xbt_new(void *, rank);
1360 for(other = 0; other < rank; other++) {
1361 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1363 tmpbufs[index] = xbt_malloc(count * dataext);
1365 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1369 for(other = rank + 1; other < size; other++) {
1371 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1374 // Wait for completion of all comms.
1375 smpi_mpi_startall(size - 1, requests);
1376 if(smpi_op_is_commute(op)){
1377 for(other = 0; other < size - 1; other++) {
1378 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1379 if(index == MPI_UNDEFINED) {
1383 if(recvbuf_is_empty){
1384 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1387 // #Request is below rank: it's a irecv
1388 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1392 //non commutative case, wait in order
1393 for(other = 0; other < size - 1; other++) {
1394 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1396 if(recvbuf_is_empty){
1397 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1399 }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1403 for(index = 0; index < rank; index++) {
1404 xbt_free(tmpbufs[index]);