bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Two problems with result of num_processors (NPROC_CURRENT_OVERRIDABL


From: Pádraig Brady
Subject: Re: Two problems with result of num_processors (NPROC_CURRENT_OVERRIDABLE)
Date: Mon, 20 Feb 2017 20:59:26 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0

On 28/11/16 06:34, Oliver Heimlich wrote:
> Follow-Up: Savannah bugs #48534 #48535.
> 
> Hi,
> I have two problems with the function num_processors in libgnu's nproc.c.
> 
> 
> FIRST PROBLEM
> 
> num_processors (NPROC_CURRENT_OVERRIDABLE) returns the value of
> OMP_NUM_THREADS. However, if a thread limit is defined, it should return
> the minimum of OMP_NUM_THREADS and OMP_THREAD_LIMIT.
> 
> 
> OMP_THREAD_LIMIT=1 OMP_NUM_THREADS=2 ./run-my-program
> - What I get: 2
> - What I expect: 1
> - Reason: The effective number of threads cannot exceed the thread limit.
> 
> 
> SECOND PROBLEM
> 
> num_processors (NPROC_CURRENT_OVERRIDABLE) parses the environment
> variable OMP_NUM_THREADS for an integer. However, according to OpenMP
> documentation it may be a list of integers. In that case, the function
> ignores the value of OMP_NUM_THREADS. See
> https://gcc.gnu.org/onlinedocs/libgomp/OMP_005fNUM_005fTHREADS.html
> 
> 
> OMP_NUM_THREADS=2,2,1 ./run-my-program
> - What I get: 4
> - What I expect: 2 or 1 depending on the current OpenMP nesting level

There is no way to determine the current nesting level,
without actually using OpenMP, so I added support
for calling omp_get_num_threads() if _OPENMP is defined.
I'm not 100% sure it's valid to do that without depending on
the openmp module (which I think it's best not for nproc to do),
so I'm open to removing that portion.

Also I added support for deferring to the first nesting level
in OMP_NUM_THREADS, and for OMP_THREADS_LIMIT like you suggest.

Tesing coreutils' nproc (without _OPENMP) with this change gives:

$ src/nproc
4
$ OMP_THREADS_LIMIT=1 src/nproc
4
$ OMP_NUM_THREADS=2,2,1 src/nproc
2
$ OMP_NUM_THREADS=2bad src/nproc
4
$ OMP_THREADS_LIMIT=1 OMP_NUM_THREADS=2,2,1 src/nproc
1
$ OMP_THREADS_LIMIT=0 OMP_NUM_THREADS=2,2,1 src/nproc
2
$ OMP_THREADS_LIMIT=1bad OMP_NUM_THREADS=2,2,1 src/nproc
2
$ OMP_THREADS_LIMIT=1b OMP_NUM_THREADS=12,2,1 src/nproc
12
$ OMP_THREADS_LIMIT=1 OMP_NUM_THREADS=12 src/nproc
1
$ OMP_THREADS_LIMIT=111 OMP_NUM_THREADS=12 src/nproc
12
$ OMP_NUM_THREADS=12 nproc
12

cheers,
Pádraig

Attachment: nproc-thread-limit.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]