Clone
2
Ellis GPU Worker
Craig Williams edited this page 2021-02-25 17:03:29 +00:00
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("no", 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
}