From: Pádraig Brady
Date: Sun, 26 Feb 2017 14:32:19 +0000 (-0800)
Subject: nproc: adjust handling of OpenMP environment variables
X-Git-Tag: v1.0~6347
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=0481ab52033694e1c137d4911f4d025a186d1a2a;p=gnulib.git
nproc: adjust handling of OpenMP environment variables
Adjust 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()
added in the previous recent commit, 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.
---
diff --git a/ChangeLog b/ChangeLog
index 318b50b76e..7113fb114c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2017-02-26 Pádraig Brady
+
+ 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
nproc: support nested OMP_NUM_THREADS, and OMP_THREAD_LIMIT
diff --git a/lib/nproc.c b/lib/nproc.c
index 3d9007b1aa..db3ca9b3f4 100644
--- a/lib/nproc.c
+++ b/lib/nproc.c
@@ -20,6 +20,7 @@
#include
#include "nproc.h"
+#include
#include
#include
@@ -54,10 +55,6 @@
# include
#endif
-#if defined(_OPENMP)
-# include
-#endif
-
#include "c-ctype.h"
#include "minmax.h"
@@ -203,7 +200,7 @@ num_processors_via_affinity_mask (void)
}
/* 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)
{
@@ -217,7 +214,7 @@ 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;
@@ -228,11 +225,11 @@ parse_omp_threads (char const* threads)
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;
}
}
@@ -242,28 +239,20 @@ parse_omp_threads (char const* threads)
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;
}
@@ -290,7 +279,7 @@ num_processors (enum nproc_query query)
unsigned long nprocs = num_processors_via_affinity_mask ();
if (nprocs > 0)
- return nprocs;
+ return MIN (nprocs, omp_env_limit);
}
#if defined _SC_NPROCESSORS_ONLN
@@ -298,7 +287,7 @@ num_processors (enum nproc_query query)
Cygwin, Haiku. */
long int nprocs = sysconf (_SC_NPROCESSORS_ONLN);
if (nprocs > 0)
- return nprocs;
+ return MIN (nprocs, omp_env_limit);
}
#endif
}
@@ -341,7 +330,7 @@ num_processors (enum nproc_query query)
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
{
@@ -362,7 +351,7 @@ num_processors (enum nproc_query query)
? MP_NAPROCS
: MP_NPROCS);
if (nprocs > 0)
- return nprocs;
+ return MIN (nprocs, omp_env_limit);
}
#endif
@@ -378,7 +367,7 @@ num_processors (enum nproc_query query)
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
@@ -387,7 +376,7 @@ num_processors (enum nproc_query query)
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