Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ded44f2000743acb9b244146e87cf868653937cf
[simgrid.git] / src / simix / smx_network.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "xbt/log.h"
9 #include "mc/mc.h"
10 #include "xbt/dict.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_network, simix,
13                                 "Logging specific to SIMIX (network)");
14
15 static xbt_dict_t rdv_points = NULL;
16
17 static XBT_INLINE void SIMIX_comm_start(smx_action_t action);
18 static void SIMIX_comm_finish(smx_action_t action);
19 static void SIMIX_waitany_req_remove_from_actions(smx_req_t req);
20 static void SIMIX_comm_copy_data(smx_action_t comm);
21 static smx_action_t SIMIX_comm_new(e_smx_comm_type_t type);
22 static XBT_INLINE void SIMIX_comm_wait_for_completion(smx_action_t comm,
23                                                       double timeout);
24 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm);
25 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_action_t comm);
26 static smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
27                                                                                   int (*match_fun)(void *, void *), void *);
28 static void SIMIX_rdv_free(void *data);
29
30 void SIMIX_network_init(void)
31 {
32   rdv_points = xbt_dict_new();
33 }
34
35 void SIMIX_network_exit(void)
36 {
37   xbt_dict_free(&rdv_points);
38 }
39
40 /******************************************************************************/
41 /*                           Rendez-Vous Points                               */
42 /******************************************************************************/
43
44 smx_rdv_t SIMIX_rdv_create(const char *name)
45 {
46   /* two processes may have pushed the same rdv_create request at the same time */
47   smx_rdv_t rdv = name ? xbt_dict_get_or_null(rdv_points, name) : NULL;
48
49   if (!rdv) {
50     rdv = xbt_new0(s_smx_rvpoint_t, 1);
51     rdv->name = name ? xbt_strdup(name) : NULL;
52     rdv->comm_fifo = xbt_fifo_new();
53
54     if (name)
55       xbt_dict_set(rdv_points, name, rdv, SIMIX_rdv_free);
56   }
57   return rdv;
58 }
59
60 void SIMIX_rdv_destroy(smx_rdv_t rdv)
61 {
62   if (rdv->name)
63     xbt_dict_remove(rdv_points, rdv->name); 
64 }
65
66 void SIMIX_rdv_free(void *data)
67 {
68   smx_rdv_t rdv = (smx_rdv_t) data;
69   if (rdv->name)
70     xbt_free(rdv->name);
71   xbt_fifo_free(rdv->comm_fifo);
72   xbt_free(rdv);  
73 }
74
75 smx_rdv_t SIMIX_rdv_get_by_name(const char *name)
76 {
77   return xbt_dict_get_or_null(rdv_points, name);
78 }
79
80 int SIMIX_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host)
81 {
82   smx_action_t comm = NULL;
83   xbt_fifo_item_t item = NULL;
84   int count = 0;
85
86   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_action_t) {
87     if (comm->comm.src_proc->smx_host == host)
88       count++;
89   }
90
91   return count;
92 }
93
94 smx_action_t SIMIX_rdv_get_head(smx_rdv_t rdv)
95 {
96   return xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
97 }
98
99 /**
100  *  \brief Push a communication request into a rendez-vous point
101  *  \param rdv The rendez-vous point
102  *  \param comm The communication request
103  */
104 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_action_t comm)
105 {
106   xbt_fifo_push(rdv->comm_fifo, comm);
107   comm->comm.rdv = rdv;
108 }
109
110 /**
111  *  \brief Remove a communication request from a rendez-vous point
112  *  \param rdv The rendez-vous point
113  *  \param comm The communication request
114  */
115 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_action_t comm)
116 {
117   xbt_fifo_remove(rdv->comm_fifo, comm);
118   comm->comm.rdv = NULL;
119 }
120
121 /**
122  *  \brief Checks if there is a communication request queued in a rendez-vous matching our needs
123  *  \param type The type of communication we are looking for (comm_send, comm_recv)
124  *  \return The communication request if found, NULL otherwise
125  */
126 smx_action_t SIMIX_rdv_get_request(smx_rdv_t rdv, e_smx_comm_type_t type,
127                                                                    int (*match_fun)(void *, void *), void *data)
128 {
129   smx_action_t req;
130   xbt_fifo_item_t item;
131   void* req_data = NULL;
132
133   xbt_fifo_foreach(rdv->comm_fifo, item, req, smx_action_t){
134     if(req->comm.type == SIMIX_COMM_SEND) {
135       req_data = req->comm.src_data;
136     } else if(req->comm.type == SIMIX_COMM_RECEIVE) {
137       req_data = req->comm.dst_data;
138     }
139          if(req->comm.type == type && (!match_fun || match_fun(data, req_data))) {
140            xbt_fifo_remove_item(rdv->comm_fifo, item);
141            req->comm.refcount++;
142            req->comm.rdv = NULL;
143            return req;
144          }
145   }
146   DEBUG0("Communication request not found");
147   return NULL;
148 }
149
150 /******************************************************************************/
151 /*                            Comunication Actions                            */
152 /******************************************************************************/
153
154 /**
155  *  \brief Creates a new comunicate action
156  *  \param type The type of request (comm_send, comm_recv)
157  *  \return The new comunicate action
158  */
159 smx_action_t SIMIX_comm_new(e_smx_comm_type_t type)
160 {
161   smx_action_t act;
162
163   /* alloc structures */
164   act = xbt_new0(s_smx_action_t, 1);
165   act->type = SIMIX_ACTION_COMMUNICATE;
166   act->state = SIMIX_WAITING;
167   act->request_list = xbt_fifo_new();
168
169   /* set communication */
170   act->comm.type = type;
171   act->comm.refcount = 1;
172
173 #ifdef HAVE_TRACING
174   act->category = NULL;
175 #endif
176
177   DEBUG1("Create communicate action %p", act);
178
179   return act;
180 }
181
182 /**
183  *  \brief Destroy a communicate action
184  *  \param action The communicate action to be destroyed
185  */
186 void SIMIX_comm_destroy(smx_action_t action)
187 {
188   DEBUG1("Destroy action %p", action);
189
190   if (action->comm.refcount <= 0)
191     xbt_die(bprintf("the refcount of comm %p is already 0 before decreasing it. That's a bug!",action));
192
193 #ifdef HAVE_LATENCY_BOUND_TRACKING
194   //save is latency limited flag to use afterwards
195   if (action->comm.surf_comm) {
196     DEBUG2("adding key %p with latency limited value %d to the dict", action,
197            SIMIX_comm_is_latency_bounded(action));
198     xbt_dicti_set(simix_global->latency_limited_dict, (uintptr_t) action,
199                   SIMIX_comm_is_latency_bounded(action));
200   }
201 #endif
202
203   action->comm.refcount--;
204   if (action->comm.refcount > 0)
205     return;
206   VERB2("Really free communication %p; refcount is now %d", action,
207         action->comm.refcount);
208
209 #ifdef HAVE_TRACING
210   TRACE_smx_action_destroy(action);
211 #endif
212
213   if (action->name)
214     xbt_free(action->name);
215
216   xbt_fifo_free(action->request_list);
217
218   SIMIX_comm_destroy_internal_actions(action);
219
220   xbt_free(action);
221 }
222
223 void SIMIX_comm_destroy_internal_actions(smx_action_t action)
224 {
225   if (action->comm.surf_comm){
226     action->comm.surf_comm->model_type->action_unref(action->comm.surf_comm);
227     action->comm.surf_comm = NULL;
228   }
229
230   if (action->comm.src_timeout){
231     action->comm.src_timeout->model_type->action_unref(action->comm.src_timeout);
232     action->comm.src_timeout = NULL;
233   }
234
235   if (action->comm.dst_timeout){
236     action->comm.dst_timeout->model_type->action_unref(action->comm.dst_timeout);
237     action->comm.dst_timeout = NULL;
238   }
239 }
240
241 smx_action_t SIMIX_comm_isend(smx_process_t src_proc, smx_rdv_t rdv,
242                               double task_size, double rate,
243                               void *src_buff, size_t src_buff_size,
244                               int (*match_fun)(void *, void *), void *data)
245 {
246   smx_action_t action;
247
248   /* Look for communication request matching our needs.
249      If it is not found then create it and push it into the rendez-vous point */
250   action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_RECEIVE, match_fun, data);
251
252   if (!action) {
253     action = SIMIX_comm_new(SIMIX_COMM_SEND);
254     SIMIX_rdv_push(rdv, action);
255   } else {
256     action->state = SIMIX_READY;
257     action->comm.type = SIMIX_COMM_READY;
258   }
259
260   /* Setup the communication request */
261   action->comm.src_proc = src_proc;
262   action->comm.task_size = task_size;
263   action->comm.rate = rate;
264   action->comm.src_buff = src_buff;
265   action->comm.src_buff_size = src_buff_size;
266   action->comm.src_data = data;
267
268   if (MC_IS_ENABLED) {
269     action->state = SIMIX_RUNNING;
270     return action;
271   }
272
273   SIMIX_comm_start(action);
274   return action;
275 }
276
277 smx_action_t SIMIX_comm_irecv(smx_process_t dst_proc, smx_rdv_t rdv,
278                       void *dst_buff, size_t *dst_buff_size,
279                       int (*match_fun)(void *, void *), void *data)
280 {
281   smx_action_t action;
282
283   /* Look for communication request matching our needs.
284    * If it is not found then create it and push it into the rendez-vous point
285    */
286   action = SIMIX_rdv_get_request(rdv, SIMIX_COMM_SEND, match_fun, data);
287
288   if (!action) {
289     action = SIMIX_comm_new(SIMIX_COMM_RECEIVE);
290     SIMIX_rdv_push(rdv, action);
291   } else {
292     action->state = SIMIX_READY;
293     action->comm.type = SIMIX_COMM_READY;
294   }
295
296   /* Setup communication request */
297   action->comm.dst_proc = dst_proc;
298   action->comm.dst_buff = dst_buff;
299   action->comm.dst_buff_size = dst_buff_size;
300   action->comm.dst_data = data;
301
302   if (MC_IS_ENABLED) {
303     action->state = SIMIX_RUNNING;
304     return action;
305   }
306
307   SIMIX_comm_start(action);
308   return action;
309 }
310
311 void SIMIX_pre_comm_wait(smx_req_t req)
312 {
313   smx_action_t action = req->comm_wait.comm;
314   double timeout = req->comm_wait.timeout;
315   surf_action_t sleep;
316
317   /* Associate this request to the action */
318   xbt_fifo_push(action->request_list, req);
319   req->issuer->waiting_action = action;
320
321   if (MC_IS_ENABLED){
322     action->state = SIMIX_DONE;
323     SIMIX_comm_finish(action);
324   }
325
326   /* If the action has already finish perform the error handling, */
327   /* otherwise set up a waiting timeout on the right side         */
328   if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
329     SIMIX_comm_finish(action);
330   } 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 */
331     sleep = surf_workstation_model->extension.workstation.sleep(req->issuer->smx_host->host, timeout);
332     surf_workstation_model->action_data_set(sleep, action);
333
334     if (req->issuer == action->comm.src_proc)
335       action->comm.src_timeout = sleep;
336     else
337       action->comm.dst_timeout = sleep;
338   }
339 }
340
341 void SIMIX_pre_comm_test(smx_req_t req)
342 {
343   smx_action_t action = req->comm_test.comm;
344   req->comm_test.result = (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING);
345
346   if (req->comm_test.result) {
347     xbt_fifo_push(action->request_list, req);
348     SIMIX_comm_finish(action);
349   }
350   else {
351     SIMIX_request_answer(req);
352   }
353 }
354
355 void SIMIX_pre_comm_testany(smx_req_t req)
356 {
357   unsigned int cursor;
358   smx_action_t action;
359   req->comm_testany.result = -1;
360   xbt_dynar_foreach(req->comm_testany.comms,cursor,action) {
361     if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING) {
362       req->comm_testany.result = cursor;
363       xbt_fifo_push(action->request_list, req);
364       SIMIX_comm_finish(action);
365       break;
366     }
367   }
368   SIMIX_request_answer(req);
369 }
370
371 void SIMIX_pre_comm_waitany(smx_req_t req)
372 {
373   smx_action_t action;
374   unsigned int cursor = 0;
375   xbt_dynar_t actions = req->comm_waitany.comms;
376   xbt_dynar_foreach(actions, cursor, action){
377     /* Associate this request to the action */
378     xbt_fifo_push(action->request_list, req);
379     if (action->state != SIMIX_WAITING && action->state != SIMIX_RUNNING){
380       SIMIX_comm_finish(action);
381       break;
382     }
383   }
384 }
385
386 void SIMIX_waitany_req_remove_from_actions(smx_req_t req)
387 {
388   smx_action_t action;
389   unsigned int cursor = 0;
390   xbt_dynar_t actions = req->comm_waitany.comms;
391
392   xbt_dynar_foreach(actions, cursor, action){
393     xbt_fifo_remove(action->request_list, req);
394   }
395 }
396
397 /**
398  *  \brief Start the simulation of a communication request
399  *  \param action The communication action
400  */
401 static XBT_INLINE void SIMIX_comm_start(smx_action_t action)
402 {
403   /* If both the sender and the receiver are already there, start the communication */
404   if (action->state == SIMIX_READY) {
405     smx_host_t sender = action->comm.src_proc->smx_host;
406     smx_host_t receiver = action->comm.dst_proc->smx_host;
407
408     DEBUG3("Starting communication %p from '%s' to '%s'", action,
409            SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
410
411     action->comm.surf_comm = surf_workstation_model->extension.workstation.
412         communicate(sender->host, receiver->host, action->comm.task_size, action->comm.rate);
413
414     surf_workstation_model->action_data_set(action->comm.surf_comm, action);
415
416     action->state = SIMIX_RUNNING;
417
418 #ifdef HAVE_TRACING
419     TRACE_smx_action_communicate(action, action->comm.src_proc);
420 #endif
421
422     /* If a link is failed, detect it immediately */
423     if (surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED) {
424       DEBUG2("Communication from '%s' to '%s' failed to start because of a link failure",
425           SIMIX_host_get_name(sender), SIMIX_host_get_name(receiver));
426       action->state = SIMIX_LINK_FAILURE;
427       SIMIX_comm_destroy_internal_actions(action);
428     }
429
430     /* If any of the process is suspend, create the action but stop its execution,
431        it will be restarted when the sender process resume */
432     if (SIMIX_process_is_suspended(action->comm.src_proc) ||
433         SIMIX_process_is_suspended(action->comm.dst_proc)) {
434       /* FIXME: check what should happen with the action state */
435       surf_workstation_model->suspend(action->comm.surf_comm);
436     }
437   }
438 }
439
440 void SIMIX_comm_finish(smx_action_t action)
441 {
442   smx_req_t req;
443
444   while ((req = xbt_fifo_shift(action->request_list))) {
445
446     /* If a waitany request is waiting for this action to finish, then remove
447        it from the other actions in the waitany list. Afterwards, get the
448        position of the actual action in the waitany request's actions dynar and
449        return it as the result of the call */
450     if (req->call == REQ_COMM_WAITANY) {
451       SIMIX_waitany_req_remove_from_actions(req);
452       req->comm_waitany.result = xbt_dynar_search(req->comm_waitany.comms, &action);
453     }
454
455     /* If the action is still in a rendez-vous point then remove from it */
456     if (action->comm.rdv)
457       SIMIX_rdv_remove(action->comm.rdv, action);
458
459     DEBUG1("SIMIX_comm_finish: action state = %d", action->state);
460
461     /* Check out for errors */
462     switch (action->state) {
463
464       case SIMIX_DONE:
465         DEBUG1("Communication %p complete!", action);
466         SIMIX_comm_copy_data(action);
467         break;
468
469       case SIMIX_SRC_TIMEOUT:
470         TRY {
471           THROW0(timeout_error, 0, "Communication timeouted because of sender");
472         }
473         CATCH(req->issuer->running_ctx->exception) {
474           req->issuer->doexception = 1;
475         }
476         break;
477
478       case SIMIX_DST_TIMEOUT:
479         TRY {
480           THROW0(timeout_error, 0, "Communication timeouted because of receiver");
481         }
482         CATCH(req->issuer->running_ctx->exception) {
483           req->issuer->doexception = 1;
484         }
485         break;
486
487       case SIMIX_SRC_HOST_FAILURE:
488         TRY {
489           if (req->issuer == action->comm.src_proc)
490             THROW0(host_error, 0, "Host failed");
491           else
492             THROW0(network_error, 0, "Remote peer failed");
493         }
494         CATCH(req->issuer->running_ctx->exception) {
495           req->issuer->doexception = 1;
496         }
497         break;
498
499       case SIMIX_DST_HOST_FAILURE:
500         TRY {
501           if (req->issuer == action->comm.dst_proc)
502             THROW0(host_error, 0, "Host failed");
503           else
504             THROW0(network_error, 0, "Remote peer failed");
505         }
506         CATCH(req->issuer->running_ctx->exception) {
507           req->issuer->doexception = 1;
508         }
509         break;
510
511       case SIMIX_LINK_FAILURE:
512         TRY {
513           DEBUG5("Link failure in action %p between '%s' and '%s': posting an exception to the issuer: %s (%p)",
514               action, action->comm.src_proc->smx_host->name, action->comm.dst_proc->smx_host->name,
515               req->issuer->name, req->issuer);
516           THROW0(network_error, 0, "Link failure");
517         }
518         CATCH(req->issuer->running_ctx->exception) {
519           req->issuer->doexception = 1;
520         }
521         break;
522
523       default:
524         THROW_IMPOSSIBLE;
525     }
526     req->issuer->waiting_action = NULL;
527     SIMIX_request_answer(req);
528   }
529 }
530
531 void SIMIX_post_comm(smx_action_t action)
532 {
533   /* Update action state */
534   if (action->comm.src_timeout &&
535      surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_DONE)
536      action->state = SIMIX_SRC_TIMEOUT;
537   else if (action->comm.dst_timeout &&
538           surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_DONE)
539      action->state = SIMIX_DST_TIMEOUT;
540   else if (action->comm.src_timeout &&
541           surf_workstation_model->action_state_get(action->comm.src_timeout) == SURF_ACTION_FAILED)
542      action->state = SIMIX_SRC_HOST_FAILURE;
543   else if (action->comm.dst_timeout &&
544           surf_workstation_model->action_state_get(action->comm.dst_timeout) == SURF_ACTION_FAILED)
545      action->state = SIMIX_DST_HOST_FAILURE;
546   else if (action->comm.surf_comm &&
547           surf_workstation_model->action_state_get(action->comm.surf_comm) == SURF_ACTION_FAILED)
548      action->state = SIMIX_LINK_FAILURE;
549   else
550     action->state = SIMIX_DONE;
551
552   DEBUG1("SIMIX_post_comm: action state = %d", action->state);
553
554   /* After this point the surf actions associated with the simix communicate
555      action are no longer needed, thus we delete them. */
556   SIMIX_comm_destroy_internal_actions(action);
557
558   /* If there are requests associated with the action, then answer them */
559   if (xbt_fifo_size(action->request_list))
560     SIMIX_comm_finish(action);
561 }
562
563 void SIMIX_comm_cancel(smx_action_t action)
564 {
565   /* If the action is a waiting state means that it is still in a rdv */
566   /* so remove from it and delete it */
567   if (action->state == SIMIX_WAITING) {
568     SIMIX_rdv_remove(action->comm.rdv, action);
569     action->state = SIMIX_FAILED;
570   } else {
571     surf_workstation_model->action_cancel(action->comm.surf_comm);
572   }
573 }
574
575 void SIMIX_comm_suspend(smx_action_t action)
576 {
577   /*FIXME: shall we suspend also the timeout actions? */
578   surf_workstation_model->suspend(action->comm.surf_comm);
579 }
580
581 void SIMIX_comm_resume(smx_action_t action)
582 {
583   /*FIXME: check what happen with the timeouts */
584   surf_workstation_model->resume(action->comm.surf_comm);
585 }
586
587
588 /************* Action Getters **************/
589
590 /**
591  *  \brief get the amount remaining from the communication
592  *  \param action The communication
593  */
594 double SIMIX_comm_get_remains(smx_action_t action)
595 {
596   double remains;
597
598   switch (action->state) {
599
600     case SIMIX_RUNNING:
601       remains = surf_workstation_model->get_remains(action->comm.surf_comm);
602       break;
603
604     case SIMIX_WAITING:
605     case SIMIX_READY:
606       remains = 0; /*FIXME: check what should be returned */
607       break;
608
609     default:
610       remains = 0; /*FIXME: is this correct? */
611       break;
612   }
613   return remains;
614 }
615
616 e_smx_state_t SIMIX_comm_get_state(smx_action_t action)
617 {
618   return action->state;
619 }
620
621 /**
622  *  \brief Return the user data associated to the sender of the communication
623  *  \param action The communication
624  *  \return the user data
625  */
626 void* SIMIX_comm_get_src_data(smx_action_t action)
627 {
628   return action->comm.src_data;
629 }
630
631 /**
632  *  \brief Return the user data associated to the receiver of the communication
633  *  \param action The communication
634  *  \return the user data
635  */
636 void* SIMIX_comm_get_dst_data(smx_action_t action)
637 {
638   return action->comm.dst_data;
639 }
640
641 void* SIMIX_comm_get_src_buff(smx_action_t action)
642 {
643   return action->comm.src_buff;
644 }
645
646 void* SIMIX_comm_get_dst_buff(smx_action_t action)
647 {
648   return action->comm.dst_buff;
649 }
650
651 size_t SIMIX_comm_get_src_buff_size(smx_action_t action)
652 {
653   return action->comm.src_buff_size;
654 }
655
656 size_t SIMIX_comm_get_dst_buff_size(smx_action_t action)
657 {
658   size_t buff_size;
659
660   if (action->comm.dst_buff_size)
661     buff_size = *(action->comm.dst_buff_size);
662   else
663     buff_size = 0;
664
665   return buff_size;
666 }
667
668 smx_process_t SIMIX_comm_get_src_proc(smx_action_t action)
669 {
670   return action->comm.src_proc;
671 }
672
673 smx_process_t SIMIX_comm_get_dst_proc(smx_action_t action)
674 {
675   return action->comm.dst_proc;
676 }
677
678 #ifdef HAVE_LATENCY_BOUND_TRACKING
679 /**
680  *  \brief verify if communication is latency bounded
681  *  \param comm The communication
682  */
683 XBT_INLINE int SIMIX_comm_is_latency_bounded(smx_action_t action)
684 {
685   //try to find comm on the list of finished flows
686   uintptr_t key = 0;
687   uintptr_t data = 0;
688   xbt_dict_cursor_t cursor;
689   xbt_dict_foreach(simix_global->latency_limited_dict, cursor, key, data) {
690     DEBUG2("comparing key=%p with comm=%p", (void *) key, (void *) action);
691     if ((void *) action == (void *) key) {
692       DEBUG2("key %p found, return value latency limited value %d",
693              (void *) key, (int) data);
694       xbt_dict_cursor_free(&cursor);
695       return (int) data;
696     }
697   }
698
699   return surf_workstation_model->get_latency_limited(action->comm.surf_comm);
700 }
701 #endif
702
703 /******************************************************************************/
704 /*                    SIMIX_comm_copy_data callbacks                       */
705 /******************************************************************************/
706 static void (*SIMIX_comm_copy_data_callback) (smx_action_t, size_t) =
707     &SIMIX_comm_copy_pointer_callback;
708
709 void
710 SIMIX_comm_set_copy_data_callback(void (*callback) (smx_action_t, size_t))
711 {
712   SIMIX_comm_copy_data_callback = callback;
713 }
714
715 void SIMIX_comm_copy_pointer_callback(smx_action_t comm, size_t buff_size)
716 {
717   xbt_assert1((buff_size == sizeof(void *)),
718               "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
719   *(void **) (comm->comm.dst_buff) = comm->comm.src_buff;
720 }
721
722 void SIMIX_comm_copy_buffer_callback(smx_action_t comm, size_t buff_size)
723 {
724   memcpy(comm->comm.dst_buff, comm->comm.src_buff, buff_size);
725 }
726
727 /**
728  *  \brief Copy the communication data from the sender's buffer to the receiver's one
729  *  \param comm The communication
730  */
731 void SIMIX_comm_copy_data(smx_action_t comm)
732 {
733   size_t buff_size = comm->comm.src_buff_size;
734   /* If there is no data to be copy then return */
735   if (!comm->comm.src_buff || !comm->comm.dst_buff || comm->comm.copied == 1)
736     return;
737
738   DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
739          comm,
740          comm->comm.src_proc->smx_host->name, comm->comm.src_buff,
741          comm->comm.dst_proc->smx_host->name, comm->comm.dst_buff, buff_size);
742
743   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
744   if (comm->comm.dst_buff_size)
745     buff_size = MIN(buff_size, *(comm->comm.dst_buff_size));
746
747   /* Update the receiver's buffer size to the copied amount */
748   if (comm->comm.dst_buff_size)
749     *comm->comm.dst_buff_size = buff_size;
750
751   if (buff_size == 0)
752     return;
753
754   (*SIMIX_comm_copy_data_callback) (comm, buff_size);
755
756   /* Set the copied flag so we copy data only once */
757   /* (this function might be called from both communication ends) */
758   comm->comm.copied = 1;
759 }