It seems to me that this code in sort.c: unsigned long int np2 = num_processors (NPROC_CURRENT_OVERRIDABLE); if (!nthreads || nthreads > np2) nthreads = np2;
is now obsolete. It was written assuming spin locks, but now that we use mutexes, shouldn't we respect an explicit --parallel=N flag? Something like the following, say? This would let the user override the environment in the command line, which is normally what people would expect. >From bbc60da9222e38bb7983464cec35c42ad41f2eb8 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Sat, 11 Dec 2010 01:02:57 -0800 Subject: [PATCH] sort: explicit --parallel=N now overrides environment * NEWS: Document this. * doc/coreutils.texi (sort invocation): Likewise. * src/sort.c (main): Implement it. --- NEWS | 4 ++++ doc/coreutils.texi | 3 +-- src/sort.c | 13 +++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 9f55cbb..d70de31 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,10 @@ GNU coreutils NEWS -*- outline -*- ** New features + sort --parallel=N now respects values of N greater than the number + of processors available, thus overriding any settings in the + OMP_NUM_THREADS environment variable. + split accepts the --number option to generate a specific number of files. diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 34d9ff0..3ecc4d2 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -4128,8 +4128,7 @@ disks and controllers. @opindex --parallel @cindex multithreaded sort Limit the number of sorts run in parallel to @var{n}. By default, -...@var{n} is set to the number of available processors, and values -greater than that are reduced to that limit. Also see +...@var{n} is set to the number of available processors. Also see @ref{nproc invocation}. @item -u diff --git a/src/sort.c b/src/sort.c index 2c0f852..babc437 100644 --- a/src/sort.c +++ b/src/sort.c @@ -4624,14 +4624,15 @@ main (int argc, char **argv) } else { - unsigned long int np2 = num_processors (NPROC_CURRENT_OVERRIDABLE); - if (!nthreads || nthreads > np2) - nthreads = np2; - /* Avoid integer overflow later. */ size_t nthreads_max = SIZE_MAX / (2 * sizeof (struct merge_node)); - if (nthreads_max < nthreads) - nthreads = nthreads_max; + if (nthreads) + nthreads = MIN (nthreads, nthreads_max); + else + { + unsigned long int np2 = num_processors (NPROC_CURRENT_OVERRIDABLE); + nthreads = MIN (np2, nthreads_max); + } sort (files, nfiles, outfile, nthreads); } -- 1.7.2