Logo AND Algorithmique Numérique Distribuée

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