1 /* Copyright (c) 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. */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
13 "Logging specific to SIMIX (network)");
15 static xbt_dict_t rdv_points = NULL;
16 unsigned long int smx_total_comms = 0;
18 static void SIMIX_waitany_req_remove_from_actions(smx_req_t req);
19 static void SIMIX_comm_copy_data(smx_action_t comm);
20 static smx_action_t SIMIX_comm_new(e_smx_comm_type_t type);
21 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm);
22 static smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
23 int (*match_fun)(void *, void *), void *);
24 static void SIMIX_rdv_free(void *data);
26 void SIMIX_network_init(void)
28 rdv_points = xbt_dict_new_homogeneous(SIMIX_rdv_free);
31 void SIMIX_network_exit(void)
33 xbt_dict_free(&rdv_points);
36 /******************************************************************************/
37 /* Rendez-Vous Points */
38 /******************************************************************************/
40 smx_rdv_t SIMIX_rdv_create(const char *name)
42 /* two processes may have pushed the same rdv_create request at the same time */
43 smx_rdv_t rdv = name ? xbt_dict_get_or_null(rdv_points, name) : NULL;
46 rdv = xbt_new0(s_smx_rvpoint_t, 1);
47 rdv->name = name ? xbt_strdup(name) : NULL;
48 rdv->comm_fifo = xbt_fifo_new();
51 xbt_dict_set(rdv_points, rdv->name, rdv, NULL);
56 void SIMIX_rdv_destroy(smx_rdv_t rdv)
59 xbt_dict_remove(rdv_points, rdv->name);
62 void SIMIX_rdv_free(void *data)
64 smx_rdv_t rdv = (smx_rdv_t) data;
66 xbt_fifo_free(rdv->comm_fifo);
70 smx_rdv_t SIMIX_rdv_get_by_name(const char *name)
72 return xbt_dict_get_or_null(rdv_points, name);
75 int SIMIX_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host)
77 smx_action_t comm = NULL;
78 xbt_fifo_item_t item = NULL;
81 xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_action_t) {
82 if (comm->comm.src_proc->smx_host == host)
89 smx_action_t SIMIX_rdv_get_head(smx_rdv_t rdv)
91 return xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
95 * \brief Push a communication request into a rendez-vous point
96 * \param rdv The rendez-vous point
97 * \param comm The communication request
99 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm)
101 xbt_fifo_push(rdv->comm_fifo, comm);
102 comm->comm.rdv = rdv;
106 * \brief Remove a communication request from a rendez-vous point
107 * \param rdv The rendez-vous point
108 * \param comm The communication request
110 XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_action_t comm)
112 xbt_fifo_remove(rdv->comm_fifo, comm);
113 comm->comm.rdv = NULL;
117 * \brief Wrapper to SIMIX_rdv_get_request
119 smx_action_t SIMIX_comm_get_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data) {
120 return SIMIX_rdv_get_request(rdv, SIMIX_COMM_SEND, match_fun, data);
124 * \brief Checks if there is a communication action queued in a rendez-vous matching our needs
125 * \param type The type of communication we are looking for (comm_send, comm_recv)
126 * \return The communication action if found, NULL otherwise
128 smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
129 int (*match_fun)(void *, void *), void *data)
131 // FIXME rewrite this function by using SIMIX_rdv_has_send/recv_match
133 xbt_fifo_item_t item;
134 void* req_data = NULL;
136 xbt_fifo_foreach(rdv->comm_fifo, item, action, smx_action_t) {
137 if (action->comm.type == SIMIX_COMM_SEND) {
138 req_data = action->comm.src_data;
139 } else if (action->comm.type == SIMIX_COMM_RECEIVE) {
140 req_data = action->comm.dst_data;
142 if (action->comm.type == type && (!match_fun || match_fun(data, req_data))) {
143 XBT_DEBUG("Found a matching communication action %p", action);
144 xbt_fifo_remove_item(rdv->comm_fifo, item);
145 xbt_fifo_free_item(item);
146 action->comm.refcount++;
147 action->comm.rdv = NULL;
150 XBT_DEBUG("Sorry, communication action %p does not match our needs:"
151 " its type is %d but we are looking for a comm of type %d",
152 action, action->comm.type, type);
154 XBT_DEBUG("No matching communication action found");
159 * \brief Checks if there is a send communication action
160 * queued in a rendez-vous matching our needs.
161 * \return 1 if found, 0 otherwise
163 int SIMIX_comm_has_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data) {
166 xbt_fifo_item_t item;
168 xbt_fifo_foreach(rdv->comm_fifo, item, action, smx_action_t){
169 if (action->comm.type == SIMIX_COMM_SEND
170 && (!match_fun || match_fun(data, action->comm.src_data))) {
171 XBT_DEBUG("Found a matching communication action %p", action);
175 XBT_DEBUG("No matching communication action found");
180 * \brief Checks if there is a recv communication action
181 * queued in a rendez-vous matching our needs.
182 * \return 1 if found, 0 otherwise
184 int SIMIX_comm_has_recv_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data) {
187 xbt_fifo_item_t item;
189 xbt_fifo_foreach(rdv->comm_fifo, item, action, smx_action_t) {
190 if (action->comm.type == SIMIX_COMM_RECEIVE
191 && (!match_fun || match_fun(data, action->comm.dst_data))) {
192 XBT_DEBUG("Found a matching communication action %p", action);
196 XBT_DEBUG("No matching communication action found");
200 /******************************************************************************/
201 /* Comunication Actions */
202 /******************************************************************************/
205 * \brief Creates a new comunicate action
206 * \param type The type of request (comm_send, comm_recv)
207 * \return The new comunicate action
209 smx_action_t SIMIX_comm_new(e_smx_comm_type_t type)
213 /* alloc structures */
214 act = xbt_mallocator_get(simix_global->action_mallocator);
216 act->type = SIMIX_ACTION_COMMUNICATE;
217 act->state = SIMIX_WAITING;
219 /* set communication */
220 act->comm.type = type;
221 act->comm.refcount = 1;
223 #ifdef HAVE_LATENCY_BOUND_TRACKING
224 //initialize with unknown value
225 act->latency_limited = -1;
229 act->category = NULL;
232 XBT_DEBUG("Create communicate action %p", act);
239 * \brief Destroy a communicate action
240 * \param action The communicate action to be destroyed
242 void SIMIX_comm_destroy(smx_action_t action)
244 XBT_DEBUG("Destroy action %p (refcount: %d), state: %d",
245 action, action->comm.refcount, action->state);
247 if (action->comm.refcount <= 0) {
248 xbt_backtrace_display_current();
249 xbt_die("the refcount of comm %p is already 0 before decreasing it. "
250 "That's a bug!", action);
252 action->comm.refcount--;
253 if (action->comm.refcount > 0)
255 XBT_DEBUG("Really free communication %p; refcount is now %d", action,
256 action->comm.refcount);
258 #ifdef HAVE_LATENCY_BOUND_TRACKING
259 action->latency_limited = SIMIX_comm_is_latency_bounded( action ) ;
262 xbt_free(action->name);
263 SIMIX_comm_destroy_internal_actions(action);
265 if (action->comm.detached && action->state != SIMIX_DONE) {
266 /* the communication has failed and was detached:
267 * we have to free the buffer */
268 if (action->comm.clean_fun) {
269 action->comm.clean_fun(action->comm.src_buff);
271 action->comm.src_buff = NULL;
274 xbt_mallocator_release(simix_global->action_mallocator, action);
277 void SIMIX_comm_destroy_internal_actions(smx_action_t action)
279 if (action->comm.surf_comm){
280 #ifdef HAVE_LATENCY_BOUND_TRACKING
281 action->latency_limited = SIMIX_comm_is_latency_bounded(action);
283 action->comm.surf_comm->model_type->action_unref(action->comm.surf_comm);
284 action->comm.surf_comm = NULL;
287 if (action->comm.src_timeout){
288 action->comm.src_timeout->model_type->action_unref(action->comm.src_timeout);
289 action->comm.src_timeout = NULL;
292 if (action->comm.dst_timeout){
293 action->comm.dst_timeout->model_type->action_unref(action->comm.dst_timeout);
294 action->comm.dst_timeout = NULL;
298 smx_action_t SIMIX_comm_isend(smx_process_t src_proc, smx_rdv_t rdv,
299 double task_size, double rate,
300 void *src_buff, size_t src_buff_size,
301 int (*match_fun)(void *, void *),
302 void (*clean_fun)(void *), // used to free the action in case of problem after a detached send
308 /* Look for communication request matching our needs.
309 If it is not found then create it and push it into the rendez-vous point */
310 action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_RECEIVE, match_fun, data);
313 action = SIMIX_comm_new(SIMIX_COMM_SEND);
314 SIMIX_rdv_push(rdv, action);
316 action->state = SIMIX_READY;
317 action->comm.type = SIMIX_COMM_READY;
319 xbt_fifo_push(src_proc->comms, action);
321 /* if the communication action is detached then decrease the refcount
322 * by one, so it will be eliminated by the receiver's destroy call */
324 action->comm.detached = 1;
325 action->comm.refcount--;
326 action->comm.clean_fun = clean_fun;
328 action->comm.clean_fun = NULL;
331 /* Setup the communication request */
332 action->comm.src_proc = src_proc;
333 action->comm.task_size = task_size;
334 action->comm.rate = rate;
335 action->comm.src_buff = src_buff;
336 action->comm.src_buff_size = src_buff_size;
337 action->comm.src_data = data;
340 action->state = SIMIX_RUNNING;
344 SIMIX_comm_start(action);
345 return (detached ? NULL : action);
348 smx_action_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_rdv_t rdv,
349 void *dst_buff, size_t *dst_buff_size,
350 int (*match_fun)(void *, void *), void *data)
354 /* Look for communication request matching our needs.
355 * If it is not found then create it and push it into the rendez-vous point
357 action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_SEND, match_fun, data);
360 action = SIMIX_comm_new(SIMIX_COMM_RECEIVE);
361 SIMIX_rdv_push(rdv, action);
363 action->state = SIMIX_READY;
364 action->comm.type = SIMIX_COMM_READY;
366 xbt_fifo_push(dst_proc->comms, action);
368 /* Setup communication request */
369 action->comm.dst_proc = dst_proc;
370 action->comm.dst_buff = dst_buff;
371 action->comm.dst_buff_size = dst_buff_size;
372 action->comm.dst_data = data;
375 action->state = SIMIX_RUNNING;
379 SIMIX_comm_start(action);
383 void SIMIX_pre_comm_wait(smx_req_t req, smx_action_t action, double timeout, int idx)
386 /* the request may be a wait, a send or a recv */
389 /* Associate this request to the action */
390 xbt_fifo_push(action->request_list, req);
391 req->issuer->waiting_action = action;
395 action->state = SIMIX_DONE;
397 /* If we reached this point, the wait request must have a timeout */
398 /* Otherwise it shouldn't be enabled and executed by the MC */
402 if (action->comm.src_proc == req->issuer)
403 action->state = SIMIX_SRC_TIMEOUT;
405 action->state = SIMIX_DST_TIMEOUT;
408 SIMIX_comm_finish(action);
412 /* If the action has already finish perform the error handling, */
413 /* otherwise set up a waiting timeout on the right side */
414 if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
415 SIMIX_comm_finish(action);
416 } else { /* if (timeout >= 0) { we need a surf sleep action even when there is no timeout, otherwise surf won't tell us when the host fails */
417 sleep = surf_workstation_model->extension.workstation.sleep(req->issuer->smx_host->host, timeout);
418 surf_workstation_model->action_data_set(sleep, action);
420 if (req->issuer == action->comm.src_proc)
421 action->comm.src_timeout = sleep;
423 action->comm.dst_timeout = sleep;
427 void SIMIX_pre_comm_test(smx_req_t req)
429 smx_action_t action = req->comm_test.comm;
432 req->comm_test.result = action->comm.src_proc && action->comm.dst_proc;
433 if(req->comm_test.result){
434 action->state = SIMIX_DONE;
435 xbt_fifo_push(action->request_list, req);
436 SIMIX_comm_finish(action);
438 SIMIX_request_answer(req);
443 req->comm_test.result = (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING);
444 if (req->comm_test.result) {
445 xbt_fifo_push(action->request_list, req);
446 SIMIX_comm_finish(action);
448 SIMIX_request_answer(req);
452 void SIMIX_pre_comm_testany(smx_req_t req, int idx)
456 xbt_dynar_t actions = req->comm_testany.comms;
457 req->comm_testany.result = -1;
461 SIMIX_request_answer(req);
463 action = xbt_dynar_get_as(actions, idx, smx_action_t);
464 req->comm_testany.result = idx;
465 xbt_fifo_push(action->request_list, req);
466 action->state = SIMIX_DONE;
467 SIMIX_comm_finish(action);
472 xbt_dynar_foreach(req->comm_testany.comms,cursor,action) {
473 if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
474 req->comm_testany.result = cursor;
475 xbt_fifo_push(action->request_list, req);
476 SIMIX_comm_finish(action);
480 SIMIX_request_answer(req);
483 void SIMIX_pre_comm_waitany(smx_req_t req, int idx)
486 unsigned int cursor = 0;
487 xbt_dynar_t actions = req->comm_waitany.comms;
490 action = xbt_dynar_get_as(actions, idx, smx_action_t);
491 xbt_fifo_push(action->request_list, req);
492 req->comm_waitany.result = idx;
493 action->state = SIMIX_DONE;
494 SIMIX_comm_finish(action);
498 xbt_dynar_foreach(actions, cursor, action){
499 /* associate this request to the the action */
500 xbt_fifo_push(action->request_list, req);
502 /* see if the action is already finished */
503 if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING){
504 SIMIX_comm_finish(action);
510 void SIMIX_waitany_req_remove_from_actions(smx_req_t req)
513 unsigned int cursor = 0;
514 xbt_dynar_t actions = req->comm_waitany.comms;
516 xbt_dynar_foreach(actions, cursor, action){
517 xbt_fifo_remove(action->request_list, req);
522 * \brief Start the simulation of a communication request
523 * \param action The communication action
526 XBT_INLINE void SIMIX_comm_start(smx_action_t action)
528 /* If both the sender and the receiver are already there, start the communication */
529 if (action->state == SIMIX_READY) {
531 smx_host_t sender = action->comm.src_proc->smx_host;
532 smx_host_t receiver = action->comm.dst_proc->smx_host;
534 XBT_DEBUG("Starting communication %p from '%s' to '%s'", action,
535 SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
537 action->comm.surf_comm = surf_workstation_model->extension.workstation.
538 communicate(sender->host, receiver->host, action->comm.task_size, action->comm.rate);
540 surf_workstation_model->action_data_set(action->comm.surf_comm, action);
542 action->state = SIMIX_RUNNING;
544 /* If a link is failed, detect it immediately */
545 if (surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED) {
546 XBT_DEBUG("Communication from '%s' to '%s' failed to start because of a link failure",
547 SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
548 action->state = SIMIX_LINK_FAILURE;
549 SIMIX_comm_destroy_internal_actions(action);
552 /* If any of the process is suspend, create the action but stop its execution,
553 it will be restarted when the sender process resume */
554 if (SIMIX_process_is_suspended(action->comm.src_proc) ||
555 SIMIX_process_is_suspended(action->comm.dst_proc)) {
556 /* FIXME: check what should happen with the action state */
557 surf_workstation_model->suspend(action->comm.surf_comm);
563 * \brief Answers the SIMIX requests associated to a communication action.
564 * \param action a finished communication action
566 void SIMIX_comm_finish(smx_action_t action)
568 volatile unsigned int destroy_count = 0;
571 while ((req = xbt_fifo_shift(action->request_list))) {
573 /* If a waitany request is waiting for this action to finish, then remove
574 it from the other actions in the waitany list. Afterwards, get the
575 position of the actual action in the waitany request's actions dynar and
576 return it as the result of the call */
577 if (req->call == REQ_COMM_WAITANY) {
578 SIMIX_waitany_req_remove_from_actions(req);
580 req->comm_waitany.result = xbt_dynar_search(req->comm_waitany.comms, &action);
583 /* If the action is still in a rendez-vous point then remove from it */
584 if (action->comm.rdv)
585 SIMIX_rdv_remove(action->comm.rdv, action);
587 XBT_DEBUG("SIMIX_comm_finish: action state = %d", action->state);
589 /* Check out for errors */
590 switch (action->state) {
593 XBT_DEBUG("Communication %p complete!", action);
594 SIMIX_comm_copy_data(action);
597 case SIMIX_SRC_TIMEOUT:
599 THROWF(timeout_error, 0, "Communication timeouted because of sender");
601 CATCH(req->issuer->running_ctx->exception) {
602 req->issuer->doexception = 1;
606 case SIMIX_DST_TIMEOUT:
608 THROWF(timeout_error, 0, "Communication timeouted because of receiver");
610 CATCH(req->issuer->running_ctx->exception) {
611 req->issuer->doexception = 1;
615 case SIMIX_SRC_HOST_FAILURE:
617 if (req->issuer == action->comm.src_proc)
618 THROWF(host_error, 0, "Host failed");
620 THROWF(network_error, 0, "Remote peer failed");
622 CATCH(req->issuer->running_ctx->exception) {
623 req->issuer->doexception = 1;
627 case SIMIX_DST_HOST_FAILURE:
629 if (req->issuer == action->comm.dst_proc)
630 THROWF(host_error, 0, "Host failed");
632 THROWF(network_error, 0, "Remote peer failed");
634 CATCH(req->issuer->running_ctx->exception) {
635 req->issuer->doexception = 1;
639 case SIMIX_LINK_FAILURE:
641 XBT_DEBUG("Link failure in action %p between '%s' and '%s': posting an exception to the issuer: %s (%p) detached:%d",
643 action->comm.src_proc ? action->comm.src_proc->smx_host->name : NULL,
644 action->comm.dst_proc ? action->comm.dst_proc->smx_host->name : NULL,
645 req->issuer->name, req->issuer, action->comm.detached);
646 if (action->comm.src_proc == req->issuer) {
647 XBT_DEBUG("I'm source");
648 } else if (action->comm.dst_proc == req->issuer) {
649 XBT_DEBUG("I'm dest");
651 XBT_DEBUG("I'm neither source nor dest");
653 THROWF(network_error, 0, "Link failure");
655 CATCH(req->issuer->running_ctx->exception) {
656 req->issuer->doexception = 1;
662 if (req->issuer == action->comm.dst_proc) {
663 THROWF(cancel_error, 0, "Communication canceled by the sender");
666 THROWF(cancel_error, 0, "Communication canceled by the receiver");
669 CATCH(req->issuer->running_ctx->exception) {
670 req->issuer->doexception = 1;
675 xbt_die("Unexpected action state in SIMIX_comm_finish: %d", action->state);
678 /* if there is an exception during a waitany or a testany, indicate the position of the failed communication */
679 if (req->issuer->doexception) {
680 if (req->call == REQ_COMM_WAITANY) {
681 req->issuer->running_ctx->exception.value = xbt_dynar_search(req->comm_waitany.comms, &action);
683 else if (req->call == REQ_COMM_TESTANY) {
684 req->issuer->running_ctx->exception.value = xbt_dynar_search(req->comm_testany.comms, &action);
688 req->issuer->waiting_action = NULL;
689 xbt_fifo_remove(req->issuer->comms, action);
690 SIMIX_request_answer(req);
694 while (destroy_count-- > 0)
695 SIMIX_comm_destroy(action);
699 * \brief This function is called when a Surf communication action is finished.
700 * \param action the corresponding Simix communication
702 void SIMIX_post_comm(smx_action_t action)
704 /* Update action state */
705 if (action->comm.src_timeout &&
706 surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_DONE)
707 action->state = SIMIX_SRC_TIMEOUT;
708 else if (action->comm.dst_timeout &&
709 surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_DONE)
710 action->state = SIMIX_DST_TIMEOUT;
711 else if (action->comm.src_timeout &&
712 surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_FAILED)
713 action->state = SIMIX_SRC_HOST_FAILURE;
714 else if (action->comm.dst_timeout &&
715 surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_FAILED)
716 action->state = SIMIX_DST_HOST_FAILURE;
717 else if (action->comm.surf_comm &&
718 surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED) {
719 XBT_DEBUG("Puta madre. Surf says that the link broke");
720 action->state = SIMIX_LINK_FAILURE;
722 action->state = SIMIX_DONE;
724 XBT_DEBUG("SIMIX_post_comm: comm %p, state %d, src_proc %p, dst_proc %p, detached: %d",
725 action, action->state, action->comm.src_proc, action->comm.dst_proc, action->comm.detached);
727 /* destroy the surf actions associated with the Simix communication */
728 SIMIX_comm_destroy_internal_actions(action);
730 /* remove the communication action from the list of pending communications
731 * of both processes (if they still exist) */
732 if (action->comm.src_proc) {
733 xbt_fifo_remove(action->comm.src_proc->comms, action);
735 if (action->comm.dst_proc) {
736 xbt_fifo_remove(action->comm.dst_proc->comms, action);
739 /* if there are requests associated with the action, then answer them */
740 if (xbt_fifo_size(action->request_list)) {
741 SIMIX_comm_finish(action);
745 void SIMIX_comm_cancel(smx_action_t action)
747 /* if the action is a waiting state means that it is still in a rdv */
748 /* so remove from it and delete it */
749 if (action->state == SIMIX_WAITING) {
750 SIMIX_rdv_remove(action->comm.rdv, action);
751 action->state = SIMIX_CANCELED;
753 else if (!MC_IS_ENABLED /* when running the MC there are no surf actions */
754 && (action->state == SIMIX_READY || action->state == SIMIX_RUNNING)) {
756 surf_workstation_model->action_cancel(action->comm.surf_comm);
760 void SIMIX_comm_suspend(smx_action_t action)
762 /*FIXME: shall we suspend also the timeout actions? */
763 surf_workstation_model->suspend(action->comm.surf_comm);
766 void SIMIX_comm_resume(smx_action_t action)
768 /*FIXME: check what happen with the timeouts */
769 surf_workstation_model->resume(action->comm.surf_comm);
773 /************* Action Getters **************/
776 * \brief get the amount remaining from the communication
777 * \param action The communication
779 double SIMIX_comm_get_remains(smx_action_t action)
787 switch (action->state) {
790 remains = surf_workstation_model->get_remains(action->comm.surf_comm);
795 remains = 0; /*FIXME: check what should be returned */
799 remains = 0; /*FIXME: is this correct? */
805 e_smx_state_t SIMIX_comm_get_state(smx_action_t action)
807 return action->state;
811 * \brief Return the user data associated to the sender of the communication
812 * \param action The communication
813 * \return the user data
815 void* SIMIX_comm_get_src_data(smx_action_t action)
817 return action->comm.src_data;
821 * \brief Return the user data associated to the receiver of the communication
822 * \param action The communication
823 * \return the user data
825 void* SIMIX_comm_get_dst_data(smx_action_t action)
827 return action->comm.dst_data;
830 smx_process_t SIMIX_comm_get_src_proc(smx_action_t action)
832 return action->comm.src_proc;
835 smx_process_t SIMIX_comm_get_dst_proc(smx_action_t action)
837 return action->comm.dst_proc;
840 #ifdef HAVE_LATENCY_BOUND_TRACKING
842 * \brief verify if communication is latency bounded
843 * \param comm The communication
845 XBT_INLINE int SIMIX_comm_is_latency_bounded(smx_action_t action)
850 if (action->comm.surf_comm){
851 XBT_DEBUG("Getting latency limited for surf_action (%p)", action->comm.surf_comm);
852 action->latency_limited = surf_workstation_model->get_latency_limited(action->comm.surf_comm);
853 XBT_DEBUG("Action limited is %d", action->latency_limited);
855 return action->latency_limited;
859 /******************************************************************************/
860 /* SIMIX_comm_copy_data callbacks */
861 /******************************************************************************/
862 static void (*SIMIX_comm_copy_data_callback) (smx_action_t, void*, size_t) =
863 &SIMIX_comm_copy_pointer_callback;
866 SIMIX_comm_set_copy_data_callback(void (*callback) (smx_action_t, void*, size_t))
868 SIMIX_comm_copy_data_callback = callback;
871 void SIMIX_comm_copy_pointer_callback(smx_action_t comm, void* buff, size_t buff_size)
873 xbt_assert((buff_size == sizeof(void *)),
874 "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
875 *(void **) (comm->comm.dst_buff) = buff;
878 void SIMIX_comm_copy_buffer_callback(smx_action_t comm, void* buff, size_t buff_size)
880 XBT_DEBUG("Copy the data over");
881 memcpy(comm->comm.dst_buff, buff, buff_size);
884 void smpi_comm_copy_data_callback(smx_action_t comm, void* buff, size_t buff_size)
886 XBT_DEBUG("Copy the data over");
887 memcpy(comm->comm.dst_buff, buff, buff_size);
888 if (comm->comm.detached) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the original buffer available to the application ASAP
890 comm->comm.src_buff = NULL;
895 * \brief Copy the communication data from the sender's buffer to the receiver's one
896 * \param comm The communication
898 void SIMIX_comm_copy_data(smx_action_t comm)
900 size_t buff_size = comm->comm.src_buff_size;
901 /* If there is no data to be copy then return */
902 if (!comm->comm.src_buff || !comm->comm.dst_buff || comm->comm.copied == 1)
905 XBT_DEBUG("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
907 comm->comm.src_proc ? comm->comm.src_proc->smx_host->name : "a finished process",
909 comm->comm.dst_proc ? comm->comm.dst_proc->smx_host->name : "a finished process",
910 comm->comm.dst_buff, buff_size);
912 /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
913 if (comm->comm.dst_buff_size)
914 buff_size = MIN(buff_size, *(comm->comm.dst_buff_size));
916 /* Update the receiver's buffer size to the copied amount */
917 if (comm->comm.dst_buff_size)
918 *comm->comm.dst_buff_size = buff_size;
921 SIMIX_comm_copy_data_callback (comm, comm->comm.src_buff, buff_size);
923 /* Set the copied flag so we copy data only once */
924 /* (this function might be called from both communication ends) */
925 comm->comm.copied = 1;