bioFAM / MOFA

Multi-Omics Factor Analysis
GNU Lesser General Public License v3.0
231 stars 57 forks source link

BLAS libraries use maximum number of available cores. #21

Closed sritchie73 closed 5 years ago

sritchie73 commented 5 years ago

This has come up recently when diagnosing problems with one of the nodes on our cluster. Another user was using this MOFA R package, and that R process had spawned 64 threads even though the user had requested only 1 cpu on the cluster. We were able to solve this problem by restricting the number of threads available to BLAS/OMP.

This issue commonly arises when using matrix algebra code (e.g. SVD), which themselves call lower level matrix algebra libraries (e.g. Open BLAS) - as is the case in R.

I'm not familiar with this R package, but you can solve this problem by adding the following to the relevant R function that uses these libraries (e.g. any calls to svd(), or prcomp()):

library(RhpcBLASctl)  # needs to be added as an Imports: in the DESCRIPTION and NAMESPACE files

mofa <- function(..., nThreads) {
  # This code explicitly allows the user to control how many threads the matrix algebra calls can use,
  # then restores the previous state:
  oldOMPThreads <- omp_get_max_threads()
  oldBLASThreads <- blas_get_num_procs()

  omp_set_num_threads(nThreads)
  blas_set_num_threads(nThreads)

  # Restore to previous state when the function exits (either successfully or on error)
  on.exit({
    omp_set_num_threads(oldOMPThreads)
    blas_set_num_threads(oldBLASThreads)
  }, add=TRUE)
}
QinqinHuang commented 5 years ago

Hi Scott, FYI it’s the runMOFA() function.

rargelaguet commented 5 years ago

Hi Scott, thanks for posting the function, I will include it in the next release.