Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
65b78093c9c2198cbf1c9599b1eb83bec0816725
[simgrid.git] / src / msg / msg_gos.cpp
1 /* Copyright (c) 2004-2018. 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 "simgrid/Exception.hpp"
7
8 #include "simgrid/s4u/Mailbox.hpp"
9 #include "src/instr/instr_private.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/msg/msg_private.hpp"
12 #include "src/simix/smx_private.hpp" /* MSG_task_listen looks inside the rdv directly. Not clean. */
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg, "Logging specific to MSG (gos)");
15
16 /** @ingroup msg_task_usage
17  * @brief Executes a task and waits for its termination.
18  *
19  * This function is used for describing the behavior of a process. It 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 or #MSG_HOST_FAILURE otherwise
22  */
23 msg_error_t MSG_task_execute(msg_task_t task)
24 {
25   return MSG_parallel_task_execute(task);
26 }
27
28 /** @ingroup msg_task_usage
29  * @brief Executes a parallel task and waits for its termination.
30  *
31  * @param task a #msg_task_t to execute on the location on which the process is running.
32  *
33  * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED
34  * or #MSG_HOST_FAILURE otherwise
35  */
36 msg_error_t MSG_parallel_task_execute(msg_task_t task)
37 {
38   return MSG_parallel_task_execute_with_timeout(task, -1);
39 }
40
41 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
42 {
43   simdata_task_t simdata = task->simdata;
44   e_smx_state_t comp_state;
45   msg_error_t status = MSG_OK;
46
47   TRACE_msg_task_execute_start(task);
48
49   xbt_assert((not simdata->compute) && not task->simdata->isused,
50              "This task is executed somewhere else. Go fix your code!");
51
52   XBT_DEBUG("Computing on %s", MSG_process_get_name(MSG_process_self()));
53
54   if (simdata->flops_amount <= 0.0 && not simdata->host_nb) {
55     TRACE_msg_task_execute_end(task);
56     return MSG_OK;
57   }
58
59   try {
60     simdata->setUsed();
61
62     if (simdata->host_nb > 0) {
63       simdata->compute =
64           boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(simcall_execution_parallel_start(
65               task->name ?: "", simdata->host_nb, simdata->host_list, simdata->flops_parallel_amount,
66               simdata->bytes_parallel_amount, -1.0, timeout));
67       XBT_DEBUG("Parallel execution action created: %p", simdata->compute.get());
68       if (task->category != nullptr)
69         simcall_set_category(simdata->compute, task->category);
70     } else {
71       simdata->compute = boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(
72           simcall_execution_start(task->name ?: "", task->category ?: "", simdata->flops_amount, simdata->priority,
73                                   simdata->bound, MSG_process_get_host(MSG_process_self())));
74     }
75
76     comp_state = simcall_execution_wait(simdata->compute);
77
78     simdata->setNotUsed();
79
80     XBT_DEBUG("Execution task '%s' finished in state %d", task->name, (int)comp_state);
81   } catch (simgrid::HostFailureException& e) {
82     status = MSG_HOST_FAILURE;
83   } catch (simgrid::TimeoutError& e) {
84     status = MSG_TIMEOUT;
85   } catch (xbt_ex& e) {
86     switch (e.category) {
87     case cancel_error:
88       status = MSG_TASK_CANCELED;
89       break;
90     default:
91       throw;
92     }
93   }
94
95   /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
96   simdata->flops_amount = 0.0;
97   simdata->comm = nullptr;
98   simdata->compute = nullptr;
99   TRACE_msg_task_execute_end(task);
100
101   return status;
102 }
103
104 /** @ingroup msg_task_usage
105  * @brief Sleep for the specified number of seconds
106  *
107  * Makes the current process sleep until @a time seconds have elapsed.
108  *
109  * @param nb_sec a number of second
110  */
111 msg_error_t MSG_process_sleep(double nb_sec)
112 {
113   msg_error_t status = MSG_OK;
114
115   try {
116     simgrid::s4u::this_actor::sleep_for(nb_sec);
117   }
118   catch(xbt_ex& e) {
119     if (e.category == cancel_error) {
120       XBT_DEBUG("According to the JAVA API, a sleep call should only deal with HostFailureException, I'm lost.");
121       // adsein: MSG_TASK_CANCELED is assigned when someone kills the process that made the sleep, this is not
122       // correct. For instance, when the node is turned off, the error should be MSG_HOST_FAILURE, which is by the way
123       // and according to the JAVA document, the only exception that can be triggered by MSG_Process_sleep call.
124       // To avoid possible impacts in the code, I just raised a host_failure exception for the moment in the JAVA code
125       // and did not change anythings at the C level.
126       // See comment in the jmsg_process.c file, function JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
127       status = MSG_TASK_CANCELED;
128     } else
129       throw;
130   }
131
132   return status;
133 }
134
135 /** @ingroup msg_task_usage
136  * @brief Receives a task from a mailbox.
137  *
138  * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
139  * for receiving tasks asynchronously.
140  *
141  * @param task a memory location for storing a #msg_task_t.
142  * @param alias name of the mailbox to receive the task from
143  *
144  * @return Returns
145  * #MSG_OK if the task was successfully received,
146  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
147  */
148 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
149 {
150   return MSG_task_receive_with_timeout(task, alias, -1);
151 }
152
153 /** @ingroup msg_task_usage
154  * @brief Receives a task from a mailbox at a given rate.
155  *
156  * @param task a memory location for storing a #msg_task_t.
157  * @param alias name of the mailbox to receive the task from
158  * @param rate limit the reception to rate bandwidth (byte/sec)
159  *
160  * The rate parameter can be used to receive a task with a limited
161  * bandwidth (smaller than the physical available value). Use
162  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
163  * rate value do disable this feature).
164  *
165  * @return Returns
166  * #MSG_OK if the task was successfully received,
167  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
168  */
169 msg_error_t MSG_task_receive_bounded(msg_task_t * task, const char *alias, double rate)
170 {
171   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
172 }
173
174 /** @ingroup msg_task_usage
175  * @brief Receives a task from a mailbox with a given timeout.
176  *
177  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
178  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously.  You can provide a -1 timeout
179  * to obtain an infinite timeout.
180  *
181  * @param task a memory location for storing a #msg_task_t.
182  * @param alias name of the mailbox to receive the task from
183  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
184  *
185  * @return Returns
186  * #MSG_OK if the task was successfully received,
187  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
188  */
189 msg_error_t MSG_task_receive_with_timeout(msg_task_t * task, const char *alias, double timeout)
190 {
191   return MSG_task_receive_ext(task, alias, timeout, nullptr);
192 }
193
194 /** @ingroup msg_task_usage
195  * @brief Receives a task from a mailbox with a given timeout and at a given rate.
196  *
197  * @param task a memory location for storing a #msg_task_t.
198  * @param alias name of the mailbox to receive the task from
199  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
200  * @param rate limit the reception to rate bandwidth (byte/sec)
201  *
202  * The rate parameter can be used to send a task with a limited
203  * bandwidth (smaller than the physical available value). Use
204  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
205  * rate value do disable this feature).
206  *
207  * @return Returns
208  * #MSG_OK if the task was successfully received,
209  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
210  */
211 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t * task, const char *alias, double timeout,double rate)
212 {
213   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
214 }
215
216 /** @ingroup msg_task_usage
217  * @brief Receives a task from a mailbox from a specific host with a given timeout.
218  *
219  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
220  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
221  * to obtain an infinite timeout.
222  *
223  * @param task a memory location for storing a #msg_task_t.
224  * @param alias name of the mailbox to receive the task from
225  * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
226  * @param host a #msg_host_t host from where the task was sent
227  *
228  * @return Returns
229  * #MSG_OK if the task was successfully received,
230  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
231  */
232 msg_error_t MSG_task_receive_ext(msg_task_t * task, const char *alias, double timeout, msg_host_t host)
233 {
234   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
235   return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
236 }
237
238 /** @ingroup msg_task_usage
239  * @brief Receives a task from a mailbox from a specific host with a given timeout  and at a given rate.
240  *
241  * @param task a memory location for storing a #msg_task_t.
242  * @param alias name of the mailbox to receive the task from
243  * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
244  * @param host a #msg_host_t host from where the task was sent
245  * @param rate limit the reception to rate bandwidth (byte/sec)
246  *
247  * The rate parameter can be used to receive a task with a limited
248  * bandwidth (smaller than the physical available value). Use
249  * MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a
250  * rate value do disable this feature).
251  *
252  * @return Returns
253  * #MSG_OK if the task was successfully received,
254  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
255  */
256 msg_error_t MSG_task_receive_ext_bounded(msg_task_t * task, const char *alias, double timeout, msg_host_t host,
257                                          double rate)
258 {
259   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
260   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
261   msg_error_t ret = MSG_OK;
262   /* We no longer support getting a task from a specific host */
263   if (host)
264     THROW_UNIMPLEMENTED;
265
266   TRACE_msg_task_get_start();
267
268   /* Sanity check */
269   xbt_assert(task, "Null pointer for the task storage");
270
271   if (*task)
272     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
273
274   /* Try to receive it by calling SIMIX network layer */
275   try {
276     simcall_comm_recv(MSG_process_self()->get_impl(), mailbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr,
277                       timeout, rate);
278     XBT_DEBUG("Got task %s from %s", (*task)->name, mailbox->get_cname());
279     (*task)->simdata->setNotUsed();
280   } catch (simgrid::HostFailureException& e) {
281     ret = MSG_HOST_FAILURE;
282   } catch (simgrid::TimeoutError& e) {
283     ret = MSG_TIMEOUT;
284   } catch (xbt_ex& e) {
285     switch (e.category) {
286     case cancel_error:
287       ret = MSG_HOST_FAILURE;
288       break;
289     case network_error:
290       ret = MSG_TRANSFER_FAILURE;
291       break;
292     default:
293       throw;
294     }
295   }
296
297   if (ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
298     TRACE_msg_task_get_end(*task);
299   }
300   return ret;
301 }
302
303 /* Internal function used to factorize code between MSG_task_isend(), MSG_task_isend_bounded(), and MSG_task_dsend(). */
304 static inline msg_comm_t MSG_task_isend_internal(msg_task_t task, const char* alias,
305                                                  void_f_pvoid_t cleanup, int detached)
306 {
307   simdata_task_t t_simdata = nullptr;
308   msg_process_t myself = MSG_process_self();
309   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
310   TRACE_msg_task_put_start(task);
311
312   /* Prepare the task to send */
313   t_simdata = task->simdata;
314   t_simdata->sender = myself;
315   t_simdata->source = MSG_host_self();
316   t_simdata->setUsed();
317   t_simdata->comm = nullptr;
318   msg_global->sent_msg++;
319
320   /* Send it by calling SIMIX network layer */
321   smx_activity_t act =
322       simcall_comm_isend(myself->get_impl(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
323                          sizeof(void*), nullptr, cleanup, nullptr, nullptr, detached);
324   t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(act);
325
326   msg_comm_t comm = nullptr;
327   if (not detached) {
328     comm = new simgrid::msg::Comm(task, nullptr, act);
329   }
330
331   if (TRACE_is_enabled())
332     simcall_set_category(act, task->category);
333   TRACE_msg_task_put_end();
334
335   return comm;
336 }
337
338 /** @ingroup msg_task_usage
339  * @brief Sends a task on a mailbox.
340  *
341  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
342  *
343  * @param task a #msg_task_t to send on another location.
344  * @param alias name of the mailbox to sent the task to
345  * @return the msg_comm_t communication created
346  */
347 msg_comm_t MSG_task_isend(msg_task_t task, const char *alias)
348 {
349   return MSG_task_isend_internal(task, alias, nullptr, 0);
350 }
351
352 /** @ingroup msg_task_usage
353  * @brief Sends a task on a mailbox with a maximum rate
354  *
355  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
356  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
357  *
358  * @param task a #msg_task_t to send on another location.
359  * @param alias name of the mailbox to sent the task to
360  * @param maxrate the maximum communication rate for sending this task (byte/sec).
361  * @return the msg_comm_t communication created
362  */
363 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char *alias, double maxrate)
364 {
365   task->simdata->rate = maxrate;
366   return MSG_task_isend_internal(task, alias, nullptr, 0);
367 }
368
369 /** @ingroup msg_task_usage
370  * @brief Sends a task on a mailbox.
371  *
372  * This is a non blocking detached send function.
373  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
374  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
375  * usual. More details on this can be obtained on
376  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
377  * in the SimGrid-user mailing list archive.
378  *
379  * @param task a #msg_task_t to send on another location.
380  * @param alias name of the mailbox to sent the task to
381  * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
382  * (if nullptr, no function will be called)
383  */
384 void MSG_task_dsend(msg_task_t task, const char *alias, void_f_pvoid_t cleanup)
385 {
386   msg_comm_t XBT_ATTRIB_UNUSED comm = MSG_task_isend_internal(task, alias, cleanup, 1);
387   xbt_assert(comm == nullptr);
388 }
389
390 /** @ingroup msg_task_usage
391  * @brief Sends a task on a mailbox with a maximal rate.
392  *
393  * This is a non blocking detached send function.
394  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
395  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
396  * usual. More details on this can be obtained on
397  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
398  * in the SimGrid-user mailing list archive.
399  *
400  * The rate parameter can be used to send a task with a limited
401  * bandwidth (smaller than the physical available value). Use
402  * MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate
403  * value do disable this feature).
404  *
405  * @param task a #msg_task_t to send on another location.
406  * @param alias name of the mailbox to sent the task to
407  * @param cleanup a function to destroy the task if the
408  * communication fails, e.g. MSG_task_destroy
409  * (if nullptr, no function will be called)
410  * @param maxrate the maximum communication rate for sending this task (byte/sec)
411  *
412  */
413 void MSG_task_dsend_bounded(msg_task_t task, const char *alias, void_f_pvoid_t cleanup, double maxrate)
414 {
415   task->simdata->rate = maxrate;
416   MSG_task_dsend(task, alias, cleanup);
417 }
418
419 /** @ingroup msg_task_usage
420  * @brief Starts listening for receiving a task from an asynchronous communication.
421  *
422  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
423  *
424  * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
425  * @param name of the mailbox to receive the task on
426  * @return the msg_comm_t communication created
427  */
428 msg_comm_t MSG_task_irecv(msg_task_t *task, const char *name)
429 {
430   return MSG_task_irecv_bounded(task, name, -1.0);
431 }
432
433 /** @ingroup msg_task_usage
434  * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
435  *
436  * The rate parameter can be used to receive a task with a limited
437  * bandwidth (smaller than the physical available value). Use
438  * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
439  * value do disable this feature).
440  *
441  * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
442  * @param name of the mailbox to receive the task on
443  * @param rate limit the bandwidth to the given rate (byte/sec)
444  * @return the msg_comm_t communication created
445  */
446 msg_comm_t MSG_task_irecv_bounded(msg_task_t *task, const char *name, double rate)
447 {
448   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(name);
449
450   /* FIXME: these functions are not traceable */
451   /* Sanity check */
452   xbt_assert(task, "Null pointer for the task storage");
453
454   if (*task)
455     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
456
457   /* Try to receive it by calling SIMIX network layer */
458   msg_comm_t comm = new simgrid::msg::Comm(
459       nullptr, task,
460       simcall_comm_irecv(SIMIX_process_self(), mbox->get_impl(), task, nullptr, nullptr, nullptr, nullptr, rate));
461
462   return comm;
463 }
464
465 /** @ingroup msg_task_usage
466  * @brief Checks whether a communication is done, and if yes, finalizes it.
467  * @param comm the communication to test
468  * @return 'true' if the communication is finished
469  * (but it may have failed, use MSG_comm_get_status() to know its status)
470  * or 'false' if the communication is not finished yet
471  * If the status is 'false', don't forget to use MSG_process_sleep() after the test.
472  */
473 int MSG_comm_test(msg_comm_t comm)
474 {
475   bool finished = false;
476
477   try {
478     finished = simcall_comm_test(comm->s_comm);
479     if (finished && comm->task_received != nullptr) {
480       /* I am the receiver */
481       (*comm->task_received)->simdata->setNotUsed();
482     }
483   } catch (simgrid::TimeoutError& e) {
484     comm->status = MSG_TIMEOUT;
485     finished     = true;
486   }
487   catch (xbt_ex& e) {
488     switch (e.category) {
489       case network_error:
490         comm->status = MSG_TRANSFER_FAILURE;
491         finished     = true;
492         break;
493       default:
494         throw;
495     }
496   }
497
498   return finished;
499 }
500
501 /** @ingroup msg_task_usage
502  * @brief This function checks if a communication is finished.
503  * @param comms a vector of communications
504  * @return the position of the finished communication if any
505  * (but it may have failed, use MSG_comm_get_status() to know its status),
506  * or -1 if none is finished
507  */
508 int MSG_comm_testany(xbt_dynar_t comms)
509 {
510   int finished_index = -1;
511
512   /* Create the equivalent array with SIMIX objects: */
513   std::vector<simgrid::kernel::activity::ActivityImplPtr> s_comms;
514   s_comms.reserve(xbt_dynar_length(comms));
515   msg_comm_t comm;
516   unsigned int cursor;
517   xbt_dynar_foreach(comms, cursor, comm) {
518     s_comms.push_back(comm->s_comm);
519   }
520
521   msg_error_t status = MSG_OK;
522   try {
523     finished_index = simcall_comm_testany(s_comms.data(), s_comms.size());
524   } catch (simgrid::TimeoutError& e) {
525     finished_index = e.value;
526     status         = MSG_TIMEOUT;
527   }
528   catch (xbt_ex& e) {
529     switch (e.category) {
530       case network_error:
531         finished_index = e.value;
532         status = MSG_TRANSFER_FAILURE;
533         break;
534       default:
535         throw;
536     }
537   }
538
539   if (finished_index != -1) {
540     comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
541     /* the communication is finished */
542     comm->status = status;
543
544     if (status == MSG_OK && comm->task_received != nullptr) {
545       /* I am the receiver */
546       (*comm->task_received)->simdata->setNotUsed();
547     }
548   }
549
550   return finished_index;
551 }
552
553 /** @ingroup msg_task_usage
554  * @brief Destroys a communication.
555  * @param comm the communication to destroy.
556  */
557 void MSG_comm_destroy(msg_comm_t comm)
558 {
559   delete comm;
560 }
561
562 /** @ingroup msg_task_usage
563  * @brief Wait for the completion of a communication.
564  *
565  * It takes two parameters.
566  * @param comm the communication to wait.
567  * @param timeout Wait until the communication terminates or the timeout occurs.
568  *                You can provide a -1 timeout to obtain an infinite timeout.
569  * @return msg_error_t
570  */
571 msg_error_t MSG_comm_wait(msg_comm_t comm, double timeout)
572 {
573   try {
574     simcall_comm_wait(comm->s_comm, timeout);
575
576     if (comm->task_received != nullptr) {
577       /* I am the receiver */
578       (*comm->task_received)->simdata->setNotUsed();
579     }
580
581     /* FIXME: these functions are not traceable */
582   } catch (simgrid::TimeoutError& e) {
583     comm->status = MSG_TIMEOUT;
584   }
585   catch (xbt_ex& e) {
586     switch (e.category) {
587     case network_error:
588       comm->status = MSG_TRANSFER_FAILURE;
589       break;
590     default:
591       throw;
592     }
593   }
594
595   return comm->status;
596 }
597
598 /** @ingroup msg_task_usage
599  * @brief This function is called by a sender and permit to wait for each communication
600  *
601  * @param comm a vector of communication
602  * @param nb_elem is the size of the comm vector
603  * @param timeout for each call of MSG_comm_wait
604  */
605 void MSG_comm_waitall(msg_comm_t * comm, int nb_elem, double timeout)
606 {
607   for (int i = 0; i < nb_elem; i++)
608     MSG_comm_wait(comm[i], timeout);
609 }
610
611 /** @ingroup msg_task_usage
612  * @brief This function waits for the first communication finished in a list.
613  * @param comms a vector of communications
614  * @return the position of the first finished communication
615  * (but it may have failed, use MSG_comm_get_status() to know its status)
616  */
617 int MSG_comm_waitany(xbt_dynar_t comms)
618 {
619   int finished_index = -1;
620
621   /* create the equivalent dynar with SIMIX objects */
622   xbt_dynar_t s_comms = xbt_dynar_new(sizeof(smx_activity_t), [](void*ptr){
623     intrusive_ptr_release(*(simgrid::kernel::activity::ActivityImpl**)ptr);
624   });
625   msg_comm_t comm;
626   unsigned int cursor;
627   xbt_dynar_foreach(comms, cursor, comm) {
628     intrusive_ptr_add_ref(comm->s_comm.get());
629     xbt_dynar_push_as(s_comms, simgrid::kernel::activity::ActivityImpl*, comm->s_comm.get());
630   }
631
632   msg_error_t status = MSG_OK;
633   try {
634     finished_index = simcall_comm_waitany(s_comms, -1);
635   } catch (simgrid::TimeoutError& e) {
636     finished_index = e.value;
637     status         = MSG_TIMEOUT;
638   }
639   catch(xbt_ex& e) {
640     switch (e.category) {
641       case network_error:
642         finished_index = e.value;
643         status = MSG_TRANSFER_FAILURE;
644         break;
645       default:
646         throw;
647     }
648   }
649
650   xbt_assert(finished_index != -1, "WaitAny returned -1");
651   xbt_dynar_free(&s_comms);
652
653   comm = xbt_dynar_get_as(comms, finished_index, msg_comm_t);
654   /* the communication is finished */
655   comm->status = status;
656
657   if (comm->task_received != nullptr) {
658     /* I am the receiver */
659     (*comm->task_received)->simdata->setNotUsed();
660   }
661
662   return finished_index;
663 }
664
665 /**
666  * @ingroup msg_task_usage
667  * @brief Returns the error (if any) that occurred during a finished communication.
668  * @param comm a finished communication
669  * @return the status of the communication, or #MSG_OK if no error occurred
670  * during the communication
671  */
672 msg_error_t MSG_comm_get_status(msg_comm_t comm) {
673
674   return comm->status;
675 }
676
677 /** @ingroup msg_task_usage
678  * @brief Get a task (#msg_task_t) from a communication
679  *
680  * @param comm the communication where to get the task
681  * @return the task from the communication
682  */
683 msg_task_t MSG_comm_get_task(msg_comm_t comm)
684 {
685   xbt_assert(comm, "Invalid parameter");
686
687   return comm->task_received ? *comm->task_received : comm->task_sent;
688 }
689
690 /**
691  * @brief This function is called by SIMIX in kernel mode to copy the data of a comm.
692  * @param synchro the comm
693  * @param buff the data copied
694  * @param buff_size size of the buffer
695  */
696 void MSG_comm_copy_data_from_SIMIX(smx_activity_t synchro, void* buff, size_t buff_size)
697 {
698   simgrid::kernel::activity::CommImplPtr comm =
699       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
700
701   SIMIX_comm_copy_pointer_callback(comm, buff, buff_size);
702
703   // notify the user callback if any
704   if (msg_global->task_copy_callback) {
705     msg_task_t task = static_cast<msg_task_t>(buff);
706     msg_global->task_copy_callback(task, comm->src_proc->ciface(), comm->dst_proc->ciface());
707   }
708 }
709
710 /** @ingroup msg_task_usage
711  * @brief Sends a task to a mailbox
712  *
713  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
714  * side if #MSG_task_receive is used).
715  * See #MSG_task_isend for sending tasks asynchronously.
716  *
717  * @param task the task to be sent
718  * @param alias the mailbox name to where the task is sent
719  *
720  * @return Returns #MSG_OK if the task was successfully sent,
721  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
722  */
723 msg_error_t MSG_task_send(msg_task_t task, const char *alias)
724 {
725   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
726   return MSG_task_send_with_timeout(task, alias, -1);
727 }
728
729 /** @ingroup msg_task_usage
730  * @brief Sends a task to a mailbox with a maximum rate
731  *
732  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
733  * the application to limit the bandwidth utilization of network links when sending the task.
734  *
735  * The maxrate parameter can be used to send a task with a limited
736  * bandwidth (smaller than the physical available value). Use
737  * MSG_task_send() if you don't limit the rate (or pass -1 as a rate
738  * value do disable this feature).
739  *
740  * @param task the task to be sent
741  * @param alias the mailbox name to where the task is sent
742  * @param maxrate the maximum communication rate for sending this task (byte/sec)
743  *
744  * @return Returns #MSG_OK if the task was successfully sent,
745  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
746  */
747 msg_error_t MSG_task_send_bounded(msg_task_t task, const char *alias, double maxrate)
748 {
749   task->simdata->rate = maxrate;
750   return MSG_task_send(task, alias);
751 }
752
753 /** @ingroup msg_task_usage
754  * @brief Sends a task to a mailbox with a timeout
755  *
756  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
757  *
758  * @param task the task to be sent
759  * @param alias the mailbox name to where the task is sent
760  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
761  *
762  * @return Returns #MSG_OK if the task was successfully sent,
763  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
764  */
765 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char *alias, double timeout)
766 {
767   msg_error_t ret = MSG_OK;
768   simdata_task_t t_simdata = nullptr;
769   msg_process_t process = MSG_process_self();
770   simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::by_name(alias);
771
772   TRACE_msg_task_put_start(task);
773
774   /* Prepare the task to send */
775   t_simdata = task->simdata;
776   t_simdata->sender = process;
777   t_simdata->source = MSG_host_self();
778
779   t_simdata->setUsed();
780
781   t_simdata->comm = nullptr;
782   msg_global->sent_msg++;
783
784   /* Try to send it by calling SIMIX network layer */
785   try {
786     smx_activity_t comm = nullptr; /* MC needs the comm to be set to nullptr during the simix call  */
787     comm = simcall_comm_isend(SIMIX_process_self(), mailbox->get_impl(), t_simdata->bytes_amount, t_simdata->rate, task,
788                               sizeof(void*), nullptr, nullptr, nullptr, nullptr, 0);
789     if (TRACE_is_enabled() && task->category != nullptr)
790       simcall_set_category(comm, task->category);
791     t_simdata->comm = boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(comm);
792     simcall_comm_wait(comm, timeout);
793   } catch (simgrid::TimeoutError& e) {
794     ret = MSG_TIMEOUT;
795   }
796   catch (xbt_ex& e) {
797     switch (e.category) {
798     case cancel_error:
799       ret = MSG_HOST_FAILURE;
800       break;
801     case network_error:
802       ret = MSG_TRANSFER_FAILURE;
803       break;
804     default:
805       throw;
806     }
807
808     /* If the send failed, it is not used anymore */
809     t_simdata->setNotUsed();
810   }
811
812   TRACE_msg_task_put_end();
813   return ret;
814 }
815
816 /** @ingroup msg_task_usage
817  * @brief Sends a task to a mailbox with a timeout and with a maximum rate
818  *
819  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
820  *
821  * The maxrate parameter can be used to send a task with a limited
822  * bandwidth (smaller than the physical available value). Use
823  * MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate
824  * value do disable this feature).
825  *
826  * @param task the task to be sent
827  * @param alias the mailbox name to where the task is sent
828  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
829  * @param maxrate the maximum communication rate for sending this task (byte/sec)
830  *
831  * @return Returns #MSG_OK if the task was successfully sent,
832  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
833  */
834 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char *alias, double timeout, double maxrate)
835 {
836   task->simdata->rate = maxrate;
837   return MSG_task_send_with_timeout(task, alias, timeout);
838 }
839
840 /** @ingroup msg_task_usage
841  * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
842  *
843  * @param alias the name of the mailbox to be considered
844  *
845  * @return Returns the PID of sender process,
846  * -1 if there is no communication in the mailbox.
847  */
848 int MSG_task_listen_from(const char *alias)
849 {
850   simgrid::s4u::MailboxPtr mbox = simgrid::s4u::Mailbox::by_name(alias);
851   simgrid::kernel::activity::CommImplPtr comm =
852       boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(mbox->front());
853
854   if (not comm)
855     return -1;
856
857   return MSG_process_get_PID(static_cast<msg_task_t>(comm->src_buff)->simdata->sender);
858 }
859
860 /** @ingroup msg_task_usage
861  * @brief Sets the tracing category of a task.
862  *
863  * This function should be called after the creation of a MSG task, to define the category of that task. The
864  * first parameter task must contain a task that was  created with the function #MSG_task_create. The second
865  * parameter category must contain a category that was previously declared with the function #TRACE_category
866  * (or with #TRACE_category_with_color).
867  *
868  * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
869  *
870  * @param task the task that is going to be categorized
871  * @param category the name of the category to be associated to the task
872  *
873  * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
874  */
875 void MSG_task_set_category (msg_task_t task, const char *category)
876 {
877   TRACE_msg_set_task_category (task, category);
878 }
879
880 /** @ingroup msg_task_usage
881  *
882  * @brief Gets the current tracing category of a task.
883  *
884  * @param task the task to be considered
885  *
886  * @see MSG_task_set_category
887  *
888  * @return Returns the name of the tracing category of the given task, nullptr otherwise
889  */
890 const char *MSG_task_get_category (msg_task_t task)
891 {
892   return task->category;
893 }