Logo AND Algorithmique Numérique Distribuée

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