Logo AND Algorithmique Numérique Distribuée

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