Logo AND Algorithmique Numérique Distribuée

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