R/ctools.R

#Define the main configuration files

#' Configure options for cores directly
#'
#' ctools sets the default cores number to the maximum possible,
#' this function allows the user to change the number of cores used
#' in local-only parallel, i.e. PSOCKS clusters and FORK parallel.
#' NOTE: Changing cores for PSOCKS, used exclusively on Windows machines,
#' restarts the cluster, thus all exported objects are lost. Thus, it's
#' recommended that this function be used shortly after loading the ctools
#' package. Unix-like machines, e.g. Linux or OSX, use forking, so the core
#' number can be changed at any time
#' @param cores the number of cores to assign, defaults to maximum number.
#' @export c.config
#' @examples
#' c.config(cores = 2)		# Set cores number to 2
c.config <- function(cores = get("maxcores", envir = cluster)) {

	if (get("use.mpi", envir = cluster)) {
		message("Only able to change cores available to FORK and PSOCKS clusters.", appendLF = T)
	} else if (get("use.cluster", envir = cluster)) {
		# Stop the current cluster
		cluster$cluster.done(get("cluster.object", envir = cluster))
		# Restart the cluster with the desired number of cores
		use.PSOCKS(cores)
	} else {
		use.FORK(cores)
	}

}

#' Portable package loader
#'
#' If utilizing FORK style parallel, load all packages in the
#' package list into the main R process. If utilizing a cluster, additionally
#' load the packages into all cluster nodes.
#' @param ... quoted names of packages
#' @export c.library
#' @examples
#' c.libary("ggplot2","reshape2")
c.library <- function(...) {

	if (get("use.cluster", envir = cluster)) {
		cl.call <- get("cluster.call", envir = cluster)
		cl <- get("cluster.object", envir = cluster)
		capture <- lapply(list(...),require, character.only = TRUE, quietly = TRUE)
		capture <- cl.call(cl, function(...) {
									lapply(list(...),require, character.only = TRUE, quietly = TRUE)
								}, ...)
	} else {
		capture <- lapply(list(...),require, character.only = TRUE, quietly = TRUE)
	}

}

#' Return the number of cores configured for use
#'
#' This function returns the number of cores configured for use with ctools
#' @export c.cores
#' @examples
#' cores <- c.cores()		# Return the number of cores in use
c.cores <- function() {
	get("cores", envir = cluster)
}

#' Return the maximum number of cores possible to use
#'
#' This function returns the maximum number of cores possible to use with ctools
#' @export c.maxcores
#' @examples
#' maxcores <- c.maxcores()		# Return the maximum number of cores available
c.maxcores <- function() {
	get("maxcores", envir = cluster)
}

#' Return the number of cores configured for use
#'
#' Set seet for random number generators.
#' @export c.set.seed
#' @examples
#' c.set.seed(42) # set the seed on all workers
c.set.seed <- function(seed) {

	if (get("use.mpi", envir = cluster)) {
		cl <- get("cluster.object", envir = cluster)
		cl.seed <- get("cluster.seeder", envir = cluster)
		cl.seed(cl, seed)
	} else if (get("use.cluster", envir = cluster)) {
		cl.call <- get("cluster.call", envir = cluster)
		cl <- get("cluster.object", envir = cluster)
		cl.call(cl, set.seed, seed=seed )
	}
	set.seed(seed)
}
bamonroe/ctools documentation built on May 11, 2019, 6:19 p.m.