From 2a5bd78fdce41ae97131d7642a4214f50113cc78 Mon Sep 17 00:00:00 2001 From: Craig Williams Date: Thu, 25 Feb 2021 17:01:13 +0000 Subject: [PATCH] --- Ellis-GPU-Worker.md | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Ellis-GPU-Worker.md diff --git a/Ellis-GPU-Worker.md b/Ellis-GPU-Worker.md new file mode 100644 index 0000000..816de81 --- /dev/null +++ b/Ellis-GPU-Worker.md @@ -0,0 +1,54 @@ +```js +const threads = require('worker_threads') +const child_process = require('child_process') + +// Worker task info +/* maxParallel: [0,1,n] 0 = auto (usually nproc), 1 = single instance, + * n = max 'n' instances + * shareParallel: [true/false] indicates if the worker can be run in parallel + * with other workers. + * usesGPU: [true/false] indicates if the worker requires the GPU + */ +const taskInfo = { + maxParallel: 0, + shareParallel: true, + usesGPU: true +} + +// The task to be run +const task = function() { + threads.parentPort.postMessage('Running R worker') + + const scenId = threads.workerData.id + + const rWorker = child_process.exec('/usr/bin/R ' + [ + '--vanilla', + '--quiet', + '-e', + '"source(\'coreScript.R\'); res <- runEllis(' + scenId + ',T,T); q(status = res)"' + ].join(" "), { + cwd: '../EllisCalc' + }) + // TODO: provide a way of clearing stdout/stderr + rWorker.stdout.pipe(process.stdout) + rWorker.stderr.pipe(process.stderr) + + new Promise((resolve, reject) => { + rWorker.on('close', (code) => { + threads.parentPort.postMessage('Worker finished') + process.exit(code) + resolve(code) + }) + }) +} + +if (!module.parent) { + if (!threads.isMainThread) { + task() + } +} else { + module.exports = taskInfo + module.task = task +} + +``` \ No newline at end of file