4 * Copyright 2006,2007 Martin Quinson, Malek Cherier
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.
13 /* Task member functions implementation.
16 #include <MsgProcess.hpp>
17 #include <MsgHost.hpp>
19 #include <MsgTask.hpp>
31 MSG_IMPLEMENT_DYNAMIC(Task, Object)
39 Task::Task(const Task& rTask)
41 this->nativeTask = rTask.nativeTask;
48 if(NULL != nativeTask)
49 if(MSG_OK != MSG_task_destroy(nativeTask))
50 throw MsgException("MSG_task_destroy() failed");
54 Task::Task(const char* name, double computeDuration, double messageSize)
55 throw(InvalidArgumentException, NullPointerException)
58 if(computeDuration < 0)
59 throw InvalidArgumentException("computeDuration");
62 throw InvalidArgumentException("messageSize");
65 throw NullPointerException("name");
68 nativeTask = MSG_task_create(name, computeDuration, messageSize, NULL);
70 nativeTask->data = (void*)this;
73 Task::Task(const char* name, Host* hosts, double* computeDurations, double* messageSizes, int hostCount)
74 throw(NullPointerException, InvalidArgumentException)
76 // check the parameters
79 throw NullPointerException("name");
82 throw NullPointerException("hosts");
85 throw NullPointerException("computeDurations");
88 throw NullPointerException("messageSizes");
91 throw InvalidArgumentException("hostCount (must not be zero)");
94 m_host_t* nativeHosts;
99 nativeHosts = xbt_new0(m_host_t, hostCount);
100 durations = xbt_new0(double,hostCount);
101 sizes = xbt_new0(double, hostCount * hostCount);
104 for(int index = 0; index < hostCount; index++)
107 nativeHosts[index] = hosts[index].nativeHost;
108 durations[index] = computeDurations[index];
111 for(int index = 0; index < hostCount*hostCount; index++)
112 sizes[index] = messageSizes[index];
115 nativeTask = MSG_parallel_task_create(name, hostCount, nativeHosts, durations, sizes,NULL);
119 this->nativeTask->data = (void*)this;
123 const char* Task::getName(void) const
125 return nativeTask->name;
128 Process& Task::getSender(void) const
130 m_process_t nativeProcess = MSG_task_get_sender(nativeTask);
132 return (*((Process*)(nativeProcess->data)));
135 Host& Task::getSource(void) const
137 m_host_t nativeHost = MSG_task_get_source(nativeTask);
139 return (*((Host*)(nativeHost->data)));
142 double Task::getComputeDuration(void) const
144 return MSG_task_get_compute_duration(nativeTask);
147 double Task::getRemainingDuration(void) const
149 return MSG_task_get_remaining_computation(nativeTask);
152 void Task::setPriority(double priority)
153 throw(InvalidArgumentException)
155 // check the parameters
158 throw InvalidArgumentException("priority");
160 MSG_task_set_priority(nativeTask, priority);
163 Task* Task::get(int channel)
164 throw(InvalidArgumentException, MsgException)
166 // check the parameters
169 throw InvalidArgumentException("channel (must not be negative)");
171 m_task_t nativeTask = NULL;
173 if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , -1.0, NULL))
174 throw MsgException("MSG_task_get_ext() failed");
176 return ((Task*)(nativeTask->data));
179 Task* Task::get(int channel, const Host& rHost)
180 throw(InvalidArgumentException, MsgException)
182 // check the parameters
185 throw InvalidArgumentException("channel (must not be negative)");
187 m_task_t nativeTask = NULL;
190 if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , -1.0, rHost.nativeHost))
191 throw MsgException("MSG_task_get_ext() failed");
193 return (Task*)(nativeTask->data);
196 Task* Task::get(int channel, double timeout, const Host& rHost)
197 throw(InvalidArgumentException, MsgException)
199 // check the parameters
202 throw InvalidArgumentException("channel (must not be negative)");
204 if(timeout < 0 && timeout !=-1.0)
205 throw InvalidArgumentException("timeout (must not be negative and different thant -1.0)");
207 m_task_t nativeTask = NULL;
210 if(MSG_OK != MSG_task_get_ext(&nativeTask, channel , timeout, rHost.nativeHost))
211 throw MsgException("MSG_task_get_ext() failed");
213 return (Task*)(nativeTask->data);
216 int Task::probe(int channel)
217 throw(InvalidArgumentException)
219 // check the parameters
222 throw InvalidArgumentException("channel (must not be negative)");
224 return MSG_task_Iprobe(channel);
227 int Task::probe(int channel, const Host& rHost)
228 throw(InvalidArgumentException)
230 // check the parameters
233 throw InvalidArgumentException("channel (must not be negative)");
235 return MSG_task_probe_from_host(channel,rHost.nativeHost);
238 void Task::execute(void)
241 if(MSG_OK != MSG_task_execute(nativeTask))
242 throw MsgException("MSG_task_execute() failed");
245 void Task::cancel(void)
248 if(MSG_OK != MSG_task_cancel(nativeTask))
249 throw MsgException("MSG_task_cancel() failed");
252 void Task::send(void)
253 throw(BadAllocException, MsgException)
255 char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char));
258 throw BadAllocException("alias");
260 sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName());
262 MSG_error_t rv = MSG_task_send_with_timeout(nativeTask, alias, -1.0);
267 throw MsgException("MSG_task_send_with_timeout() failed");
270 void Task::send(const char* alias)
271 throw(NullPointerException, MsgException)
273 // check the parameters
276 throw NullPointerException("alias");
278 if(MSG_OK != MSG_task_send_with_timeout(nativeTask, alias, -1.0))
279 throw MsgException("MSG_task_send_with_timeout() failed");
282 void Task::send(double timeout)
283 throw(BadAllocException, InvalidArgumentException, MsgException)
285 // check the parameters
287 if(timeout < 0 && timeout != -1.0)
288 throw InvalidArgumentException("timeout (must not be negative and different than -1.0");
290 char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char));
293 throw BadAllocException("alias");
295 sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName());
297 MSG_error_t rv = MSG_task_send_with_timeout(nativeTask, alias, timeout);
302 throw MsgException("MSG_task_send_with_timeout() failed");
305 void Task::send(const char* alias, double timeout)
306 throw(NullPointerException, InvalidArgumentException, MsgException)
308 // check the parameters
311 throw NullPointerException("alias");
313 if(timeout < 0 && timeout != -1.0)
314 throw InvalidArgumentException("timeout (must not be negative and different than -1.0");
317 if(MSG_OK != MSG_task_send_with_timeout(nativeTask, alias, timeout))
318 throw MsgException("MSG_task_send_with_timeout() failed");
321 void Task::sendBounded(double maxRate)
322 throw(BadAllocException, InvalidArgumentException, MsgException)
324 // check the parameters
326 if(maxRate < 0 && maxRate != -1.0)
327 throw InvalidArgumentException("maxRate (must not be negative and different than -1.0");
329 char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char));
332 throw BadAllocException("alias");
334 sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName());
336 MSG_error_t rv = MSG_task_send_bounded(nativeTask, alias, maxRate);
341 throw MsgException("MSG_task_send_bounded() failed");
345 void Task::sendBounded(const char* alias, double maxRate)
346 throw(NullPointerException, InvalidArgumentException, MsgException)
348 // check the parameters
350 if(maxRate < 0 && maxRate != -1.0)
351 throw InvalidArgumentException("maxRate (must not be negative and different than -1.0");
354 throw NullPointerException("alias");
356 if(MSG_OK != MSG_task_send_bounded(nativeTask, alias, maxRate))
357 throw MsgException("MSG_task_send_bounded() failed");
360 Task* Task::receive(void)
361 throw(BadAllocException, MsgException)
363 char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char));
366 throw BadAllocException("alias");
368 sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName());
370 m_task_t nativeTask = NULL;
372 MSG_error_t rv = MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL);
377 throw MsgException("MSG_task_receive_ext() failed");
379 return (Task*)(nativeTask->data);
382 Task* Task::receive(const char* alias)
383 throw(NullPointerException, MsgException)
385 // check the parameters
388 throw NullPointerException("alias");
390 m_task_t nativeTask = NULL;
392 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, NULL))
393 throw MsgException("MSG_task_receive_ext() failed");
395 return (Task*)(nativeTask->data);
398 Task* Task::receive(const char* alias, double timeout)
399 throw(NullPointerException, InvalidArgumentException, MsgException)
401 // check the parameters
404 throw NullPointerException("alias");
406 if(timeout < 0 && timeout != -1.0)
407 throw InvalidArgumentException("timeout (must not be negative and differnt than -1.0)");
409 m_task_t nativeTask = NULL;
411 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, NULL))
412 throw MsgException("MSG_task_receive_ext() failed");
414 return (Task*)(nativeTask->data);
417 Task* Task::receive(const char* alias, const Host& rHost)
418 throw(NullPointerException, MsgException)
420 // check the parameters
423 throw NullPointerException("alias");
425 m_task_t nativeTask = NULL;
427 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, -1.0, rHost.nativeHost))
428 throw MsgException("MSG_task_receive_ext() failed");
430 return (Task*)(nativeTask->data);
433 Task* Task::receive(const char* alias, double timeout, const Host& rHost)
434 throw(NullPointerException, InvalidArgumentException, MsgException)
436 // check the parameters
439 throw NullPointerException("alias");
441 if(timeout < 0 && timeout != -1.0)
442 throw InvalidArgumentException("timeout (must not be negative and differnt than -1.0)");
444 m_task_t nativeTask = NULL;
447 if(MSG_OK != MSG_task_receive_ext(&nativeTask, alias, timeout, rHost.nativeHost))
448 throw MsgException("MSG_task_receive_ext() failed");
450 return (Task*)(nativeTask->data);
453 int Task::listen(void)
454 throw(BadAllocException)
456 char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char));
459 throw BadAllocException("alias");
461 sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName());
463 int rv = MSG_task_listen(alias);
470 int Task::listen(const char* alias)
471 throw(NullPointerException)
473 // check the parameters
476 throw NullPointerException("alias");
478 return MSG_task_listen(alias);
481 int Task::listenFrom(void)
482 throw(BadAllocException)
484 char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char));
487 throw BadAllocException("alias");
489 sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName());
491 int rv = MSG_task_listen_from(alias);
498 int Task::listenFrom(const char* alias)
499 throw(NullPointerException)
502 throw NullPointerException("alias");
504 return MSG_task_listen_from(alias);
508 int Task::listenFromHost(const Host& rHost)
509 throw(BadAllocException)
511 char* alias = (char*)calloc(strlen(Process::currentProcess().getName()) + strlen(Host::currentHost().getName()) + 2, sizeof(char));
514 throw BadAllocException("alias");
516 sprintf(alias,"%s:%s", Host::currentHost().getName(), Process::currentProcess().getName());
518 int rv = MSG_task_listen_from_host(alias, rHost.nativeHost);
525 int Task::listenFromHost(const char* alias, const Host& rHost)
526 throw(NullPointerException)
528 // check the parameters
530 throw NullPointerException("alias");
532 return MSG_task_listen_from_host(alias, rHost.nativeHost);
535 const Task& Task::operator = (const Task& rTask)
537 this->nativeTask = rTask.nativeTask;
541 } // namespace SimGrid