54
Ellis-GPU-Worker.md
Normal file
54
Ellis-GPU-Worker.md
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user