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