Logo AND Algorithmique Numérique Distribuée

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