Logo AND Algorithmique Numérique Distribuée

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