1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved. */
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. */
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>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg, "Logging specific to MSG (task)");
21 Task::Task(std::string name, double flops_amount, double bytes_amount, void* data)
22 : name_(std::move(name)), userdata_(data), flops_amount(flops_amount), bytes_amount(bytes_amount)
24 static std::atomic_ullong counter{0};
27 MC_ignore_heap(&(id_), sizeof(id_));
30 Task::Task(std::string name, std::vector<s4u::Host*> hosts, std::vector<double> flops_amount,
31 std::vector<double> bytes_amount, void* data)
32 : Task(std::move(name), 1.0, 0, data)
35 hosts_ = std::move(hosts);
36 flops_parallel_amount = std::move(flops_amount);
37 bytes_parallel_amount = std::move(bytes_amount);
40 Task* Task::create(std::string name, double flops_amount, double bytes_amount, void* data)
42 return new Task(std::move(name), flops_amount, bytes_amount, data);
45 Task* Task::create_parallel(std::string name, int host_nb, const msg_host_t* host_list, double* flops_amount,
46 double* bytes_amount, void* data)
48 std::vector<s4u::Host*> hosts;
49 std::vector<double> flops;
50 std::vector<double> bytes;
52 for (int i = 0; i < host_nb; i++) {
53 hosts.push_back(host_list[i]);
54 if (flops_amount != nullptr)
55 flops.push_back(flops_amount[i]);
56 if (bytes_amount != nullptr) {
57 for (int j = 0; j < host_nb; j++)
58 bytes.push_back(bytes_amount[host_nb * i + j]);
61 return new Task(std::move(name), std::move(hosts), std::move(flops), std::move(bytes), data);
64 msg_error_t Task::execute()
66 /* checking for infinite values */
67 xbt_assert(std::isfinite(flops_amount), "flops_amount is not finite!");
69 msg_error_t status = MSG_OK;
70 if (flops_amount <= 0.0)
76 compute = s4u::this_actor::exec_init(hosts_, flops_parallel_amount, bytes_parallel_amount);
78 compute = s4u::this_actor::exec_init(flops_amount);
80 compute->set_name(name_)
81 ->set_tracing_category(tracing_category_)
82 ->set_timeout(timeout_)
83 ->set_priority(1 / priority_)
88 XBT_DEBUG("Execution task '%s' finished", get_cname());
89 } catch (HostFailureException& e) {
90 status = MSG_HOST_FAILURE;
91 } catch (TimeoutError& e) {
93 } catch (CancelException& e) {
94 status = MSG_TASK_CANCELED;
97 /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
105 s4u::CommPtr Task::send_async(std::string alias, void_f_pvoid_t cleanup, bool detached)
107 if (TRACE_actor_is_enabled()) {
108 container_t process_container = simgrid::instr::Container::by_name(instr_pid(MSG_process_self()));
109 std::string key = std::string("p") + std::to_string(get_id());
110 simgrid::instr::Container::get_root()->get_link("ACTOR_TASK_LINK")->start_event(process_container, "SR", key);
113 /* Prepare the task to send */
115 this->comm = nullptr;
116 msg_global->sent_msg++;
118 s4u::CommPtr comm = s4u::Mailbox::by_name(alias)->put_init(this, bytes_amount)->set_rate(get_rate());
122 comm->detach(cleanup);
126 if (TRACE_is_enabled() && has_tracing_category())
127 simgrid::simix::simcall([comm, this] { comm->get_impl()->set_category(std::move(tracing_category_)); });
135 simgrid::simix::simcall([this] { compute->cancel(); });
142 void Task::set_priority(double priority)
144 xbt_assert(std::isfinite(1.0 / priority), "priority is not finite!");
145 priority_ = 1.0 / priority;
148 s4u::Actor* Task::get_sender()
150 return comm ? comm->get_sender().get() : nullptr;
153 s4u::Host* Task::get_source()
155 return comm ? comm->get_sender()->get_host() : nullptr;
158 void Task::set_used()
161 report_multiple_use();
165 void Task::report_multiple_use() const
167 if (msg_global->debug_multiple_use){
168 XBT_ERROR("This task is already used in there:");
170 XBT_ERROR("<missing backtrace>");
171 XBT_ERROR("And you try to reuse it from here:");
172 xbt_backtrace_display_current();
174 xbt_die("This task is still being used somewhere else. You cannot send it now. Go fix your code!"
175 "(use --cfg=msg/debug-multiple-use:on to get the backtrace of the other process)");
179 } // namespace simgrid
181 /********************************* Task **************************************/
182 /** @brief Creates a new task
184 * A constructor for msg_task_t taking four arguments.
186 * @param name a name for the object. It is for user-level information and can be nullptr.
187 * @param flop_amount a value of the processing amount (in flop) needed to process this new task.
188 * If 0, then it cannot be executed with MSG_task_execute(). This value has to be >=0.
189 * @param message_size a value of the amount of data (in bytes) needed to transfer this new task. If 0, then it cannot
190 * be transfered with MSG_task_send() and MSG_task_recv(). This value has to be >=0.
191 * @param data a pointer to any data may want to attach to the new object. It is for user-level information and can
192 * be nullptr. It can be retrieved with the function @ref MSG_task_get_data.
193 * @return The new corresponding object.
195 msg_task_t MSG_task_create(const char *name, double flop_amount, double message_size, void *data)
197 return simgrid::msg::Task::create(name ? std::string(name) : "", flop_amount, message_size, data);
200 /** @brief Creates a new parallel task
202 * A constructor for #msg_task_t taking six arguments.
205 * See :cpp:func:`void simgrid::s4u::this_actor::parallel_execute(int, s4u::Host**, double*, double*)` for
206 * the exact semantic of the parameters.
209 * @param name a name for the object. It is for user-level information and can be nullptr.
210 * @param host_nb the number of hosts implied in the parallel task.
211 * @param host_list an array of @p host_nb msg_host_t.
212 * @param flops_amount an array of @p host_nb doubles.
213 * flops_amount[i] is the total number of operations that have to be performed on host_list[i].
214 * @param bytes_amount an array of @p host_nb* @p host_nb doubles.
215 * @param data a pointer to any data may want to attach to the new object.
216 * It is for user-level information and can be nullptr.
217 * It can be retrieved with the function @ref MSG_task_get_data().
219 msg_task_t MSG_parallel_task_create(const char *name, int host_nb, const msg_host_t * host_list,
220 double *flops_amount, double *bytes_amount, void *data)
222 // Task's flops amount is set to an arbitrary value > 0.0 to be able to distinguish, in
223 // MSG_task_get_remaining_work_ratio(), a finished task and a task that has not started yet.
224 return simgrid::msg::Task::create_parallel(name ? name : "", host_nb, host_list, flops_amount, bytes_amount, data);
227 /** @brief Return the user data of the given task */
228 void* MSG_task_get_data(msg_task_t task)
230 return task->get_user_data();
233 /** @brief Sets the user data of a given task */
234 void MSG_task_set_data(msg_task_t task, void *data)
236 task->set_user_data(data);
239 /** @brief Sets a function to be called when a task has just been copied.
240 * @param callback a callback function
242 void MSG_task_set_copy_callback(void (*callback) (msg_task_t task, msg_process_t sender, msg_process_t receiver)) {
244 msg_global->task_copy_callback = callback;
247 SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
249 SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
253 /** @brief Returns the sender of the given task */
254 msg_process_t MSG_task_get_sender(msg_task_t task)
256 return task->get_sender();
259 /** @brief Returns the source (the sender's host) of the given task */
260 msg_host_t MSG_task_get_source(msg_task_t task)
262 return task->get_source();
265 /** @brief Returns the name of the given task. */
266 const char *MSG_task_get_name(msg_task_t task)
268 return task->get_cname();
271 /** @brief Sets the name of the given task. */
272 void MSG_task_set_name(msg_task_t task, const char *name)
274 task->set_name(name);
278 * @brief Executes a task and waits for its termination.
280 * This function is used for describing the behavior of a process. It takes only one parameter.
281 * @param task a #msg_task_t to execute on the location on which the process is running.
282 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
284 msg_error_t MSG_task_execute(msg_task_t task)
286 return task->execute();
290 * @brief Executes a parallel task and waits for its termination.
292 * @param task a #msg_task_t to execute on the location on which the process is running.
294 * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
296 msg_error_t MSG_parallel_task_execute(msg_task_t task)
298 return task->execute();
301 msg_error_t MSG_parallel_task_execute_with_timeout(msg_task_t task, double timeout)
303 task->set_timeout(timeout);
304 return task->execute();
308 * @brief Sends a task on a mailbox.
310 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication.
312 * @param task a #msg_task_t to send on another location.
313 * @param alias name of the mailbox to sent the task to
314 * @return the msg_comm_t communication created
316 msg_comm_t MSG_task_isend(msg_task_t task, const char* alias)
318 return new simgrid::msg::Comm(task, nullptr, task->send_async(alias, nullptr, false));
322 * @brief Sends a task on a mailbox with a maximum rate
324 * This is a non blocking function: use MSG_comm_wait() or MSG_comm_test() to end the communication. The maxrate
325 * parameter allows the application to limit the bandwidth utilization of network links when sending the task.
327 * @param task a #msg_task_t to send on another location.
328 * @param alias name of the mailbox to sent the task to
329 * @param maxrate the maximum communication rate for sending this task (byte/sec).
330 * @return the msg_comm_t communication created
332 msg_comm_t MSG_task_isend_bounded(msg_task_t task, const char* alias, double maxrate)
334 task->set_rate(maxrate);
335 return new simgrid::msg::Comm(task, nullptr, task->send_async(alias, nullptr, false));
339 * @brief Sends a task on a mailbox.
341 * This is a non blocking detached send function.
342 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
343 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
344 * usual. More details on this can be obtained on
345 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
346 * in the SimGrid-user mailing list archive.
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 cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy
351 * (if nullptr, no function will be called)
353 void MSG_task_dsend(msg_task_t task, const char* alias, void_f_pvoid_t cleanup)
355 task->send_async(alias, cleanup, true);
359 * @brief Sends a task on a mailbox with a maximal rate.
361 * This is a non blocking detached send function.
362 * Think of it as a best effort send. Keep in mind that the third parameter is only called if the communication fails.
363 * If the communication does work, it is responsibility of the receiver code to free anything related to the task, as
364 * usual. More details on this can be obtained on
365 * <a href="http://lists.gforge.inria.fr/pipermail/simgrid-user/2011-November/002649.html">this thread</a>
366 * in the SimGrid-user mailing list archive.
368 * The rate parameter can be used to send a task with a limited bandwidth (smaller than the physical available value).
369 * Use MSG_task_dsend() if you don't limit the rate (or pass -1 as a rate value do disable this feature).
371 * @param task a #msg_task_t to send on another location.
372 * @param alias name of the mailbox to sent the task to
373 * @param cleanup a function to destroy the task if the communication fails, e.g. MSG_task_destroy (if nullptr, no
374 * function will be called)
375 * @param maxrate the maximum communication rate for sending this task (byte/sec)
378 void MSG_task_dsend_bounded(msg_task_t task, const char* alias, void_f_pvoid_t cleanup, double maxrate)
380 task->set_rate(maxrate);
381 task->send_async(alias, cleanup, true);
384 /** @brief Destroys the given task.
386 * You should free user data, if any, @b before calling this destructor.
388 * Only the process that owns the task can destroy it.
389 * The owner changes after a successful send.
390 * If a task is successfully sent, the receiver becomes the owner and is supposed to destroy it. The sender should not
392 * If the task failed to be sent, the sender remains the owner of the task.
394 msg_error_t MSG_task_destroy(msg_task_t task)
396 if (task->is_used()) {
397 /* the task is being sent or executed: cancel it first */
401 /* free main structures */
407 /** @brief Cancel the given task
409 * If it was currently executed or transfered, the working process is stopped.
411 msg_error_t MSG_task_cancel(msg_task_t task)
413 xbt_assert((task != nullptr), "Cannot cancel a nullptr task");
418 /** @brief Returns a value in ]0,1[ that represent the task remaining work
419 * to do: starts at 1 and goes to 0. Returns 0 if not started or finished.
421 * It works for either parallel or sequential tasks.
423 double MSG_task_get_remaining_work_ratio(msg_task_t task) {
425 xbt_assert((task != nullptr), "Cannot get information from a nullptr task");
428 return task->compute->get_remaining_ratio();
430 // Task not started (flops_amount is > 0.0) or finished (flops_amount is set to 0.0)
431 return task->flops_amount > 0.0 ? 1.0 : 0.0;
435 /** @brief Returns the amount of flops that remain to be computed
437 * The returned value is initially the cost that you defined for the task, then it decreases until it reaches 0
439 * It works for sequential tasks, but the remaining amount of work is not a scalar value for parallel tasks.
440 * So you will get an exception if you call this function on parallel tasks. Just don't do it.
442 double MSG_task_get_flops_amount(msg_task_t task) {
443 if (task->compute != nullptr) {
444 return task->compute->get_remaining();
446 // Not started or already done.
447 // - Before starting, flops_amount is initially the task cost
448 // - After execution, flops_amount is set to 0 (until someone uses MSG_task_set_flops_amount, if any)
449 return task->flops_amount;
453 /** @brief set the computation amount needed to process the given task.
455 * @warning If the computation is ongoing (already started and not finished),
456 * it is not modified by this call. Moreover, after its completion, the ongoing execution with set the flops_amount to
457 * zero, overriding any value set during the execution.
459 void MSG_task_set_flops_amount(msg_task_t task, double flops_amount)
461 task->flops_amount = flops_amount;
464 /** @brief set the amount data attached with the given task.
466 * @warning If the transfer is ongoing (already started and not finished), it is not modified by this call.
468 void MSG_task_set_bytes_amount(msg_task_t task, double data_size)
470 task->bytes_amount = data_size;
473 /** @brief Returns the total amount received by the given task
475 * If the communication does not exist it will return 0.
476 * So, if the communication has FINISHED or FAILED it returns zero.
478 double MSG_task_get_remaining_communication(msg_task_t task)
480 XBT_DEBUG("calling simcall_communication_get_remains(%p)", task->comm.get());
481 return task->comm->get_remaining();
484 /** @brief Returns the size of the data attached to the given task. */
485 double MSG_task_get_bytes_amount(msg_task_t task)
487 xbt_assert(task != nullptr, "Invalid parameter");
488 return task->bytes_amount;
491 /** @brief Changes the priority of a computation task.
493 * This priority doesn't affect the transfer rate. A priority of 2
494 * will make a task receive two times more cpu power than regular tasks.
496 void MSG_task_set_priority(msg_task_t task, double priority)
498 task->set_priority(priority);
501 /** @brief Changes the maximum CPU utilization of a computation task (in flops/s).
503 * For VMs, there is a pitfall. Please see MSG_vm_set_bound().
505 void MSG_task_set_bound(msg_task_t task, double bound)
507 if (bound < 1e-12) /* close enough to 0 without any floating precision surprise */
508 XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
509 task->set_bound(bound);
513 * @brief Sets the tracing category of a task.
515 * This function should be called after the creation of a MSG task, to define the category of that task. The
516 * first parameter task must contain a task that was =created with the function #MSG_task_create. The second
517 * parameter category must contain a category that was previously declared with the function #TRACE_category
518 * (or with #TRACE_category_with_color).
520 * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
522 * @param task the task that is going to be categorized
523 * @param category the name of the category to be associated to the task
525 * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
527 void MSG_task_set_category(msg_task_t task, const char* category)
529 xbt_assert(not task->has_tracing_category(), "Task %p(%s) already has a category (%s).", task, task->get_cname(),
530 task->get_tracing_category().c_str());
532 // if user provides a nullptr category, task is no longer traced
533 if (category == nullptr) {
534 task->set_tracing_category("");
535 XBT_DEBUG("MSG task %p(%s), category removed", task, task->get_cname());
538 task->set_tracing_category(category);
539 XBT_DEBUG("MSG task %p(%s), category %s", task, task->get_cname(), task->get_tracing_category().c_str());
544 * @brief Gets the current tracing category of a task. (@see MSG_task_set_category)
545 * @param task the task to be considered
546 * @return Returns the name of the tracing category of the given task, "" otherwise
548 const char* MSG_task_get_category(msg_task_t task)
550 return task->get_tracing_category().c_str();