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 "mc/mc_replay.h"
11 #include "xbt/replay.h"
13 #include "simix/smx_private.h"
14 #include "surf/surf.h"
15 #include "simgrid/sg_config.h"
16 #include "colls/colls.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
21 static int match_recv(void* a, void* b, smx_synchro_t ignored) {
22 MPI_Request ref = (MPI_Request)a;
23 MPI_Request req = (MPI_Request)b;
24 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
26 xbt_assert(ref, "Cannot match recv against null reference");
27 xbt_assert(req, "Cannot match recv against null request");
28 if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
29 && ((ref->tag == MPI_ANY_TAG && req->tag >=0) || req->tag == ref->tag)){
30 //we match, we can transfer some values
31 // FIXME : move this to the copy function ?
32 if(ref->src == MPI_ANY_SOURCE)ref->real_src = req->src;
33 if(ref->tag == MPI_ANY_TAG)ref->real_tag = req->tag;
34 if(ref->real_size < req->real_size) ref->truncated = 1;
36 ref->detached_sender=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
38 XBT_DEBUG("match succeeded");
43 static int match_send(void* a, void* b,smx_synchro_t ignored) {
44 MPI_Request ref = (MPI_Request)a;
45 MPI_Request req = (MPI_Request)b;
46 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
47 xbt_assert(ref, "Cannot match send against null reference");
48 xbt_assert(req, "Cannot match send against null request");
50 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
51 && ((req->tag == MPI_ANY_TAG && ref->tag >=0)|| req->tag == ref->tag))
53 if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
54 if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
55 if(req->real_size < ref->real_size) req->truncated = 1;
57 req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
59 XBT_DEBUG("match succeeded");
65 typedef struct s_smpi_factor *smpi_factor_t;
66 typedef struct s_smpi_factor {
69 double values[4];//arbitrary set to 4
71 xbt_dynar_t smpi_os_values = NULL;
72 xbt_dynar_t smpi_or_values = NULL;
73 xbt_dynar_t smpi_ois_values = NULL;
75 double smpi_wtime_sleep = 0.0;
76 double smpi_iprobe_sleep = 1e-4;
77 double smpi_test_sleep = 1e-4;
80 // Methods used to parse and store the values for timing injections in smpi
81 // These are taken from surf/network.c and generalized to have more factors
82 // These methods should be merged with those in surf/network.c (moved somewhere in xbt ?)
84 static int factor_cmp(const void *pa, const void *pb)
86 return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor) ? 1 :
87 (((s_smpi_factor_t*)pa)->factor < ((s_smpi_factor_t*)pb)->factor) ? -1 : 0;
91 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
94 unsigned int iter = 0;
98 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
100 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
101 radical_elements = xbt_str_split(smpi_coef_string, ";");
102 xbt_dynar_foreach(radical_elements, iter, value) {
103 memset(&fact, 0, sizeof(s_smpi_factor_t));
104 radical_elements2 = xbt_str_split(value, ":");
105 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
106 xbt_die("Malformed radical for smpi factor!");
107 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
109 fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
111 fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
116 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
117 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
118 xbt_dynar_free(&radical_elements2);
120 xbt_dynar_free(&radical_elements);
122 xbt_dynar_sort(smpi_factor, &factor_cmp);
123 xbt_dynar_foreach(smpi_factor, iter, fact) {
124 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
129 static double smpi_os(double size)
131 if (!smpi_os_values) {
132 smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
133 smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
135 unsigned int iter = 0;
136 s_smpi_factor_t fact;
138 xbt_dynar_foreach(smpi_os_values, iter, fact) {
139 if (size <= fact.factor) {
140 XBT_DEBUG("os : %f <= %ld return %f", size, fact.factor, current);
143 current=fact.values[0]+fact.values[1]*size;
146 XBT_DEBUG("os : %f > %ld return %f", size, fact.factor, current);
151 static double smpi_ois(double size)
153 if (!smpi_ois_values) {
154 smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
155 smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
157 unsigned int iter = 0;
158 s_smpi_factor_t fact;
160 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
161 if (size <= fact.factor) {
162 XBT_DEBUG("ois : %f <= %ld return %f", size, fact.factor, current);
165 current=fact.values[0]+fact.values[1]*size;
168 XBT_DEBUG("ois : %f > %ld return %f", size, fact.factor, current);
173 static double smpi_or(double size)
175 if (!smpi_or_values) {
176 smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
177 smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
179 unsigned int iter = 0;
180 s_smpi_factor_t fact;
182 xbt_dynar_foreach(smpi_or_values, iter, fact) {
183 if (size <= fact.factor) {
184 XBT_DEBUG("or : %f <= %ld return %f", size, fact.factor, current);
187 current=fact.values[0]+fact.values[1]*size;
189 XBT_DEBUG("or : %f > %ld return %f", size, fact.factor, current);
194 double smpi_mpi_wtime(){
196 if (smpi_process_initialized() && !smpi_process_finalized() && !smpi_process_get_sampling()) {
198 time = SIMIX_get_clock();
199 //to avoid deadlocks if called too many times
200 if(smpi_wtime_sleep > 0) simcall_process_sleep(smpi_wtime_sleep);
203 time = SIMIX_get_clock();
208 static MPI_Request build_request(void *buf, int count,
209 MPI_Datatype datatype, int src, int dst,
210 int tag, MPI_Comm comm, unsigned flags)
212 MPI_Request request = NULL;
214 void *old_buf = NULL;
216 request = xbt_new(s_smpi_mpi_request_t, 1);
218 s_smpi_subtype_t *subtype = datatype->substruct;
220 if(((flags & RECV) && (flags & ACCUMULATE)) || (datatype->has_subtype == 1)){
221 // This part handles the problem of non-contiguous memory
223 buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
224 if ((datatype->has_subtype == 1) && (flags & SEND)) {
225 subtype->serialize(old_buf, buf, count, datatype->substruct);
230 // This part handles the problem of non-contiguous memory (for the
231 // unserialisation at the reception)
232 request->old_buf = old_buf;
233 request->old_type = datatype;
235 request->size = smpi_datatype_size(datatype) * count;
239 request->comm = comm;
240 request->action = NULL;
241 request->flags = flags;
242 request->detached = 0;
243 request->detached_sender = NULL;
244 request->real_src = 0;
246 request->truncated = 0;
247 request->real_size = 0;
248 request->real_tag = 0;
249 if(flags & PERSISTENT)
250 request->refcount = 1;
252 request->refcount = 0;
253 request->op = MPI_REPLACE;
256 if (flags & SEND) smpi_datatype_unuse(datatype);
262 void smpi_empty_status(MPI_Status * status)
264 if(status != MPI_STATUS_IGNORE) {
265 status->MPI_SOURCE = MPI_ANY_SOURCE;
266 status->MPI_TAG = MPI_ANY_TAG;
267 status->MPI_ERROR = MPI_SUCCESS;
272 void smpi_action_trace_run(char *path)
276 xbt_dict_cursor_t cursor;
280 action_fp = fopen(path, "r");
281 if (action_fp == NULL)
282 xbt_die("Cannot open %s: %s", path, strerror(errno));
285 if (!xbt_dict_is_empty(action_queues)) {
287 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
290 xbt_dict_foreach(action_queues, cursor, name, todo) {
291 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
297 xbt_dict_free(&action_queues);
298 action_queues = xbt_dict_new_homogeneous(NULL);
301 static void smpi_mpi_request_free_voidp(void* request)
303 MPI_Request req = request;
304 smpi_mpi_request_free(&req);
307 /* MPI Low level calls */
308 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
309 int dst, int tag, MPI_Comm comm)
311 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
312 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
313 comm, PERSISTENT | SEND | PREPARED);
317 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
318 int dst, int tag, MPI_Comm comm)
320 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
321 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
322 comm, PERSISTENT | SSEND | SEND | PREPARED);
326 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
327 int src, int tag, MPI_Comm comm)
329 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
330 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,
331 comm, PERSISTENT | RECV | PREPARED);
335 void smpi_mpi_start(MPI_Request request)
339 xbt_assert(!request->action, "Cannot (re)start a non-finished communication");
340 request->flags &= ~PREPARED;
341 request->flags &= ~FINISHED;
344 if (request->flags & RECV) {
345 print_request("New recv", request);
347 xbt_mutex_t mut=smpi_process_mailboxes_mutex();
348 xbt_mutex_acquire(mut);
350 if (request->flags & RMA || request->size < sg_cfg_get_int("smpi/async_small_thres")){
351 //We have to check both mailboxes (because SSEND messages are sent to the large mbox). begin with the more appropriate one : the small one.
352 mailbox = smpi_process_mailbox_small();
353 XBT_DEBUG("Is there a corresponding send already posted in the small mailbox %p (in case of SSEND)?", mailbox);
354 smx_synchro_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
357 mailbox = smpi_process_mailbox();
358 XBT_DEBUG("No, nothing in the small mailbox test the other one : %p", mailbox);
359 action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
361 XBT_DEBUG("Still nothing, switch back to the small mailbox : %p", mailbox);
362 mailbox = smpi_process_mailbox_small();
365 XBT_DEBUG("yes there was something for us in the large mailbox");
368 mailbox = smpi_process_mailbox_small();
369 XBT_DEBUG("Is there a corresponding send already posted the small mailbox?");
370 smx_synchro_t action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
373 XBT_DEBUG("No, nothing in the permanent receive mailbox");
374 mailbox = smpi_process_mailbox();
376 XBT_DEBUG("yes there was something for us in the small mailbox");
380 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
381 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
383 simcall_process_sleep(sleeptime);
384 XBT_DEBUG("receiving size of %zu : sleep %f ", request->size, smpi_or(request->size));
387 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
388 request->real_size=request->size;
389 smpi_datatype_use(request->old_type);
390 smpi_comm_use(request->comm);
391 request->action = simcall_comm_irecv(mailbox, request->buf,
392 &request->real_size, &match_recv,
393 !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
394 : &smpi_comm_null_copy_buffer_callback,
396 XBT_DEBUG("recv simcall posted");
398 xbt_mutex_release(mut);
402 int receiver = request->dst;
404 int rank = request->src;
405 if (TRACE_smpi_view_internals()) {
406 TRACE_smpi_send(rank, rank, receiver,request->size);
408 print_request("New send", request);
410 //if we are giving back the control to the user without waiting for completion, we have to inject timings
411 double sleeptime = 0.0;
412 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
413 //isend and send timings may be different
414 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
417 if(sleeptime != 0.0){
418 simcall_process_sleep(sleeptime);
419 XBT_DEBUG("sending size of %zu : sleep %f ", request->size, smpi_os(request->size));
422 xbt_mutex_t mut=smpi_process_remote_mailboxes_mutex(receiver);
423 xbt_mutex_acquire(mut);
425 if (request->flags & RMA || request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
426 mailbox = smpi_process_remote_mailbox(receiver);
427 XBT_DEBUG("Is there a corresponding recv already posted in the large mailbox %p?", mailbox);
428 smx_synchro_t action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, (void*)request);
430 if (! (request->flags & SSEND)){
431 mailbox = smpi_process_remote_mailbox_small(receiver);
432 XBT_DEBUG("No, nothing in the large mailbox, message is to be sent on the small one %p", mailbox);
434 mailbox = smpi_process_remote_mailbox_small(receiver);
435 XBT_DEBUG("SSEND : Is there a corresponding recv already posted in the small mailbox %p?", mailbox);
436 action = simcall_comm_iprobe(mailbox, 1,request->dst, request->tag, &match_send, (void*)request);
438 XBT_DEBUG("No, we are first, send to large mailbox");
439 mailbox = smpi_process_remote_mailbox(receiver);
443 XBT_DEBUG("Yes there was something for us in the large mailbox");
446 mailbox = smpi_process_remote_mailbox(receiver);
447 XBT_DEBUG("Send request %p is in the large mailbox %p (buf: %p)",mailbox, request,request->buf);
450 void* buf = request->buf;
451 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
453 request->detached = 1;
454 XBT_DEBUG("Send request %p is detached", request);
456 if(request->old_type->has_subtype == 0){
457 oldbuf = request->buf;
458 if (!smpi_process_get_replaying() && oldbuf && request->size!=0){
459 if((smpi_privatize_global_variables)
460 && ((char*) request->buf >= smpi_start_data_exe)
461 && ((char*)request->buf < smpi_start_data_exe + smpi_size_data_exe )){
462 XBT_DEBUG("Privatization : We are sending from a zone inside global memory. Switch data segment ");
463 smpi_switch_data_segment(request->src);
465 buf = xbt_malloc(request->size);
466 memcpy(buf,oldbuf,request->size);
467 XBT_DEBUG("buf %p copied into %p",oldbuf,buf);
472 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
473 request->real_size=request->size;
474 smpi_datatype_use(request->old_type);
475 smpi_comm_use(request->comm);
477 simcall_comm_isend(SIMIX_process_from_PID(request->src+1), mailbox, request->size, -1.0,
478 buf, request->real_size,
480 &xbt_free_f, // how to free the userdata if a detached send fails
481 !smpi_process_get_replaying()? &smpi_comm_copy_buffer_callback
482 : &smpi_comm_null_copy_buffer_callback,
484 // detach if msg size < eager/rdv switch limit
486 XBT_DEBUG("send simcall posted");
490 /* FIXME: detached sends are not traceable (request->action == NULL) */
492 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
494 xbt_mutex_release(mut);
499 void smpi_mpi_startall(int count, MPI_Request * requests)
502 if(requests==NULL) return;
504 for(i = 0; i < count; i++) {
505 smpi_mpi_start(requests[i]);
509 void smpi_mpi_request_free(MPI_Request * request)
511 if((*request) != MPI_REQUEST_NULL){
512 (*request)->refcount--;
513 if((*request)->refcount<0) xbt_die("wrong refcount");
515 if((*request)->refcount==0){
516 print_request("Destroying", (*request));
518 *request = MPI_REQUEST_NULL;
520 print_request("Decrementing", (*request));
523 xbt_die("freeing an already free request");
528 MPI_Request smpi_rma_send_init(void *buf, int count, MPI_Datatype datatype,
529 int src, int dst, int tag, MPI_Comm comm, MPI_Op op)
531 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
533 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, src, dst, tag,
534 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED);
536 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
537 comm, RMA | NON_PERSISTENT | ISEND | SEND | PREPARED | ACCUMULATE);
543 MPI_Request smpi_rma_recv_init(void *buf, int count, MPI_Datatype datatype,
544 int src, int dst, int tag, MPI_Comm comm, MPI_Op op)
546 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
548 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
549 comm, RMA | NON_PERSISTENT | RECV | PREPARED);
551 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src, dst, tag,
552 comm, RMA | NON_PERSISTENT | RECV | PREPARED | ACCUMULATE);
559 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
560 int dst, int tag, MPI_Comm comm)
562 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
563 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
564 comm, PERSISTENT | ISEND | SEND | PREPARED);
568 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
569 int dst, int tag, MPI_Comm comm)
571 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
572 request = build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
573 comm, NON_PERSISTENT | ISEND | SEND);
574 smpi_mpi_start(request);
578 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
579 int dst, int tag, MPI_Comm comm)
581 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
582 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
583 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
584 smpi_mpi_start(request);
588 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
589 int src, int tag, MPI_Comm comm)
591 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
592 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,
593 comm, PERSISTENT | RECV | PREPARED);
597 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
598 int src, int tag, MPI_Comm comm)
600 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
601 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,
602 comm, NON_PERSISTENT | RECV);
603 smpi_mpi_start(request);
607 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
608 int tag, MPI_Comm comm, MPI_Status * status)
610 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
611 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
612 smpi_mpi_wait(&request, status);
618 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
619 int tag, MPI_Comm comm)
621 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
622 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
623 comm, NON_PERSISTENT | SEND);
625 smpi_mpi_start(request);
626 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
630 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
631 int dst, int tag, MPI_Comm comm)
633 MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
634 request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
635 comm, NON_PERSISTENT | SSEND | SEND);
637 smpi_mpi_start(request);
638 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
642 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
643 int dst, int sendtag, void *recvbuf, int recvcount,
644 MPI_Datatype recvtype, int src, int recvtag,
645 MPI_Comm comm, MPI_Status * status)
647 MPI_Request requests[2];
649 int myid=smpi_process_index();
650 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
651 smpi_datatype_copy(sendbuf, sendcount, sendtype,
652 recvbuf, recvcount, recvtype);
656 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
658 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
659 smpi_mpi_startall(2, requests);
660 smpi_mpi_waitall(2, requests, stats);
661 smpi_mpi_request_free(&requests[0]);
662 smpi_mpi_request_free(&requests[1]);
663 if(status != MPI_STATUS_IGNORE) {
664 // Copy receive status
669 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
671 return status->count / smpi_datatype_size(datatype);
674 static void finish_wait(MPI_Request * request, MPI_Status * status)
676 MPI_Request req = *request;
677 smpi_empty_status(status);
679 if(!(req->detached && req->flags & SEND)
680 && !(req->flags & PREPARED)){
681 if(status != MPI_STATUS_IGNORE) {
682 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
683 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
684 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
685 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
686 // this handles the case were size in receive differs from size in send
687 // FIXME: really this should just contain the count of receive-type blocks,
689 status->count = req->real_size;
692 print_request("Finishing", req);
693 MPI_Datatype datatype = req->old_type;
695 if((req->flags & ACCUMULATE) || (datatype->has_subtype == 1)){
696 if (!smpi_process_get_replaying()){
697 if( smpi_privatize_global_variables
698 && ((char*)req->old_buf >= smpi_start_data_exe)
699 && ((char*)req->old_buf < smpi_start_data_exe + smpi_size_data_exe )
701 XBT_VERB("Privatization : We are unserializing to a zone in global memory - Switch data segment ");
702 smpi_switch_data_segment(smpi_process_index());
706 if(datatype->has_subtype == 1){
707 // This part handles the problem of non-contignous memory
708 // the unserialization at the reception
709 s_smpi_subtype_t *subtype = datatype->substruct;
710 if(req->flags & RECV)
711 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct, req->op);
712 if(req->detached == 0) free(req->buf);
713 }else if(req->flags & RECV){//apply op on contiguous buffer for accumulate
714 int n =req->real_size/smpi_datatype_size(datatype);
715 smpi_op_apply(req->op, req->buf, req->old_buf, &n, &datatype);
718 smpi_comm_unuse(req->comm);
719 smpi_datatype_unuse(datatype);
723 if (TRACE_smpi_view_internals()) {
724 if(req->flags & RECV){
725 int rank = smpi_process_index();
726 int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
727 TRACE_smpi_recv(rank, src_traced, rank);
731 if(req->detached_sender!=NULL){
732 smpi_mpi_request_free(&(req->detached_sender));
734 if(req->flags & PERSISTENT)
736 req->flags |= FINISHED;
738 smpi_mpi_request_free(request);
742 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
745 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
748 //multiplier to the sleeptime, to increase speed of execution, each failed test will increase it
749 static int nsleeps = 1;
750 if(smpi_test_sleep > 0) simcall_process_sleep(nsleeps*smpi_test_sleep);
752 smpi_empty_status(status);
754 if (!((*request)->flags & PREPARED)) {
755 if ((*request)->action != NULL)
756 flag = simcall_comm_test((*request)->action);
758 finish_wait(request, status);
759 nsleeps=1;//reset the number of sleeps we will do next time
760 if (*request != MPI_REQUEST_NULL && !((*request)->flags & PERSISTENT))
761 *request = MPI_REQUEST_NULL;
769 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
776 *index = MPI_UNDEFINED;
778 comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
779 map = xbt_new(int, count);
781 for(i = 0; i < count; i++) {
782 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action &&
783 !(requests[i]->flags & PREPARED)) {
784 xbt_dynar_push(comms, &requests[i]->action);
790 //multiplier to the sleeptime, to increase speed of execution, each failed testany will increase it
791 static int nsleeps = 1;
792 if(smpi_test_sleep > 0) simcall_process_sleep(nsleeps*smpi_test_sleep);
794 i = simcall_comm_testany(comms);
795 // not MPI_UNDEFINED, as this is a simix return code
798 finish_wait(&requests[*index], status);
799 if (requests[*index] != MPI_REQUEST_NULL && (requests[*index]->flags & NON_PERSISTENT))
800 requests[*index] = MPI_REQUEST_NULL;
807 //all requests are null or inactive, return true
809 smpi_empty_status(status);
812 xbt_dynar_free(&comms);
818 int smpi_mpi_testall(int count, MPI_Request requests[],
822 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
825 for(i=0; i<count; i++){
826 if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
827 if (smpi_mpi_test(&requests[i], pstat)!=1){
830 requests[i]=MPI_REQUEST_NULL;
833 smpi_empty_status(pstat);
835 if(status != MPI_STATUSES_IGNORE) {
842 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
844 //FIXME find another wait to avoid busy waiting ?
845 // the issue here is that we have to wait on a nonexistent comm
847 smpi_mpi_iprobe(source, tag, comm, &flag, status);
848 XBT_DEBUG("Busy Waiting on probing : %d", flag);
852 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
854 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,
855 comm, PERSISTENT | RECV);
857 //to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
858 //multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
859 static int nsleeps = 1;
860 if(smpi_iprobe_sleep > 0) simcall_process_sleep(nsleeps*smpi_iprobe_sleep);
861 // behave like a receive, but don't do it
864 print_request("New iprobe", request);
865 // We have to test both mailboxes as we don't know if we will receive one one or another
866 if (sg_cfg_get_int("smpi/async_small_thres")>0){
867 mailbox = smpi_process_mailbox_small();
868 XBT_DEBUG("trying to probe the perm recv mailbox");
869 request->action = simcall_comm_iprobe(mailbox, 0, request->src, request->tag, &match_recv, (void*)request);
871 if (request->action==NULL){
872 mailbox = smpi_process_mailbox();
873 XBT_DEBUG("trying to probe the other mailbox");
874 request->action = simcall_comm_iprobe(mailbox, 0, request->src,request->tag, &match_recv, (void*)request);
878 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
880 if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
881 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
882 status->MPI_TAG = req->tag;
883 status->MPI_ERROR = MPI_SUCCESS;
884 status->count = req->real_size;
886 nsleeps=1;//reset the number of sleeps we will do next time
892 smpi_mpi_request_free(&request);
897 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
899 print_request("Waiting", *request);
900 if ((*request)->flags & PREPARED) {
901 smpi_empty_status(status);
905 if ((*request)->action != NULL) { // this is not a detached send
906 simcall_comm_wait((*request)->action, -1.0);
908 if((MC_is_active() || MC_record_replay_is_active()) && (*request)->action)
909 (*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
912 finish_wait(request, status);
913 if (*request != MPI_REQUEST_NULL && ((*request)->flags & NON_PERSISTENT))
914 *request = MPI_REQUEST_NULL;
915 // FIXME for a detached send, finish_wait is not called:
918 int smpi_mpi_waitany(int count, MPI_Request requests[],
925 index = MPI_UNDEFINED;
927 // Wait for a request to complete
928 comms = xbt_dynar_new(sizeof(smx_synchro_t), NULL);
929 map = xbt_new(int, count);
931 XBT_DEBUG("Wait for one of %d", count);
932 for(i = 0; i < count; i++) {
933 if (requests[i] != MPI_REQUEST_NULL
934 && !(requests[i]->flags & PREPARED)
935 && !(requests[i]->flags & FINISHED)) {
936 if (requests[i]->action != NULL) {
937 XBT_DEBUG("Waiting any %p ", requests[i]);
938 xbt_dynar_push(comms, &requests[i]->action);
942 //This is a finished detached request, let's return this one
943 size=0;//so we free the dynar but don't do the waitany call
945 finish_wait(&requests[i], status);//cleanup if refcount = 0
946 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
947 requests[i]=MPI_REQUEST_NULL;//set to null
953 i = simcall_comm_waitany(comms);
955 // not MPI_UNDEFINED, as this is a simix return code
958 finish_wait(&requests[index], status);
959 if (requests[i] != MPI_REQUEST_NULL && (requests[i]->flags & NON_PERSISTENT))
960 requests[index] = MPI_REQUEST_NULL;
964 xbt_dynar_free(&comms);
967 if (index==MPI_UNDEFINED)
968 smpi_empty_status(status);
973 int smpi_mpi_waitall(int count, MPI_Request requests[],
978 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
979 int retvalue = MPI_SUCCESS;
980 //tag invalid requests in the set
981 if (status != MPI_STATUSES_IGNORE) {
982 for (c = 0; c < count; c++) {
983 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ||
984 (requests[c]->flags & PREPARED)) {
985 smpi_empty_status(&status[c]);
986 } else if (requests[c]->src == MPI_PROC_NULL) {
987 smpi_empty_status(&status[c]);
988 status[c].MPI_SOURCE = MPI_PROC_NULL;
992 for(c = 0; c < count; c++) {
994 if (MC_is_active() || MC_record_replay_is_active()) {
995 smpi_mpi_wait(&requests[c], pstat);
998 index = smpi_mpi_waitany(count, requests, pstat);
999 if (index == MPI_UNDEFINED)
1001 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1002 requests[index]=MPI_REQUEST_NULL;
1004 if (status != MPI_STATUSES_IGNORE) {
1005 status[index] = *pstat;
1006 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
1007 retvalue = MPI_ERR_IN_STATUS;
1014 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
1015 MPI_Status status[])
1017 int i, count, index;
1019 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1022 for(i = 0; i < incount; i++)
1024 index=smpi_mpi_waitany(incount, requests, pstat);
1025 if(index!=MPI_UNDEFINED){
1026 indices[count] = index;
1028 if(status != MPI_STATUSES_IGNORE) {
1029 status[index] = *pstat;
1031 if (requests[index] != MPI_REQUEST_NULL && (requests[index]->flags & NON_PERSISTENT))
1032 requests[index]=MPI_REQUEST_NULL;
1034 return MPI_UNDEFINED;
1040 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
1041 MPI_Status status[])
1043 int i, count, count_dead;
1045 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
1049 for(i = 0; i < incount; i++) {
1050 if((requests[i] != MPI_REQUEST_NULL)) {
1051 if(smpi_mpi_test(&requests[i], pstat)) {
1054 if(status != MPI_STATUSES_IGNORE) {
1057 if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->flags & NON_PERSISTENT)
1058 requests[i]=MPI_REQUEST_NULL;
1064 if(count_dead==incount)return MPI_UNDEFINED;
1068 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
1071 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
1072 nary_tree_bcast(buf, count, datatype, root, comm, 4);
1075 void smpi_mpi_barrier(MPI_Comm comm)
1077 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
1078 nary_tree_barrier(comm, 4);
1081 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1082 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1083 int root, MPI_Comm comm)
1085 int system_tag = COLL_TAG_GATHER;
1086 int rank, size, src, index;
1087 MPI_Aint lb = 0, recvext = 0;
1088 MPI_Request *requests;
1090 rank = smpi_comm_rank(comm);
1091 size = smpi_comm_size(comm);
1093 // Send buffer to root
1094 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1096 // FIXME: check for errors
1097 smpi_datatype_extent(recvtype, &lb, &recvext);
1098 // Local copy from root
1099 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1100 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
1101 // Receive buffers from senders
1102 requests = xbt_new(MPI_Request, size - 1);
1104 for(src = 0; src < size; src++) {
1106 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
1107 recvcount, recvtype,
1108 src, system_tag, comm);
1112 // Wait for completion of irecv's.
1113 smpi_mpi_startall(size - 1, requests);
1114 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1115 for(src = 0; src < size-1; src++) {
1116 smpi_mpi_request_free(&requests[src]);
1123 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
1124 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1128 int rank = smpi_process_index();
1131 /* arbitrarily choose root as rank 0 */
1132 size = smpi_comm_size(comm);
1134 displs = xbt_new(int, size);
1135 for (i = 0; i < size; i++) {
1137 count += recvcounts[i];
1139 tmpbuf=(void*)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
1141 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
1142 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
1143 recvcounts[rank], datatype, 0, comm);
1145 smpi_free_tmp_buffer(tmpbuf);
1148 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1149 void *recvbuf, int *recvcounts, int *displs,
1150 MPI_Datatype recvtype, int root, MPI_Comm comm)
1152 int system_tag = COLL_TAG_GATHERV;
1153 int rank, size, src, index;
1154 MPI_Aint lb = 0, recvext = 0;
1155 MPI_Request *requests;
1157 rank = smpi_comm_rank(comm);
1158 size = smpi_comm_size(comm);
1160 // Send buffer to root
1161 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1163 // FIXME: check for errors
1164 smpi_datatype_extent(recvtype, &lb, &recvext);
1165 // Local copy from root
1166 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1167 (char *)recvbuf + displs[root] * recvext,
1168 recvcounts[root], recvtype);
1169 // Receive buffers from senders
1170 requests = xbt_new(MPI_Request, size - 1);
1172 for(src = 0; src < size; src++) {
1175 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1176 recvcounts[src], recvtype, src, system_tag, comm);
1180 // Wait for completion of irecv's.
1181 smpi_mpi_startall(size - 1, requests);
1182 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1183 for(src = 0; src < size-1; src++) {
1184 smpi_mpi_request_free(&requests[src]);
1190 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1191 MPI_Datatype sendtype, void *recvbuf,
1192 int recvcount, MPI_Datatype recvtype,
1195 int system_tag = COLL_TAG_ALLGATHER;
1196 int rank, size, other, index;
1197 MPI_Aint lb = 0, recvext = 0;
1198 MPI_Request *requests;
1200 rank = smpi_comm_rank(comm);
1201 size = smpi_comm_size(comm);
1202 // FIXME: check for errors
1203 smpi_datatype_extent(recvtype, &lb, &recvext);
1204 // Local copy from self
1205 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1206 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1208 // Send/Recv buffers to/from others;
1209 requests = xbt_new(MPI_Request, 2 * (size - 1));
1211 for(other = 0; other < size; other++) {
1214 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1217 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1218 recvcount, recvtype, other,
1223 // Wait for completion of all comms.
1224 smpi_mpi_startall(2 * (size - 1), requests);
1225 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1226 for(other = 0; other < 2*(size-1); other++) {
1227 smpi_mpi_request_free(&requests[other]);
1232 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1233 MPI_Datatype sendtype, void *recvbuf,
1234 int *recvcounts, int *displs,
1235 MPI_Datatype recvtype, MPI_Comm comm)
1237 int system_tag = COLL_TAG_ALLGATHERV;
1238 int rank, size, other, index;
1239 MPI_Aint lb = 0, recvext = 0;
1240 MPI_Request *requests;
1242 rank = smpi_comm_rank(comm);
1243 size = smpi_comm_size(comm);
1244 // FIXME: check for errors
1245 smpi_datatype_extent(recvtype, &lb, &recvext);
1246 // Local copy from self
1247 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1248 (char *)recvbuf + displs[rank] * recvext,
1249 recvcounts[rank], recvtype);
1250 // Send buffers to others;
1251 requests = xbt_new(MPI_Request, 2 * (size - 1));
1253 for(other = 0; other < size; other++) {
1256 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1260 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1261 recvtype, other, system_tag, comm);
1265 // Wait for completion of all comms.
1266 smpi_mpi_startall(2 * (size - 1), requests);
1267 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1268 for(other = 0; other < 2*(size-1); other++) {
1269 smpi_mpi_request_free(&requests[other]);
1274 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1275 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1276 int root, MPI_Comm comm)
1278 int system_tag = COLL_TAG_SCATTER;
1279 int rank, size, dst, index;
1280 MPI_Aint lb = 0, sendext = 0;
1281 MPI_Request *requests;
1283 rank = smpi_comm_rank(comm);
1284 size = smpi_comm_size(comm);
1286 // Recv buffer from root
1287 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1290 // FIXME: check for errors
1291 smpi_datatype_extent(sendtype, &lb, &sendext);
1292 // Local copy from root
1293 if(recvbuf!=MPI_IN_PLACE){
1294 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1295 sendcount, sendtype, recvbuf, recvcount, recvtype);
1297 // Send buffers to receivers
1298 requests = xbt_new(MPI_Request, size - 1);
1300 for(dst = 0; dst < size; dst++) {
1302 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1303 sendcount, sendtype, dst,
1308 // Wait for completion of isend's.
1309 smpi_mpi_startall(size - 1, requests);
1310 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1311 for(dst = 0; dst < size-1; dst++) {
1312 smpi_mpi_request_free(&requests[dst]);
1318 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1319 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1320 MPI_Datatype recvtype, int root, MPI_Comm comm)
1322 int system_tag = COLL_TAG_SCATTERV;
1323 int rank, size, dst, index;
1324 MPI_Aint lb = 0, sendext = 0;
1325 MPI_Request *requests;
1327 rank = smpi_comm_rank(comm);
1328 size = smpi_comm_size(comm);
1330 // Recv buffer from root
1331 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1334 // FIXME: check for errors
1335 smpi_datatype_extent(sendtype, &lb, &sendext);
1336 // Local copy from root
1337 if(recvbuf!=MPI_IN_PLACE){
1338 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1339 sendtype, recvbuf, recvcount, recvtype);
1341 // Send buffers to receivers
1342 requests = xbt_new(MPI_Request, size - 1);
1344 for(dst = 0; dst < size; dst++) {
1347 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1348 sendtype, dst, system_tag, comm);
1352 // Wait for completion of isend's.
1353 smpi_mpi_startall(size - 1, requests);
1354 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1355 for(dst = 0; dst < size-1; dst++) {
1356 smpi_mpi_request_free(&requests[dst]);
1362 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1363 MPI_Datatype datatype, MPI_Op op, int root,
1366 int system_tag = COLL_TAG_REDUCE;
1367 int rank, size, src, index;
1368 MPI_Aint lb = 0, dataext = 0;
1369 MPI_Request *requests;
1373 char* sendtmpbuf = (char*) sendbuf;
1374 if( sendbuf == MPI_IN_PLACE ) {
1375 sendtmpbuf = (char *)smpi_get_tmp_sendbuffer(count*smpi_datatype_get_extent(datatype));
1376 smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1379 rank = smpi_comm_rank(comm);
1380 size = smpi_comm_size(comm);
1381 //non commutative case, use a working algo from openmpi
1382 if(!smpi_op_is_commute(op)){
1383 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1384 datatype, op, root, comm);
1389 // Send buffer to root
1390 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1392 // FIXME: check for errors
1393 smpi_datatype_extent(datatype, &lb, &dataext);
1394 // Local copy from root
1395 if (sendtmpbuf && recvbuf)
1396 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1397 // Receive buffers from senders
1398 //TODO: make a MPI_barrier here ?
1399 requests = xbt_new(MPI_Request, size - 1);
1400 tmpbufs = xbt_new(void *, size - 1);
1402 for(src = 0; src < size; src++) {
1404 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1406 if (!smpi_process_get_replaying())
1407 tmpbufs[index] = xbt_malloc(count * dataext);
1409 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1411 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1416 // Wait for completion of irecv's.
1417 smpi_mpi_startall(size - 1, requests);
1418 for(src = 0; src < size - 1; src++) {
1419 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1420 XBT_DEBUG("finished waiting any request with index %d", index);
1421 if(index == MPI_UNDEFINED) {
1424 smpi_mpi_request_free(&requests[index]);
1426 if(op) /* op can be MPI_OP_NULL that does nothing */
1427 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1429 for(index = 0; index < size - 1; index++) {
1430 smpi_free_tmp_buffer(tmpbufs[index]);
1435 if( sendbuf == MPI_IN_PLACE ) {
1436 smpi_free_tmp_buffer(sendtmpbuf);
1441 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1442 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1444 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1445 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1448 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1449 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1451 int system_tag = -888;
1452 int rank, size, other, index;
1453 MPI_Aint lb = 0, dataext = 0;
1454 MPI_Request *requests;
1457 rank = smpi_comm_rank(comm);
1458 size = smpi_comm_size(comm);
1460 // FIXME: check for errors
1461 smpi_datatype_extent(datatype, &lb, &dataext);
1463 // Local copy from self
1464 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1466 // Send/Recv buffers to/from others;
1467 requests = xbt_new(MPI_Request, size - 1);
1468 tmpbufs = xbt_new(void *, rank);
1470 for(other = 0; other < rank; other++) {
1471 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1473 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1475 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1479 for(other = rank + 1; other < size; other++) {
1481 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1484 // Wait for completion of all comms.
1485 smpi_mpi_startall(size - 1, requests);
1487 if(smpi_op_is_commute(op)){
1488 for(other = 0; other < size - 1; other++) {
1489 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1490 if(index == MPI_UNDEFINED) {
1494 // #Request is below rank: it's a irecv
1495 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1499 //non commutative case, wait in order
1500 for(other = 0; other < size - 1; other++) {
1501 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1503 smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1507 for(index = 0; index < rank; index++) {
1508 smpi_free_tmp_buffer(tmpbufs[index]);
1510 for(index = 0; index < size-1; index++) {
1511 smpi_mpi_request_free(&requests[index]);
1517 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1518 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1520 int system_tag = -888;
1521 int rank, size, other, index;
1522 MPI_Aint lb = 0, dataext = 0;
1523 MPI_Request *requests;
1525 int recvbuf_is_empty=1;
1526 rank = smpi_comm_rank(comm);
1527 size = smpi_comm_size(comm);
1529 // FIXME: check for errors
1530 smpi_datatype_extent(datatype, &lb, &dataext);
1532 // Send/Recv buffers to/from others;
1533 requests = xbt_new(MPI_Request, size - 1);
1534 tmpbufs = xbt_new(void *, rank);
1536 for(other = 0; other < rank; other++) {
1537 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1539 tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
1541 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1545 for(other = rank + 1; other < size; other++) {
1547 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1550 // Wait for completion of all comms.
1551 smpi_mpi_startall(size - 1, requests);
1552 if(smpi_op_is_commute(op)){
1553 for(other = 0; other < size - 1; other++) {
1554 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1555 if(index == MPI_UNDEFINED) {
1559 if(recvbuf_is_empty){
1560 smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1563 // #Request is below rank: it's a irecv
1564 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1568 //non commutative case, wait in order
1569 for(other = 0; other < size - 1; other++) {
1570 smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1572 if(recvbuf_is_empty){
1573 smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1575 }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1579 for(index = 0; index < rank; index++) {
1580 smpi_free_tmp_buffer(tmpbufs[index]);
1582 for(index = 0; index < size-1; index++) {
1583 smpi_mpi_request_free(&requests[index]);