Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement CRTP in kernel::activity
[simgrid.git] / src / msg / msg_task.cpp
1 /* Copyright (c) 2004-2019. 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.hpp"
7 #include "src/instr/instr_private.hpp"
8 #include <simgrid/s4u/Comm.hpp>
9 #include <simgrid/s4u/Exec.hpp>
10 #include <simgrid/s4u/Host.hpp>
11 #include <simgrid/s4u/Mailbox.hpp>
12
13 #include <algorithm>
14 #include <vector>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg, "Logging specific to MSG (task)");
17
18 namespace simgrid {
19 namespace msg {
20
21 Task::Task(const std::string& name, double flops_amount, double bytes_amount, void* data)
22     : name_(name), userdata_(data), flops_amount(flops_amount), bytes_amount(bytes_amount)
23 {
24   static std::atomic_ullong counter{0};
25   id_ = counter++;
26   if (MC_is_active())
27     MC_ignore_heap(&(id_), sizeof(id_));
28 }
29
30 Task::Task(const std::string& name, std::vector<s4u::Host*>&& hosts, std::vector<double>&& flops_amount,
31            std::vector<double>&& bytes_amount, void* data)
32     : Task(name, 1.0, 0, data)
33 {
34   parallel_             = true;
35   hosts_                = std::move(hosts);
36   flops_parallel_amount = std::move(flops_amount);
37   bytes_parallel_amount = std::move(bytes_amount);
38 }
39
40 Task* Task::create(const std::string& name, double flops_amount, double bytes_amount, void* data)
41 {
42   return new Task(name, flops_amount, bytes_amount, data);
43 }
44
45 Task* Task::create_parallel(const std::string& name, int host_nb, const msg_host_t* host_list, double* flops_amount,
46                             double* bytes_amount, void* data)
47 {
48   std::vector<simgrid::s4u::Host*> hosts(host_list, host_list + host_nb);
49   std::vector<double> flops;
50   std::vector<double> bytes;
51   if (flops_amount != nullptr)
52     flops = std::vector<double>(flops_amount, flops_amount + host_nb);
53   if (bytes_amount != nullptr)
54     bytes = std::vector<double>(bytes_amount, bytes_amount + host_nb * host_nb);
55
56   return new Task(name, std::move(hosts), std::move(flops), std::move(bytes), data);
57 }
58
59 msg_error_t Task::execute()
60 {
61   /* checking for infinite values */
62   xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
63
64   msg_error_t status = MSG_OK;
65   if (flops_amount <= 0.0)
66     return MSG_OK;
67
68   try {
69     set_used();
70     if (parallel_)
71       compute = s4u::this_actor::exec_init(hosts_, flops_parallel_amount, bytes_parallel_amount);
72     else
73       compute = s4u::this_actor::exec_init(flops_amount);
74
75     compute->set_name(name_)
76         ->set_tracing_category(tracing_category_)
77         ->set_timeout(timeout_)
78         ->set_priority(1 / priority_)
79         ->set_bound(bound_)
80         ->wait();
81
82     set_not_used();
83     XBT_DEBUG("Execution task '%s' finished", get_cname());
84   } catch (HostFailureException& e) {
85     status = MSG_HOST_FAILURE;
86   } catch (TimeoutError& e) {
87     status = MSG_TIMEOUT;
88   } catch (CancelException& e) {
89     status = MSG_TASK_CANCELED;
90   }
91
92   /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
93   flops_amount = 0.0;
94   comm         = nullptr;
95   compute      = nullptr;
96
97   return status;
98 }
99
100 s4u::CommPtr Task::send_async(const std::string& alias, void_f_pvoid_t cleanup, bool detached)
101 {
102   if (TRACE_actor_is_enabled()) {
103     container_t process_container = simgrid::instr::Container::by_name(instr_pid(*MSG_process_self()));
104     std::string key               = std::string("p") + std::to_string(get_id());
105     simgrid::instr::Container::get_root()->get_link("ACTOR_TASK_LINK")->start_event(process_container, "SR", key);
106   }
107
108   /* Prepare the task to send */
109   set_used();
110   this->comm = nullptr;
111   msg_global->sent_msg++;
112
113   s4u::CommPtr s4u_comm = s4u::Mailbox::by_name(alias)->put_init(this, bytes_amount)->set_rate(get_rate());
114   if (TRACE_is_enabled() && has_tracing_category())
115     s4u_comm->set_tracing_category(tracing_category_);
116
117   comm                  = s4u_comm;
118
119   if (detached)
120     comm->detach(cleanup);
121   else
122     comm->start();
123
124   return comm;
125 }
126
127 msg_error_t Task::send(const std::string& alias, double timeout)
128 {
129   msg_error_t ret = MSG_OK;
130   /* Try to send it */
131   try {
132     comm = nullptr; // needed, otherwise MC gets confused.
133     s4u::CommPtr s4u_comm = send_async(alias, nullptr, false);
134     comm                  = s4u_comm;
135     comm->wait_for(timeout);
136   } catch (simgrid::TimeoutError& e) {
137     ret = MSG_TIMEOUT;
138   } catch (simgrid::CancelException& e) {
139     ret = MSG_HOST_FAILURE;
140   } catch (xbt_ex& e) {
141     if (e.category == network_error)
142       ret = MSG_TRANSFER_FAILURE;
143     else
144       throw;
145
146     /* If the send failed, it is not used anymore */
147     set_not_used();
148   }
149
150   return ret;
151 }
152 void Task::cancel()
153 {
154   if (compute) {
155     compute->cancel();
156   } else if (comm) {
157     comm->cancel();
158   }
159   set_not_used();
160 }
161
162 void Task::set_priority(double priority)
163 {
164   xbt_assert(std::isfinite(1.0 / priority), "priority is not finite!");
165   priority_ = 1.0 / priority;
166 }
167
168 s4u::Actor* Task::get_sender()
169 {
170   return comm ? comm->get_sender().get() : nullptr;
171 }
172
173 s4u::Host* Task::get_source()
174 {
175   return comm ? comm->get_sender()->get_host() : nullptr;
176 }
177
178 void Task::set_used()
179 {
180   if (is_used_)
181     report_multiple_use();
182   is_used_ = true;
183 }
184
185 void Task::report_multiple_use() const
186 {
187   if (MSG_Global_t::debug_multiple_use) {
188     XBT_ERROR("This task is already used in there:");
189     // TODO, backtrace
190     XBT_ERROR("<missing backtrace>");
191     XBT_ERROR("And you try to reuse it from here:");
192     xbt_backtrace_display_current();
193   } else {
194     xbt_die("This task is still being used somewhere else. You cannot send it now. Go fix your code!"
195              "(use --cfg=msg/debug-multiple-use:on to get the backtrace of the other process)");
196   }
197 }
198 } // namespace msg
199 } // namespace simgrid
200
201 /********************************* Task **************************************/
202 /** @brief Creates a new task
203  *
204  * A constructor for msg_task_t taking four arguments.
205  *
206  * @param name a name for the object. It is for user-level information and can be nullptr.
207  * @param flop_amount a value of the processing amount (in flop) needed to process this new task.
208  * If 0, then it cannot be executed with MSG_task_execute(). This value has to be >=0.
209  * @param message_size a value of the amount of data (in bytes) needed to transfer this new task. If 0, then it cannot
210  * be transfered with MSG_task_send() and MSG_task_recv(). This value has to be >=0.
211  * @param data a pointer to any data may want to attach to the new object.  It is for user-level information and can
212  * be nullptr. It can be retrieved with the function @ref MSG_task_get_data.
213  * @return The new corresponding object.
214  */
215 msg_task_t MSG_task_create(const char *name, double flop_amount, double message_size, void *data)
216 {
217   return simgrid::msg::Task::create(name ? std::string(name) : "", flop_amount, message_size, data);
218 }
219
220 /** @brief Creates a new parallel task
221  *
222  * A constructor for #msg_task_t taking six arguments.
223  *
224  * \rst
225  * See :cpp:func:`void simgrid::s4u::this_actor::parallel_execute(int, s4u::Host**, double*, double*)` for
226  * the exact semantic of the parameters.
227  * \endrst
228  *
229  * @param name a name for the object. It is for user-level information and can be nullptr.
230  * @param host_nb the number of hosts implied in the parallel task.
231  * @param host_list an array of @p host_nb msg_host_t.
232  * @param flops_amount an array of @p host_nb doubles.
233  *        flops_amount[i] is the total number of operations that have to be performed on host_list[i].
234  * @param bytes_amount an array of @p host_nb* @p host_nb doubles.
235  * @param data a pointer to any data may want to attach to the new object.
236  *             It is for user-level information and can be nullptr.
237  *             It can be retrieved with the function @ref MSG_task_get_data().
238  */
239 msg_task_t MSG_parallel_task_create(const char *name, int host_nb, const msg_host_t * host_list,
240                                     double *flops_amount, double *bytes_amount, void *data)
241 {
242   // Task's flops amount is set to an arbitrary value > 0.0 to be able to distinguish, in
243   // MSG_task_get_remaining_work_ratio(), a finished task and a task that has not started yet.
244   return simgrid::msg::Task::create_parallel(name ? name : "", host_nb, host_list, flops_amount, bytes_amount, data);
245 }
246
247 /** @brief Return the user data of the given task */
248 void* MSG_task_get_data(msg_task_t task)
249 {
250   return task->get_user_data();
251 }
252
253 /** @brief Sets the user data of a given task */
254 void MSG_task_set_data(msg_task_t task, void *data)
255 {
256   task->set_user_data(data);
257 }
258
259 /** @brief Sets a function to be called when a task has just been copied.
260  * @param callback a callback function
261  */
262 // deprecated
263 void MSG_task_set_copy_callback(void (*callback) (msg_task_t task, msg_process_t sender, msg_process_t receiver)) {
264
265   msg_global->task_copy_callback = callback;
266
267   if (callback) {
268     SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
269   } else {
270     SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
271   }
272 }
273
274 /** @brief Returns the sender of the given task */
275 msg_process_t MSG_task_get_sender(msg_task_t task)
276 {
277   return task->get_sender();
278 }
279
280 /** @brief Returns the source (the sender's host) of the given task */
281 msg_host_t MSG_task_get_source(msg_task_t task)
282 {
283   return task->get_source();
284 }
285
286 /** @brief Returns the name of the given task. */
287 const char *MSG_task_get_name(msg_task_t task)
288 {
289   return task->get_cname();
290 }
291
292 /** @brief Sets the name of the given task. */
293 void MSG_task_set_name(msg_task_t task, const char *name)
294 {
295   task->set_name(name);
296 }
297
298 /**
299  * @brief Executes a task and waits for its termination.
300  *
301  * This function is used for describing the behavior of a process. It takes only one parameter.
302  * @param task a #msg_task_t to execute on the location on which the process is running.
303  * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
304  */
305 msg_error_t MSG_task_execute(msg_task_t task)
306 {
307   return task->execute();
308 }
309
310 /**
311  * @brief Executes a parallel task and waits for its termination.
312  *
313  * @param task a #msg_task_t to execute on the location on which the process is running.
314  *
315  * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
316  */
317 msg_error_t MSG_parallel_task_execute(msg_task_t task)
318 {
319   return task->execute();
320 }
321
322 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
323 {
324   task->set_timeout(timeout);
325   return task->execute();
326 }
327
328 /**
329  * @brief Sends a task on a mailbox.
330  *
331  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
332  *
333  * @param task a #msg_task_t to send on another location.
334  * @param alias name of the mailbox to sent the task to
335  * @return the msg_comm_t communication created
336  */
337 msg_comm_t MSG_task_isend(msg_task_t task, const char* alias)
338 {
339   return new simgrid::msg::Comm(task, nullptr, task->send_async(alias, nullptr, false));
340 }
341
342 /**
343  * @brief Sends a task on a mailbox with a maximum rate
344  *
345  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
346  * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
347  *
348  * @param task a #msg_task_t to send on another location.
349  * @param alias name of the mailbox to sent the task to
350  * @param maxrate the maximum communication rate for sending this task (byte/sec).
351  * @return the msg_comm_t communication created
352  */
353 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char* alias, double maxrate)
354 {
355   task->set_rate(maxrate);
356   return new simgrid::msg::Comm(task, nullptr, task->send_async(alias, nullptr, false));
357 }
358
359 /**
360  * @brief Sends a task on a mailbox.
361  *
362  * This is a non blocking detached send function.
363  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
364  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
365  * usual. More details on this can be obtained on
366  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
367  * in the SimGrid-user mailing list archive.
368  *
369  * @param task a #msg_task_t to send on another location.
370  * @param alias name of the mailbox to sent the task to
371  * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
372  * (if nullptr, no function will be called)
373  */
374 void MSG_task_dsend(msg_task_t task, const char* alias, void_f_pvoid_t cleanup)
375 {
376   task->send_async(alias, cleanup, true);
377 }
378
379 /**
380  * @brief Sends a task on a mailbox with a maximal rate.
381  *
382  * This is a non blocking detached send function.
383  * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
384  * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
385  * usual. More details on this can be obtained on
386  * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
387  * in the SimGrid-user mailing list archive.
388  *
389  * The rate parameter can be used to send a task with a limited bandwidth (smaller than the physical available value).
390  * Use MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
391  *
392  * @param task a #msg_task_t to send on another location.
393  * @param alias name of the mailbox to sent the task to
394  * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy (if nullptr, no
395  *        function will be called)
396  * @param maxrate the maximum communication rate for sending this task (byte/sec)
397  *
398  */
399 void MSG_task_dsend_bounded(msg_task_t task, const char* alias, void_f_pvoid_t cleanup, double maxrate)
400 {
401   task->set_rate(maxrate);
402   task->send_async(alias, cleanup, true);
403 }
404 /**
405  * @brief Sends a task to a mailbox
406  *
407  * This is a blocking function, the execution flow will be blocked until the task is sent (and received on the other
408  * side if #MSG_task_receive is used).
409  * See #MSG_task_isend for sending tasks asynchronously.
410  *
411  * @param task the task to be sent
412  * @param alias the mailbox name to where the task is sent
413  *
414  * @return Returns #MSG_OK if the task was successfully sent,
415  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
416  */
417 msg_error_t MSG_task_send(msg_task_t task, const char* alias)
418 {
419   XBT_DEBUG("MSG_task_send: Trying to send a message on mailbox '%s'", alias);
420   return task->send(alias, -1);
421 }
422
423 /**
424  * @brief Sends a task to a mailbox with a maximum rate
425  *
426  * This is a blocking function, the execution flow will be blocked until the task is sent. The maxrate parameter allows
427  * the application to limit the bandwidth utilization of network links when sending the task.
428  *
429  * The maxrate parameter can be used to send a task with a limited bandwidth (smaller than the physical available
430  * value). Use MSG_task_send() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
431  *
432  * @param task the task to be sent
433  * @param alias the mailbox name to where the task is sent
434  * @param maxrate the maximum communication rate for sending this task (byte/sec)
435  *
436  * @return Returns #MSG_OK if the task was successfully sent,
437  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
438  */
439 msg_error_t MSG_task_send_bounded(msg_task_t task, const char* alias, double maxrate)
440 {
441   task->set_rate(maxrate);
442   return task->send(alias, -1);
443 }
444
445 /**
446  * @brief Sends a task to a mailbox with a timeout
447  *
448  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
449  *
450  * @param task the task to be sent
451  * @param alias the mailbox name to where the task is sent
452  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
453  *
454  * @return Returns #MSG_OK if the task was successfully sent,
455  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
456  */
457 msg_error_t MSG_task_send_with_timeout(msg_task_t task, const char* alias, double timeout)
458 {
459   return task->send(alias, timeout);
460 }
461
462 /**
463  * @brief Sends a task to a mailbox with a timeout and with a maximum rate
464  *
465  * This is a blocking function, the execution flow will be blocked until the task is sent or the timeout is achieved.
466  *
467  * The maxrate parameter can be used to send a task with a limited bandwidth (smaller than the physical available
468  * value). Use MSG_task_send_with_timeout() if you don't limit the rate (or pass -1 as a rate value do disable this
469  * feature).
470  *
471  * @param task the task to be sent
472  * @param alias the mailbox name to where the task is sent
473  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_send)
474  * @param maxrate the maximum communication rate for sending this task (byte/sec)
475  *
476  * @return Returns #MSG_OK if the task was successfully sent,
477  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
478  */
479 msg_error_t MSG_task_send_with_timeout_bounded(msg_task_t task, const char* alias, double timeout, double maxrate)
480 {
481   task->set_rate(maxrate);
482   return task->send(alias, timeout);
483 }
484
485 /**
486  * @brief Receives a task from a mailbox.
487  *
488  * This is a blocking function, the execution flow will be blocked until the task is received. See #MSG_task_irecv
489  * for receiving tasks asynchronously.
490  *
491  * @param task a memory location for storing a #msg_task_t.
492  * @param alias name of the mailbox to receive the task from
493  *
494  * @return Returns
495  * #MSG_OK if the task was successfully received,
496  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
497  */
498 msg_error_t MSG_task_receive(msg_task_t * task, const char *alias)
499 {
500   return MSG_task_receive_with_timeout(task, alias, -1);
501 }
502
503 /**
504  * @brief Receives a task from a mailbox at a given rate.
505  *
506  * @param task a memory location for storing a #msg_task_t.
507  * @param alias name of the mailbox to receive the task from
508  * @param rate limit the reception to rate bandwidth (byte/sec)
509  *
510  * The rate parameter can be used to receive a task with a limited bandwidth (smaller than the physical available
511  * value). Use MSG_task_receive() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
512  *
513  * @return Returns
514  * #MSG_OK if the task was successfully received,
515  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE otherwise.
516  */
517 msg_error_t MSG_task_receive_bounded(msg_task_t* task, const char* alias, double rate)
518 {
519   return MSG_task_receive_with_timeout_bounded(task, alias, -1, rate);
520 }
521
522 /**
523  * @brief Receives a task from a mailbox with a given timeout.
524  *
525  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
526  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously.  You can provide a -1 timeout
527  * to obtain an infinite timeout.
528  *
529  * @param task a memory location for storing a #msg_task_t.
530  * @param alias name of the mailbox to receive the task from
531  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
532  *
533  * @return Returns
534  * #MSG_OK if the task was successfully received,
535  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
536  */
537 msg_error_t MSG_task_receive_with_timeout(msg_task_t* task, const char* alias, double timeout)
538 {
539   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, -1);
540 }
541
542 /**
543  * @brief Receives a task from a mailbox with a given timeout and at a given rate.
544  *
545  * @param task a memory location for storing a #msg_task_t.
546  * @param alias name of the mailbox to receive the task from
547  * @param timeout is the maximum wait time for completion (if -1, this call is the same as #MSG_task_receive)
548  * @param rate limit the reception to rate bandwidth (byte/sec)
549  *
550  * The rate parameter can be used to send a task with a limited
551  * bandwidth (smaller than the physical available value). Use
552  * MSG_task_receive() if you don't limit the rate (or pass -1 as a
553  * rate value do disable this feature).
554  *
555  * @return Returns
556  * #MSG_OK if the task was successfully received,
557  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
558  */
559 msg_error_t MSG_task_receive_with_timeout_bounded(msg_task_t* task, const char* alias, double timeout, double rate)
560 {
561   return MSG_task_receive_ext_bounded(task, alias, timeout, nullptr, rate);
562 }
563
564 /**
565  * @brief Receives a task from a mailbox from a specific host with a given timeout.
566  *
567  * This is a blocking function with a timeout, the execution flow will be blocked until the task is received or the
568  * timeout is achieved. See #MSG_task_irecv for receiving tasks asynchronously. You can provide a -1 timeout
569  * to obtain an infinite timeout.
570  *
571  * @param task a memory location for storing a #msg_task_t.
572  * @param alias name of the mailbox to receive the task from
573  * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
574  * @param host a #msg_host_t host from where the task was sent
575  *
576  * @return Returns
577  * #MSG_OK if the task was successfully received,
578  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
579  */
580 msg_error_t MSG_task_receive_ext(msg_task_t* task, const char* alias, double timeout, msg_host_t host)
581 {
582   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
583   return MSG_task_receive_ext_bounded(task, alias, timeout, host, -1.0);
584 }
585
586 /**
587  * @brief Receives a task from a mailbox from a specific host with a given timeout  and at a given rate.
588  *
589  * @param task a memory location for storing a #msg_task_t.
590  * @param alias name of the mailbox to receive the task from
591  * @param timeout is the maximum wait time for completion (provide -1 for no timeout)
592  * @param host a #msg_host_t host from where the task was sent
593  * @param rate limit the reception to rate bandwidth (byte/sec)
594  *
595  * The rate parameter can be used to receive a task with a limited bandwidth (smaller than the physical available
596  * value). Use MSG_task_receive_ext() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
597  *
598  * @return Returns
599  * #MSG_OK if the task was successfully received,
600  * #MSG_HOST_FAILURE, or #MSG_TRANSFER_FAILURE, or #MSG_TIMEOUT otherwise.
601  */
602 msg_error_t MSG_task_receive_ext_bounded(msg_task_t* task, const char* alias, double timeout, msg_host_t host,
603                                          double rate)
604 {
605   XBT_DEBUG("MSG_task_receive_ext: Trying to receive a message on mailbox '%s'", alias);
606   msg_error_t ret = MSG_OK;
607   /* We no longer support getting a task from a specific host */
608   if (host)
609     THROW_UNIMPLEMENTED;
610
611   /* Sanity check */
612   xbt_assert(task, "Null pointer for the task storage");
613
614   if (*task)
615     XBT_WARN("Asked to write the received task in a non empty struct -- proceeding.");
616
617   /* Try to receive it by calling SIMIX network layer */
618   try {
619     void* payload;
620     simgrid::s4u::Mailbox::by_name(alias)
621         ->get_init()
622         ->set_dst_data(&payload, sizeof(msg_task_t*))
623         ->set_rate(rate)
624         ->wait_for(timeout);
625     *task = static_cast<msg_task_t>(payload);
626     XBT_DEBUG("Got task %s from %s", (*task)->get_cname(), alias);
627     (*task)->set_not_used();
628   } catch (simgrid::HostFailureException& e) {
629     ret = MSG_HOST_FAILURE;
630   } catch (simgrid::TimeoutError& e) {
631     ret = MSG_TIMEOUT;
632   } catch (simgrid::CancelException& e) {
633     ret = MSG_TASK_CANCELED;
634   } catch (xbt_ex& e) {
635     if (e.category == network_error)
636       ret = MSG_TRANSFER_FAILURE;
637     else
638       throw;
639   }
640
641   if (TRACE_actor_is_enabled() && ret != MSG_HOST_FAILURE && ret != MSG_TRANSFER_FAILURE && ret != MSG_TIMEOUT) {
642     container_t process_container = simgrid::instr::Container::by_name(instr_pid(*MSG_process_self()));
643
644     std::string key = std::string("p") + std::to_string((*task)->get_id());
645     simgrid::instr::Container::get_root()->get_link("ACTOR_TASK_LINK")->end_event(process_container, "SR", key);
646   }
647   return ret;
648 }
649
650 /**
651  * @brief Starts listening for receiving a task from an asynchronous communication.
652  *
653  * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
654  *
655  * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
656  * @param name of the mailbox to receive the task on
657  * @return the msg_comm_t communication created
658  */
659 msg_comm_t MSG_task_irecv(msg_task_t* task, const char* name)
660 {
661   return MSG_task_irecv_bounded(task, name, -1.0);
662 }
663
664 /**
665  * @brief Starts listening for receiving a task from an asynchronous communication at a given rate.
666  *
667  * The rate parameter can be used to receive a task with a limited
668  * bandwidth (smaller than the physical available value). Use
669  * MSG_task_irecv() if you don't limit the rate (or pass -1 as a rate
670  * value do disable this feature).
671  *
672  * @param task a memory location for storing a #msg_task_t. has to be valid until the end of the communication.
673  * @param name of the mailbox to receive the task on
674  * @param rate limit the bandwidth to the given rate (byte/sec)
675  * @return the msg_comm_t communication created
676  */
677 msg_comm_t MSG_task_irecv_bounded(msg_task_t* task, const char* name, double rate)
678 {
679   /* FIXME: these functions are not traceable */
680   /* Sanity check */
681   xbt_assert(task, "Null pointer for the task storage");
682
683   if (*task)
684     XBT_CRITICAL("MSG_task_irecv() was asked to write in a non empty task struct.");
685
686   /* Try to receive it by calling SIMIX network layer */
687   simgrid::s4u::CommPtr comm = simgrid::s4u::Mailbox::by_name(name)
688                                    ->get_init()
689                                    ->set_dst_data((void**)task, sizeof(msg_task_t*))
690                                    ->set_rate(rate)
691                                    ->start();
692
693   return new simgrid::msg::Comm(nullptr, task, comm);
694 }
695
696 /**
697  * @brief Look if there is a communication on a mailbox and return the PID of the sender process.
698  *
699  * @param alias the name of the mailbox to be considered
700  *
701  * @return Returns the PID of sender process,
702  * -1 if there is no communication in the mailbox.#include <cmath>
703  *
704  */
705 int MSG_task_listen_from(const char* alias)
706 {
707   /* looks inside the rdv directly. Not clean. */
708   simgrid::kernel::activity::CommImplPtr comm = simgrid::s4u::Mailbox::by_name(alias)->front();
709
710   if (comm && comm->src_actor_)
711     return comm->src_actor_->get_pid();
712   else
713     return -1;
714 }
715
716 /** @brief Destroys the given task.
717  *
718  * You should free user data, if any, @b before calling this destructor.
719  *
720  * Only the process that owns the task can destroy it.
721  * The owner changes after a successful send.
722  * If a task is successfully sent, the receiver becomes the owner and is supposed to destroy it. The sender should not
723  * use it anymore.
724  * If the task failed to be sent, the sender remains the owner of the task.
725  */
726 msg_error_t MSG_task_destroy(msg_task_t task)
727 {
728   if (task->is_used()) {
729     /* the task is being sent or executed: cancel it first */
730     task->cancel();
731   }
732
733   /* free main structures */
734   delete task;
735
736   return MSG_OK;
737 }
738
739 /** @brief Cancel the given task
740  *
741  * If it was currently executed or transfered, the working process is stopped.
742  */
743 msg_error_t MSG_task_cancel(msg_task_t task)
744 {
745   xbt_assert((task != nullptr), "Cannot cancel a nullptr task");
746   task->cancel();
747   return MSG_OK;
748 }
749
750 /** @brief Returns a value in ]0,1[ that represent the task remaining work
751  *    to do: starts at 1 and goes to 0. Returns 0 if not started or finished.
752  *
753  * It works for either parallel or sequential tasks.
754  */
755 double MSG_task_get_remaining_work_ratio(msg_task_t task) {
756
757   xbt_assert((task != nullptr), "Cannot get information from a nullptr task");
758   if (task->compute) {
759     // Task in progress
760     return task->compute->get_remaining_ratio();
761   } else {
762     // Task not started (flops_amount is > 0.0) or finished (flops_amount is set to 0.0)
763     return task->flops_amount > 0.0 ? 1.0 : 0.0;
764   }
765 }
766
767 /** @brief Returns the amount of flops that remain to be computed
768  *
769  * The returned value is initially the cost that you defined for the task, then it decreases until it reaches 0
770  *
771  * It works for sequential tasks, but the remaining amount of work is not a scalar value for parallel tasks.
772  * So you will get an exception if you call this function on parallel tasks. Just don't do it.
773  */
774 double MSG_task_get_flops_amount(msg_task_t task) {
775   if (task->compute != nullptr) {
776     return task->compute->get_remaining();
777   } else {
778     // Not started or already done.
779     // - Before starting, flops_amount is initially the task cost
780     // - After execution, flops_amount is set to 0 (until someone uses MSG_task_set_flops_amount, if any)
781     return task->flops_amount;
782   }
783 }
784
785 /** @brief set the computation amount needed to process the given task.
786  *
787  * @warning If the computation is ongoing (already started and not finished),
788  * it is not modified by this call. Moreover, after its completion, the ongoing execution with set the flops_amount to
789  * zero, overriding any value set during the execution.
790  */
791 void MSG_task_set_flops_amount(msg_task_t task, double flops_amount)
792 {
793   task->flops_amount = flops_amount;
794 }
795
796 /** @brief set the amount data attached with the given task.
797  *
798  * @warning If the transfer is ongoing (already started and not finished), it is not modified by this call.
799  */
800 void MSG_task_set_bytes_amount(msg_task_t task, double data_size)
801 {
802   task->bytes_amount = data_size;
803 }
804
805 /** @brief Returns the total amount received by the given task
806  *
807  *  If the communication does not exist it will return 0.
808  *  So, if the communication has FINISHED or FAILED it returns zero.
809  */
810 double MSG_task_get_remaining_communication(msg_task_t task)
811 {
812   XBT_DEBUG("calling simcall_communication_get_remains(%p)", task->comm.get());
813   return task->comm->get_remaining();
814 }
815
816 /** @brief Returns the size of the data attached to the given task. */
817 double MSG_task_get_bytes_amount(msg_task_t task)
818 {
819   xbt_assert(task != nullptr, "Invalid parameter");
820   return task->bytes_amount;
821 }
822
823 /** @brief Changes the priority of a computation task.
824  *
825  * This priority doesn't affect the transfer rate. A priority of 2
826  * will make a task receive two times more cpu power than regular tasks.
827  */
828 void MSG_task_set_priority(msg_task_t task, double priority)
829 {
830   task->set_priority(priority);
831 }
832
833 /** @brief Changes the maximum CPU utilization of a computation task (in flops/s).
834  *
835  * For VMs, there is a pitfall. Please see MSG_vm_set_bound().
836  */
837 void MSG_task_set_bound(msg_task_t task, double bound)
838 {
839   if (bound < 1e-12) /* close enough to 0 without any floating precision surprise */
840     XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
841   task->set_bound(bound);
842 }
843
844 /**
845  * @brief Sets the tracing category of a task.
846  *
847  * This function should be called after the creation of a MSG task, to define the category of that task. The
848  * first parameter task must contain a task that was  =created with the function #MSG_task_create. The second
849  * parameter category must contain a category that was previously declared with the function #TRACE_category
850  * (or with #TRACE_category_with_color).
851  *
852  * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
853  *
854  * @param task the task that is going to be categorized
855  * @param category the name of the category to be associated to the task
856  *
857  * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
858  */
859 void MSG_task_set_category(msg_task_t task, const char* category)
860 {
861   xbt_assert(not task->has_tracing_category(), "Task %p(%s) already has a category (%s).", task, task->get_cname(),
862              task->get_tracing_category().c_str());
863
864   // if user provides a nullptr category, task is no longer traced
865   if (category == nullptr) {
866     task->set_tracing_category("");
867     XBT_DEBUG("MSG task %p(%s), category removed", task, task->get_cname());
868   } else {
869     // set task category
870     task->set_tracing_category(category);
871     XBT_DEBUG("MSG task %p(%s), category %s", task, task->get_cname(), task->get_tracing_category().c_str());
872   }
873 }
874
875 /**
876  * @brief Gets the current tracing category of a task. (@see MSG_task_set_category)
877  * @param task the task to be considered
878  * @return Returns the name of the tracing category of the given task, "" otherwise
879  */
880 const char* MSG_task_get_category(msg_task_t task)
881 {
882   return task->get_tracing_category().c_str();
883 }