Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Refactoring of code and documentation.
[simgrid.git] / src / cxx / Task.cxx
1 #include <Task.hpp>\r
2 \r
3 namespace SimGrid\r
4 {\r
5         namespace Msg\r
6         {\r
7         \r
8                 Task::Task()\r
9                 {\r
10                         nativeTask = NULL;\r
11                 }\r
12                 \r
13                 \r
14                 Task::Task(const Task& rTask)\r
15                 {\r
16                 }\r
17                 \r
18                 \r
19                 Task::~Task()\r
20                 throw(NativeException)\r
21                 {\r
22                         if(NULL != nativeTask)\r
23                                 if(MSG_OK != MSG_task_destroy(nativeTask))\r
24                                         throw MsgException("MSG_task_destroy() failed");\r
25                 }\r
26                 \r
27                 \r
28                 Task::Task(const char* name, double computeDuration, double messageSize)\r
29                 throw(InvalidArgumentException, NullPointerException)\r
30                 {\r
31                          \r
32                         if(computeDuration < 0) \r
33                                 throw InvalidArgumentException("computeDuration");\r
34                         \r
35                         if(messageSize < 0) \r
36                                 throw InvalidArgumentException("messageSize");\r
37                         \r
38                         if(!name) \r
39                                 throw NullPointerException("name");\r
40                         \r
41                         // create the task\r
42                         nativeTask = MSG_task_create(name, computeDuration, messageSize, NULL);\r
43                         \r
44                         nativeTask->data = (void*)this;\r
45                 }\r
46                 \r
47                 Task::Task(const char* name, Host* hosts, double* computeDurations, double* messageSizes, int hostCount)\r
48                 throw(NullPointerException, InvalidArgumentException)\r
49                 {\r
50                         \r
51                         // check the parameters\r
52                         \r
53                         if(!name) \r
54                                 throw NullPointerException("name");\r
55                         \r
56                         if(!hosts) \r
57                                 throw NullPointerException("hosts");\r
58                         \r
59                         if(!computeDurations) \r
60                                 throw NullPointerException("computeDurations");\r
61                                 \r
62                         if(!messageSizes) \r
63                                 throw NullPointerException("messageSizes");\r
64                                 \r
65                         if(!hostCount)\r
66                                 throw InvalidArgumentException("hostCount (must not be zero)");\r
67                                 \r
68                         \r
69                         m_host_t* nativeHosts;\r
70                         double* durations;\r
71                         double* sizes;\r
72                         \r
73                         \r
74                         nativeHosts = xbt_new0(m_host_t, hostCount);\r
75                         durations = xbt_new0(double,hostCount);\r
76                         sizes = xbt_new0(double, hostCount * hostCount);\r
77                         \r
78                         \r
79                         for(int index = 0; index < hostCount; index++) \r
80                         {\r
81                                 \r
82                                 nativeHosts[index] = hosts[index].nativeHost;\r
83                                 durations[index] = computeDurations[index];\r
84                         }\r
85                         \r
86                         for(int index = 0; index < hostCount*hostCount; index++) \r
87                                 sizes[index] = messageSizes[index];\r
88                         \r
89                         \r
90                         nativeTask = MSG_parallel_task_create(name, hostCount, nativeHosts, durations, sizes,NULL);\r
91                         \r
92                         \r
93                         \r
94                         task->data = (void*)this;\r
95                         \r
96                 }\r
97                 \r
98                 const char* Task::getName(void) const\r
99                 {\r
100                         return nativeTask->name;\r
101                 }\r
102                 \r
103                 Process& Task::getSender(void) const\r
104                 {\r
105                         m_proccess_t nativeProcess = MSG_task_get_sender(nativeTask);\r
106                         \r
107                         return (*((Process*)(nativeProcess->data)));\r
108                 }\r
109                 \r
110                 Host& Task::getSource(void) const \r
111                 {\r
112                         m_host_t nativeHost = MSG_task_get_source(nativeTask);\r
113                         \r
114                         return (*((Host*)(nativeHost->data)));  \r
115                 }\r
116                 \r
117                 double Task::getComputeDuration(void) const\r
118                 {\r
119                         return MSG_task_get_compute_duration(nativeTask);\r
120                 }\r
121                 \r
122                 double Task::getRemainingDuration(void) const\r
123                 {\r
124                         return MSG_task_get_remaining_computation(nativeTask);\r
125                 }\r
126                 \r
127                 void Task::setPriority(double priority)\r
128                 throw(InvalidArgumentException)\r
129                 {\r
130                         // check the parameters\r
131                         \r
132                         if(priority < 0.0)\r
133                                 throw InvalidArgumentException("priority");\r
134                                 \r
135                         MSG_task_set_priority(nativeTask, priority);\r
136                 }\r
137                 \r
138                 Task& Task::get(int channel) \r
139                 throw(InvalidArgumentException, MsgException)\r
140                 {\r
141                         // check the parameters\r
142                         \r
143                         if(channel < 0)\r
144                                 throw InvalidArgumentException("channel (must not be negative)");\r
145                                 \r
146                         m_task_t nativeTask = NULL;\r
147                         \r
148                         if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , -1.0, NULL)) \r
149                                 throw MsgException("MSG_task_get_ext() failed");\r
150                         \r
151                         return (*((Task*)(nativeTask->data)));\r
152                 }\r
153                 \r
154                 Task& Task::get(int channel, const Host& rHost) \r
155                 throw(InvalidArgumentException, MsgException)\r
156                 {\r
157                         // check the parameters\r
158                         \r
159                         if(channel < 0)\r
160                                 throw InvalidArgumentException("channel (must not be negative)");\r
161                                 \r
162                         m_task_t nativeTask = NULL;\r
163                         \r
164                         \r
165                         if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , -1.0, rHost.nativeHost)) \r
166                                 throw MsgException("MSG_task_get_ext() failed");\r
167                         \r
168                         return (*((Task*)(nativeTask->data)));\r
169                 }\r
170                 \r
171                 Task& Task::get(int channel, double timeout, const Host& rHost) \r
172                 throw(InvalidArgumentException, MsgException)\r
173                 {\r
174                         // check the parameters\r
175                         \r
176                         if(channel < 0)\r
177                                 throw InvalidArgumentException("channel (must not be negative)");\r
178                         \r
179                         if(timeout < 0 && timeout !=-1.0)\r
180                                 throw InvalidArgumentException("timeout (must not be negative and different thant -1.0)");      \r
181                                 \r
182                         m_task_t nativeTask = NULL;\r
183                         \r
184                         \r
185                         if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , timeout, rHost.nativeHost)) \r
186                                 throw MsgException("MSG_task_get_ext() failed");\r
187                         \r
188                         return (*((Task*)(nativeTask->data)));\r
189                 }\r
190                 \r
191                 bool static Task::probe(int channel)\r
192                 throw(InvalidArgumentException)\r
193                 {\r
194                         // check the parameters\r
195                         \r
196                         if(channel < 0)\r
197                                 throw InvalidArgumentException("channel (must not be negative)");\r
198                                 \r
199                         return (bool)MSG_task_Iprobe(channel);\r
200                 }\r
201                 \r
202                 int Task::probe(int channel, const Host& rHost)\r
203                 throw(InvalidArgumentException)\r
204                 {\r
205                         // check the parameters\r
206                         \r
207                         if(channel < 0)\r
208                                 throw InvalidArgumentException("channel (must not be negative)");\r
209                                 \r
210                         return MSG_task_probe_from_host(chan_id,rHost.nativeHost);\r
211                 }\r
212                 \r
213                 void Task::execute(void) \r
214                 throw(MsgException)\r
215                 {\r
216                         if(MSG_OK != MSG_task_execute(nativeTask))\r
217                                 throw MsgException("MSG_task_execute() failed");\r
218                 }\r
219                 \r
220                 void Task::cancel(void) \r
221                 throw(MsgException)\r
222                 {\r
223                         if(MSG_OK != MSG_task_cancel(nativeTask))\r
224                                 throw MsgException("MSG_task_cancel() failed");\r
225                 }\r
226                 \r
227                 void Task::send(void) \r
228                 throw(BadAllocException, MsgException)\r
229                 {       \r
230                         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
231                         \r
232                         if(!alias)\r
233                                 throw BadAllocException("alias");\r
234                                 \r
235                         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
236                                 \r
237                         MSG_error_t rv = MSG_task_send_with_timeout(nativeTask, alias, -1.0);\r
238                 \r
239                         free(alias)\r
240                 \r
241                         if(MSG_OK != rv)\r
242                                 throw MsgException("MSG_task_send_with_timeout() failed");\r
243                 }\r
244                 \r
245                 void Task::send(const char* alias) \r
246                 throw(NullPointerException, MsgException)\r
247                 {\r
248                         // check the parameters\r
249                         \r
250                         if(!alias)\r
251                                 throw NullPointerException("alias");\r
252                 \r
253                         if(MSG_OK != MSG_task_send_with_timeout(nativeTask, alias, -1.0))\r
254                                 throw MsgException("MSG_task_send_with_timeout() failed");\r
255                 }\r
256                 \r
257                 void Task::send(double timeout) \r
258                 throw(BadAllocationException, InvalidArgumentException, MsgException)\r
259                 {\r
260                         // check the parameters\r
261                         \r
262                         if(timeout < 0  && timeout != -1.0)\r
263                                 throw InvalidArgumentException("timeout (must not be negative and different than -1.0");\r
264                                                 \r
265                         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
266                         \r
267                         if(!alias)\r
268                                 throw BadAllocException("alias");\r
269                                 \r
270                         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
271                 \r
272                         MSG_error_t rv = MSG_task_send_with_timeout(nativeTask, alias, timeout);\r
273                 \r
274                         free(alias)\r
275                 \r
276                         if(MSG_OK != rv)\r
277                                 throw MsgException("MSG_task_send_with_timeout() failed");\r
278                 }       \r
279                 \r
280                 void Task::send(const char* alias, double timeout) \r
281                 throw(NullPointerException, InvalidArgumentException, MsgException)\r
282                 {\r
283                         // check the parameters\r
284                         \r
285                         if(!alias)\r
286                                 throw NullPointerException("alias");\r
287                                 \r
288                         if(timeout < 0  && timeout != -1.0)\r
289                                 throw InvalidArgumentException("timeout (must not be negative and different than -1.0");\r
290                                                 \r
291                                 \r
292                         if(MSG_OK != MSG_task_send_with_timeout(nativeTask, alias, timeout))\r
293                                 throw MsgException("MSG_task_send_with_timeout() failed");\r
294                 }\r
295                 \r
296                 void Task::sendBounded(double maxRate) \r
297                 throw(BadAllocException, InvalidArgumentException, MsgException)\r
298                 {\r
299                         // check the parameters\r
300                         \r
301                         if(maxRate < 0 && maxRate != -1.0)\r
302                                 throw InvalidArgumentException("maxRate (must not be negative and different than -1.0");\r
303                                         \r
304                         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
305                         \r
306                         if(!alias)\r
307                                 throw BadAllocException("alias");\r
308                         \r
309                         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
310                         \r
311                         MSG_error_t rv = MSG_task_send_bounded(nativeTask, alias, maxRate);\r
312                         \r
313                         free(alias);    \r
314                         \r
315                         if(MSG_OK != rv)\r
316                                 throw MsgException("MSG_task_send_bounded() failed");\r
317                                 \r
318                 }\r
319                 \r
320                 void Task::sendBounded(const char* alias, double maxRate) \r
321                 throw(NullPointerException, InvalidArgumentException, MsgException)\r
322                 {\r
323                         // check the parameters\r
324                         \r
325                         if(maxRate < 0 && maxRate != -1.0)\r
326                                 throw InvalidArgumentException("maxRate (must not be negative and different than -1.0");\r
327                                                         \r
328                         if(!alias)\r
329                                 throw NullPointerException("alias");\r
330                         \r
331                         if(MSG_OK != MSG_task_send_bounded(nativeTask, alias, maxRate))\r
332                                 throw MsgException("MSG_task_send_bounded() failed");\r
333                 }\r
334                 \r
335                 Task& Task::receive(void) \r
336                 throw(throw(BadAllocException, MsgException))\r
337                 {\r
338                         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
339                         \r
340                         if(!alias)\r
341                                 throw BadAllocException("alias");\r
342                                 \r
343                         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
344                                 \r
345                         m_task_t nativeTask = NULL;\r
346                         \r
347                         MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL);  \r
348                 \r
349                         free(alias);\r
350                         \r
351                         if(MSG_OK != rv) \r
352                                 throw MsgException("MSG_task_receive_ext() failed");\r
353                 \r
354                         return (*((Task*)nativeTask->data));\r
355                 }\r
356                 \r
357                 Task& Task::receive(const char* alias) \r
358                 throw(NullPointerException, MsgException)\r
359                 {\r
360                         // check the parameters\r
361                         \r
362                         if(!alias)\r
363                                 throw NullPointerException("alias");\r
364                                 \r
365                         m_task_t nativeTask = NULL;\r
366                         \r
367                         if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL)) \r
368                                 throw MsgException("MSG_task_receive_ext() failed");\r
369                 \r
370                         return (*((Task*)nativeTask->data));\r
371                 }\r
372                 \r
373                 Task& Task::receive(const char* alias, double timeout) \r
374                 throw(NullPointerException, InvalidArgumentException, MsgException)\r
375                 {\r
376                         // check the parameters\r
377                         \r
378                         if(!alias)\r
379                                 throw NullPointerException("alias");\r
380                         \r
381                         if(timeout < 0 && alias != -1.0)\r
382                                 throw InvalidArgumentException("timeout (must not be negative and differnt than -1.0)");\r
383                                 \r
384                         m_task_t nativeTask = NULL;\r
385                         \r
386                         if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, NULL)) \r
387                                 throw MsgException("MSG_task_receive_ext() failed");\r
388                 \r
389                         return (*((Task*)nativeTask->data));\r
390                 }\r
391                 \r
392                 Task& Task::receive(const char* alias, const Host& rHost) \r
393                 throw(NullPointerException, MsgException)\r
394                 {\r
395                         // check the parameters\r
396                         \r
397                         if(!alias)\r
398                                 throw NullPointerException("alias");\r
399                         \r
400                         m_task_t nativeTask = NULL;\r
401                         \r
402                         if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost)) \r
403                                 throw MsgException("MSG_task_receive_ext() failed");\r
404                 \r
405                         return (*((Task*)nativeTask->data));\r
406                 }       \r
407                 \r
408                 Task& Task::receive(const char* alias, double timeout, const Host& rHost) \r
409                 throw(NullPointerException, InvalidArgumentException, MsgException)\r
410                 {\r
411                         // check the parameters\r
412                         \r
413                         if(!alias)\r
414                                 throw NullPointerException("alias");\r
415                         \r
416                         if(timeout < 0 && alias != -1.0)\r
417                                 throw InvalidArgumentException("timeout (must not be negative and differnt than -1.0)");\r
418                                 \r
419                         m_task_t nativeTask = NULL;\r
420                         \r
421                         \r
422                         if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost)) \r
423                                 throw MsgException("MSG_task_receive_ext() failed");\r
424                 \r
425                         return (*((Task*)nativeTask->data));\r
426                 }\r
427                 \r
428                 bool Task::listen(void) \r
429                 throw(BadAllocException)\r
430                 {\r
431                         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
432                         \r
433                         if(!alias)\r
434                                 throw BadAllocException("alias");\r
435                                 \r
436                         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
437                                 \r
438                         int rv = MSG_task_listen(alias);\r
439                         \r
440                         free(alias);\r
441                         \r
442                         return (bool)rv;\r
443                 }       \r
444                 \r
445                 bool Task::listen(const char* alias) \r
446                 throw(NullPointerException)\r
447                 {\r
448                         // check the parameters\r
449                         \r
450                         if(!alias)\r
451                                 throw NullPointerException("alias");\r
452                                 \r
453                         return (bool)MSG_task_listen(alias);\r
454                 }\r
455                 \r
456                 int Task::listenFrom(void) \r
457                 throw(BadAllocException)\r
458                 {\r
459                         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
460                         \r
461                         if(!alias)\r
462                                 throw BadAllocException("alias");\r
463                                 \r
464                         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
465                         \r
466                         int rv = MSG_task_listen_from(alias);\r
467                         \r
468                         free(alias);\r
469                         \r
470                         return rv;\r
471                 }       \r
472                 \r
473                 int Task::listenFrom(const char* alias) \r
474                 throw(NullPointerException)\r
475                 {\r
476                         if(!alias)\r
477                                 throw NullPointerException("alias");\r
478                                 \r
479                         return MSG_task_listen_from(alias);\r
480                         \r
481                 }\r
482                 \r
483                 int Task::listenFromHost(const Host& rHost) \r
484                 throw(BadAllocException)\r
485                 {\r
486                         char* alias = (char*)calloc(strlen(Process::currentProcess().getName() + strlen(Host::currentHost().getName()) + 2);\r
487                         \r
488                         if(!alias)\r
489                                 throw BadAllocException("alias");\r
490                         \r
491                         sprintf(alias,"%s:%s", Process::currentProcess().getName(),Host::currentHost().getName());\r
492                         \r
493                         int rv = MSG_task_listen_from_host(alias, rHost.nativeHost);\r
494                         \r
495                         free(alias);\r
496                         \r
497                         return rv;\r
498                 }\r
499                         \r
500                 int Task::listenFromHost(const char* alias, const Host& rHost) \r
501                 throw(NullPointerException)\r
502                 {\r
503                         // check the parameters\r
504                         if(!alias)\r
505                                 throw NullPointerException("alias");\r
506                                 \r
507                         return MSG_task_listen_from_host(alias, rHost.nativeHost);\r
508                 }       \r
509         } // namespace Msg                                                                                                                                                                                              \r
510 } // namespace SimGrid\r
511 \r