Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/node_modules/@stdlib/utils/parallel/lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// MODULES //

var numCPUs = require( '@stdlib/os/num-cpus' );
var max = require( '@stdlib/math/base/special/fast/max' );


// MAIN //
Expand All @@ -36,12 +37,17 @@ var numCPUs = require( '@stdlib/os/num-cpus' );
* // returns {...}
*/
function defaults() {
var n;

// Accommodate single-core systems by ensuring at least 1 worker and 1 concurrent script:
n = max( 1, numCPUs - 1 );

return {
// Number of workers:
'workers': numCPUs - 1,
'workers': n,

// Number of scripts to execute concurrently:
'concurrency': numCPUs - 1,
'concurrency': n,

// Executable file/command:
'cmd': 'node',
Expand Down
6 changes: 3 additions & 3 deletions lib/node_modules/@stdlib/utils/parallel/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ function parallel() {
if ( !isFunction( clbk ) ) {
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) );
}
// Prevent the number of concurrent scripts exceeding the number of actual scripts to run.
// Prevent the number of concurrent scripts exceeding the number of actual scripts to run:
if ( opts.concurrency > files.length ) {
opts.concurrency = files.length;
opts.concurrency = files.length || 1;
}
// Prevent the number of workers exceeding the number of concurrent scripts (excess capacity), as some workers would never be allocated scripts to run and always be idle.
// Prevent the number of workers exceeding the number of concurrent scripts (excess capacity), as some workers would never be allocated scripts to run and always be idle:
if ( opts.workers > opts.concurrency ) {
opts.workers = opts.concurrency;
}
Expand Down
39 changes: 39 additions & 0 deletions lib/node_modules/@stdlib/utils/parallel/test/test.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,45 @@ tape( 'if the number of workers is greater than the concurrency, the function se
}
});

tape( 'the function ensures at least one worker and a concurrency greater than or equal to unity', function test( t ) {
var parallel;

parallel = proxyquire( './../lib/main.js', {
'./node': exec,
'./defaults.js': mockDefaults
});

parallel( files(), done );

function mockDefaults() {
return {
'workers': 1,
'concurrency': 1,
'cmd': 'node',
'ordered': false,
'uid': null,
'gid': null,
'encoding': 'buffer',
'maxBuffer': 200 * 1024 * 1024
};
}

function done( error ) {
if ( error ) {
t.ok( false, error.message );
} else {
t.ok( true, 'runs scripts successfully' );
}
t.end();
}

function exec( files, opts, clbk ) {
t.strictEqual( opts.workers, 1, 'has at least 1 worker' );
t.strictEqual( opts.concurrency, 1, 'has at least 1 concurrency' );
clbk();
}
});

tape( 'the function runs scripts in parallel', function test( t ) {
parallel( files(), done );
function done( error ) {
Expand Down