3 /* Copyright (c) 2002-2007 Arnaud Legrand. */
4 /* Copyright (c) 2007 Bruno Donassolo. */
5 /* All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "msg/private.h"
11 #include "xbt/sysdep.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_gos, msg,
17 "Logging specific to MSG (gos)");
19 /** \ingroup msg_gos_functions
21 * \brief Return the last value returned by a MSG function (except
27 return PROCESS_GET_ERRNO();
30 /** \ingroup msg_gos_functions
31 * \brief Executes a task and waits for its termination.
33 * This function is used for describing the behavior of an agent. It
34 * takes only one parameter.
35 * \param task a #m_task_t to execute on the location on which the
37 * \return #MSG_FATAL if \a task is not properly initialized and
41 MSG_task_execute(m_task_t task)
43 simdata_task_t simdata = NULL;
44 m_process_t self = MSG_process_self();
47 simdata = task->simdata;
48 xbt_assert0((!simdata->compute) && (task->simdata->refcount == 1),"This task is executed somewhere else. Go fix your code!");
50 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
53 SIMIX_mutex_lock(simdata->mutex);
54 simdata->compute = SIMIX_action_execute(SIMIX_host_self(), task->name, simdata->computation_amount);
55 SIMIX_action_set_priority(simdata->compute, simdata->priority);
57 self->simdata->waiting_task = task;
58 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
59 SIMIX_cond_wait(simdata->cond, simdata->mutex);
60 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
61 self->simdata->waiting_task = NULL;
63 SIMIX_mutex_unlock(simdata->mutex);
66 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE)
68 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
69 SIMIX_action_destroy(task->simdata->compute);
70 simdata->computation_amount = 0.0;
72 simdata->compute = NULL;
75 else if(SIMIX_host_get_state(SIMIX_host_self()) == 0)
77 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
78 SIMIX_action_destroy(task->simdata->compute);
80 simdata->compute = NULL;
81 MSG_RETURN(MSG_HOST_FAILURE);
85 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
86 SIMIX_action_destroy(task->simdata->compute);
88 simdata->compute = NULL;
89 MSG_RETURN(MSG_TASK_CANCELLED);
93 /** \ingroup m_task_management
94 * \brief Creates a new #m_task_t (a parallel one....).
96 * A constructor for #m_task_t taking six arguments and returning the
98 * \param name a name for the object. It is for user-level information
100 * \param host_nb the number of hosts implied in the parallel task.
101 * \param host_list an array of \p host_nb m_host_t.
102 * \param computation_amount an array of \p host_nb
103 doubles. computation_amount[i] is the total number of operations
104 that have to be performed on host_list[i].
105 * \param communication_amount an array of \p host_nb* \p host_nb doubles.
106 * \param data a pointer to any data may want to attach to the new
107 object. It is for user-level information and can be NULL. It can
108 be retrieved with the function \ref MSG_task_get_data.
110 * \return The new corresponding object.
113 MSG_parallel_task_create(const char *name,int host_nb, const m_host_t * host_list, double *computation_amount, double *communication_amount, void *data)
116 simdata_task_t simdata = xbt_new0(s_simdata_task_t, 1);
117 m_task_t task = xbt_new0(s_m_task_t, 1);
118 task->simdata = simdata;
121 task->name = xbt_strdup(name);
125 simdata->computation_amount = 0;
126 simdata->message_size = 0;
127 simdata->cond = SIMIX_cond_init();
128 simdata->mutex = SIMIX_mutex_init();
129 simdata->compute = NULL;
130 simdata->comm = NULL;
131 simdata->rate = -1.0;
132 simdata->refcount = 1;
133 simdata->sender = NULL;
134 simdata->receiver = NULL;
135 simdata->source = NULL;
137 simdata->host_nb = host_nb;
138 simdata->host_list = xbt_new0(smx_host_t, host_nb);
139 simdata->comp_amount = computation_amount;
140 simdata->comm_amount = communication_amount;
142 for (i = 0; i < host_nb; i++)
143 simdata->host_list[i] = host_list[i]->simdata->smx_host;
149 MSG_parallel_task_execute(m_task_t task)
151 simdata_task_t simdata = NULL;
152 m_process_t self = MSG_process_self();
155 simdata = task->simdata;
157 xbt_assert0((!simdata->compute) && (task->simdata->refcount == 1),"This task is executed somewhere else. Go fix your code!");
159 xbt_assert0(simdata->host_nb,"This is not a parallel task. Go to hell.");
161 DEBUG1("Computing on %s", MSG_process_self()->simdata->m_host->name);
163 simdata->refcount ++;
165 SIMIX_mutex_lock(simdata->mutex);
167 SIMIX_action_parallel_execute(task->name, simdata->host_nb, simdata->host_list, simdata->comp_amount, simdata->comm_amount, 1.0, -1.0);
169 self->simdata->waiting_task = task;
170 SIMIX_register_action_to_condition(simdata->compute, simdata->cond);
171 SIMIX_cond_wait(simdata->cond, simdata->mutex);
172 SIMIX_unregister_action_to_condition(simdata->compute, simdata->cond);
173 self->simdata->waiting_task = NULL;
176 SIMIX_mutex_unlock(simdata->mutex);
177 simdata->refcount --;
179 if (SIMIX_action_get_state(task->simdata->compute) == SURF_ACTION_DONE)
181 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
182 SIMIX_action_destroy(task->simdata->compute);
183 simdata->computation_amount = 0.0;
184 simdata->comm = NULL;
185 simdata->compute = NULL;
188 else if(SIMIX_host_get_state(SIMIX_host_self()) == 0)
190 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
191 SIMIX_action_destroy(task->simdata->compute);
192 simdata->comm = NULL;
193 simdata->compute = NULL;
194 MSG_RETURN(MSG_HOST_FAILURE);
198 /* action ended, set comm and compute = NULL, the actions is already destroyed in the main function */
199 SIMIX_action_destroy(task->simdata->compute);
200 simdata->comm = NULL;
201 simdata->compute = NULL;
202 MSG_RETURN(MSG_TASK_CANCELLED);
208 /** \ingroup msg_gos_functions
209 * \brief Sleep for the specified number of seconds
211 * Makes the current process sleep until \a time seconds have elapsed.
213 * \param nb_sec a number of second
216 MSG_process_sleep(double nb_sec)
218 smx_action_t act_sleep;
219 m_process_t proc = MSG_process_self();
223 /* create action to sleep */
224 act_sleep = SIMIX_action_sleep(SIMIX_process_get_host(proc->simdata->s_process), nb_sec);
226 mutex = SIMIX_mutex_init();
227 SIMIX_mutex_lock(mutex);
229 /* create conditional and register action to it */
230 cond = SIMIX_cond_init();
232 SIMIX_register_action_to_condition(act_sleep, cond);
233 SIMIX_cond_wait(cond, mutex);
234 SIMIX_unregister_action_to_condition(act_sleep, cond);
235 SIMIX_mutex_unlock(mutex);
237 /* remove variables */
238 SIMIX_cond_destroy(cond);
239 SIMIX_mutex_destroy(mutex);
241 if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE)
243 if (SIMIX_host_get_state(SIMIX_host_self()) == SURF_CPU_OFF)
245 SIMIX_action_destroy(act_sleep);
246 MSG_RETURN(MSG_HOST_FAILURE);
251 SIMIX_action_destroy(act_sleep);
252 MSG_RETURN(MSG_HOST_FAILURE);
255 SIMIX_action_destroy(act_sleep);
259 /** \ingroup msg_gos_functions
260 * \brief Return the number of MSG tasks currently running on
261 * the host of the current running process.
264 MSG_get_msgload(void)
266 xbt_die("not implemented yet");
272 /** \ingroup msg_gos_functions
273 * \brief Listen on \a channel and waits for receiving a task from \a host.
275 * It takes three parameters.
276 * \param task a memory location for storing a #m_task_t. It will
277 hold a task when this function will return. Thus \a task should not
278 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
279 those two condition does not hold, there will be a warning message.
280 * \param channel the channel on which the agent should be
281 listening. This value has to be >=0 and < than the maximal
282 number of channels fixed with MSG_set_channel_number().
283 * \param host the host that is to be watched.
284 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
285 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
288 MSG_task_get_from_host(m_task_t * task, m_channel_t channel, m_host_t host)
290 return MSG_task_get_ext(task, channel, -1, host);
293 /** \ingroup msg_gos_functions
294 * \brief Listen on a channel and wait for receiving a task.
296 * It takes two parameters.
297 * \param task a memory location for storing a #m_task_t. It will
298 hold a task when this function will return. Thus \a task should not
299 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
300 those two condition does not hold, there will be a warning message.
301 * \param channel the channel on which the agent should be
302 listening. This value has to be >=0 and < than the maximal
303 number of channels fixed with MSG_set_channel_number().
304 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
305 * if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
308 MSG_task_get(m_task_t * task, m_channel_t channel)
310 return MSG_task_get_with_timeout(task, channel, -1);
313 /** \ingroup msg_gos_functions
314 * \brief Listen on a channel and wait for receiving a task with a timeout.
316 * It takes three parameters.
317 * \param task a memory location for storing a #m_task_t. It will
318 hold a task when this function will return. Thus \a task should not
319 be equal to \c NULL and \a *task should be equal to \c NULL. If one of
320 those two condition does not hold, there will be a warning message.
321 * \param channel the channel on which the agent should be
322 listening. This value has to be >=0 and < than the maximal
323 number of channels fixed with MSG_set_channel_number().
324 * \param max_duration the maximum time to wait for a task before giving
325 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
326 will not be modified and will still be
327 equal to \c NULL when returning.
328 * \return #MSG_FATAL if \a task is equal to \c NULL, #MSG_WARNING
329 if \a *task is not equal to \c NULL, and #MSG_OK otherwise.
332 MSG_task_get_with_timeout(m_task_t * task, m_channel_t channel, double max_duration)
334 return MSG_task_get_ext(task, channel, max_duration, NULL);
337 /** \defgroup msg_gos_functions MSG Operating System Functions
338 * \brief This section describes the functions that can be used
339 * by an agent for handling some task.
343 MSG_task_get_ext(m_task_t * task, m_channel_t channel, double timeout,m_host_t host)
345 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d",channel);
347 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_channel(MSG_host_self(), channel), task, host, timeout);
351 MSG_task_receive_from_host(m_task_t * task, const char* alias, m_host_t host)
353 return MSG_task_receive_ext(task, alias, -1, host);
357 MSG_task_receive(m_task_t * task, const char* alias)
359 return MSG_task_receive_with_timeout(task, alias, -1);
363 MSG_task_receive_with_timeout(m_task_t * task, const char* alias, double timeout)
365 return MSG_task_receive_ext(task, alias, timeout, NULL);
369 MSG_task_receive_ext(m_task_t* task, const char* alias, double timeout, m_host_t host)
371 return MSG_mailbox_get_task_ext(MSG_mailbox_get_by_alias(alias), task, host, timeout);
375 /** \ingroup msg_gos_functions
376 * \brief Put a task on a channel of an host and waits for the end of the
379 * This function is used for describing the behavior of an agent. It
380 * takes three parameter.
381 * \param task a #m_task_t to send on another location. This task
382 will not be usable anymore when the function will return. There is
383 no automatic task duplication and you have to save your parameters
384 before calling this function. Tasks are unique and once it has been
385 sent to another location, you should not access it anymore. You do
386 not need to call MSG_task_destroy() but to avoid using, as an
387 effect of inattention, this task anymore, you definitely should
388 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
389 can be transfered iff it has been correctly created with
391 * \param dest the destination of the message
392 * \param channel the channel on which the agent should put this
393 task. This value has to be >=0 and < than the maximal number of
394 channels fixed with MSG_set_channel_number().
395 * \return #MSG_FATAL if \a task is not properly initialized and
396 * #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
397 * this function was called was shut down. Returns
398 * #MSG_TRANSFER_FAILURE if the transfer could not be properly done
399 * (network failure, dest failure)
402 MSG_task_put(m_task_t task, m_host_t dest, m_channel_t channel)
404 return MSG_task_put_with_timeout(task, dest, channel, -1.0);
407 /** \ingroup msg_gos_functions
408 * \brief Does exactly the same as MSG_task_put but with a bounded transmition
414 MSG_task_put_bounded(m_task_t task, m_host_t dest, m_channel_t channel, double maxrate)
416 task->simdata->rate = maxrate;
417 return MSG_task_put(task, dest, channel);
420 /** \ingroup msg_gos_functions \brief Put a task on a channel of an
421 * host (with a timeout on the waiting of the destination host) and
422 * waits for the end of the transmission.
424 * This function is used for describing the behavior of an agent. It
425 * takes four parameter.
426 * \param task a #m_task_t to send on another location. This task
427 will not be usable anymore when the function will return. There is
428 no automatic task duplication and you have to save your parameters
429 before calling this function. Tasks are unique and once it has been
430 sent to another location, you should not access it anymore. You do
431 not need to call MSG_task_destroy() but to avoid using, as an
432 effect of inattention, this task anymore, you definitely should
433 renitialize it with #MSG_TASK_UNINITIALIZED. Note that this task
434 can be transfered iff it has been correctly created with
436 * \param dest the destination of the message
437 * \param channel the channel on which the agent should put this
438 task. This value has to be >=0 and < than the maximal number of
439 channels fixed with MSG_set_channel_number().
440 * \param timeout the maximum time to wait for a task before giving
441 up. In such a case, #MSG_TRANSFER_FAILURE will be returned, \a task
443 * \return #MSG_FATAL if \a task is not properly initialized and
444 #MSG_OK otherwise. Returns #MSG_HOST_FAILURE if the host on which
445 this function was called was shut down. Returns
446 #MSG_TRANSFER_FAILURE if the transfer could not be properly done
447 (network failure, dest failure, timeout...)
450 MSG_task_put_with_timeout(m_task_t task, m_host_t dest, m_channel_t channel, double timeout)
452 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d", channel);
454 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_channel(dest, channel), task, timeout);
458 MSG_task_send(m_task_t task,const char* alias)
460 return MSG_task_send_with_timeout(task, alias, -1);
465 MSG_task_send_bounded(m_task_t task, const char* alias, double maxrate)
467 task->simdata->rate = maxrate;
468 return MSG_task_send(task, alias);
473 MSG_task_send_with_timeout(m_task_t task, const char* alias, double timeout)
475 return MSG_mailbox_put_with_timeout(MSG_mailbox_get_by_alias(alias), task, timeout);
479 MSG_task_listen(const char* alias)
483 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_alias(alias));
486 /** \ingroup msg_gos_functions
487 * \brief Test whether there is a pending communication on a channel.
489 * It takes one parameter.
490 * \param channel the channel on which the agent should be
491 listening. This value has to be >=0 and < than the maximal
492 number of channels fixed with MSG_set_channel_number().
493 * \return 1 if there is a pending communication and 0 otherwise
496 MSG_task_Iprobe(m_channel_t channel)
498 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d", channel);
502 return !MSG_mailbox_is_empty(MSG_mailbox_get_by_channel(MSG_host_self(), channel));
505 /** \ingroup msg_gos_functions
507 * \brief Return the number of tasks waiting to be received on a \a
508 channel and sent by \a host.
510 * It takes two parameters.
511 * \param channel the channel on which the agent should be
512 listening. This value has to be >=0 and < than the maximal
513 number of channels fixed with MSG_set_channel_number().
514 * \param host the host that is to be watched.
515 * \return the number of tasks waiting to be received on \a channel
519 MSG_task_probe_from_host(int channel, m_host_t host)
521 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d", channel);
525 return MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_channel(MSG_host_self(), channel),host);
530 MSG_task_listen_from_host(const char* alias, m_host_t host)
534 return MSG_mailbox_get_count_host_waiting_tasks(MSG_mailbox_get_by_alias(alias),host);
537 /** \ingroup msg_gos_functions
538 * \brief Test whether there is a pending communication on a channel, and who sent it.
540 * It takes one parameter.
541 * \param channel the channel on which the agent should be
542 listening. This value has to be >=0 and < than the maximal
543 number of channels fixed with MSG_set_channel_number().
544 * \return -1 if there is no pending communication and the PID of the process who sent it otherwise
547 MSG_task_probe_from(m_channel_t channel)
553 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d", channel);
555 if(NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_channel(MSG_host_self(), channel))))
558 return MSG_process_get_PID(task->simdata->sender);
562 MSG_task_listen_from(const char* alias)
568 if(NULL == (task = MSG_mailbox_get_head(MSG_mailbox_get_by_alias(alias))))
571 return MSG_process_get_PID(task->simdata->sender);
574 /** \ingroup msg_gos_functions
575 * \brief Wait for at most \a max_duration second for a task reception
578 * \a PID is updated with the PID of the first process that triggered this event if any.
580 * It takes three parameters:
581 * \param channel the channel on which the agent should be
582 listening. This value has to be >=0 and < than the maximal.
583 number of channels fixed with MSG_set_channel_number().
584 * \param PID a memory location for storing an int.
585 * \param timeout the maximum time to wait for a task before
586 giving up. In the case of a reception, *\a PID will be updated
587 with the PID of the first process to send a task.
588 * \return #MSG_HOST_FAILURE if the host is shut down in the meantime
589 and #MSG_OK otherwise.
592 MSG_channel_select_from(m_channel_t channel,double timeout, int *PID)
595 simdata_host_t h_simdata = NULL;
599 msg_mailbox_t mailbox;
601 xbt_assert1((channel >= 0) && (channel < msg_global->max_channel), "Invalid channel %d",channel);
610 *PID = MSG_task_probe_from(channel);
617 h_simdata = h->simdata;
619 mailbox = MSG_mailbox_get_by_channel(MSG_host_self(), channel);
621 while(MSG_mailbox_is_empty(mailbox))
631 SIMIX_mutex_lock(h_simdata->mutex);
633 xbt_assert1(!MSG_mailbox_get_cond(mailbox),"A process is already blocked on this channel %d",channel);
635 cond = SIMIX_cond_init();
637 MSG_mailbox_set_cond(mailbox, cond);
641 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
645 SIMIX_cond_wait(cond, h_simdata->mutex);
648 SIMIX_cond_destroy(cond);
649 SIMIX_mutex_unlock(h_simdata->mutex);
651 if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
653 MSG_RETURN(MSG_HOST_FAILURE);
656 MSG_mailbox_set_cond(mailbox,NULL);
660 if(NULL == (t = MSG_mailbox_get_head(mailbox)))
666 *PID = MSG_process_get_PID(t->simdata->sender);
675 MSG_alias_select_from(const char* alias, double timeout, int* PID)
678 simdata_host_t h_simdata = NULL;
682 msg_mailbox_t mailbox;
691 *PID = MSG_task_listen_from(alias);
698 h_simdata = h->simdata;
700 DEBUG2("Probing on alias %s (%s)", alias, h->name);
702 mailbox = MSG_mailbox_get_by_alias(alias);
704 while(MSG_mailbox_is_empty(mailbox))
714 SIMIX_mutex_lock(h_simdata->mutex);
716 xbt_assert1(!MSG_mailbox_get_cond(mailbox),"A process is already blocked on this alias %s",alias);
718 cond = SIMIX_cond_init();
720 MSG_mailbox_set_cond(mailbox, cond);
724 SIMIX_cond_wait_timeout(cond, h_simdata->mutex, timeout);
728 SIMIX_cond_wait(cond, h_simdata->mutex);
731 SIMIX_cond_destroy(cond);
732 SIMIX_mutex_unlock(h_simdata->mutex);
734 if (SIMIX_host_get_state(h_simdata->smx_host) == 0)
736 MSG_RETURN(MSG_HOST_FAILURE);
739 MSG_mailbox_set_cond(mailbox,NULL);
743 if(NULL == (t = MSG_mailbox_get_head(mailbox)))
749 *PID = MSG_process_get_PID(t->simdata->sender);