Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add an smpi/ois factor, to account for the fact that MPI_Send and MPI_Isend internal...
[simgrid.git] / src / msg / msg_gos.c
1 /* Copyright (c) 2004-2012. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "msg_private.h"
7 #include "msg_mailbox.h"
8 #include "mc/mc.h"
9 #include "xbt/log.h"
10 #include "xbt/sysdep.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
13                                 "Logging specific to MSG (gos)");
14
15 /** \ingroup msg_task_usage
16  * \brief Executes a task and waits for its termination.
17  *
18  * This function is used for describing the behavior of a process. It
19  * takes only one parameter.
20  * \param task a #msg_task_t to execute on the location on which the process is running.
21  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
22  * or #MSG_HOST_FAILURE otherwise
23  */
24 msg_error_t MSG_task_execute(msg_task_t task)
25 {
26   return MSG_parallel_task_execute(task);
27 }
28
29 /** \ingroup msg_task_usage
30  * \brief Executes a parallel task and waits for its termination.
31  *
32  * \param task a #msg_task_t to execute on the location on which the process is running.
33  *
34  * \return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
35  * or #MSG_HOST_FAILURE otherwise
36  */
37 msg_error_t MSG_parallel_task_execute(msg_task_t task)
38 {
39   xbt_ex_t e;
40   simdata_task_t simdata = task->simdata;
41   msg_process_t self = SIMIX_process_self();
42   simdata_process_t p_simdata = SIMIX_process_self_get_data(self);
43   e_smx_state_t comp_state;
44   msg_error_t status = MSG_OK;
45
46 #ifdef HAVE_TRACING
47   TRACE_msg_task_execute_start(task);
48 #endif
49
50   xbt_assert((!simdata->compute) && (task->simdata->isused == 0),
51              "This task is executed somewhere else. Go fix your code! %d",
52              task->simdata->isused);
53
54   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
55
56   if (simdata->computation_amount == 0 && !simdata->host_nb) {
57 #ifdef HAVE_TRACING
58     TRACE_msg_task_execute_end(task);
59 #endif
60     return MSG_OK;
61   }
62
63
64   TRY {
65
66     simdata->isused=1;
67
68     if (simdata->host_nb > 0) {
69       simdata->compute = simcall_host_parallel_execute(task->name,
70                                                        simdata->host_nb,
71                                                        simdata->host_list,
72                                                        simdata->comp_amount,
73                                                        simdata->comm_amount,
74                                                        1.0, -1.0);
75       XBT_DEBUG("Parallel execution action created: %p", simdata->compute);
76     } else {
77       simdata->compute = simcall_host_execute(task->name,
78                                               p_simdata->m_host,
79                                               simdata->computation_amount,
80                                               simdata->priority);
81
82     }
83 #ifdef HAVE_TRACING
84     simcall_set_category(simdata->compute, task->category);
85 #endif
86     p_simdata->waiting_action = simdata->compute;
87     comp_state = simcall_host_execution_wait(simdata->compute);
88
89     p_simdata->waiting_action = NULL;
90
91     simdata->isused=0;
92
93     XBT_DEBUG("Execution task '%s' finished in state %d",
94               task->name, (int)comp_state);
95   }
96   CATCH(e) {
97     switch (e.category) {
98     case cancel_error:
99       status = MSG_TASK_CANCELED;
100       break;
101     default:
102       RETHROW;
103     }
104     xbt_ex_free(e);
105   }
106   /* action ended, set comm and compute = NULL, the actions is already destroyed
107    * in the main function */
108   simdata->computation_amount = 0.0;
109   simdata->comm = NULL;
110   simdata->compute = NULL;
111 #ifdef HAVE_TRACING
112   TRACE_msg_task_execute_end(task);
113 #endif
114
115   MSG_RETURN(status);
116 }
117
118
119 /** \ingroup msg_task_usage
120  * \brief Sleep for the specified number of seconds
121  *
122  * Makes the current process sleep until \a time seconds have elapsed.
123  *
124  * \param nb_sec a number of second
125  */
126 msg_error_t MSG_process_sleep(double nb_sec)
127 {
128   xbt_ex_t e;
129   msg_error_t status = MSG_OK;
130   /*msg_process_t proc = MSG_process_self();*/
131
132 #ifdef HAVE_TRACING
133   TRACE_msg_process_sleep_in(MSG_process_self());
134 #endif
135
136   /* create action to sleep */
137
138   /*proc->simdata->waiting_action = act_sleep;
139
140   FIXME: check if not setting the waiting_action breaks something on msg
141   
142   proc->simdata->waiting_action = NULL;*/
143
144   TRY {
145     simcall_process_sleep(nb_sec);
146   }
147   CATCH(e) {
148     switch (e.category) {
149     case cancel_error:
150       status = MSG_TASK_CANCELED;
151       break;
152     default:
153       RETHROW;
154     }
155     xbt_ex_free(e);
156   }
157
158   #ifdef HAVE_TRACING
159     TRACE_msg_process_sleep_out(MSG_process_self());
160   #endif
161   MSG_RETURN(status);
162 }
163
164 /** \ingroup msg_task_usage
165  * \brief Deprecated function that used to receive a task from a mailbox from a specific host.
166  *
167  * Sorry, this function is not supported anymore. That wouldn't be
168  * impossible to reimplement it, but we are lacking the time to do so ourselves.
169  * If you need this functionality, you can either:
170  *
171  *  - implement the buffering mechanism on the user-level by queuing all messages
172  *    received in the mailbox that do not match your expectation
173  *  - change your application logic to leverage the mailboxes features. For example,
174  *    if you have A receiving messages from B and C, you could have A waiting on
175  *    mailbox "A" most of the time, but on "A#B" when it's waiting for specific
176  *    messages from B and "A#C" when waiting for messages from C. You could even get A
177  *    sometime waiting on all these mailboxes using @ref MSG_comm_waitany. You can find
178  *    an example of use of this function in the @ref MSG_examples section.
179  *  - Provide a proper patch to implement this functionality back in MSG. That wouldn't be
180  *    very difficult actually. Check the function @ref MSG_mailbox_get_task_ext. During its call to
181  *    simcall_comm_recv(), the 5th argument, match_fun, is NULL. Create a function that filters
182  *    messages according to the host (that you will pass as sixth argument to simcall_comm_recv()
183  *    and that your filtering function will receive as first parameter, and then, the filter could
184  *    simply compare the host names, for example. After sufficient testing, provide an example that
185  *    we could add to the distribution, and your first contribution to SimGrid is ready. Thanks in advance.
186  *
187  * \param task a memory location for storing a #msg_task_t.
188  * \param alias name of the mailbox to receive the task from
189  * \param host a #msg_host_t host from where the task was sent
190  *
191  * \return Returns
192  * #MSG_OK if the task was successfully received,
193  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
194  */
195 msg_error_t
196 MSG_task_receive_from_host(msg_task_t * task, const char *alias,
197                            msg_host_t host)
198 {
199   return MSG_task_receive_ext(task, alias, -1, host);
200 }
201
202 /** \ingroup msg_task_usage
203  * \brief Receives a task from a mailbox.
204  *
205  * This is a blocking function, the execution flow will be blocked
206  * until the task is received. See #MSG_task_irecv
207  * for receiving tasks asynchronously.
208  *
209  * \param task a memory location for storing a #msg_task_t.
210  * \param alias name of the mailbox to receive the task from
211  *
212  * \return Returns
213  * #MSG_OK if the task was successfully received,
214  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
215  */
216 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
217 {
218   return MSG_task_receive_with_timeout(task, alias, -1);
219 }
220
221 /** \ingroup msg_task_usage
222  * \brief Receives a task from a mailbox with a given timeout.
223  *
224  * This is a blocking function with a timeout, the execution flow will be blocked
225  * until the task is received or the timeout is achieved. See #MSG_task_irecv
226  * for receiving tasks asynchronously.  You can provide a -1 timeout
227  * to obtain an infinite timeout.
228  *
229  * \param task a memory location for storing a #msg_task_t.
230  * \param alias name of the mailbox to receive the task from
231  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
232  *
233  * \return Returns
234  * #MSG_OK if the task was successfully received,
235  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
236  */
237 msg_error_t
238 MSG_task_receive_with_timeout(msg_task_t * task, const char *alias,
239                               double timeout)
240 {
241   return MSG_task_receive_ext(task, alias, timeout, NULL);
242 }
243
244 /** \ingroup msg_task_usage
245  * \brief Receives a task from a mailbox from a specific host with a given timeout.
246  *
247  * This is a blocking function with a timeout, the execution flow will be blocked
248  * until the task is received or the timeout is achieved. See #MSG_task_irecv
249  * for receiving tasks asynchronously. You can provide a -1 timeout
250  * to obtain an infinite timeout.
251  *
252  * \param task a memory location for storing a #msg_task_t.
253  * \param alias name of the mailbox to receive the task from
254  * \param timeout is the maximum wait time for completion (provide -1 for no timeout)
255  * \param host a #msg_host_t host from where the task was sent
256  *
257  * \return Returns
258  * #MSG_OK if the task was successfully received,
259 * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
260  */
261 msg_error_t
262 MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout,
263                      msg_host_t host)
264 {
265   xbt_ex_t e;
266   msg_error_t ret = MSG_OK;
267   XBT_DEBUG
268       ("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'",
269        alias);
270   TRY {
271     ret = MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task,
272                                    host, timeout);
273   }
274   CATCH(e) {
275     switch (e.category) {
276     case cancel_error:          /* may be thrown by MSG_mailbox_get_by_alias */
277       ret = MSG_HOST_FAILURE;
278       break;
279     default:
280       RETHROW;
281     }
282     xbt_ex_free(e);
283   }
284   return ret;
285 }
286
287 /** \ingroup msg_task_usage
288  * \brief Sends a task on a mailbox.
289  *
290  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
291  * to end the communication.
292  *
293  * \param task a #msg_task_t to send on another location.
294  * \param alias name of the mailbox to sent the task to
295  * \return the msg_comm_t communication created
296  */
297 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
298 {
299   return MSG_task_isend_with_matching(task,alias,NULL,NULL);
300 }
301
302 /** \ingroup msg_task_usage
303  * \brief Sends a task on a mailbox with a maximum rate
304  *
305  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
306  * to end the communication. The maxrate parameter allows the application
307  * to limit the bandwidth utilization of network links when sending the task.
308  *
309  * \param task a #msg_task_t to send on another location.
310  * \param alias name of the mailbox to sent the task to
311  * \param maxrate the maximum communication rate for sending this task .
312  * \return the msg_comm_t communication created
313  */
314 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
315 {
316   task->simdata->rate = maxrate;
317   return MSG_task_isend_with_matching(task,alias,NULL,NULL);
318 }
319
320
321 /** \ingroup msg_task_usage
322  * \brief Sends a task on a mailbox, with support for matching requests
323  *
324  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
325  * to end the communication.
326  *
327  * \param task a #msg_task_t to send on another location.
328  * \param alias name of the mailbox to sent the task to
329  * \param match_fun boolean function which parameters are:
330  *        - match_data_provided_here
331  *        - match_data_provided_by_other_side_if_any
332  *        - the_smx_action_describing_the_other_side
333  * \param match_data user provided data passed to match_fun
334  * \return the msg_comm_t communication created
335  */
336 XBT_INLINE msg_comm_t MSG_task_isend_with_matching(msg_task_t task, const char *alias,
337     int (*match_fun)(void*,void*, smx_action_t),
338     void *match_data)
339 {
340   simdata_task_t t_simdata = NULL;
341   msg_process_t process = MSG_process_self();
342   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
343
344 #ifdef HAVE_TRACING
345   int call_end = TRACE_msg_task_put_start(task);
346 #endif
347
348   /* Prepare the task to send */
349   t_simdata = task->simdata;
350   t_simdata->sender = process;
351   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
352
353   xbt_assert(t_simdata->isused == 0,
354               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
355
356   t_simdata->isused = 1;
357   t_simdata->comm = NULL;
358   msg_global->sent_msg++;
359
360   /* Send it by calling SIMIX network layer */
361   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
362   comm->task_sent = task;
363   comm->task_received = NULL;
364   comm->status = MSG_OK;
365   comm->s_comm =
366     simcall_comm_isend(mailbox, t_simdata->message_size,
367                          t_simdata->rate, task, sizeof(void *), match_fun, NULL, match_data, 0);
368   t_simdata->comm = comm->s_comm; /* FIXME: is the field t_simdata->comm still useful? */
369 #ifdef HAVE_TRACING
370     if (TRACE_is_enabled()) {
371       simcall_set_category(comm->s_comm, task->category);
372     }
373 #endif
374
375 #ifdef HAVE_TRACING
376   if (call_end)
377     TRACE_msg_task_put_end();
378 #endif
379
380   return comm;
381 }
382
383 /** \ingroup msg_task_usage
384  * \brief Sends a task on a mailbox.
385  *
386  * This is a non blocking detached send function.
387  * Think of it as a best effort send. Keep in mind that the third parameter
388  * is only called if the communication fails. If the communication does work,
389  * it is responsibility of the receiver code to free anything related to
390  * the task, as usual. More details on this can be obtained on
391  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
392  * in the SimGrid-user mailing list archive.
393  *
394  * \param task a #msg_task_t to send on another location.
395  * \param alias name of the mailbox to sent the task to
396  * \param cleanup a function to destroy the task if the
397  * communication fails, e.g. MSG_task_destroy
398  * (if NULL, no function will be called)
399  */
400 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
401 {
402   simdata_task_t t_simdata = NULL;
403   msg_process_t process = MSG_process_self();
404   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
405
406   /* Prepare the task to send */
407   t_simdata = task->simdata;
408   t_simdata->sender = process;
409   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
410
411   xbt_assert(t_simdata->isused == 0,
412               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
413
414   t_simdata->isused = 1;
415   t_simdata->comm = NULL;
416   msg_global->sent_msg++;
417
418 #ifdef HAVE_TRACING
419   int call_end = TRACE_msg_task_put_start(task);
420 #endif
421
422   /* Send it by calling SIMIX network layer */
423   smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
424                        t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
425   t_simdata->comm = comm;
426 #ifdef HAVE_TRACING
427     if (TRACE_is_enabled()) {
428       simcall_set_category(comm, task->category);
429     }
430 #endif
431
432 #ifdef HAVE_TRACING
433   if (call_end)
434     TRACE_msg_task_put_end();
435 #endif
436 }
437
438
439 /** \ingroup msg_task_usage
440  * \brief Sends a task on a mailbox with a maximal rate.
441  *
442  * This is a non blocking detached send function.
443  * Think of it as a best effort send. Keep in mind that the third parameter
444  * is only called if the communication fails. If the communication does work,
445  * it is responsibility of the receiver code to free anything related to
446  * the task, as usual. More details on this can be obtained on
447  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
448  * in the SimGrid-user mailing list archive.
449  *
450  * \param task a #msg_task_t to send on another location.
451  * \param alias name of the mailbox to sent the task to
452  * \param cleanup a function to destroy the task if the
453  * communication fails, e.g. MSG_task_destroy
454  * (if NULL, no function will be called)
455  * \param maxrate the maximum communication rate for sending this task
456  * 
457  */
458 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
459 {
460   task->simdata->rate = maxrate;
461   
462   simdata_task_t t_simdata = NULL;
463   msg_process_t process = MSG_process_self();
464   msg_mailbox_t mailbox = MSG_mailbox_get_by_alias(alias);
465
466   /* Prepare the task to send */
467   t_simdata = task->simdata;
468   t_simdata->sender = process;
469   t_simdata->source = ((simdata_process_t) SIMIX_process_self_get_data(process))->m_host;
470
471   xbt_assert(t_simdata->isused == 0,
472               "This task is still being used somewhere else. You cannot send it now. Go fix your code!");
473
474   t_simdata->isused = 1;
475   t_simdata->comm = NULL;
476   msg_global->sent_msg++;
477
478 #ifdef HAVE_TRACING
479   int call_end = TRACE_msg_task_put_start(task);
480 #endif
481
482   /* Send it by calling SIMIX network layer */
483   smx_action_t comm = simcall_comm_isend(mailbox, t_simdata->message_size,
484                        t_simdata->rate, task, sizeof(void *), NULL, cleanup, NULL, 1);
485   t_simdata->comm = comm;
486 #ifdef HAVE_TRACING
487     if (TRACE_is_enabled()) {
488       simcall_set_category(comm, task->category);
489     }
490 #endif
491
492 #ifdef HAVE_TRACING
493   if (call_end)
494     TRACE_msg_task_put_end();
495 #endif
496 }
497
498 /** \ingroup msg_task_usage
499  * \brief Starts listening for receiving a task from an asynchronous communication.
500  *
501  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test()
502  * to end the communication.
503  * 
504  * \param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
505  * \param name of the mailbox to receive the task on
506  * \return the msg_comm_t communication created
507  */
508 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
509 {
510   smx_rdv_t rdv = MSG_mailbox_get_by_alias(name);
511
512   /* FIXME: these functions are not traceable */
513
514   /* Sanity check */
515   xbt_assert(task, "Null pointer for the task storage");
516
517   if (*task)
518     XBT_CRITICAL
519         ("MSG_task_irecv() was asked to write in a non empty task struct.");
520
521   /* Try to receive it by calling SIMIX network layer */
522   msg_comm_t comm = xbt_new0(s_msg_comm_t, 1);
523   comm->task_sent = NULL;
524   comm->task_received = task;
525   comm->status = MSG_OK;
526   comm->s_comm = simcall_comm_irecv(rdv, task, NULL, NULL, NULL);
527
528   return comm;
529 }
530
531 /** \ingroup msg_task_usage
532  * \brief Checks whether a communication is done, and if yes, finalizes it.
533  * \param comm the communication to test
534  * \return TRUE if the communication is finished
535  * (but it may have failed, use MSG_comm_get_status() to know its status)
536  * or FALSE if the communication is not finished yet
537  * If the status is FALSE, don't forget to use MSG_process_sleep() after the test.
538  */
539 int MSG_comm_test(msg_comm_t comm)
540 {
541   xbt_ex_t e;
542   int finished = 0;
543
544   TRY {
545     finished = simcall_comm_test(comm->s_comm);
546
547     if (finished && comm->task_received != NULL) {
548       /* I am the receiver */
549       (*comm->task_received)->simdata->isused = 0;
550     }
551   }
552   CATCH(e) {
553     switch (e.category) {
554       case network_error:
555         comm->status = MSG_TRANSFER_FAILURE;
556         finished = 1;
557         break;
558
559       case timeout_error:
560         comm->status = MSG_TIMEOUT;
561         finished = 1;
562         break;
563
564       default:
565         RETHROW;
566     }
567     xbt_ex_free(e);
568   }
569
570   return finished;
571 }
572
573 /** \ingroup msg_task_usage
574  * \brief This function checks if a communication is finished.
575  * \param comms a vector of communications
576  * \return the position of the finished communication if any
577  * (but it may have failed, use MSG_comm_get_status() to know its status),
578  * or -1 if none is finished
579  */
580 int MSG_comm_testany(xbt_dynar_t comms)
581 {
582   xbt_ex_t e;
583   int finished_index = -1;
584
585   /* create the equivalent dynar with SIMIX objects */
586   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
587   msg_comm_t comm;
588   unsigned int cursor;
589   xbt_dynar_foreach(comms, cursor, comm) {
590     xbt_dynar_push(s_comms, &comm->s_comm);
591   }
592
593   msg_error_t status = MSG_OK;
594   TRY {
595     finished_index = simcall_comm_testany(s_comms);
596   }
597   CATCH(e) {
598     switch (e.category) {
599       case network_error:
600         finished_index = e.value;
601         status = MSG_TRANSFER_FAILURE;
602         break;
603
604       case timeout_error:
605         finished_index = e.value;
606         status = MSG_TIMEOUT;
607         break;
608
609       default:
610         RETHROW;
611     }
612     xbt_ex_free(e);
613   }
614   xbt_dynar_free(&s_comms);
615
616   if (finished_index != -1) {
617     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
618     /* the communication is finished */
619     comm->status = status;
620
621     if (status == MSG_OK && comm->task_received != NULL) {
622       /* I am the receiver */
623       (*comm->task_received)->simdata->isused = 0;
624     }
625   }
626
627   return finished_index;
628 }
629
630 /** \ingroup msg_task_usage
631  * \brief Destroys a communication.
632  * \param comm the communication to destroy.
633  */
634 void MSG_comm_destroy(msg_comm_t comm)
635 {
636   xbt_free(comm);
637 }
638
639 /** \ingroup msg_task_usage
640  * \brief Wait for the completion of a communication.
641  *
642  * It takes two parameters.
643  * \param comm the communication to wait.
644  * \param timeout Wait until the communication terminates or the timeout 
645  * occurs. You can provide a -1 timeout to obtain an infinite timeout.
646  * \return msg_error_t
647  */
648 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
649 {
650   xbt_ex_t e;
651   TRY {
652     simcall_comm_wait(comm->s_comm, timeout);
653
654     if (comm->task_received != NULL) {
655       /* I am the receiver */
656       (*comm->task_received)->simdata->isused = 0;
657     }
658
659     /* FIXME: these functions are not traceable */
660   }
661   CATCH(e) {
662     switch (e.category) {
663     case network_error:
664       comm->status = MSG_TRANSFER_FAILURE;
665       break;
666     case timeout_error:
667       comm->status = MSG_TIMEOUT;
668       break;
669     default:
670       RETHROW;
671     }
672     xbt_ex_free(e);
673   }
674
675   return comm->status;
676 }
677
678 /** \ingroup msg_task_usage
679 * \brief This function is called by a sender and permit to wait for each communication
680 *
681 * \param comm a vector of communication
682 * \param nb_elem is the size of the comm vector
683 * \param timeout for each call of MSG_comm_wait
684 */
685 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
686 {
687   int i = 0;
688   for (i = 0; i < nb_elem; i++) {
689     MSG_comm_wait(comm[i], timeout);
690   }
691 }
692
693 /** \ingroup msg_task_usage
694  * \brief This function waits for the first communication finished in a list.
695  * \param comms a vector of communications
696  * \return the position of the first finished communication
697  * (but it may have failed, use MSG_comm_get_status() to know its status)
698  */
699 int MSG_comm_waitany(xbt_dynar_t comms)
700 {
701   xbt_ex_t e;
702   int finished_index = -1;
703
704   /* create the equivalent dynar with SIMIX objects */
705   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
706   msg_comm_t comm;
707   unsigned int cursor;
708   xbt_dynar_foreach(comms, cursor, comm) {
709     xbt_dynar_push(s_comms, &comm->s_comm);
710   }
711
712   msg_error_t status = MSG_OK;
713   TRY {
714     finished_index = simcall_comm_waitany(s_comms);
715   }
716   CATCH(e) {
717     switch (e.category) {
718       case network_error:
719         finished_index = e.value;
720         status = MSG_TRANSFER_FAILURE;
721         break;
722
723       case timeout_error:
724         finished_index = e.value;
725         status = MSG_TIMEOUT;
726         break;
727
728       default:
729         RETHROW;
730     }
731     xbt_ex_free(e);
732   }
733
734   xbt_assert(finished_index != -1, "WaitAny returned -1");
735   xbt_dynar_free(&s_comms);
736
737   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
738   /* the communication is finished */
739   comm->status = status;
740
741   if (comm->task_received != NULL) {
742     /* I am the receiver */
743     (*comm->task_received)->simdata->isused = 0;
744   }
745
746   return finished_index;
747 }
748
749 /**
750  * \ingroup msg_task_usage
751  * \brief Returns the error (if any) that occured during a finished communication.
752  * \param comm a finished communication
753  * \return the status of the communication, or #MSG_OK if no error occured
754  * during the communication
755  */
756 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
757
758   return comm->status;
759 }
760
761 /** \ingroup msg_task_usage
762  * \brief Get a task (#msg_task_t) from a communication
763  *
764  * \param comm the communication where to get the task
765  * \return the task from the communication
766  */
767 msg_task_t MSG_comm_get_task(msg_comm_t comm)
768 {
769   xbt_assert(comm, "Invalid parameter");
770
771   return comm->task_received ? *comm->task_received : comm->task_sent;
772 }
773
774 /**
775  * \brief This function is called by SIMIX in kernel mode to copy the data of a comm.
776  * \param comm the comm
777  * \param buff the data copied
778  * \param buff_size size of the buffer
779  */
780 void MSG_comm_copy_data_from_SIMIX(smx_action_t comm, void* buff, size_t buff_size) {
781
782   // copy the task
783   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
784
785   // notify the user callback if any
786   if (msg_global->task_copy_callback) {
787     msg_task_t task = buff;
788     msg_global->task_copy_callback(task,
789         simcall_comm_get_src_proc(comm), simcall_comm_get_dst_proc(comm));
790   }
791 }
792
793 /** \ingroup msg_task_usage
794  * \brief Sends a task to a mailbox
795  *
796  * This is a blocking function, the execution flow will be blocked
797  * until the task is sent (and received in the other side if #MSG_task_receive is used).
798  * See #MSG_task_isend for sending tasks asynchronously.
799  *
800  * \param task the task to be sent
801  * \param alias the mailbox name to where the task is sent
802  *
803  * \return Returns #MSG_OK if the task was successfully sent,
804  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
805  */
806 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
807 {
808   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
809   return MSG_task_send_with_timeout(task, alias, -1);
810 }
811
812 /** \ingroup msg_task_usage
813  * \brief Sends a task to a mailbox with a maximum rate
814  *
815  * This is a blocking function, the execution flow will be blocked
816  * until the task is sent. The maxrate parameter allows the application
817  * to limit the bandwidth utilization of network links when sending the task.
818  *
819  * \param task the task to be sent
820  * \param alias the mailbox name to where the task is sent
821  * \param maxrate the maximum communication rate for sending this task
822  *
823  * \return Returns #MSG_OK if the task was successfully sent,
824  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
825  */
826 msg_error_t
827 MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
828 {
829   task->simdata->rate = maxrate;
830   return MSG_task_send(task, alias);
831 }
832
833 /** \ingroup msg_task_usage
834  * \brief Sends a task to a mailbox with a timeout
835  *
836  * This is a blocking function, the execution flow will be blocked
837  * until the task is sent or the timeout is achieved.
838  *
839  * \param task the task to be sent
840  * \param alias the mailbox name to where the task is sent
841  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
842  *
843  * \return Returns #MSG_OK if the task was successfully sent,
844  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
845  */
846 msg_error_t
847 MSG_task_send_with_timeout(msg_task_t task, const char *alias,
848                            double timeout)
849 {
850   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
851                                       task, timeout);
852 }
853
854 /** \ingroup msg_task_usage
855  * \brief Sends a task to a mailbox with a timeout and with a maximum rate
856  *
857  * This is a blocking function, the execution flow will be blocked
858  * until the task is sent or the timeout is achieved.
859  *
860  * \param task the task to be sent
861  * \param alias the mailbox name to where the task is sent
862  * \param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
863  * \param maxrate the maximum communication rate for sending this task
864  *
865  * \return Returns #MSG_OK if the task was successfully sent,
866  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
867  */
868 msg_error_t
869 MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias,
870                            double timeout, double maxrate)
871 {
872   task->simdata->rate = maxrate;
873   return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias),
874                                       task, timeout);
875 }
876
877 /** \ingroup msg_task_usage
878  * \brief Check if there is a communication going on in a mailbox.
879  *
880  * \param alias the name of the mailbox to be considered
881  *
882  * \return Returns 1 if there is a communication, 0 otherwise
883  */
884 int MSG_task_listen(const char *alias)
885 {
886   return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
887 }
888
889 /** \ingroup msg_task_usage
890  * \brief Check the number of communication actions of a given host pending in a mailbox.
891  *
892  * \param alias the name of the mailbox to be considered
893  * \param host the host to check for communication
894  *
895  * \return Returns the number of pending communication actions of the host in the
896  * given mailbox, 0 if there is no pending communication actions.
897  *
898  */
899 int MSG_task_listen_from_host(const char *alias, msg_host_t host)
900 {
901   return
902       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias
903                                                (alias), host);
904 }
905
906 /** \ingroup msg_task_usage
907  * \brief Look if there is a communication on a mailbox and return the
908  * PID of the sender process.
909  *
910  * \param alias the name of the mailbox to be considered
911  *
912  * \return Returns the PID of sender process,
913  * -1 if there is no communication in the mailbox.
914  */
915 int MSG_task_listen_from(const char *alias)
916 {
917   msg_task_t task;
918
919   if (NULL ==
920       (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
921     return -1;
922
923   return MSG_process_get_PID(task->simdata->sender);
924 }
925
926 /** \ingroup msg_task_usage
927  * \brief Sets the tracing category of a task.
928  *
929  * This function should be called after the creation of
930  * a MSG task, to define the category of that task. The
931  * first parameter task must contain a task that was
932  * created with the function #MSG_task_create. The second
933  * parameter category must contain a category that was
934  * previously declared with the function #TRACE_category
935  * (or with #TRACE_category_with_color).
936  *
937  * See \ref tracing for details on how to trace
938  * the (categorized) resource utilization.
939  *
940  * \param task the task that is going to be categorized
941  * \param category the name of the category to be associated to the task
942  *
943  * \see MSG_task_get_category, TRACE_category, TRACE_category_with_color
944  */
945 void MSG_task_set_category (msg_task_t task, const char *category)
946 {
947 #ifdef HAVE_TRACING
948   TRACE_msg_set_task_category (task, category);
949 #endif
950 }
951
952 /** \ingroup msg_task_usage
953  *
954  * \brief Gets the current tracing category of a task.
955  *
956  * \param task the task to be considered
957  *
958  * \see MSG_task_set_category
959  *
960  * \return Returns the name of the tracing category of the given task, NULL otherwise
961  */
962 const char *MSG_task_get_category (msg_task_t task)
963 {
964 #ifdef HAVE_TRACING
965   return task->category;
966 #else
967   return NULL;
968 #endif
969 }
970
971 /**
972  * \brief Returns the value of a given AS or router property
973  *
974  * \param asr the name of a router or AS
975  * \param name a property name
976  * \return value of a property (or NULL if property not set)
977  */
978 const char *MSG_as_router_get_property_value(const char* asr, const char *name)
979 {
980   return xbt_dict_get_or_null(MSG_as_router_get_properties(asr), name);
981 }
982
983 /**
984  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to
985  * a the AS or router
986  *
987  * \param asr the name of a router or AS
988  * \return a dict containing the properties
989  */
990 xbt_dict_t MSG_as_router_get_properties(const char* asr)
991 {
992   return (simcall_asr_get_properties(asr));
993 }
994
995 /**
996  * \brief Change the value of a given AS or router
997  *
998  * \param asr the name of a router or AS
999  * \param name a property name
1000  * \param value what to change the property to
1001  * \param free_ctn the freeing function to use to kill the value on need
1002  */
1003 void MSG_as_router_set_property_value(const char* asr, const char *name, char *value,void_f_pvoid_t free_ctn) {
1004   xbt_dict_set(MSG_as_router_get_properties(asr), name, value,free_ctn);
1005 }
1006
1007 #ifdef MSG_USE_DEPRECATED
1008 /** \ingroup msg_deprecated_functions
1009  *
1010  * \brief Return the last value returned by a MSG function (except
1011  * MSG_get_errno...).
1012  */
1013 msg_error_t MSG_get_errno(void)
1014 {
1015   return PROCESS_GET_ERRNO();
1016 }
1017
1018 /** \ingroup msg_deprecated_functions
1019  * \brief Put a task on a channel of an host and waits for the end of the
1020  * transmission.
1021  *
1022  * This function is used for describing the behavior of a process. It
1023  * takes three parameter.
1024  * \param task a #msg_task_t to send on another location. This task
1025  will not be usable anymore when the function will return. There is
1026  no automatic task duplication and you have to save your parameters
1027  before calling this function. Tasks are unique and once it has been
1028  sent to another location, you should not access it anymore. You do
1029  not need to call MSG_task_destroy() but to avoid using, as an
1030  effect of inattention, this task anymore, you definitely should
1031  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1032  can be transfered iff it has been correctly created with
1033  MSG_task_create().
1034  * \param dest the destination of the message
1035  * \param channel the channel on which the process should put this
1036  task. This value has to be >=0 and < than the maximal number of
1037  channels fixed with MSG_set_channel_number().
1038  * \return #MSG_HOST_FAILURE if the host on which
1039  * this function was called was shut down,
1040  * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1041  * (network failure, dest failure) or #MSG_OK if it succeeded.
1042  */
1043 msg_error_t MSG_task_put(msg_task_t task, msg_host_t dest, m_channel_t channel)
1044 {
1045   XBT_WARN("DEPRECATED! Now use MSG_task_send");
1046   return MSG_task_put_with_timeout(task, dest, channel, -1.0);
1047 }
1048
1049 /** \ingroup msg_deprecated_functions
1050  * \brief Does exactly the same as MSG_task_put but with a bounded transmition
1051  * rate.
1052  *
1053  * \sa MSG_task_put
1054  */
1055 msg_error_t
1056 MSG_task_put_bounded(msg_task_t task, msg_host_t dest, m_channel_t channel,
1057                      double maxrate)
1058 {
1059   XBT_WARN("DEPRECATED! Now use MSG_task_send_bounded");
1060   task->simdata->rate = maxrate;
1061   return MSG_task_put(task, dest, channel);
1062 }
1063
1064 /** \ingroup msg_deprecated_functions
1065  *
1066  * \brief Put a task on a channel of an
1067  * host (with a timeout on the waiting of the destination host) and
1068  * waits for the end of the transmission.
1069  *
1070  * This function is used for describing the behavior of a process. It
1071  * takes four parameter.
1072  * \param task a #msg_task_t to send on another location. This task
1073  will not be usable anymore when the function will return. There is
1074  no automatic task duplication and you have to save your parameters
1075  before calling this function. Tasks are unique and once it has been
1076  sent to another location, you should not access it anymore. You do
1077  not need to call MSG_task_destroy() but to avoid using, as an
1078  effect of inattention, this task anymore, you definitely should
1079  renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
1080  can be transfered iff it has been correctly created with
1081  MSG_task_create().
1082  * \param dest the destination of the message
1083  * \param channel the channel on which the process should put this
1084  task. This value has to be >=0 and < than the maximal number of
1085  channels fixed with MSG_set_channel_number().
1086  * \param timeout the maximum time to wait for a task before giving
1087  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1088  will not be modified
1089  * \return #MSG_HOST_FAILURE if the host on which
1090 this function was called was shut down,
1091 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
1092 (network failure, dest failure, timeout...) or #MSG_OK if the communication succeeded.
1093  */
1094 msg_error_t
1095 MSG_task_put_with_timeout(msg_task_t task, msg_host_t dest,
1096                           m_channel_t channel, double timeout)
1097 {
1098   XBT_WARN("DEPRECATED! Now use MSG_task_send_with_timeout");
1099   xbt_assert((channel >= 0)
1100               && (channel < msg_global->max_channel), "Invalid channel %d",
1101               channel);
1102
1103   XBT_DEBUG("MSG_task_put_with_timout: Trying to send a task to '%s'", SIMIX_host_get_name(dest->smx_host));
1104   return
1105       MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel
1106                                    (dest, channel), task, timeout);
1107 }
1108
1109 /** \ingroup msg_deprecated_functions
1110  * \brief Test whether there is a pending communication on a channel, and who sent it.
1111  *
1112  * It takes one parameter.
1113  * \param channel the channel on which the process should be
1114  listening. This value has to be >=0 and < than the maximal
1115  number of channels fixed with MSG_set_channel_number().
1116  * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
1117  */
1118 int MSG_task_probe_from(m_channel_t channel)
1119 {
1120   XBT_WARN("DEPRECATED! Now use MSG_task_listen_from");
1121   msg_task_t task;
1122
1123   xbt_assert((channel >= 0)
1124               && (channel < msg_global->max_channel), "Invalid channel %d",
1125               channel);
1126
1127   if (NULL ==
1128       (task =
1129        MSG_mailbox_get_head(MSG_mailbox_get_by_channel
1130                             (MSG_host_self(), channel))))
1131     return -1;
1132
1133   return MSG_process_get_PID(task->simdata->sender);
1134 }
1135
1136 /** \ingroup msg_deprecated_functions
1137  * \brief Test whether there is a pending communication on a channel.
1138  *
1139  * It takes one parameter.
1140  * \param channel the channel on which the process should be
1141  listening. This value has to be >=0 and < than the maximal
1142  number of channels fixed with MSG_set_channel_number().
1143  * \return 1 if there is a pending communication and 0 otherwise
1144  */
1145 int MSG_task_Iprobe(m_channel_t channel)
1146 {
1147   XBT_WARN("DEPRECATED!");
1148   xbt_assert((channel >= 0)
1149               && (channel < msg_global->max_channel), "Invalid channel %d",
1150               channel);
1151
1152   return
1153       !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel
1154                             (MSG_host_self(), channel));
1155 }
1156
1157 /** \ingroup msg_deprecated_functions
1158
1159  * \brief Return the number of tasks waiting to be received on a \a
1160  channel and sent by \a host.
1161  *
1162  * It takes two parameters.
1163  * \param channel the channel on which the process should be
1164  listening. This value has to be >=0 and < than the maximal
1165  number of channels fixed with MSG_set_channel_number().
1166  * \param host the host that is to be watched.
1167  * \return the number of tasks waiting to be received on \a channel
1168  and sent by \a host.
1169  */
1170 int MSG_task_probe_from_host(int channel, msg_host_t host)
1171 {
1172   XBT_WARN("DEPRECATED! Now use MSG_task_listen_from_host");
1173   xbt_assert((channel >= 0)
1174               && (channel < msg_global->max_channel), "Invalid channel %d",
1175               channel);
1176
1177   return
1178       MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel
1179                                                (MSG_host_self(), channel),
1180                                                host);
1181
1182 }
1183
1184 /** \ingroup msg_deprecated_functions
1185  * \brief Listen on \a channel and waits for receiving a task from \a host.
1186  *
1187  * It takes three parameters.
1188  * \param task a memory location for storing a #msg_task_t. It will
1189  hold a task when this function will return. Thus \a task should not
1190  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1191  those two condition does not hold, there will be a warning message.
1192  * \param channel the channel on which the process should be
1193  listening. This value has to be >=0 and < than the maximal
1194  number of channels fixed with MSG_set_channel_number().
1195  * \param host the host that is to be watched.
1196  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1197  */
1198 msg_error_t
1199 MSG_task_get_from_host(msg_task_t * task, m_channel_t channel, msg_host_t host)
1200 {
1201   XBT_WARN("DEPRECATED! Now use MSG_task_receive_from_host");
1202   return MSG_task_get_ext(task, channel, -1, host);
1203 }
1204
1205 /** \ingroup msg_deprecated_functions
1206  * \brief Listen on a channel and wait for receiving a task.
1207  *
1208  * It takes two parameters.
1209  * \param task a memory location for storing a #msg_task_t. It will
1210  hold a task when this function will return. Thus \a task should not
1211  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1212  those two condition does not hold, there will be a warning message.
1213  * \param channel the channel on which the process should be
1214  listening. This value has to be >=0 and < than the maximal
1215  number of channels fixed with MSG_set_channel_number().
1216  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1217  */
1218 msg_error_t MSG_task_get(msg_task_t * task, m_channel_t channel)
1219 {
1220   XBT_WARN("DEPRECATED! Now use MSG_task_receive");
1221   return MSG_task_get_with_timeout(task, channel, -1);
1222 }
1223
1224 /** \ingroup msg_deprecated_functions
1225  * \brief Listen on a channel and wait for receiving a task with a timeout.
1226  *
1227  * It takes three parameters.
1228  * \param task a memory location for storing a #msg_task_t. It will
1229  hold a task when this function will return. Thus \a task should not
1230  be equal to \c NULL and \a *task should be equal to \c NULL. If one of
1231  those two condition does not hold, there will be a warning message.
1232  * \param channel the channel on which the process should be
1233  listening. This value has to be >=0 and < than the maximal
1234  number of channels fixed with MSG_set_channel_number().
1235  * \param max_duration the maximum time to wait for a task before giving
1236  up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
1237  will not be modified and will still be
1238  equal to \c NULL when returning.
1239  * \return a #msg_error_t indicating whether the operation was successful (#MSG_OK), or why it failed otherwise.
1240  */
1241 msg_error_t
1242 MSG_task_get_with_timeout(msg_task_t * task, m_channel_t channel,
1243                           double max_duration)
1244 {
1245   XBT_WARN("DEPRECATED! Now use MSG_task_receive_with_timeout");
1246   return MSG_task_get_ext(task, channel, max_duration, NULL);
1247 }
1248
1249 msg_error_t
1250 MSG_task_get_ext(msg_task_t * task, m_channel_t channel, double timeout,
1251                  msg_host_t host)
1252 {
1253   XBT_WARN("DEPRECATED! Now use MSG_task_receive_ext");
1254   xbt_assert((channel >= 0)
1255               && (channel < msg_global->max_channel), "Invalid channel %d",
1256               channel);
1257
1258   return
1259       MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel
1260                                (MSG_host_self(), channel), task, host,
1261                                timeout);
1262 }
1263
1264 #endif