Logo AND Algorithmique Numérique Distribuée

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