2004-11-22 03:37:56 +03:00
|
|
|
/*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
|
|
|
|
* All rights reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-11-22 03:37:56 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-10-20 05:03:09 +04:00
|
|
|
#include "ompi_config.h"
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-01-20 03:03:23 +03:00
|
|
|
#ifdef HAVE_SCHED_H
|
2004-12-07 18:39:57 +03:00
|
|
|
#include <sched.h>
|
2005-01-20 03:03:23 +03:00
|
|
|
#endif
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-07-04 01:57:43 +04:00
|
|
|
#include "opal/runtime/opal_progress.h"
|
2005-07-04 03:09:55 +04:00
|
|
|
#include "opal/event/event.h"
|
2005-03-14 23:57:21 +03:00
|
|
|
#include "include/constants.h"
|
2005-07-02 19:54:41 +04:00
|
|
|
#include "mca/base/mca_base_param.h"
|
2004-12-12 18:29:29 +03:00
|
|
|
|
2005-04-21 18:58:25 +04:00
|
|
|
/*
|
|
|
|
* default parameters
|
|
|
|
*/
|
2005-07-04 03:09:55 +04:00
|
|
|
static int opal_progress_event_flag = OPAL_EVLOOP_ONCE;
|
2005-07-04 01:57:43 +04:00
|
|
|
static const int opal_progress_default_tick_rate = 100;
|
2005-03-30 05:40:26 +04:00
|
|
|
|
2005-04-21 18:58:25 +04:00
|
|
|
/*
|
|
|
|
* Local variables
|
|
|
|
*/
|
2005-03-29 01:02:02 +04:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 01:38:51 +04:00
|
|
|
static opal_atomic_lock_t progress_lock;
|
2005-03-29 01:02:02 +04:00
|
|
|
#endif /* OMPI_HAVE_THREAD_SUPPORT */
|
2005-03-30 05:40:26 +04:00
|
|
|
|
2005-04-21 18:58:25 +04:00
|
|
|
/* callbacks to progress */
|
2005-07-04 01:57:43 +04:00
|
|
|
static opal_progress_callback_t *callbacks = NULL;
|
2005-03-14 23:57:21 +03:00
|
|
|
static size_t callbacks_len = 0;
|
|
|
|
static size_t callbacks_size = 0;
|
2004-12-12 18:29:29 +03:00
|
|
|
|
2005-04-21 18:58:25 +04:00
|
|
|
/* do we want to call sched_yield() if nothing happened */
|
2005-03-30 05:40:26 +04:00
|
|
|
static int call_yield = 1;
|
|
|
|
|
2005-04-21 18:58:25 +04:00
|
|
|
/* current count down until we tick the event library */
|
2005-03-30 05:40:26 +04:00
|
|
|
static long event_progress_counter = 0;
|
2005-04-21 18:58:25 +04:00
|
|
|
/* reset value for counter when it hits 0 */
|
2005-03-30 05:40:26 +04:00
|
|
|
static long event_progress_counter_reset = 0;
|
2005-04-21 18:58:25 +04:00
|
|
|
/* users of the event library from MPI cause the tick rate to
|
|
|
|
be every time */
|
|
|
|
static int32_t event_num_mpi_users = 0;
|
2005-03-30 05:40:26 +04:00
|
|
|
|
|
|
|
|
2005-04-14 22:55:53 +04:00
|
|
|
/* init the progress engine - called from orte_init */
|
2005-02-16 20:42:07 +03:00
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_init(void)
|
2005-02-16 20:42:07 +03:00
|
|
|
{
|
2005-03-30 05:40:26 +04:00
|
|
|
/* reentrant issues */
|
2005-03-29 01:02:02 +04:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_init(&progress_lock, OPAL_ATOMIC_UNLOCKED);
|
2005-03-29 01:02:02 +04:00
|
|
|
#endif /* OMPI_HAVE_THREAD_SUPPORT */
|
2005-03-30 05:40:26 +04:00
|
|
|
|
|
|
|
/* always call sched yield when in the rte only... */
|
|
|
|
call_yield = 1;
|
|
|
|
|
|
|
|
event_progress_counter = event_progress_counter_reset = 0;
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_mpi_init(void)
|
2005-04-14 22:55:53 +04:00
|
|
|
{
|
2005-04-21 18:58:25 +04:00
|
|
|
event_num_mpi_users = 0;
|
|
|
|
|
2005-04-14 22:55:53 +04:00
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* turn on MPI optimizations */
|
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_mpi_enable(void)
|
2005-03-30 05:40:26 +04:00
|
|
|
{
|
|
|
|
int param, value;
|
|
|
|
|
2005-04-14 22:55:53 +04:00
|
|
|
/* call sched yield when oversubscribed. */
|
2005-03-30 05:40:26 +04:00
|
|
|
param = mca_base_param_find("mpi", NULL, "yield_when_idle");
|
2005-04-14 22:55:53 +04:00
|
|
|
mca_base_param_lookup_int(param, &value);
|
|
|
|
|
|
|
|
if (value < 0) {
|
2005-06-24 20:59:37 +04:00
|
|
|
/* this should never happen set to 1 if it somehow does */
|
2005-04-14 22:55:53 +04:00
|
|
|
call_yield = 1;
|
|
|
|
} else {
|
|
|
|
call_yield = value;
|
|
|
|
}
|
|
|
|
|
2005-03-30 05:40:26 +04:00
|
|
|
/* set the event tick rate */
|
|
|
|
param = mca_base_param_find("mpi", NULL, "event_tick_rate");
|
|
|
|
mca_base_param_lookup_int(param, &value);
|
|
|
|
|
2005-04-14 22:55:53 +04:00
|
|
|
if (value < 0) {
|
2005-04-21 18:58:25 +04:00
|
|
|
/* user didn't specify - default tick rate */
|
2005-07-04 01:57:43 +04:00
|
|
|
event_progress_counter_reset = opal_progress_default_tick_rate;
|
2005-04-14 22:55:53 +04:00
|
|
|
} else if (value == 0) {
|
2005-04-21 18:58:25 +04:00
|
|
|
/* user specified as never tick - don't count often */
|
2005-03-30 05:40:26 +04:00
|
|
|
event_progress_counter_reset = INT_MAX;
|
|
|
|
} else {
|
|
|
|
/* subtract one so that we can do post-fix subtraction
|
|
|
|
in the inner loop and go faster */
|
|
|
|
event_progress_counter_reset = value - 1;
|
|
|
|
}
|
|
|
|
|
2005-04-21 18:58:25 +04:00
|
|
|
/* it's possible that an init function bumped up our tick rate.
|
|
|
|
* If so, set the event_progress counter to 0. Otherwise, set it to
|
|
|
|
* the reset value */
|
|
|
|
event_progress_counter = (event_num_mpi_users > 0) ?
|
|
|
|
0 : event_progress_counter_reset;
|
2005-03-30 05:40:26 +04:00
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_mpi_disable(void)
|
2005-03-30 05:40:26 +04:00
|
|
|
{
|
|
|
|
/* always call sched yield from here on... */
|
|
|
|
call_yield = 1;
|
|
|
|
|
|
|
|
/* always tick the event library */
|
|
|
|
event_progress_counter = event_progress_counter_reset = 0;
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_finalize(void)
|
2005-03-30 05:40:26 +04:00
|
|
|
{
|
|
|
|
/* don't need to free the progess lock */
|
|
|
|
|
|
|
|
/* free memory associated with the callbacks */
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_lock(&progress_lock);
|
2005-03-30 05:40:26 +04:00
|
|
|
#endif
|
|
|
|
|
2005-03-30 22:03:08 +04:00
|
|
|
if (NULL != callbacks) {
|
|
|
|
free(callbacks);
|
|
|
|
callbacks = NULL;
|
|
|
|
}
|
2005-03-30 05:40:26 +04:00
|
|
|
callbacks_len = 0;
|
|
|
|
callbacks_size = 0;
|
|
|
|
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_unlock(&progress_lock);
|
2005-03-30 05:40:26 +04:00
|
|
|
#endif
|
|
|
|
|
2005-02-16 20:42:07 +03:00
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-03-30 05:40:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_events(int flag)
|
2004-10-28 19:40:46 +04:00
|
|
|
{
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_event_flag = flag;
|
2004-10-28 19:40:46 +04:00
|
|
|
}
|
2004-12-12 18:29:29 +03:00
|
|
|
|
|
|
|
|
2005-04-05 21:58:43 +04:00
|
|
|
/*
|
|
|
|
* Progress the event library and any functions that have registered to
|
|
|
|
* be called. We don't propogate errors from the progress functions,
|
|
|
|
* so no action is taken if they return failures. The functions are
|
|
|
|
* expected to return the number of events progressed, to determine
|
|
|
|
* whether or not we should call sched_yield() during MPI progress.
|
|
|
|
* This is only losely tracked, as an error return can cause the number
|
|
|
|
* of progressed events to appear lower than it actually is. We don't
|
|
|
|
* care, as the cost of that happening is far outweighed by the cost
|
|
|
|
* of the if checks (they were resulting in bad pipe stalling behabior)
|
|
|
|
*/
|
2005-03-30 05:40:26 +04:00
|
|
|
void
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress(void)
|
2004-04-06 20:32:40 +04:00
|
|
|
{
|
2005-03-14 23:57:21 +03:00
|
|
|
size_t i;
|
2005-04-05 21:58:43 +04:00
|
|
|
int events = 0;
|
2005-03-30 05:40:26 +04:00
|
|
|
|
2005-02-16 20:42:07 +03:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
|
|
|
/* NOTE: BWB - XXX - FIXME: Currently, there are some progress functions
|
|
|
|
(the event library, for one) that are not reentrant. It is not known
|
|
|
|
if the PTL progress functions are all reentrant or not. The I/O
|
|
|
|
code should all be reentrant. Because of the uncertainty, we are
|
|
|
|
preventing more than one thread of execution from progressing the
|
2005-07-04 01:57:43 +04:00
|
|
|
via opal_progress (which is usually only called when there are
|
2005-02-16 20:42:07 +03:00
|
|
|
no PROGRESS_THREADS running). This should be made more fine-grained
|
|
|
|
at some point in the future. */
|
2005-07-04 01:38:51 +04:00
|
|
|
if (! opal_atomic_trylock(&progress_lock)) {
|
2005-02-16 20:42:07 +03:00
|
|
|
/* someone is already progressing - return */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if OMPI_ENABLE_PROGRESS_THREADS == 0
|
2005-03-30 05:40:26 +04:00
|
|
|
/* trip the event library if we've reached our tick rate and we are
|
|
|
|
enabled */
|
2005-07-04 01:57:43 +04:00
|
|
|
if (event_progress_counter-- <= 0 && opal_progress_event_flag != 0) {
|
2005-04-21 18:58:25 +04:00
|
|
|
event_progress_counter =
|
2005-08-12 19:20:37 +04:00
|
|
|
(event_num_mpi_users > 0) ? 0 : event_progress_counter_reset;
|
2005-07-04 03:09:55 +04:00
|
|
|
events += opal_event_loop(opal_progress_event_flag);
|
2004-12-12 18:29:29 +03:00
|
|
|
}
|
2004-12-07 18:39:57 +03:00
|
|
|
#endif
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-03-30 05:40:26 +04:00
|
|
|
/* progress all registered callbacks */
|
2005-03-14 23:57:21 +03:00
|
|
|
for (i = 0 ; i < callbacks_len ; ++i) {
|
2005-04-05 21:58:43 +04:00
|
|
|
#if OMPI_ENABLE_DEBUG
|
2005-03-18 06:43:59 +03:00
|
|
|
if (NULL != callbacks[i]) {
|
2005-04-05 21:58:43 +04:00
|
|
|
#endif
|
|
|
|
events += (callbacks[i])();
|
|
|
|
#if OMPI_ENABLE_DEBUG
|
|
|
|
} else {
|
2005-07-04 03:31:27 +04:00
|
|
|
opal_output(0, "WARNING: ompi_progess attempted to call NULL"
|
2005-04-05 21:58:43 +04:00
|
|
|
" function at line %d, index %d.", __LINE__, i);
|
|
|
|
}
|
|
|
|
#endif
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
|
2005-02-23 11:21:02 +03:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-02-16 20:42:07 +03:00
|
|
|
/* release the lock before yielding, for obvious reasons */
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_unlock(&progress_lock);
|
2005-02-23 11:21:02 +03:00
|
|
|
#endif /* OMPI_HAVE_THREAD_SUPPORT */
|
2005-02-16 20:42:07 +03:00
|
|
|
|
2005-08-12 19:21:59 +04:00
|
|
|
#if !defined(WIN32) && defined(HAVE_SCHED_YIELD)
|
2005-06-13 23:32:52 +04:00
|
|
|
if (call_yield && events <= 0) {
|
2005-04-21 18:58:25 +04:00
|
|
|
/* If there is nothing to do - yield the processor - otherwise
|
2005-03-30 05:40:26 +04:00
|
|
|
* we could consume the processor for the entire time slice. If
|
|
|
|
* the processor is oversubscribed - this will result in a best-case
|
|
|
|
* latency equivalent to the time-slice.
|
|
|
|
*/
|
2005-01-20 03:03:23 +03:00
|
|
|
/* TODO: Find the windows equivalent for this */
|
2005-03-29 01:03:24 +04:00
|
|
|
sched_yield();
|
2004-12-12 18:29:29 +03:00
|
|
|
}
|
2005-08-12 19:21:59 +04:00
|
|
|
#endif
|
2004-04-06 20:32:40 +04:00
|
|
|
}
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_register(opal_progress_callback_t cb)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
2005-03-30 05:40:26 +04:00
|
|
|
int ret = OMPI_SUCCESS;
|
|
|
|
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_lock(&progress_lock);
|
2005-03-30 05:40:26 +04:00
|
|
|
#endif
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
/* see if we need to allocate more space */
|
|
|
|
if (callbacks_len + 1 > callbacks_size) {
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_callback_t *tmp;
|
|
|
|
tmp = realloc(callbacks, sizeof(opal_progress_callback_t) * (callbacks_size + 4));
|
2005-03-30 05:40:26 +04:00
|
|
|
if (tmp == NULL) {
|
|
|
|
ret = OMPI_ERR_TEMP_OUT_OF_RESOURCE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
callbacks = tmp;
|
|
|
|
callbacks_size += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
callbacks[callbacks_len++] = cb;
|
|
|
|
|
2005-03-30 05:40:26 +04:00
|
|
|
cleanup:
|
|
|
|
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_unlock(&progress_lock);
|
2005-03-30 05:40:26 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
2005-03-18 06:43:59 +03:00
|
|
|
|
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_unregister(opal_progress_callback_t cb)
|
2005-03-18 06:43:59 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
2005-04-15 00:10:27 +04:00
|
|
|
int ret = OMPI_ERR_NOT_FOUND;
|
2005-03-30 05:40:26 +04:00
|
|
|
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_lock(&progress_lock);
|
2005-03-30 05:40:26 +04:00
|
|
|
#endif
|
2005-03-18 06:43:59 +03:00
|
|
|
|
|
|
|
for (i = 0 ; i < callbacks_len ; ++i) {
|
|
|
|
if (cb == callbacks[i]) {
|
|
|
|
callbacks[i] = NULL;
|
2005-03-30 05:40:26 +04:00
|
|
|
ret = OMPI_SUCCESS;
|
2005-04-05 21:58:43 +04:00
|
|
|
break;
|
2005-03-18 06:43:59 +03:00
|
|
|
}
|
|
|
|
}
|
2005-04-05 21:58:43 +04:00
|
|
|
|
2005-04-15 00:10:27 +04:00
|
|
|
/* If we found the function we're unregistering: If callbacks_len
|
|
|
|
is 0, we're not goig to do anything interesting anyway, so
|
|
|
|
skip. If callbacks_len is 1, it will soon be 0, so no need to
|
|
|
|
do any repacking. size_t can be unsigned, so 0 - 1 is bad for
|
|
|
|
a loop condition :). */
|
|
|
|
if (OMPI_SUCCESS == ret) {
|
|
|
|
if (callbacks_len > 1 ) {
|
|
|
|
/* now tightly pack the array */
|
|
|
|
for ( ; i < callbacks_len - 1 ; ++i) {
|
|
|
|
callbacks[i] = callbacks[i + 1];
|
|
|
|
}
|
2005-04-07 00:17:45 +04:00
|
|
|
}
|
2005-04-15 00:10:27 +04:00
|
|
|
callbacks[callbacks_len - 1] = NULL;
|
|
|
|
callbacks_len--;
|
2005-04-05 21:58:43 +04:00
|
|
|
}
|
2005-03-30 05:40:26 +04:00
|
|
|
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_unlock(&progress_lock);
|
2005-03-30 05:40:26 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2005-03-18 06:43:59 +03:00
|
|
|
}
|
2005-04-21 18:58:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_event_increment()
|
2005-04-21 18:58:25 +04:00
|
|
|
{
|
|
|
|
int32_t val;
|
2005-07-04 01:38:51 +04:00
|
|
|
val = opal_atomic_add_32(&event_num_mpi_users, 1);
|
2005-04-21 18:58:25 +04:00
|
|
|
/* always reset the tick rate - can't hurt */
|
|
|
|
event_progress_counter = 0;
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress_event_decrement()
|
2005-04-21 18:58:25 +04:00
|
|
|
{
|
|
|
|
int32_t val;
|
2005-07-04 01:38:51 +04:00
|
|
|
val = opal_atomic_sub_32(&event_num_mpi_users, 1);
|
2005-04-21 18:58:25 +04:00
|
|
|
if (val >= 0) {
|
|
|
|
event_progress_counter = event_progress_counter_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|