+2017-02-26 Pádraig Brady <P@draigBrady.com>
+
+ nproc: adjust handling of OpenMP environment variables
+ to match the return value from omp_get_num_threads(), i.e.:
+ - honor OMP_THREAD_LIMIT without OMP_NUM_THREADS
+ - Treat 0 as an invalid value and ignore
+ Also remove the call to omp_get_num_threads() because
+ it's ineffective without the omp pragmas in place.
+ * lib/nproc.c (parse_omp_threads): Return 0 if specified,
+ so that it can be ignored.
+ (num_processors): Honor OMP_THREAD_LIMIT even without
+ OMP_NUM_THREADS being set. Also fix a typo in the environment
+ variable being checked, from the previous recent commit.
+
2017-02-26 Pádraig Brady <P@draigBrady.com>
nproc: support nested OMP_NUM_THREADS, and OMP_THREAD_LIMIT
#include <config.h>
#include "nproc.h"
+#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
# include <windows.h>
#endif
-#if defined(_OPENMP)
-# include <omp.h>
-#endif
-
#include "c-ctype.h"
#include "minmax.h"
}
/* Parse OMP environment variables without dependence on OMP.
- Return > 0 for valid values. */
+ Return 0 for invalid values. */
static unsigned long int
parse_omp_threads (char const* threads)
{
while (*threads != '\0' && c_isspace (*threads))
threads++;
- /* Convert it from decimal to 'unsigned long'. */
+ /* Convert it from positive decimal to 'unsigned long'. */
if (c_isdigit (*threads))
{
char *endptr = NULL;
while (*endptr != '\0' && c_isspace (*endptr))
endptr++;
if (*endptr == '\0')
- return (value > 0 ? value : 1);
+ return value;
/* Also accept the first value in a nesting level,
since we can't determine the nesting level from env vars. */
else if (*endptr == ',')
- return (value > 0 ? value : 1);
+ return value;
}
}
unsigned long int
num_processors (enum nproc_query query)
{
+ unsigned long int omp_env_limit = ULONG_MAX;
+
if (query == NPROC_CURRENT_OVERRIDABLE)
{
unsigned long int omp_env_threads;
- unsigned long int omp_env_limit;
-
-/* If OPENMP is enabled with AC_OPENMP, then use OPENMP to
- get the number of threads for the current nesting level. */
-#if defined(_OPENMP)
- return omp_get_num_threads ();
-#endif
-
/* Honor the OpenMP environment variables, recognized also by all
programs that are based on OpenMP. */
omp_env_threads = parse_omp_threads (getenv ("OMP_NUM_THREADS"));
- if (omp_env_threads)
- {
- omp_env_limit = parse_omp_threads (getenv ("OMP_THREADS_LIMIT"));
- if (omp_env_limit > 1)
- omp_env_threads = MIN (omp_env_threads, omp_env_limit);
+ omp_env_limit = parse_omp_threads (getenv ("OMP_THREAD_LIMIT"));
+ if (! omp_env_limit)
+ omp_env_limit = ULONG_MAX;
- return omp_env_threads;
- }
+ if (omp_env_threads)
+ return MIN (omp_env_threads, omp_env_limit);
query = NPROC_CURRENT;
}
unsigned long nprocs = num_processors_via_affinity_mask ();
if (nprocs > 0)
- return nprocs;
+ return MIN (nprocs, omp_env_limit);
}
#if defined _SC_NPROCESSORS_ONLN
Cygwin, Haiku. */
long int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
if (nprocs > 0)
- return nprocs;
+ return MIN (nprocs, omp_env_limit);
}
#endif
}
if (query == NPROC_CURRENT)
{
if (psd.psd_proc_cnt > 0)
- return psd.psd_proc_cnt;
+ return MIN (psd.psd_proc_cnt, omp_env_limit);
}
else
{
? MP_NAPROCS
: MP_NPROCS);
if (nprocs > 0)
- return nprocs;
+ return MIN (nprocs, omp_env_limit);
}
#endif
if (sysctl (mib, ARRAY_SIZE (mib), &nprocs, &len, NULL, 0) == 0
&& len == sizeof (nprocs)
&& 0 < nprocs)
- return nprocs;
+ return MIN (nprocs, omp_env_limit);
}
#endif
SYSTEM_INFO system_info;
GetSystemInfo (&system_info);
if (0 < system_info.dwNumberOfProcessors)
- return system_info.dwNumberOfProcessors;
+ return MIN (system_info.dwNumberOfProcessors, omp_env_limit);
}
#endif