1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/replay.h"
12 #include "simix/smx_private.h"
13 #include "surf/surf.h"
14 #include "simgrid/sg_config.h"
15 #include "colls/colls.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
20 static int match_recv(void* a, void* b, smx_action_t ignored) {
21 MPI_Request ref = (MPI_Request)a;
22 MPI_Request req = (MPI_Request)b;
23 XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
25 xbt_assert(ref, "Cannot match recv against null reference");
26 xbt_assert(req, "Cannot match recv against null request");
27 if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
28 && (ref->tag == MPI_ANY_TAG || req->tag == 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
41 static int match_send(void* a, void* b,smx_action_t ignored) {
42 MPI_Request ref = (MPI_Request)a;
43 MPI_Request req = (MPI_Request)b;
44 XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
45 xbt_assert(ref, "Cannot match send against null reference");
46 xbt_assert(req, "Cannot match send against null request");
48 if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
49 && (req->tag == MPI_ANY_TAG || req->tag == ref->tag))
51 if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
52 if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
53 if(req->real_size < ref->real_size) req->truncated = 1;
55 req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
63 typedef struct s_smpi_factor *smpi_factor_t;
64 typedef struct s_smpi_factor {
67 double values[4];//arbitrary set to 4
69 xbt_dynar_t smpi_os_values = NULL;
70 xbt_dynar_t smpi_or_values = NULL;
71 xbt_dynar_t smpi_ois_values = NULL;
73 // Methods used to parse and store the values for timing injections in smpi
74 // These are taken from surf/network.c and generalized to have more factors
75 // These methods should be merged with those in surf/network.c (moved somewhere in xbt ?)
77 static int factor_cmp(const void *pa, const void *pb)
79 return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor);
83 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
86 unsigned int iter = 0;
89 xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
91 smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
92 radical_elements = xbt_str_split(smpi_coef_string, ";");
93 xbt_dynar_foreach(radical_elements, iter, value) {
95 radical_elements2 = xbt_str_split(value, ":");
96 if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
97 xbt_die("Malformed radical for smpi factor!");
98 for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
100 fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
102 fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
107 xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
108 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
109 xbt_dynar_free(&radical_elements2);
111 xbt_dynar_free(&radical_elements);
113 xbt_dynar_sort(smpi_factor, &factor_cmp);
114 xbt_dynar_foreach(smpi_factor, iter, fact) {
115 XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
120 static double smpi_os(double size)
122 if (!smpi_os_values) {
123 smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
124 smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
126 unsigned int iter = 0;
127 s_smpi_factor_t fact;
129 xbt_dynar_foreach(smpi_os_values, iter, fact) {
130 if (size <= fact.factor) {
131 XBT_DEBUG("os : %lf <= %ld return %f", size, fact.factor, current);
134 current=fact.values[0]+fact.values[1]*size;
137 XBT_DEBUG("os : %lf > %ld return %f", size, fact.factor, current);
142 static double smpi_ois(double size)
144 if (!smpi_ois_values) {
145 smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
146 smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
148 unsigned int iter = 0;
149 s_smpi_factor_t fact;
151 xbt_dynar_foreach(smpi_ois_values, iter, fact) {
152 if (size <= fact.factor) {
153 XBT_DEBUG("ois : %lf <= %ld return %f", size, fact.factor, current);
156 current=fact.values[0]+fact.values[1]*size;
159 XBT_DEBUG("ois : %lf > %ld return %f", size, fact.factor, current);
164 static double smpi_or(double size)
166 if (!smpi_or_values) {
167 smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
168 smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
170 unsigned int iter = 0;
171 s_smpi_factor_t fact;
173 xbt_dynar_foreach(smpi_or_values, iter, fact) {
174 if (size <= fact.factor) {
175 XBT_DEBUG("or : %lf <= %ld return %f", size, fact.factor, current);
178 current=fact.values[0]+fact.values[1]*size;
180 XBT_DEBUG("or : %lf > %ld return %f", size, fact.factor, current);
185 static MPI_Request build_request(void *buf, int count,
186 MPI_Datatype datatype, int src, int dst,
187 int tag, MPI_Comm comm, unsigned flags)
191 void *old_buf = NULL;
193 request = xbt_new(s_smpi_mpi_request_t, 1);
195 s_smpi_subtype_t *subtype = datatype->substruct;
197 if(datatype->has_subtype == 1){
198 // This part handles the problem of non-contiguous memory
200 buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
202 subtype->serialize(old_buf, buf, count, datatype->substruct);
207 // This part handles the problem of non-contiguous memory (for the
208 // unserialisation at the reception)
209 request->old_buf = old_buf;
210 request->old_type = datatype;
212 request->size = smpi_datatype_size(datatype) * count;
216 request->comm = comm;
217 request->action = NULL;
218 request->flags = flags;
219 request->detached = 0;
220 request->detached_sender = NULL;
222 request->truncated = 0;
223 request->real_size = 0;
224 request->real_tag = 0;
231 if (flags & SEND) smpi_datatype_unuse(datatype);
237 void smpi_empty_status(MPI_Status * status)
239 if(status != MPI_STATUS_IGNORE) {
240 status->MPI_SOURCE = MPI_ANY_SOURCE;
241 status->MPI_TAG = MPI_ANY_TAG;
242 status->MPI_ERROR = MPI_SUCCESS;
247 void smpi_action_trace_run(char *path)
251 xbt_dict_cursor_t cursor;
255 action_fp = fopen(path, "r");
256 xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
260 if (!xbt_dict_is_empty(action_queues)) {
262 ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
265 xbt_dict_foreach(action_queues, cursor, name, todo) {
266 XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
272 xbt_dict_free(&action_queues);
273 action_queues = xbt_dict_new_homogeneous(NULL);
276 static void smpi_mpi_request_free_voidp(void* request)
278 MPI_Request req = request;
279 smpi_mpi_request_free(&req);
282 /* MPI Low level calls */
283 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
284 int dst, int tag, MPI_Comm comm)
286 MPI_Request request =
287 build_request(buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
288 comm, PERSISTENT | SEND);
293 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
294 int dst, int tag, MPI_Comm comm)
296 MPI_Request request =
297 build_request(buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
298 comm, PERSISTENT | SSEND | SEND);
303 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
304 int src, int tag, MPI_Comm comm)
306 MPI_Request request =
307 build_request(buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
308 comm, PERSISTENT | RECV);
313 void smpi_mpi_start(MPI_Request request)
317 xbt_assert(!request->action,
318 "Cannot (re)start a non-finished communication");
319 if(request->flags & RECV) {
320 print_request("New recv", request);
321 if (request->size < sg_cfg_get_int("smpi/async_small_thres"))
322 mailbox = smpi_process_mailbox_small();
324 mailbox = smpi_process_mailbox();
325 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
326 request->real_size=request->size;
327 smpi_datatype_use(request->old_type);
328 request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
330 //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
331 double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
333 simcall_process_sleep(sleeptime);
334 XBT_DEBUG("receiving size of %zu : sleep %lf ", request->size, smpi_or(request->size));
340 int receiver = request->dst;//smpi_group_index(smpi_comm_group(request->comm), request->dst);
343 int rank = smpi_process_index();
344 if (TRACE_smpi_view_internals()) {
345 TRACE_smpi_send(rank, rank, receiver);
348 /* if(receiver == MPI_UNDEFINED) {*/
349 /* XBT_WARN("Trying to send a message to a wrong rank");*/
352 print_request("New send", request);
353 if (request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
354 mailbox = smpi_process_remote_mailbox_small(receiver);
356 XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
357 mailbox = smpi_process_remote_mailbox(receiver);
359 if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
361 request->detached = 1;
363 if(request->old_type->has_subtype == 0){
364 oldbuf = request->buf;
365 if (oldbuf && request->size!=0){
366 request->buf = xbt_malloc(request->size);
367 memcpy(request->buf,oldbuf,request->size);
370 XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
373 // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
374 request->real_size=request->size;
375 smpi_datatype_use(request->old_type);
377 //if we are giving back the control to the user without waiting for completion, we have to inject timings
378 double sleeptime =0.0;
379 if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
380 //isend and send timings may be different
381 sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
385 simcall_process_sleep(sleeptime);
386 XBT_DEBUG("sending size of %zu : sleep %lf ", request->size, smpi_os(request->size));
390 simcall_comm_isend(mailbox, request->size, -1.0,
391 request->buf, request->real_size,
393 &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
395 // detach if msg size < eager/rdv switch limit
399 /* FIXME: detached sends are not traceable (request->action == NULL) */
401 simcall_set_category(request->action, TRACE_internal_smpi_get_category());
409 void smpi_mpi_startall(int count, MPI_Request * requests)
413 for(i = 0; i < count; i++) {
414 smpi_mpi_start(requests[i]);
418 void smpi_mpi_request_free(MPI_Request * request)
421 if((*request) != MPI_REQUEST_NULL){
422 (*request)->refcount--;
423 if((*request)->refcount<0) xbt_die("wrong refcount");
425 if((*request)->refcount==0){
426 print_request("Destroying", (*request));
428 *request = MPI_REQUEST_NULL;
430 print_request("Decrementing", (*request));
434 xbt_die("freeing an already free request");
438 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
439 int dst, int tag, MPI_Comm comm)
441 MPI_Request request =
442 build_request(buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
443 comm, NON_PERSISTENT | SEND);
448 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
449 int dst, int tag, MPI_Comm comm)
451 MPI_Request request =
452 build_request(buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
453 comm, NON_PERSISTENT | ISEND | SEND);
455 smpi_mpi_start(request);
459 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
460 int dst, int tag, MPI_Comm comm)
462 MPI_Request request =
463 build_request(buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
464 comm, NON_PERSISTENT | ISEND | SSEND | SEND);
465 smpi_mpi_start(request);
471 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
472 int src, int tag, MPI_Comm comm)
474 MPI_Request request =
475 build_request(buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
476 comm, NON_PERSISTENT | RECV);
480 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
481 int src, int tag, MPI_Comm comm)
483 MPI_Request request =
484 build_request(buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
485 comm, NON_PERSISTENT | RECV);
487 smpi_mpi_start(request);
491 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
492 int tag, MPI_Comm comm, MPI_Status * status)
495 request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
496 smpi_mpi_wait(&request, status);
501 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
502 int tag, MPI_Comm comm)
504 MPI_Request request =
505 build_request(buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
506 comm, NON_PERSISTENT | SEND);
507 smpi_mpi_start(request);
508 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
512 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
513 int dst, int tag, MPI_Comm comm)
515 MPI_Request request =
516 build_request(buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
517 comm, NON_PERSISTENT | SSEND | SEND);
519 smpi_mpi_start(request);
520 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
523 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
524 int dst, int sendtag, void *recvbuf, int recvcount,
525 MPI_Datatype recvtype, int src, int recvtag,
526 MPI_Comm comm, MPI_Status * status)
528 MPI_Request requests[2];
530 int myid=smpi_process_index();
531 if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
532 smpi_datatype_copy(sendbuf, sendcount, sendtype,
533 recvbuf, recvcount, recvtype);
537 smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
539 smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
540 smpi_mpi_startall(2, requests);
541 smpi_mpi_waitall(2, requests, stats);
542 if(status != MPI_STATUS_IGNORE) {
543 // Copy receive status
548 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
550 return status->count / smpi_datatype_size(datatype);
553 static void finish_wait(MPI_Request * request, MPI_Status * status)
555 MPI_Request req = *request;
556 if(status != MPI_STATUS_IGNORE)
557 smpi_empty_status(status);
559 if(!(req->detached && req->flags & SEND)){
560 if(status != MPI_STATUS_IGNORE) {
561 int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
562 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
563 status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
564 status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
565 // this handles the case were size in receive differs from size in send
566 // FIXME: really this should just contain the count of receive-type blocks,
568 status->count = req->real_size;
571 print_request("Finishing", req);
572 MPI_Datatype datatype = req->old_type;
574 if(datatype->has_subtype == 1){
575 // This part handles the problem of non-contignous memory
576 // the unserialization at the reception
577 s_smpi_subtype_t *subtype = datatype->substruct;
578 if(req->flags & RECV) {
579 subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
581 if(req->detached == 0) free(req->buf);
583 smpi_datatype_unuse(datatype);
588 if (TRACE_smpi_view_internals()) {
589 if(req->flags & RECV){
590 int rank = smpi_process_index();
591 int src_traced = smpi_group_index(smpi_comm_group(req->comm), req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
592 TRACE_smpi_recv(rank, src_traced, rank);
597 if(req->detached_sender!=NULL){
598 smpi_mpi_request_free(&(req->detached_sender));
601 if(req->flags & NON_PERSISTENT) {
602 smpi_mpi_request_free(request);
608 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
611 //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
612 if ((*request)->action == NULL)
615 flag = simcall_comm_test((*request)->action);
617 finish_wait(request, status);
618 request=MPI_REQUEST_NULL;
620 smpi_empty_status(status);
625 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
632 *index = MPI_UNDEFINED;
635 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
636 map = xbt_new(int, count);
638 for(i = 0; i < count; i++) {
639 if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
640 xbt_dynar_push(comms, &requests[i]->action);
646 i = simcall_comm_testany(comms);
647 // not MPI_UNDEFINED, as this is a simix return code
650 finish_wait(&requests[*index], status);
654 //all requests are null or inactive, return true
656 smpi_empty_status(status);
659 xbt_dynar_free(&comms);
666 int smpi_mpi_testall(int count, MPI_Request requests[],
670 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
673 for(i=0; i<count; i++){
674 if(requests[i]!= MPI_REQUEST_NULL){
675 if (smpi_mpi_test(&requests[i], pstat)!=1){
679 smpi_empty_status(pstat);
681 if(status != MPI_STATUSES_IGNORE) {
688 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
690 //FIXME find another wait to avoid busy waiting ?
691 // the issue here is that we have to wait on a nonexistent comm
693 smpi_mpi_iprobe(source, tag, comm, &flag, status);
694 XBT_DEBUG("Busy Waiting on probing : %d", flag);
698 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
700 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,
701 comm, NON_PERSISTENT | RECV);
703 //to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
704 double sleeptime= sg_cfg_get_double("smpi/iprobe");
705 //multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
706 static int nsleeps = 1;
708 simcall_process_sleep(sleeptime);
710 // behave like a receive, but don't do it
713 print_request("New iprobe", request);
714 // We have to test both mailboxes as we don't know if we will receive one one or another
715 if (sg_cfg_get_int("smpi/async_small_thres")>0){
716 mailbox = smpi_process_mailbox_small();
717 XBT_DEBUG("trying to probe the perm recv mailbox");
718 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
720 if (request->action==NULL){
721 mailbox = smpi_process_mailbox();
722 XBT_DEBUG("trying to probe the other mailbox");
723 request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
727 MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
729 if(status != MPI_STATUS_IGNORE) {
730 status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
731 status->MPI_TAG = req->tag;
732 status->MPI_ERROR = MPI_SUCCESS;
733 status->count = req->real_size;
735 nsleeps=1;//reset the number of sleeps we will do next time
741 smpi_mpi_request_free(&request);
746 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
748 print_request("Waiting", *request);
749 if ((*request)->action != NULL) { // this is not a detached send
750 simcall_comm_wait((*request)->action, -1.0);
752 finish_wait(request, status);
754 // FIXME for a detached send, finish_wait is not called:
757 int smpi_mpi_waitany(int count, MPI_Request requests[],
764 index = MPI_UNDEFINED;
766 // Wait for a request to complete
767 comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
768 map = xbt_new(int, count);
770 XBT_DEBUG("Wait for one of %d", count);
771 for(i = 0; i < count; i++) {
772 if(requests[i] != MPI_REQUEST_NULL) {
773 if (requests[i]->action != NULL) {
774 XBT_DEBUG("Waiting any %p ", requests[i]);
775 xbt_dynar_push(comms, &requests[i]->action);
779 //This is a finished detached request, let's return this one
780 size=0;//so we free the dynar but don't do the waitany call
782 finish_wait(&requests[i], status);//cleanup if refcount = 0
783 requests[i]=MPI_REQUEST_NULL;//set to null
789 i = simcall_comm_waitany(comms);
791 // not MPI_UNDEFINED, as this is a simix return code
794 finish_wait(&requests[index], status);
798 xbt_dynar_free(&comms);
801 if (index==MPI_UNDEFINED)
802 smpi_empty_status(status);
807 int smpi_mpi_waitall(int count, MPI_Request requests[],
812 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
813 int retvalue = MPI_SUCCESS;
814 //tag invalid requests in the set
815 if (status != MPI_STATUSES_IGNORE) {
816 for (c = 0; c < count; c++) {
817 if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL) {
818 smpi_empty_status(&status[c]);
819 } else if (requests[c]->src == MPI_PROC_NULL) {
820 smpi_empty_status(&status[c]);
821 status[c].MPI_SOURCE = MPI_PROC_NULL;
825 for(c = 0; c < count; c++) {
826 if (MC_is_active()) {
827 smpi_mpi_wait(&requests[c], pstat);
830 index = smpi_mpi_waitany(count, requests, pstat);
831 if (index == MPI_UNDEFINED)
833 requests[index]=MPI_REQUEST_NULL;
835 if (status != MPI_STATUSES_IGNORE) {
836 status[index] = *pstat;
837 if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
838 retvalue = MPI_ERR_IN_STATUS;
845 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
850 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
853 for(i = 0; i < incount; i++)
855 index=smpi_mpi_waitany(incount, requests, pstat);
856 if(index!=MPI_UNDEFINED){
857 indices[count] = index;
859 if(status != MPI_STATUSES_IGNORE) {
860 status[index] = *pstat;
862 requests[index]=MPI_REQUEST_NULL;
864 return MPI_UNDEFINED;
870 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
873 int i, count, count_dead;
875 MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
879 for(i = 0; i < incount; i++) {
880 if((requests[i] != MPI_REQUEST_NULL)) {
881 if(smpi_mpi_test(&requests[i], pstat)) {
884 if(status != MPI_STATUSES_IGNORE) {
887 requests[i]=MPI_REQUEST_NULL;
894 if(count_dead==incount)return MPI_UNDEFINED;
898 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
901 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
902 nary_tree_bcast(buf, count, datatype, root, comm, 4);
905 void smpi_mpi_barrier(MPI_Comm comm)
907 // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
908 nary_tree_barrier(comm, 4);
911 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
912 void *recvbuf, int recvcount, MPI_Datatype recvtype,
913 int root, MPI_Comm comm)
915 int system_tag = COLL_TAG_GATHER;
916 int rank, size, src, index;
917 MPI_Aint lb = 0, recvext = 0;
918 MPI_Request *requests;
920 rank = smpi_comm_rank(comm);
921 size = smpi_comm_size(comm);
923 // Send buffer to root
924 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
926 // FIXME: check for errors
927 smpi_datatype_extent(recvtype, &lb, &recvext);
928 // Local copy from root
929 smpi_datatype_copy(sendbuf, sendcount, sendtype,
930 (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
931 // Receive buffers from senders
932 requests = xbt_new(MPI_Request, size - 1);
934 for(src = 0; src < size; src++) {
936 requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
938 src, system_tag, comm);
942 // Wait for completion of irecv's.
943 smpi_mpi_startall(size - 1, requests);
944 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
950 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
951 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
955 int rank = smpi_process_index();
958 /* arbitrarily choose root as rank 0 */
959 size = smpi_comm_size(comm);
961 displs = xbt_new(int, size);
962 for (i = 0; i < size; i++) {
964 count += recvcounts[i];
966 tmpbuf=(void*)xbt_malloc(count*smpi_datatype_get_extent(datatype));
967 mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
968 smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
969 recvcounts[rank], datatype, 0, comm);
974 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
975 void *recvbuf, int *recvcounts, int *displs,
976 MPI_Datatype recvtype, int root, MPI_Comm comm)
978 int system_tag = COLL_TAG_GATHERV;
979 int rank, size, src, index;
980 MPI_Aint lb = 0, recvext = 0;
981 MPI_Request *requests;
983 rank = smpi_comm_rank(comm);
984 size = smpi_comm_size(comm);
986 // Send buffer to root
987 smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
989 // FIXME: check for errors
990 smpi_datatype_extent(recvtype, &lb, &recvext);
991 // Local copy from root
992 smpi_datatype_copy(sendbuf, sendcount, sendtype,
993 (char *)recvbuf + displs[root] * recvext,
994 recvcounts[root], recvtype);
995 // Receive buffers from senders
996 requests = xbt_new(MPI_Request, size - 1);
998 for(src = 0; src < size; src++) {
1001 smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1002 recvcounts[src], recvtype, src, system_tag, comm);
1006 // Wait for completion of irecv's.
1007 smpi_mpi_startall(size - 1, requests);
1008 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1013 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1014 MPI_Datatype sendtype, void *recvbuf,
1015 int recvcount, MPI_Datatype recvtype,
1018 int system_tag = COLL_TAG_ALLGATHER;
1019 int rank, size, other, index;
1020 MPI_Aint lb = 0, recvext = 0;
1021 MPI_Request *requests;
1023 rank = smpi_comm_rank(comm);
1024 size = smpi_comm_size(comm);
1025 // FIXME: check for errors
1026 smpi_datatype_extent(recvtype, &lb, &recvext);
1027 // Local copy from self
1028 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1029 (char *)recvbuf + rank * recvcount * recvext, recvcount,
1031 // Send/Recv buffers to/from others;
1032 requests = xbt_new(MPI_Request, 2 * (size - 1));
1034 for(other = 0; other < size; other++) {
1037 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1040 requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1041 recvcount, recvtype, other,
1046 // Wait for completion of all comms.
1047 smpi_mpi_startall(2 * (size - 1), requests);
1048 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1052 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1053 MPI_Datatype sendtype, void *recvbuf,
1054 int *recvcounts, int *displs,
1055 MPI_Datatype recvtype, MPI_Comm comm)
1057 int system_tag = COLL_TAG_ALLGATHERV;
1058 int rank, size, other, index;
1059 MPI_Aint lb = 0, recvext = 0;
1060 MPI_Request *requests;
1062 rank = smpi_comm_rank(comm);
1063 size = smpi_comm_size(comm);
1064 // FIXME: check for errors
1065 smpi_datatype_extent(recvtype, &lb, &recvext);
1066 // Local copy from self
1067 smpi_datatype_copy(sendbuf, sendcount, sendtype,
1068 (char *)recvbuf + displs[rank] * recvext,
1069 recvcounts[rank], recvtype);
1070 // Send buffers to others;
1071 requests = xbt_new(MPI_Request, 2 * (size - 1));
1073 for(other = 0; other < size; other++) {
1076 smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1080 smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1081 recvtype, other, system_tag, comm);
1085 // Wait for completion of all comms.
1086 smpi_mpi_startall(2 * (size - 1), requests);
1087 smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1091 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1092 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1093 int root, MPI_Comm comm)
1095 int system_tag = COLL_TAG_SCATTER;
1096 int rank, size, dst, index;
1097 MPI_Aint lb = 0, sendext = 0;
1098 MPI_Request *requests;
1100 rank = smpi_comm_rank(comm);
1101 size = smpi_comm_size(comm);
1103 // Recv buffer from root
1104 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1107 // FIXME: check for errors
1108 smpi_datatype_extent(sendtype, &lb, &sendext);
1109 // Local copy from root
1110 smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1111 sendcount, sendtype, recvbuf, recvcount, recvtype);
1112 // Send buffers to receivers
1113 requests = xbt_new(MPI_Request, size - 1);
1115 for(dst = 0; dst < size; dst++) {
1117 requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1118 sendcount, sendtype, dst,
1123 // Wait for completion of isend's.
1124 smpi_mpi_startall(size - 1, requests);
1125 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1130 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1131 MPI_Datatype sendtype, void *recvbuf, int recvcount,
1132 MPI_Datatype recvtype, int root, MPI_Comm comm)
1134 int system_tag = COLL_TAG_SCATTERV;
1135 int rank, size, dst, index;
1136 MPI_Aint lb = 0, sendext = 0;
1137 MPI_Request *requests;
1139 rank = smpi_comm_rank(comm);
1140 size = smpi_comm_size(comm);
1142 // Recv buffer from root
1143 smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1146 // FIXME: check for errors
1147 smpi_datatype_extent(sendtype, &lb, &sendext);
1148 // Local copy from root
1149 smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1150 sendtype, recvbuf, recvcount, recvtype);
1151 // Send buffers to receivers
1152 requests = xbt_new(MPI_Request, size - 1);
1154 for(dst = 0; dst < size; dst++) {
1157 smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1158 sendtype, dst, system_tag, comm);
1162 // Wait for completion of isend's.
1163 smpi_mpi_startall(size - 1, requests);
1164 smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1169 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1170 MPI_Datatype datatype, MPI_Op op, int root,
1173 int system_tag = COLL_TAG_REDUCE;
1174 int rank, size, src, index;
1175 MPI_Aint lb = 0, dataext = 0;
1176 MPI_Request *requests;
1179 char* sendtmpbuf = (char*) sendbuf;
1180 if( sendbuf == MPI_IN_PLACE ) {
1181 sendtmpbuf = (char *)recvbuf;
1184 rank = smpi_comm_rank(comm);
1185 size = smpi_comm_size(comm);
1186 //non commutative case, use a working algo from openmpi
1187 if(!smpi_op_is_commute(op)){
1188 smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1189 datatype, op, root, comm);
1194 // Send buffer to root
1195 smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1197 // FIXME: check for errors
1198 smpi_datatype_extent(datatype, &lb, &dataext);
1199 // Local copy from root
1200 if (sendtmpbuf && recvbuf)
1201 smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1202 // Receive buffers from senders
1203 //TODO: make a MPI_barrier here ?
1204 requests = xbt_new(MPI_Request, size - 1);
1205 tmpbufs = xbt_new(void *, size - 1);
1207 for(src = 0; src < size; src++) {
1209 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1211 tmpbufs[index] = xbt_malloc(count * dataext);
1213 smpi_irecv_init(tmpbufs[index], count, datatype, src,
1218 // Wait for completion of irecv's.
1219 smpi_mpi_startall(size - 1, requests);
1220 for(src = 0; src < size - 1; src++) {
1221 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1222 XBT_DEBUG("finished waiting any request with index %d", index);
1223 if(index == MPI_UNDEFINED) {
1226 if(op) /* op can be MPI_OP_NULL that does nothing */
1227 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1229 for(index = 0; index < size - 1; index++) {
1230 xbt_free(tmpbufs[index]);
1237 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1238 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1240 smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1241 smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1244 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1245 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1247 int system_tag = 888;
1248 int rank, size, other, index;
1249 MPI_Aint lb = 0, dataext = 0;
1250 MPI_Request *requests;
1253 rank = smpi_comm_rank(comm);
1254 size = smpi_comm_size(comm);
1256 // FIXME: check for errors
1257 smpi_datatype_extent(datatype, &lb, &dataext);
1259 // Local copy from self
1260 smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1262 // Send/Recv buffers to/from others;
1263 requests = xbt_new(MPI_Request, size - 1);
1264 tmpbufs = xbt_new(void *, rank);
1266 for(other = 0; other < rank; other++) {
1267 // FIXME: possibly overkill we we have contiguous/noncontiguous data
1269 tmpbufs[index] = xbt_malloc(count * dataext);
1271 smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1275 for(other = rank + 1; other < size; other++) {
1277 smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1280 // Wait for completion of all comms.
1281 smpi_mpi_startall(size - 1, requests);
1282 for(other = 0; other < size - 1; other++) {
1283 index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1284 if(index == MPI_UNDEFINED) {
1288 // #Request is below rank: it's a irecv
1289 smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1292 for(index = 0; index < rank; index++) {
1293 xbt_free(tmpbufs[index]);