[Toybox] Working on nproc-do we check OMP_NUM_THREADS ?

Isaac Dunham ibid.ag at gmail.com
Sat Nov 29 20:36:32 PST 2014


On Sat, Nov 29, 2014 at 09:31:54PM -0600, Rob Landley wrote:
> On 11/29/14 19:52, Isaac Dunham wrote:
> > Hello,
> > I noticed that nproc was on the TODO list now and started writing it.
> > I've gotten it *almost* ready, but there's one difference between current 
> > behavior and GNU nproc: 
> > 
> > GNU nproc without --all will treat OMP_NUM_THREADS as the number of CPUs,
> > interpreting 0x as hex and all other numbers as decimal.
> >
> > The number of cpus to ignore is then subtracted, and the result printed
> > in decimal.
> 
> Does the taskset info work in here at all? If your task is currently
> restricted to a subset of the available processors, does this still
> return the number the OS has for the whole system?

No taskset/sysconf calls are made at all if --all is not passed and 
OMP_NUM_THREADS is set; nproc blindly takes OMP_NUM_THREADS as the 
number of CPUs available.

Pseudocode to clarify:
if (option --all) {
  cpus=sysconf(_SC_NPROCESSORS_CONF)
} else {
  if (getenv(OMP_NUM_THREADS) {
    cpus = getenv(OMP_NUM_THREADS)
  } else {
    cpus=sysconf(_SC_NPROCESSORS_ONLN) //gnu uses __sched_cpucount directly
  }
}
if (option --ignore) {
  cpus -= ignorecpus
}
print cpus


It seems that nproc without --all is intended more as "max number of 
threads that we expect to see used if someone's using threads for 
performance" than as a number of processors or cores.

> > Currently, I'm ignoring OMP_NUM_THREADS, mainly because I haven't written
> > the code yet. If we should check it, I'd be happy to write the code.
> > Current code is attached.
> > 
> > OMP_NUM_THREADS is an environment variable that OpenMP uses to check 
> > how many threads to start; it overrides a check for the number of CPUs
> > in OpenMP-based software.
> 
> The first google hit for OMP_NUM_THREADS is the Microsoft Developer
> Network, and the second says GNU in the title, and between the two I'm
> pretty _happy_ not to support it.

It's part of OpenMP, which is a compiler feature, so of course those 
would be the first two hits: MSVC and GCC are the two most prominent
C compilers. 
LLVM/Clang just recently got OpenMP support, so they're not a major
source of documentation on it; but they use that variable too.
And IBM's compilers, Cray's compilers, the PathScale compilers, 
PGI, and Sun Studio apparently also ship runtimes using this variable.

But if you'd like to have nproc not check it, the code I sent last time 
is ready to go in.

Thanks,
Isaac Dunham

 1417322192.0


More information about the Toybox mailing list