Logo AND Algorithmique Numérique Distribuée

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