Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
one step further not using kernel execution activities in MSG.
[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/simix/smx_private.hpp"
8 #include <simgrid/s4u/Comm.hpp>
9 #include <simgrid/s4u/Exec.hpp>
10 #include <simgrid/s4u/Host.hpp>
11
12 #include <algorithm>
13 #include <vector>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg, "Logging specific to MSG (task)");
16
17 namespace simgrid {
18 namespace msg {
19
20 Task::Task(std::string name, double flops_amount, double bytes_amount, void* data)
21     : name_(std::move(name)), userdata_(data), flops_amount(flops_amount), bytes_amount(bytes_amount)
22 {
23   static std::atomic_ullong counter{0};
24   id_ = counter++;
25   if (MC_is_active())
26     MC_ignore_heap(&(id_), sizeof(id_));
27 }
28
29 Task::Task(std::string name, std::vector<s4u::Host*> hosts, std::vector<double> flops_amount,
30            std::vector<double> bytes_amount, void* data)
31     : Task(std::move(name), 1.0, 0, data)
32 {
33   parallel_             = true;
34   hosts_                = std::move(hosts);
35   flops_parallel_amount = std::move(flops_amount);
36   bytes_parallel_amount = std::move(bytes_amount);
37 }
38
39 Task* Task::create(std::string name, double flops_amount, double bytes_amount, void* data)
40 {
41   return new Task(std::move(name), flops_amount, bytes_amount, data);
42 }
43
44 Task* Task::create_parallel(std::string name, int host_nb, const msg_host_t* host_list, double* flops_amount,
45                             double* bytes_amount, void* data)
46 {
47   std::vector<s4u::Host*> hosts;
48   std::vector<double> flops;
49   std::vector<double> bytes;
50
51   for (int i = 0; i < host_nb; i++) {
52     hosts.push_back(host_list[i]);
53     if (flops_amount != nullptr)
54       flops.push_back(flops_amount[i]);
55     if (bytes_amount != nullptr) {
56       for (int j = 0; j < host_nb; j++)
57         bytes.push_back(bytes_amount[host_nb * i + j]);
58     }
59   }
60   return new Task(std::move(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
70   set_used();
71   try {
72     s4u::ExecPtr e = s4u::this_actor::exec_init(flops_amount)
73                          ->set_priority(1 / priority_)
74                          ->set_bound(bound_)
75                          ->set_tracing_category(tracing_category_)
76                          ->start();
77     compute = boost::static_pointer_cast<kernel::activity::ExecImpl>(e->get_impl());
78
79     e->wait();
80
81     set_not_used();
82     XBT_DEBUG("Execution task '%s' finished", get_cname());
83   } catch (HostFailureException& e) {
84     status = MSG_HOST_FAILURE;
85   } catch (TimeoutError& e) {
86     status = MSG_TIMEOUT;
87   } catch (CancelException& e) {
88     status = MSG_TASK_CANCELED;
89   }
90
91   /* action ended, set comm and compute = nullptr, the actions is already destroyed in the main function */
92   flops_amount = 0.0;
93   comm         = nullptr;
94   compute      = nullptr;
95
96   return status;
97 }
98
99 void Task::cancel()
100 {
101   if (compute) {
102     simgrid::simix::simcall([this] { compute->cancel(); });
103   } else if (comm) {
104     comm->cancel();
105   }
106   set_not_used();
107 }
108
109 void Task::set_priority(double priority)
110 {
111   xbt_assert(std::isfinite(1.0 / priority), "priority is not finite!");
112   priority_ = 1.0 / priority;
113 }
114
115 s4u::Actor* Task::get_sender()
116 {
117   return comm ? comm->get_sender().get() : nullptr;
118 }
119
120 s4u::Host* Task::get_source()
121 {
122   return comm ? comm->get_sender()->get_host() : nullptr;
123 }
124
125 void Task::set_used()
126 {
127   if (is_used_)
128     report_multiple_use();
129   is_used_ = true;
130 }
131
132 void Task::report_multiple_use() const
133 {
134   if (msg_global->debug_multiple_use){
135     XBT_ERROR("This task is already used in there:");
136     // TODO, backtrace
137     XBT_ERROR("<missing backtrace>");
138     XBT_ERROR("And you try to reuse it from here:");
139     xbt_backtrace_display_current();
140   } else {
141     xbt_die("This task is still being used somewhere else. You cannot send it now. Go fix your code!"
142              "(use --cfg=msg/debug-multiple-use:on to get the backtrace of the other process)");
143   }
144 }
145 } // namespace msg
146 } // namespace simgrid
147
148 /********************************* Task **************************************/
149 /** @brief Creates a new task
150  *
151  * A constructor for msg_task_t taking four arguments.
152  *
153  * @param name a name for the object. It is for user-level information and can be nullptr.
154  * @param flop_amount a value of the processing amount (in flop) needed to process this new task.
155  * If 0, then it cannot be executed with MSG_task_execute(). This value has to be >=0.
156  * @param message_size a value of the amount of data (in bytes) needed to transfer this new task. If 0, then it cannot
157  * be transfered with MSG_task_send() and MSG_task_recv(). This value has to be >=0.
158  * @param data a pointer to any data may want to attach to the new object.  It is for user-level information and can
159  * be nullptr. It can be retrieved with the function @ref MSG_task_get_data.
160  * @return The new corresponding object.
161  */
162 msg_task_t MSG_task_create(const char *name, double flop_amount, double message_size, void *data)
163 {
164   return simgrid::msg::Task::create(name ? std::string(name) : "", flop_amount, message_size, data);
165 }
166
167 /** @brief Creates a new parallel task
168  *
169  * A constructor for #msg_task_t taking six arguments.
170  *
171  * \rst
172  * See :cpp:func:`void simgrid::s4u::this_actor::parallel_execute(int, s4u::Host**, double*, double*)` for
173  * the exact semantic of the parameters.
174  * \endrst
175  *
176  * @param name a name for the object. It is for user-level information and can be nullptr.
177  * @param host_nb the number of hosts implied in the parallel task.
178  * @param host_list an array of @p host_nb msg_host_t.
179  * @param flops_amount an array of @p host_nb doubles.
180  *        flops_amount[i] is the total number of operations that have to be performed on host_list[i].
181  * @param bytes_amount an array of @p host_nb* @p host_nb doubles.
182  * @param data a pointer to any data may want to attach to the new object.
183  *             It is for user-level information and can be nullptr.
184  *             It can be retrieved with the function @ref MSG_task_get_data().
185  */
186 msg_task_t MSG_parallel_task_create(const char *name, int host_nb, const msg_host_t * host_list,
187                                     double *flops_amount, double *bytes_amount, void *data)
188 {
189   // Task's flops amount is set to an arbitrary value > 0.0 to be able to distinguish, in
190   // MSG_task_get_remaining_work_ratio(), a finished task and a task that has not started yet.
191   return simgrid::msg::Task::create_parallel(name ? name : "", host_nb, host_list, flops_amount, bytes_amount, data);
192 }
193
194 /** @brief Return the user data of the given task */
195 void* MSG_task_get_data(msg_task_t task)
196 {
197   return task->get_user_data();
198 }
199
200 /** @brief Sets the user data of a given task */
201 void MSG_task_set_data(msg_task_t task, void *data)
202 {
203   task->set_user_data(data);
204 }
205
206 /** @brief Sets a function to be called when a task has just been copied.
207  * @param callback a callback function
208  */
209 void MSG_task_set_copy_callback(void (*callback) (msg_task_t task, msg_process_t sender, msg_process_t receiver)) {
210
211   msg_global->task_copy_callback = callback;
212
213   if (callback) {
214     SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
215   } else {
216     SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
217   }
218 }
219
220 /** @brief Returns the sender of the given task */
221 msg_process_t MSG_task_get_sender(msg_task_t task)
222 {
223   return task->get_sender();
224 }
225
226 /** @brief Returns the source (the sender's host) of the given task */
227 msg_host_t MSG_task_get_source(msg_task_t task)
228 {
229   return task->get_source();
230 }
231
232 /** @brief Returns the name of the given task. */
233 const char *MSG_task_get_name(msg_task_t task)
234 {
235   return task->get_cname();
236 }
237
238 /** @brief Sets the name of the given task. */
239 void MSG_task_set_name(msg_task_t task, const char *name)
240 {
241   task->set_name(name);
242 }
243
244 /**
245  * @brief Executes a task and waits for its termination.
246  *
247  * This function is used for describing the behavior of a process. It takes only one parameter.
248  * @param task a #msg_task_t to execute on the location on which the process is running.
249  * @return #MSG_OK if the task was successfully completed, #MSG_TASK_CANCELED or #MSG_HOST_FAILURE otherwise
250  */
251 msg_error_t MSG_task_execute(msg_task_t task)
252 {
253   return task->execute();
254 }
255
256 /** @brief Destroys the given task.
257  *
258  * You should free user data, if any, @b before calling this destructor.
259  *
260  * Only the process that owns the task can destroy it.
261  * The owner changes after a successful send.
262  * If a task is successfully sent, the receiver becomes the owner and is supposed to destroy it. The sender should not
263  * use it anymore.
264  * If the task failed to be sent, the sender remains the owner of the task.
265  */
266 msg_error_t MSG_task_destroy(msg_task_t task)
267 {
268   if (task->is_used()) {
269     /* the task is being sent or executed: cancel it first */
270     task->cancel();
271   }
272
273   /* free main structures */
274   delete task;
275
276   return MSG_OK;
277 }
278
279 /** @brief Cancel the given task
280  *
281  * If it was currently executed or transfered, the working process is stopped.
282  */
283 msg_error_t MSG_task_cancel(msg_task_t task)
284 {
285   xbt_assert((task != nullptr), "Cannot cancel a nullptr task");
286   task->cancel();
287   return MSG_OK;
288 }
289
290 /** @brief Returns a value in ]0,1[ that represent the task remaining work
291  *    to do: starts at 1 and goes to 0. Returns 0 if not started or finished.
292  *
293  * It works for either parallel or sequential tasks.
294  */
295 double MSG_task_get_remaining_work_ratio(msg_task_t task) {
296
297   xbt_assert((task != nullptr), "Cannot get information from a nullptr task");
298   if (task->compute) {
299     // Task in progress
300     return task->compute->get_remaining_ratio();
301   } else {
302     // Task not started (flops_amount is > 0.0) or finished (flops_amount is set to 0.0)
303     return task->flops_amount > 0.0 ? 1.0 : 0.0;
304   }
305 }
306
307 /** @brief Returns the amount of flops that remain to be computed
308  *
309  * The returned value is initially the cost that you defined for the task, then it decreases until it reaches 0
310  *
311  * It works for sequential tasks, but the remaining amount of work is not a scalar value for parallel tasks.
312  * So you will get an exception if you call this function on parallel tasks. Just don't do it.
313  */
314 double MSG_task_get_flops_amount(msg_task_t task) {
315   if (task->compute != nullptr) {
316     return task->compute->get_remaining();
317   } else {
318     // Not started or already done.
319     // - Before starting, flops_amount is initially the task cost
320     // - After execution, flops_amount is set to 0 (until someone uses MSG_task_set_flops_amount, if any)
321     return task->flops_amount;
322   }
323 }
324
325 /** @brief set the computation amount needed to process the given task.
326  *
327  * @warning If the computation is ongoing (already started and not finished),
328  * it is not modified by this call. Moreover, after its completion, the ongoing execution with set the flops_amount to
329  * zero, overriding any value set during the execution.
330  */
331 void MSG_task_set_flops_amount(msg_task_t task, double flops_amount)
332 {
333   task->flops_amount = flops_amount;
334 }
335
336 /** @brief set the amount data attached with the given task.
337  *
338  * @warning If the transfer is ongoing (already started and not finished), it is not modified by this call.
339  */
340 void MSG_task_set_bytes_amount(msg_task_t task, double data_size)
341 {
342   task->bytes_amount = data_size;
343 }
344
345 /** @brief Returns the total amount received by the given task
346  *
347  *  If the communication does not exist it will return 0.
348  *  So, if the communication has FINISHED or FAILED it returns zero.
349  */
350 double MSG_task_get_remaining_communication(msg_task_t task)
351 {
352   XBT_DEBUG("calling simcall_communication_get_remains(%p)", task->comm.get());
353   return task->comm->get_remaining();
354 }
355
356 /** @brief Returns the size of the data attached to the given task. */
357 double MSG_task_get_bytes_amount(msg_task_t task)
358 {
359   xbt_assert(task != nullptr, "Invalid parameter");
360   return task->bytes_amount;
361 }
362
363 /** @brief Changes the priority of a computation task.
364  *
365  * This priority doesn't affect the transfer rate. A priority of 2
366  * will make a task receive two times more cpu power than regular tasks.
367  */
368 void MSG_task_set_priority(msg_task_t task, double priority)
369 {
370   task->set_priority(priority);
371 }
372
373 /** @brief Changes the maximum CPU utilization of a computation task (in flops/s).
374  *
375  * For VMs, there is a pitfall. Please see MSG_vm_set_bound().
376  */
377 void MSG_task_set_bound(msg_task_t task, double bound)
378 {
379   if (bound < 1e-12) /* close enough to 0 without any floating precision surprise */
380     XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
381   task->set_bound(bound);
382 }
383
384 /**
385  * @brief Sets the tracing category of a task.
386  *
387  * This function should be called after the creation of a MSG task, to define the category of that task. The
388  * first parameter task must contain a task that was  =created with the function #MSG_task_create. The second
389  * parameter category must contain a category that was previously declared with the function #TRACE_category
390  * (or with #TRACE_category_with_color).
391  *
392  * See @ref outcomes_vizu for details on how to trace the (categorized) resource utilization.
393  *
394  * @param task the task that is going to be categorized
395  * @param category the name of the category to be associated to the task
396  *
397  * @see MSG_task_get_category, TRACE_category, TRACE_category_with_color
398  */
399 void MSG_task_set_category(msg_task_t task, const char* category)
400 {
401   xbt_assert(not task->has_tracing_category(), "Task %p(%s) already has a category (%s).", task, task->get_cname(),
402              task->get_tracing_category().c_str());
403
404   // if user provides a nullptr category, task is no longer traced
405   if (category == nullptr) {
406     task->set_tracing_category("");
407     XBT_DEBUG("MSG task %p(%s), category removed", task, task->get_cname());
408   } else {
409     // set task category
410     task->set_tracing_category(category);
411     XBT_DEBUG("MSG task %p(%s), category %s", task, task->get_cname(), task->get_tracing_category().c_str());
412   }
413 }
414
415 /**
416  * @brief Gets the current tracing category of a task. (@see MSG_task_set_category)
417  * @param task the task to be considered
418  * @return Returns the name of the tracing category of the given task, "" otherwise
419  */
420 const char* MSG_task_get_category(msg_task_t task)
421 {
422   return task->get_tracing_category().c_str();
423 }