Logo AND Algorithmique Numérique Distribuée

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