R: Parallel optimization DEoptim - number of cores
I am trying to use an optimization package DEoptim
in R with a constant optimization problem, and since my cost function is taking a long time to evaluate (2 min), I am trying to use parallel computation. My questions:
-
What's the difference between parameters
paralleltype=1
(parallel
) and2
(foreach
)? When to use one of the two? -
Is it possible to specify the number of cores with
paralleltype=1
so as not to take all available cores for calculation (for example, 50 cores on 64 cores)?
source to share
ads. 1 - This is explained in the package documentation.
ads. 2 - It takes some fiddling with two functions: DEoptim.control and DEoptim
First, add a variable - say limitCores - to the DEoptim.control function that controls the parameters for executing DE optimization.
Second, modify the DEoptim wrapper function to act on the limit set by limitCores.
if (ctrl$parallelType == 1) {
if (!is.na(ctrl$limitCores)) {
if (ctrl$limitCores<1) useCores <- round(parallel::detectCores()*ctrl$limitCores) else useCores <- ctrl$limitCores
cl <- parallel::makeCluster(parallel::detectCores())
} else {
cl <- parallel::makeCluster(parallel::detectCores())
}
Full code: http://pastebin.com/NumDx4ae
source to share