Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
write java class files into CMAKE_CURRENT_BINARY_DIR, not into source dir
[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 /******************************************************************************/
16 /*                           Rendez-Vous Points                               */
17 /******************************************************************************/
18
19 /**
20  *  \brief Creates a new rendez-vous point
21  *  \param name The name of the rendez-vous point
22  *  \return The created rendez-vous point
23  */
24 smx_rdv_t SIMIX_rdv_create(const char *name)
25 {
26   smx_rdv_t rdv = xbt_new0(s_smx_rvpoint_t, 1);
27   rdv->name = name ? xbt_strdup(name) : NULL;
28   rdv->read = SIMIX_mutex_init();
29   rdv->write = SIMIX_mutex_init();
30   rdv->comm_fifo = xbt_fifo_new();
31
32   return rdv;
33 }
34
35 /**
36  *  \brief Destroy a rendez-vous point
37  *  \param name The rendez-vous point to destroy
38  */
39 void SIMIX_rdv_destroy(smx_rdv_t rdv)
40 {
41   if (rdv->name)
42     xbt_free(rdv->name);
43   SIMIX_mutex_destroy(rdv->read);
44   SIMIX_mutex_destroy(rdv->write);
45   xbt_fifo_free(rdv->comm_fifo);
46   xbt_free(rdv);
47 }
48
49 /**
50  *  \brief Push a communication request into a rendez-vous point
51  *  \param rdv The rendez-vous point
52  *  \param comm The communication request
53  */
54 static XBT_INLINE void SIMIX_rdv_push(smx_rdv_t rdv, smx_comm_t comm)
55 {
56   xbt_fifo_push(rdv->comm_fifo, comm);
57   comm->rdv = rdv;
58 }
59
60 /**
61  *  \brief Remove a communication request from a rendez-vous point
62  *  \param rdv The rendez-vous point
63  *  \param comm The communication request
64  */
65 static XBT_INLINE void SIMIX_rdv_remove(smx_rdv_t rdv, smx_comm_t comm)
66 {
67   xbt_fifo_remove(rdv->comm_fifo, comm);
68   comm->rdv = NULL;
69 }
70
71 /**
72  *  \brief Checks if there is a communication request queued in a rendez-vous matching our needs
73  *  \param type The type of communication we are looking for (comm_send, comm_recv)
74  *  \return The communication request if found, NULL otherwise.
75  */
76 smx_comm_t SIMIX_rdv_get_request(smx_rdv_t rdv, smx_comm_type_t type)
77 {
78   smx_comm_t comm = (smx_comm_t)
79       xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
80
81   if (comm && comm->type == type) {
82     DEBUG0("Communication request found!");
83     xbt_fifo_shift(rdv->comm_fifo);
84     SIMIX_communication_use(comm);
85     comm->rdv = NULL;
86     return comm;
87   }
88
89   DEBUG0("Communication request not found");
90   return NULL;
91 }
92
93 /**
94  *  \brief counts the number of communication requests of a given host pending
95  *         on a rendez-vous point
96  *  \param rdv The rendez-vous point
97  *  \param host The host to be counted
98  *  \return The number of comm request pending in the rdv
99  */
100 int SIMIX_rdv_get_count_waiting_comm(smx_rdv_t rdv, smx_host_t host)
101 {
102   smx_comm_t comm = NULL;
103   xbt_fifo_item_t item = NULL;
104   int count = 0;
105
106   xbt_fifo_foreach(rdv->comm_fifo, item, comm, smx_comm_t) {
107     if (comm->src_proc->smx_host == host)
108       count++;
109   }
110
111   return count;
112 }
113
114 /**
115  *  \brief returns the communication at the head of the rendez-vous
116  *  \param rdv The rendez-vous point
117  *  \return The communication or NULL if empty
118  */
119 XBT_INLINE smx_comm_t SIMIX_rdv_get_head(smx_rdv_t rdv)
120 {
121   return (smx_comm_t)
122       xbt_fifo_get_item_content(xbt_fifo_get_first_item(rdv->comm_fifo));
123 }
124
125 /** @brief adds some API-related data to the rendez-vous point */
126 XBT_INLINE void SIMIX_rdv_set_data(smx_rdv_t rdv, void *data)
127 {
128   rdv->data = data;
129 }
130
131 /** @brief gets API-related data from the rendez-vous point */
132 XBT_INLINE void *SIMIX_rdv_get_data(smx_rdv_t rdv)
133 {
134   return rdv->data;
135 }
136
137 /******************************************************************************/
138 /*                           Communication Requests                           */
139 /******************************************************************************/
140
141 /**
142  *  \brief Creates a new communication request
143  *  \param type The type of communication (comm_send, comm_recv)
144  *  \return The new communication request
145  */
146 smx_comm_t SIMIX_communication_new(smx_comm_type_t type)
147 {
148   /* alloc structures */
149   smx_comm_t comm = xbt_new0(s_smx_comm_t, 1);
150   comm->type = type;
151   comm->sem = SIMIX_sem_init(0);
152   comm->refcount = 1;
153   VERB2("Create communication %p; refcount initially %d", comm,
154          comm->refcount);
155
156   return comm;
157 }
158
159 /**
160  *  \brief Destroy a communication request
161  *  \param comm The request to be destroyed
162  */
163 void SIMIX_communication_destroy(smx_comm_t comm)
164 {
165   VERB2("Destroy communication %p; refcount initially %d", comm,
166         comm->refcount);
167
168   if(!(comm->refcount>0))
169           xbt_die(bprintf("the refcount of comm %p is already 0 before decreasing it. That's a bug!",comm));
170
171 #ifdef HAVE_LATENCY_BOUND_TRACKING
172   //save is latency limited flag to use afterwards
173   if (comm->act) {
174     DEBUG2("adding key %p with latency limited value %d to the dict", comm,
175            SIMIX_action_is_latency_bounded(comm->act));
176     xbt_dicti_set(simix_global->latency_limited_dict, (uintptr_t) comm,
177                   SIMIX_action_is_latency_bounded(comm->act));
178   }
179 #endif
180
181   comm->refcount--;
182   if (comm->refcount > 0)
183     return;
184   VERB2("Really free communication %p; refcount is now %d", comm,
185         comm->refcount);
186
187   if (comm->sem) {
188     SIMIX_sem_destroy(comm->sem);
189     comm->sem = NULL;
190   }
191
192   if (comm->act) {
193     SIMIX_action_destroy(comm->act);
194     comm->act = NULL;
195   }
196
197   if (comm->src_timeout) {
198     SIMIX_action_destroy(comm->src_timeout);
199     comm->src_timeout = NULL;
200   }
201
202   if (comm->dst_timeout) {
203     SIMIX_action_destroy(comm->dst_timeout);
204     comm->dst_timeout = NULL;
205   }
206
207
208
209   xbt_free(comm);
210 }
211
212 /**
213  *  \brief Increase the number of users of the communication.
214  *  \param comm The communication request
215  *  Each communication request can be used by more than one process, so it is
216  *  necessary to know number of them at destroy time, to avoid freeing stuff that
217  *  maybe is in use by others.
218  *  \
219  */
220 static XBT_INLINE void SIMIX_communication_use(smx_comm_t comm)
221 {
222   comm->refcount++;
223
224   VERB2("Use communication %p; refcount is now %d", comm,
225            comm->refcount);
226 }
227
228 /**
229  *  \brief Start the simulation of a communication request
230  *  \param comm The communication request
231  */
232 static XBT_INLINE void SIMIX_communication_start(smx_comm_t comm)
233 {
234   /* If both the sender and the receiver are already there, start the communication */
235   if (comm->src_proc && comm->dst_proc) {
236     DEBUG1("Starting communication %p", comm);
237     comm->act = SIMIX_action_communicate(comm->src_proc->smx_host,
238                                          comm->dst_proc->smx_host, NULL,
239                                          comm->task_size, comm->rate);
240 #ifdef HAVE_TRACING
241     TRACE_smx_action_communicate(comm->act, comm->src_proc);
242     TRACE_surf_action(comm->act->surf_action, comm->act->category);
243 #endif
244
245     /* If any of the process is suspend, create the action but stop its execution,
246        it will be restarted when the sender process resume */
247     if (SIMIX_process_is_suspended(comm->src_proc) ||
248         SIMIX_process_is_suspended(comm->dst_proc)) {
249       SIMIX_action_suspend(comm->act);
250     }
251
252     /* Add the communication as user data of the action */
253     comm->act->data = comm;
254
255     /* The semaphore will only get signaled once, but since the first unlocked guy will
256      * release_forever() the semaphore, that will unlock the second (and any other)
257      * communication partner */
258     SIMIX_register_action_to_semaphore(comm->act, comm->sem);
259   }
260 }
261
262 /**
263  * \brief Performs error checking and cleanup
264  * \param comm The communication
265  */
266 static XBT_INLINE void SIMIX_communication_cleanup(smx_comm_t comm)
267 {
268   DEBUG1("Checking errors and cleaning communication %p", comm);
269
270   /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
271   SIMIX_sem_release_forever(comm->sem);
272
273   /* Check for errors other than timeouts */
274   if (!SIMIX_host_get_state(SIMIX_host_self())) {
275     if (comm->rdv)
276       SIMIX_rdv_remove(comm->rdv, comm);
277     SIMIX_communication_destroy(comm);
278     THROW0(host_error, 0, "Host failed");
279   } else if (SIMIX_action_get_state(comm->act) == SURF_ACTION_FAILED) {
280     SIMIX_communication_destroy(comm);
281     THROW0(network_error, 0, "Link failure");
282   } else if (!SIMIX_host_get_state(SIMIX_process_get_host(comm->dst_proc))
283              ||
284              !SIMIX_host_get_state(SIMIX_process_get_host(comm->src_proc)))
285   {
286     /* We test both src&dst because we dunno who we are today, and we already tested myself above.
287      *    So, at the end, we test the remote peer only
288      * Moreover, we have to test it because if the remote peer fails, the action comm->act is not done nor failed.
289      *    In that case, we got awaken by the little endless actions created in the SIMIX_sem_acquire(comm->sem)
290      *    at the beginning of this function. */
291     SIMIX_communication_destroy(comm);
292     THROW0(network_error, 0, "Remote peer failed");
293
294   }
295   /* Copy network data */
296   SIMIX_network_copy_data(comm);
297
298   SIMIX_communication_destroy(comm);
299 }
300
301 /**
302  *  \brief Waits for communication completion
303  *  \param comm The communication
304  *  \param timeout The max amount of time to wait for the communication to finish
305  *
306  *  Throws:
307  *   - host_error if local peer failed
308  *   - timeout_error if communication reached the timeout specified (either because of local peer or remote peer)
309  *   - network_error if network failed or remote peer failed
310  */
311 static XBT_INLINE void SIMIX_communication_wait_for_completion(smx_comm_t
312                                                                comm,
313                                                                double
314                                                                timeout)
315 {
316   smx_action_t act_sleep = NULL;
317   int src_timeout = 0;
318   int dst_timeout = 0;
319
320   DEBUG1("Waiting for the completion of communication %p", comm);
321
322   if (timeout >= 0) {
323     act_sleep = SIMIX_action_sleep(SIMIX_host_self(), timeout);
324     if (SIMIX_process_self() == comm->src_proc)
325       comm->src_timeout = act_sleep;
326     else
327       comm->dst_timeout = act_sleep;
328     SIMIX_action_set_name(act_sleep,
329                           bprintf
330                           ("Timeout for comm %p and wait on semaphore %p (max_duration:%f)",
331                            comm, comm->sem, timeout));
332     SIMIX_register_action_to_semaphore(act_sleep, comm->sem);
333     SIMIX_process_self()->waiting_action = act_sleep;
334     SIMIX_sem_block_onto(comm->sem);
335     SIMIX_process_self()->waiting_action = NULL;
336     SIMIX_unregister_action_to_semaphore(act_sleep, comm->sem);
337   } else {
338     SIMIX_sem_acquire(comm->sem);
339   }
340
341   /* Check for timeouts */
342   if ((src_timeout = ((comm->src_timeout)
343                       && (SIMIX_action_get_state(comm->src_timeout) ==
344                           SURF_ACTION_DONE)))
345       || (dst_timeout = ((comm->dst_timeout)
346                          && (SIMIX_action_get_state(comm->dst_timeout) ==
347                              SURF_ACTION_DONE)))) {
348     /* Somebody did a timeout! */
349     if (src_timeout)
350       DEBUG1("Communication timeout from the src! %p", comm);
351     if (dst_timeout)
352       DEBUG1("Communication timeout from the dst! %p", comm);
353
354     if (comm->act
355         && SIMIX_action_get_state(comm->act) == SURF_ACTION_RUNNING)
356       SIMIX_communication_cancel(comm);
357     else if (comm->rdv)
358       SIMIX_rdv_remove(comm->rdv, comm);
359
360     /* Make sure that everyone sleeping on that semaphore is awake, and that nobody will ever block on it */
361     SIMIX_sem_release_forever(comm->sem);
362     SIMIX_communication_destroy(comm);
363
364     THROW1(timeout_error, 0, "Communication timeouted because of %s",
365            src_timeout ? "the source" : "the destination");
366   }
367
368   DEBUG1("Communication %p complete!", comm);
369   SIMIX_communication_cleanup(comm);
370 }
371
372 /**
373  *  \brief Cancels a communication
374  *  \brief comm The communication to cancel
375  */
376 XBT_INLINE void SIMIX_communication_cancel(smx_comm_t comm)
377 {
378   if (comm->act)
379     SIMIX_action_cancel(comm->act);
380 }
381
382 /**
383  *  \brief get the amount remaining from the communication
384  *  \param comm The communication
385  */
386 XBT_INLINE double SIMIX_communication_get_remains(smx_comm_t comm)
387 {
388   DEBUG1("calling SIMIX_action_get_remains(%p)", comm->act);
389   return SIMIX_action_get_remains(comm->act);
390 }
391
392 #ifdef HAVE_LATENCY_BOUND_TRACKING
393 /**
394  *  \brief verify if communication is latency bounded
395  *  \param comm The communication
396  */
397 XBT_INLINE int SIMIX_communication_is_latency_bounded(smx_comm_t comm)
398 {
399   //try to find comm on the list of finished flows
400   uintptr_t key = 0;
401   uintptr_t data = 0;
402   xbt_dict_cursor_t cursor;
403   xbt_dict_foreach(simix_global->latency_limited_dict, cursor, key, data) {
404     DEBUG2("comparing key=%p with comm=%p", (void *) key, (void *) comm);
405     if ((void *) comm == (void *) key) {
406       DEBUG2("key %p found, return value latency limited value %d",
407              (void *) key, (int) data);
408       return (int) data;
409     }
410   }
411
412   DEBUG1("calling SIMIX_action_is_latency_bounded(%p)", comm->act);
413   return SIMIX_action_is_latency_bounded(comm->act);
414 }
415 #endif
416
417 /******************************************************************************/
418 /*                    SIMIX_network_copy_data callbacks                       */
419 /******************************************************************************/
420 static void (*SIMIX_network_copy_data_callback) (smx_comm_t, size_t) =
421     &SIMIX_network_copy_pointer_callback;
422
423 void
424 SIMIX_network_set_copy_data_callback(void (*callback) (smx_comm_t, size_t))
425 {
426   SIMIX_network_copy_data_callback = callback;
427 }
428
429 void SIMIX_network_copy_pointer_callback(smx_comm_t comm, size_t buff_size)
430 {
431   xbt_assert1((buff_size == sizeof(void *)),
432               "Cannot copy %zu bytes: must be sizeof(void*)", buff_size);
433   *(void **) (comm->dst_buff) = comm->src_buff;
434 }
435
436 void SIMIX_network_copy_buffer_callback(smx_comm_t comm, size_t buff_size)
437 {
438   memcpy(comm->dst_buff, comm->src_buff, buff_size);
439 }
440
441 /**
442  *  \brief Copy the communication data from the sender's buffer to the receiver's one
443  *  \param comm The communication
444  */
445 void SIMIX_network_copy_data(smx_comm_t comm)
446 {
447   size_t buff_size = comm->src_buff_size;
448   uintptr_t casted_size = 0;
449   uintptr_t amount = 0;
450   /* If there is no data to be copy then return */
451   if (!comm->src_buff || !comm->dst_buff || comm->copied == 1)
452     return;
453
454   DEBUG6("Copying comm %p data from %s (%p) -> %s (%p) (%zu bytes)",
455          comm,
456          comm->src_proc->smx_host->name, comm->src_buff,
457          comm->dst_proc->smx_host->name, comm->dst_buff, buff_size);
458
459   /* Copy at most dst_buff_size bytes of the message to receiver's buffer */
460   if (comm->dst_buff_size)
461     buff_size = MIN(buff_size, *(comm->dst_buff_size));
462
463   /* Update the receiver's buffer size to the copied amount */
464   if (comm->dst_buff_size)
465     *comm->dst_buff_size = buff_size;
466
467   if (buff_size == 0)
468     return;
469   (*SIMIX_network_copy_data_callback) (comm, buff_size);
470
471   /* Set the copied flag so we copy data only once */
472   /* (this function might be called from both communication ends) */
473   comm->copied = 1;
474
475   /* pimple to display the message sizes */
476   {
477     casted_size = comm->task_size;
478     amount = xbt_dicti_get(simix_global->msg_sizes, casted_size);
479     amount++;
480
481     xbt_dicti_set(simix_global->msg_sizes, casted_size, amount);
482   }
483 }
484
485 #include "xbt.h"
486 /* pimple to display the message sizes */
487 void SIMIX_message_sizes_output(const char *filename)
488 {
489   uintptr_t key = 0;
490   uintptr_t data = 0;
491   xbt_dict_cursor_t cursor;
492   FILE *out = NULL;
493   out = fopen(filename, "w");
494   xbt_assert1(out, "Cannot open file %s", filename);
495
496   xbt_dict_foreach(simix_global->msg_sizes, cursor, key, data) {
497     fprintf(out, "%zu %zu\n", key, data);
498   }
499   fclose(out);
500 }
501
502 /**
503  *  \brief Return the user data associated to the communication
504  *  \param comm The communication
505  *  \return the user data
506  */
507 XBT_INLINE void *SIMIX_communication_get_data(smx_comm_t comm)
508 {
509   return comm->data;
510 }
511
512 XBT_PUBLIC(void *) SIMIX_communication_get_src_buf(smx_comm_t comm)
513 {
514   return comm->src_buff;
515 }
516
517 XBT_PUBLIC(void *) SIMIX_communication_get_dst_buf(smx_comm_t comm)
518 {
519   return comm->dst_buff;
520 }
521
522 XBT_PUBLIC(size_t) SIMIX_communication_get_src_buf_size(smx_comm_t comm)
523 {
524   return comm->src_buff_size;
525 }
526
527 XBT_PUBLIC(size_t) SIMIX_communication_get_dst_buf_size(smx_comm_t comm)
528 {
529   return *(comm->dst_buff_size);
530 }
531
532 /******************************************************************************/
533 /*                        Synchronous Communication                           */
534 /******************************************************************************/
535 /**
536  *  \brief Put a send communication request in a rendez-vous point and waits for
537  *         its completion (blocking)
538  *  \param rdv The rendez-vous point
539  *  \param task_size The size of the communication action (for surf simulation)
540  *  \param rate The rate of the communication action (for surf)
541  *  \param timeout The timeout used for the waiting the completion 
542  *  \param src_buff The source buffer containing the message to be sent
543  *  \param src_buff_size The size of the source buffer
544  *  \param comm_ref The communication object used for the send  (useful if someone else wants to cancel this communication afterward)
545  *  \param data User data associated to the communication object
546  *  Throws:
547  *   - host_error if peer failed
548  *   - timeout_error if communication reached the timeout specified
549  *   - network_error if network failed or peer issued a timeout
550  */
551 XBT_INLINE void SIMIX_network_send(smx_rdv_t rdv, double task_size,
552                                    double rate, double timeout,
553                                    void *src_buff, size_t src_buff_size,
554                                    smx_comm_t * comm_ref, void *data)
555 {
556   xbt_ex_t e;
557   smx_comm_t comm = *comm_ref =
558               SIMIX_network_isend(rdv, task_size, rate, src_buff, src_buff_size,
559                                   data);
560   TRY {
561     SIMIX_network_wait(comm, timeout);
562   }
563   TRY_CLEANUP {
564     SIMIX_communication_destroy(comm);
565   }
566   CATCH(e) {
567     RETHROW;
568   }
569 }
570
571 /**
572  *  \brief Put a receive communication request in a rendez-vous point and waits
573  *         for its completion (blocking)
574  *  \param rdv The rendez-vous point
575  *  \param timeout The timeout used for the waiting the completion 
576  *  \param dst_buff The destination buffer to copy the received message
577  *  \param src_buff_size The size of the destination buffer
578  *  \param comm_ref The communication object used for the send (useful if someone else wants to cancel this communication afterward)
579  *  Throws:
580  *   - host_error if peer failed
581  *   - timeout_error if communication reached the timeout specified
582  *   - network_error if network failed or peer issued a timeout
583  */
584 XBT_INLINE void SIMIX_network_recv(smx_rdv_t rdv, double timeout,
585                                    void *dst_buff, size_t * dst_buff_size,
586                                    smx_comm_t * comm_ref)
587 {
588   xbt_ex_t e;
589   smx_comm_t comm = *comm_ref =
590       (smx_comm_t) SIMIX_network_irecv(rdv, dst_buff, dst_buff_size);
591   TRY {
592     SIMIX_network_wait(comm, timeout);
593   }
594   TRY_CLEANUP {
595     SIMIX_communication_destroy(comm);
596   }
597   CATCH(e) {
598     RETHROW;
599   }
600 }
601
602 /******************************************************************************/
603 /*                        Asynchronous Communication                          */
604 /******************************************************************************/
605 smx_comm_t SIMIX_network_isend(smx_rdv_t rdv, double task_size,
606                                double rate, void *src_buff,
607                                size_t src_buff_size, void *data)
608 {
609   smx_comm_t comm;
610
611   /*If running in model-checking mode then intercept the communication action */
612 #ifdef HAVE_MC
613   if (_surf_do_model_check)
614     MC_trans_intercept_isend(rdv);
615 #endif
616   /* Look for communication request matching our needs.
617      If it is not found then create it and push it into the rendez-vous point */
618   comm = SIMIX_rdv_get_request(rdv, comm_recv);
619
620   if (!comm) {
621     comm = SIMIX_communication_new(comm_send);
622     SIMIX_rdv_push(rdv, comm);
623   }
624
625   /* Setup the communication request */
626   comm->src_proc = SIMIX_process_self();
627   comm->task_size = task_size;
628   comm->rate = rate;
629   comm->src_buff = src_buff;
630   comm->src_buff_size = src_buff_size;
631   comm->data = data;
632
633   SIMIX_communication_start(comm);
634   return comm;
635 }
636
637 smx_comm_t SIMIX_network_irecv(smx_rdv_t rdv, void *dst_buff,
638                                size_t * dst_buff_size)
639 {
640   smx_comm_t comm;
641
642   /*If running in model-checking mode then intercept the communication action */
643 #ifdef HAVE_MC
644   if (_surf_do_model_check)
645     MC_trans_intercept_irecv(rdv);
646 #endif
647   /* Look for communication request matching our needs.
648    * If it is not found then create it and push it into the rendez-vous point
649    */
650   comm = SIMIX_rdv_get_request(rdv, comm_send);
651
652   if (!comm) {
653     comm = SIMIX_communication_new(comm_recv);
654     SIMIX_rdv_push(rdv, comm);
655   }
656
657   /* Setup communication request */
658   comm->dst_proc = SIMIX_process_self();
659   comm->dst_buff = dst_buff;
660   comm->dst_buff_size = dst_buff_size;
661
662   SIMIX_communication_start(comm);
663   return comm;
664 }
665
666 /** @brief blocks until the communication terminates or the timeout occurs */
667 XBT_INLINE void SIMIX_network_wait(smx_comm_t comm, double timeout)
668 {
669   /*If running in model-checking mode then intercept the communication action */
670 #ifdef HAVE_MC
671   if (_surf_do_model_check)
672     MC_trans_intercept_wait(comm);
673 #endif
674   SIMIX_communication_use(comm);
675   /* Wait for communication completion */
676   SIMIX_communication_wait_for_completion(comm, timeout);
677 }
678
679 /** @Returns whether the (asynchronous) communication is done yet or not */
680 XBT_INLINE int SIMIX_network_test(smx_comm_t comm)
681 {
682   /*If running in model-checking mode then intercept the communication action */
683 #ifdef HAVE_MC
684   if (_surf_do_model_check)
685     MC_trans_intercept_test(comm);
686 #endif
687
688   /* Copy data if the communication is done */
689   if (comm->sem && !SIMIX_sem_would_block(comm->sem)) {
690     /* Copy network data */
691     SIMIX_network_copy_data(comm);
692     return TRUE;
693   }
694   return FALSE;
695 }
696
697 /** @brief wait for the completion of any communication of a set
698  *
699  *  @Returns the rank in the dynar of communication which finished; destroy it after identifying which one it is
700  */
701 unsigned int SIMIX_network_waitany(xbt_dynar_t comms)
702 {
703   xbt_dynar_t sems = xbt_dynar_new(sizeof(smx_sem_t), NULL);
704   unsigned int cursor, found_comm = -1;
705   smx_comm_t comm, comm_finished = NULL;
706
707   /*If running in model-checking mode then intercept the communication action */
708 #ifdef HAVE_MC
709   if (_surf_do_model_check)
710     MC_trans_intercept_waitany(comms);
711 #endif
712   xbt_dynar_foreach(comms, cursor, comm)
713       xbt_dynar_push(sems, &(comm->sem));
714
715   DEBUG1("Waiting for the completion of communication set %p", comms);
716
717   found_comm = SIMIX_sem_acquire_any(sems);
718   xbt_dynar_free_container(&sems);
719   xbt_assert0(found_comm != -1,
720               "Cannot find which communication finished");
721   xbt_dynar_get_cpy(comms, found_comm, &comm_finished);
722
723   DEBUG2("Communication %p of communication set %p finished",
724          comm_finished, comms);
725
726   /* let the regular code deal with the communication end (errors checking and cleanup).
727    * A bit of useless work will be done, but that's good for source factorization */
728   SIMIX_sem_release_forever(comm_finished->sem);
729   SIMIX_communication_use(comm_finished);
730   SIMIX_communication_wait_for_completion(comm_finished, -1);
731   return found_comm;
732 }