R/parallelization.R

Defines functions parallelStartAuto

Documented in parallelStartAuto

#' Automatically set `mode` and `cpus` for `parallelStart` 
#' given the operating system
#' @param flag logical indicating if to use parallelization or not
#' @param cpus number of cpus to use
#' @param ... additional parameters for `parallelStart`
#' @export
parallelStartAuto = function(flag = FALSE, cpus = NA_integer_, ...) {
    
    os = .Platform$OS.type
    
    if ( isTRUE(flag) ) {
        mode = switch(os,
                      unix = 'multicore',
                      windows = 'socket')
    } else if ( isFALSE(flag) ) {
        mode = 'local'
        if ( !is.na(cpus) ) {
            warning('setting cpus = NA')
        }
        cpus = NA_integer_
    } else {
        stop('invalid flag')
    }
    
    if ( (mode == 'local') && !is.na(cpus) ) {
        stop('invalid value for `cpus`')
    }
    
    parallelMap::parallelStart(mode, cpus, ...)
    
}
unoe/noe documentation built on Nov. 5, 2019, 11:05 a.m.