R foreach not using multiple cores - r

I have a case where foreach using doMC as a backend produces different behaviors on different machines.
On a linux server running Ubuntu 12.04.4 LTS the following code (adapted from the foreach vingette) runs 5 jobs simultaneously on a single core, which is not the desired behavior.
library(foreach)
library(doMC)
registerDoMC(cores=5)
getDoParWorkers()
x <- iris[which(iris[,5] != "setosa"), c(1,5)]
trials <- 10000
r <- foreach(icount(trials), .combine=cbind) %dopar% {
ind <- sample(100, 100, replace=TRUE)
result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit))
coefficients(result1)
}
Session info:
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C LC_COLLATE=C LC_MONETARY=C
[6] LC_MESSAGES=C LC_PAPER=C LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=C LC_IDENTIFICATION=C
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] doMC_1.3.3 iterators_1.0.7 foreach_1.4.2
loaded via a namespace (and not attached):
[1] codetools_0.2-8 compiler_3.1.0 tools_3.1.0
The same code run on a Mac running OSX 10.7.5 produces the desired and expected behavior of running 5 jobs on 5 different cores.
Session info:
> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] C
attached base packages:
[1] parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] doMC_1.3.2 iterators_1.0.6 foreach_1.4.1
loaded via a namespace (and not attached):
[1] codetools_0.2-8 compiler_3.0.1 tools_3.0.1
I have also observed the same behavior using other parallel backends. Both machines have 20+ cores. Any ideas on what's going on?

The issue was caused by OpenBLAS. Switching to ATLAS solved the problem. The recipe for switching between BLAS libraries in Linux is on Nathan VanHoudnos's blog:
Switching between BLAS libraries
Now we can switch between the different BLAS options that are installed:
sudo update-alternatives --config libblas.so.3gf
There are 3 choices for the alternative libblas.so.3gf (providing /usr/lib/libblas.so.3gf).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/openblas-base/libopenblas.so.0 40 auto mode
1 /usr/lib/atlas-base/atlas/libblas.so.3gf 35 manual mode
2 /usr/lib/libblas/libblas.so.3gf 10 manual mode
3 /usr/lib/openblas-base/libopenblas.so.0 40 manual mode
Press enter to keep the current choice[*], or type selection number:
Side note: If the above returned:
update-alternatives: error: no alternatives for libblas.so.3gf
Try
sudo update-alternatives --config libblas.so.3

Related

How fix character encoding in R plots?

Recently, I installed R with anaconda. Apparently, everything works fine. However, when I plot something strange boxes with numbers appear instead of the letters and numbers.
I think that I have a problem with character encoding.
How can I fix this problem?
I attach a sample plot with my problem.
my sessionInfo() output is:
R version 3.6.1 (2019-07-05)
Platform: x86_64-conda_cos6-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS
Matrix products: default
BLAS/LAPACK: /home/marco/anaconda3/envs/Rstudio/lib/R/lib/libRblas.so
locale:
[1] LC_CTYPE=es_MX.UTF-8 LC_NUMERIC=C
LC_TIME=es_MX.UTF-8 LC_COLLATE=es_MX.UTF-8
[5] LC_MONETARY=es_MX.UTF-8 LC_MESSAGES=es_MX.UTF-8
LC_PAPER=es_MX.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
LC_MEASUREMENT=es_MX.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] geometry_0.4.5 QuantumPhaseEstimation_0.1.0
loaded via a namespace (and not attached):
[1] compiler_3.6.1 rsconnect_0.8.17 tools_3.6.1 abind_1.4-5 yaml_2.2.1 Rcpp_1.0.6 pracma_2.3.3
[8] magic_1.5-9
Also, I found that I can not install Cairo library
checking for ATS font support in Cairo... no
configure: CAIRO_LIBS=-lcairo
checking for library containing deflate... -lz
checking whether Cairo programs can be compiled... configure: error: Cannot compile a simple Cairo program. See config.log for details.
ERROR: configuration failed for package ‘Cairo’

Why is solve using all cores? [duplicate]

I have a question regarding the use of BLAS parallelized matrix product in R (being the default matrix product at least since R-3.4, maybe earlier).
The default behavior (at least on my machine) is now for the matrix product (c.f. example below) to use all the cores available on the machine, which can be a problem.
Do you know how to control the number of cores used for standard matrix product in R?
Thanks in advance
Example:
n=10000
p=1000
q=5000
A = matrix(runif(n*p),nrow=n, ncol=p)
B = matrix(runif(p*q),nrow=p, ncol=q)
C = A %*% B # multi-threaded matrix product
Session info:
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.3 LTS
Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so
locale:
[1] LC_CTYPE=fr_FR.utf8 LC_NUMERIC=C
[3] LC_TIME=fr_FR.utf8 LC_COLLATE=fr_FR.utf8
[5] LC_MONETARY=fr_FR.utf8 LC_MESSAGES=fr_FR.utf8
[7] LC_PAPER=fr_FR.utf8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=fr_FR.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.1
The package RhpcBLASctl does just that.
From its DESCRIPTION:
Control the number of threads on 'BLAS' (Aka 'GotoBLAS', 'ACML' and 'MKL'). and possible to control the number of threads in 'OpenMP'. get a number of logical cores and physical cores if feasible.
We mention it in the CRAN Task View on HPC.

How to successfully call C code on super computer using ucminf package in R

I am trying to run the some unconstrained optimization on a large problem on
a super computer, so I am trying to use the ucminf (although this problem
also works if I use the ucminf option in optimx). When I run any simple
optimization, I get the following message:
Error in .Call("mfopt", fnstr, grstr, rho, PACKAGE = "ucminf") :
"mfopt" not available for .Call() for package "ucminf"
For simplicity, here is a simple bit of test code that gives me the error.
library(ucminf)
test<-function(x){
(x-3)^2}
ucminf(0,test)
All of this works fine on my personal computer, but does not work on the
super computer. I have tried this on R/3.5.0 and R/3.3.0 on the super
computer and they both give me the same error.
Here is my Session info.
R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS release 6.9 (Final)
Matrix products: default
BLAS: /usr/public/R/3.5.0.gnu/lib64/R/lib/libRblas.so
LAPACK: /usr/public/R/3.5.0.gnu/lib64/R/lib/libRlapack.so
locale:
[1] LC_CTYPE=en_US.iso885915 LC_NUMERIC=C
[3] LC_TIME=en_US.iso885915 LC_COLLATE=en_US.iso885915
[5] LC_MONETARY=en_US.iso885915 LC_MESSAGES=en_US.iso885915
[7] LC_PAPER=en_US.iso885915 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.iso885915 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ucminf_1.0-5 numDeriv_2016.8-1
loaded via a namespace (and not attached):
[1] compiler_3.5.0
I have uninstalled and reinstalled the package using Rinstall('ucminf'). Does anyone have experience with this and how to fix it?

Why "solve" function produces segmentation fault with OpenStreetMap package in R?

I was using the spatsurv package in R and when compiling my code using R in the terminal on Debian GNU/Linux 8 (jessie), segmentation fault was returned and R was closed. After some debugging, I found out that the problem appears when using solve function. In addition, one of the authors of spatsurv package suggested me to test the same problem with each imported package. Then, after making that test, the problem was only with OpenStreetMap package.
library(OpenStreetMap)
x <- solve(diag(1, 1000))
# Segmentation fault
If I use the solve function before loading OpenStreetMap, I get the following:
x <- solve(diag(1, 1000))
library(OpenStreetMap)
x <- solve(diag(1, 1000))
# An irrecoverable stack overflow has occurred.
# Please check if any of your loaded .so files has enabled executable stack (see man page execstack(8))
# Error: segfault from C stack overflow
Information of my clean session is below:
R version 3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 8 (jessie)
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
I would like to know how to solve this segmentation fault to be able to use spatsurv package.

difficulties with installing/using Cairo / R 2.12.1

I have compiled R version 2.12.1 (2010-12-16) for my system (CentOS), and afterwards I installed Cairo_1.4-5.
I'd like use Cairo to produce PNG (and maybe PDF) output of my graphs when I batch-invoke my scripts, but this gives me difficulties when I am using X (I am tunneling X through ssh and it often disconnects while the R scripts are running...), so this is why I want to use Cairo.
(I do not have root access btw).
Although it's installed according to sessionInfo
> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
I can not call CairoPDF:
> CairoPDF()
Error: could not find function "CairoPDF"
Weirdly, I can invoke a function called cairo_pdf
> cairo_pdf()
>
I can not call CairoPNG, cairo_PNG or cairo_png:
> CairoPNG()
Error: could not find function "CairoPNG"
> cairo_PNG()
Error: could not find function "cairo_PNG"
> cairo_png()
Error: could not find function "cairo_png"
Is something wrong withmy installation? Frankly I have no idea how to proceed from here, why can't I even call CairoPNG()?
On Ubuntu I have:
> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: i486-pc-linux-gnu (32-bit)
locale:
[1] LC_CTYPE=af_ZA.utf8 LC_NUMERIC=C
[3] LC_TIME=af_ZA.utf8 LC_COLLATE=af_ZA.utf8
[5] LC_MONETARY=C LC_MESSAGES=af_ZA.utf8
[7] LC_PAPER=af_ZA.utf8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=af_ZA.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Then run library(Cairo), if this is not installed
library(Cairo)
Error in library(Cairo) : there is no package called 'Cairo'
This means you will have to install the Cairo R package, which interfaces with the Cairo graphics system
> install.packages('Cairo')
It will download, build and install the package - you don't need root for this
If it was successful, you can run
>library(Cairo)
>sessionInfo()
R version 2.12.1 (2010-12-16)<br>
Platform: i486-pc-linux-gnu (32-bit)
locale:
[1] LC_CTYPE=af_ZA.utf8 LC_NUMERIC=C
[3] LC_TIME=af_ZA.utf8 LC_COLLATE=af_ZA.utf8
[5] LC_MONETARY=C LC_MESSAGES=af_ZA.utf8
[7] LC_PAPER=af_ZA.utf8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=af_ZA.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Cairo_1.4-5
HTH
This should do:
install.packages("Cairo")
library(Cairo)

Resources