]> Savannah Git Hosting - gnulib.git/commitdiff
nproc: adjust handling of OpenMP environment variables
authorPádraig Brady <P@draigBrady.com>
Sun, 26 Feb 2017 14:32:19 +0000 (06:32 -0800)
committerPádraig Brady <P@draigBrady.com>
Sun, 26 Feb 2017 15:04:08 +0000 (07:04 -0800)
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.

ChangeLog
lib/nproc.c

index 318b50b76e1b07f7e2ba3c1cbed532dd7e7268a2..7113fb114c6ed0487b92021770443d7262af31c2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+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
index 3d9007b1aaca5fc9ef3e82cd09f5cc86cea34e74..db3ca9b3f47da0efde2167e47231f415979a8627 100644 (file)
@@ -20,6 +20,7 @@
 #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"
@@ -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