Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9b3e46c373f3660064c05ea9c00c547cafe4bafc
[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 <algorithm>
9 #include <cmath>
10 #include <simgrid/modelchecker.h>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_task, msg, "Logging specific to MSG (task)");
13
14 void s_simdata_task_t::reportMultipleUse() const
15 {
16   if (msg_global->debug_multiple_use){
17     XBT_ERROR("This task is already used in there:");
18     // TODO, backtrace
19     XBT_ERROR("<missing backtrace>");
20     XBT_ERROR("And you try to reuse it from here:");
21     xbt_backtrace_display_current();
22   } else {
23     xbt_die("This task is still being used somewhere else. You cannot send it now. Go fix your code!"
24              "(use --cfg=msg/debug-multiple-use:on to get the backtrace of the other process)");
25   }
26 }
27
28 /********************************* Task **************************************/
29 /** @brief Creates a new task
30  *
31  * A constructor for msg_task_t taking four arguments.
32  *
33  * @param name a name for the object. It is for user-level information and can be nullptr.
34  * @param flop_amount a value of the processing amount (in flop) needed to process this new task.
35  * If 0, then it cannot be executed with MSG_task_execute(). This value has to be >=0.
36  * @param message_size a value of the amount of data (in bytes) needed to transfer this new task. If 0, then it cannot
37  * be transfered with MSG_task_send() and MSG_task_recv(). This value has to be >=0.
38  * @param data a pointer to any data may want to attach to the new object.  It is for user-level information and can
39  * be nullptr. It can be retrieved with the function @ref MSG_task_get_data.
40  * @return The new corresponding object.
41  */
42 msg_task_t MSG_task_create(const char *name, double flop_amount, double message_size, void *data)
43 {
44   static std::atomic_ullong counter{0};
45
46   msg_task_t task        = new s_msg_task_t;
47   simdata_task_t simdata = new s_simdata_task_t();
48   task->simdata = simdata;
49
50   /* Task structure */
51   task->name = xbt_strdup(name);
52   task->data = data;
53
54   /* Simulator Data */
55   simdata->bytes_amount = message_size;
56   simdata->flops_amount = flop_amount;
57
58   task->counter  = counter++;
59   task->category = nullptr;
60
61   if (MC_is_active())
62     MC_ignore_heap(&(task->counter), sizeof(task->counter));
63
64   return task;
65 }
66
67 /** @brief Creates a new parallel task
68  *
69  * A constructor for #msg_task_t taking six arguments.
70  *
71  * \rst
72  * See :cpp:func:`void simgrid::s4u::this_actor::parallel_execute(int, s4u::Host**, double*, double*)` for
73  * the exact semantic of the parameters.
74  * \endrst
75  *
76  * @param name a name for the object. It is for user-level information and can be nullptr.
77  * @param host_nb the number of hosts implied in the parallel task.
78  * @param host_list an array of @p host_nb msg_host_t.
79  * @param flops_amount an array of @p host_nb doubles.
80  *        flops_amount[i] is the total number of operations that have to be performed on host_list[i].
81  * @param bytes_amount an array of @p host_nb* @p host_nb doubles.
82  * @param data a pointer to any data may want to attach to the new object.
83  *             It is for user-level information and can be nullptr.
84  *             It can be retrieved with the function @ref MSG_task_get_data().
85  */
86 msg_task_t MSG_parallel_task_create(const char *name, int host_nb, const msg_host_t * host_list,
87                                     double *flops_amount, double *bytes_amount, void *data)
88 {
89   // Task's flops amount is set to an arbitrary value > 0.0 to be able to distinguish, in
90   // MSG_task_get_remaining_work_ratio(), a finished task and a task that has not started yet.
91   msg_task_t task        = MSG_task_create(name, 1.0, 0, data);
92   simdata_task_t simdata = task->simdata;
93
94   /* Simulator Data specific to parallel tasks */
95   simdata->host_nb = host_nb;
96   simdata->host_list             = new sg_host_t[host_nb];
97   std::copy_n(host_list, host_nb, simdata->host_list);
98   if (flops_amount != nullptr) {
99     simdata->flops_parallel_amount = new double[host_nb];
100     std::copy_n(flops_amount, host_nb, simdata->flops_parallel_amount);
101   }
102   if (bytes_amount != nullptr) {
103     simdata->bytes_parallel_amount = new double[host_nb * host_nb];
104     std::copy_n(bytes_amount, host_nb * host_nb, simdata->bytes_parallel_amount);
105   }
106
107   return task;
108 }
109
110 /** @brief Return the user data of the given task */
111 void *MSG_task_get_data(msg_task_t task)
112 {
113   return (task->data);
114 }
115
116 /** @brief Sets the user data of a given task */
117 void MSG_task_set_data(msg_task_t task, void *data)
118 {
119   task->data = data;
120 }
121
122 /** @brief Sets a function to be called when a task has just been copied.
123  * @param callback a callback function
124  */
125 void MSG_task_set_copy_callback(void (*callback) (msg_task_t task, msg_process_t sender, msg_process_t receiver)) {
126
127   msg_global->task_copy_callback = callback;
128
129   if (callback) {
130     SIMIX_comm_set_copy_data_callback(MSG_comm_copy_data_from_SIMIX);
131   } else {
132     SIMIX_comm_set_copy_data_callback(SIMIX_comm_copy_pointer_callback);
133   }
134 }
135
136 /** @brief Returns the sender of the given task */
137 msg_process_t MSG_task_get_sender(msg_task_t task)
138 {
139   return task->simdata->sender;
140 }
141
142 /** @brief Returns the source (the sender's host) of the given task */
143 msg_host_t MSG_task_get_source(msg_task_t task)
144 {
145   return task->simdata->source;
146 }
147
148 /** @brief Returns the name of the given task. */
149 const char *MSG_task_get_name(msg_task_t task)
150 {
151   return task->name;
152 }
153
154 /** @brief Sets the name of the given task. */
155 void MSG_task_set_name(msg_task_t task, const char *name)
156 {
157   task->name = xbt_strdup(name);
158 }
159
160 /** @brief Destroys the given task.
161  *
162  * You should free user data, if any, @b before calling this destructor.
163  *
164  * Only the process that owns the task can destroy it.
165  * The owner changes after a successful send.
166  * If a task is successfully sent, the receiver becomes the owner and is supposed to destroy it. The sender should not
167  * use it anymore.
168  * If the task failed to be sent, the sender remains the owner of the task.
169  */
170 msg_error_t MSG_task_destroy(msg_task_t task)
171 {
172   if (task->simdata->isused) {
173     /* the task is being sent or executed: cancel it first */
174     MSG_task_cancel(task);
175   }
176
177   xbt_free(task->category);
178   xbt_free(task->name);
179
180   /* free main structures */
181   delete task->simdata;
182   delete task;
183
184   return MSG_OK;
185 }
186
187 /** @brief Cancel the given task
188  *
189  * If it was currently executed or transfered, the working process is stopped.
190  */
191 msg_error_t MSG_task_cancel(msg_task_t task)
192 {
193   xbt_assert((task != nullptr), "Cannot cancel a nullptr task");
194
195   simdata_task_t simdata = task->simdata;
196   if (simdata->compute) {
197     simgrid::simix::simcall([simdata] { simdata->compute->cancel(); });
198   } else if (simdata->comm) {
199     simgrid::simix::simcall([simdata] { simdata->comm->cancel(); });
200   }
201   simdata->setNotUsed();
202   return MSG_OK;
203 }
204
205 /** @brief Returns a value in ]0,1[ that represent the task remaining work
206  *    to do: starts at 1 and goes to 0. Returns 0 if not started or finished.
207  *
208  * It works for either parallel or sequential tasks.
209  */
210 double MSG_task_get_remaining_work_ratio(msg_task_t task) {
211
212   xbt_assert((task != nullptr), "Cannot get information from a nullptr task");
213   if (task->simdata->compute) {
214     // Task in progress
215     return task->simdata->compute->get_remaining_ratio();
216   } else {
217     // Task not started (flops_amount is > 0.0) or finished (flops_amount is set to 0.0)
218     return task->simdata->flops_amount > 0.0 ? 1.0 : 0.0;
219   }
220 }
221
222 /** @brief Returns the amount of flops that remain to be computed
223  *
224  * The returned value is initially the cost that you defined for the task, then it decreases until it reaches 0
225  *
226  * It works for sequential tasks, but the remaining amount of work is not a scalar value for parallel tasks.
227  * So you will get an exception if you call this function on parallel tasks. Just don't do it.
228  */
229 double MSG_task_get_flops_amount(msg_task_t task) {
230   if (task->simdata->compute != nullptr) {
231     return task->simdata->compute->get_remaining();
232   } else {
233     // Not started or already done.
234     // - Before starting, flops_amount is initially the task cost
235     // - After execution, flops_amount is set to 0 (until someone uses MSG_task_set_flops_amount, if any)
236     return task->simdata->flops_amount;
237   }
238 }
239
240 /** @brief set the computation amount needed to process the given task.
241  *
242  * @warning If the computation is ongoing (already started and not finished),
243  * it is not modified by this call. Moreover, after its completion, the ongoing execution with set the flops_amount to
244  * zero, overriding any value set during the execution.
245  */
246 void MSG_task_set_flops_amount(msg_task_t task, double flops_amount)
247 {
248   task->simdata->flops_amount = flops_amount;
249 }
250
251 /** @brief set the amount data attached with the given task.
252  *
253  * @warning If the transfer is ongoing (already started and not finished), it is not modified by this call.
254  */
255 void MSG_task_set_bytes_amount(msg_task_t task, double data_size)
256 {
257   task->simdata->bytes_amount = data_size;
258 }
259
260 /** @brief Returns the total amount received by the given task
261  *
262  *  If the communication does not exist it will return 0.
263  *  So, if the communication has FINISHED or FAILED it returns zero.
264  */
265 double MSG_task_get_remaining_communication(msg_task_t task)
266 {
267   XBT_DEBUG("calling simcall_communication_get_remains(%p)", task->simdata->comm.get());
268   return task->simdata->comm->remains();
269 }
270
271 /** @brief Returns the size of the data attached to the given task. */
272 double MSG_task_get_bytes_amount(msg_task_t task)
273 {
274   xbt_assert((task != nullptr) && (task->simdata != nullptr), "Invalid parameter");
275   return task->simdata->bytes_amount;
276 }
277
278 /** @brief Changes the priority of a computation task.
279  *
280  * This priority doesn't affect the transfer rate. A priority of 2
281  * will make a task receive two times more cpu power than regular tasks.
282  */
283 void MSG_task_set_priority(msg_task_t task, double priority)
284 {
285   task->simdata->priority = 1 / priority;
286   xbt_assert(std::isfinite(task->simdata->priority), "priority is not finite!");
287   if (task->simdata->compute)
288     simgrid::simix::simcall([task] { task->simdata->compute->set_priority(task->simdata->priority); });
289 }
290
291 /** @brief Changes the maximum CPU utilization of a computation task (in flops/s).
292  *
293  * For VMs, there is a pitfall. Please see MSG_vm_set_bound().
294  */
295 void MSG_task_set_bound(msg_task_t task, double bound)
296 {
297   if (bound < 1e-12) /* close enough to 0 without any floating precision surprise */
298     XBT_INFO("bound == 0 means no capping (i.e., unlimited).");
299
300   task->simdata->bound = bound;
301   if (task->simdata->compute)
302     simgrid::simix::simcall([task, bound] { task->simdata->compute->set_bound(bound); });
303 }