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: Sun, 26 Feb 2017 07:01:59 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0

On 20/02/17 20:59, Pádraig Brady wrote:
> 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.

I pushed that and the attached follow up.

The result was tested in coreutils with this matrix:
avail=$(nproc)

 OMP_THREAD_LIMIT       OMP_NUM_THREADS     RESULT
----------------------------------------------------
 -                      -                   $avail
 1                      -                   1
 1                      0                   1
 -                      0                   $avail
 -                      2,2,1               2
 -                      2,ignored           2
 -                      2bad                $avail
 -                      -2                  $avail
 1                      2,2,1               1
 0                      2,2,1               2
 1bad                   2,2,1               2
 1bad                   $(($avail+1)),2,1   $(($avail+1))
 1                      $(($avail+1))       1
 $(($avail+2))          $(($avail+1))       $(($avail+1))
 $(($avail+1))          $(($avail+2))       $(($avail+1))
 -                      $(($avail+1))       $(($avail+1))

cheers,
Pádraig

Attachment: gnulib-nproc.patch
Description: Text Data


reply via email to

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