I'm trying to understand some behavior related to setting environmental variables within an R session.
Context: on computers with multiple cores, Intel MKL can induce data races during (sufficiently large) matrix multiplies. These data races occur depending on the threading model. In particular, on Ubuntu, if you do not set MKL_THREADING_LAYER = "GNU"
(not the default!) you might get data races.
can_induce_data_race <- function() {
X <- matrix(1:500 / 500, 50, 10)
Y <- matrix(1:1000 / 1000, 10, 100)
norm(X %*% Y)
}
Sys.getenv("MKL_THREADING_LAYER")
#> [1] ""
can_induce_data_race()
#> [1] 2997.423
can_induce_data_race()
#> [1] 2986.476
can_induce_data_race()
#> [1] 2757.553
Now, if I start a new R session using callr::r()
, I can both reproduce this issue, and also, by passing, MKL_THREADING_LAYER = "GNU"
, resolve it.
callr::r(can_induce_data_race)
#> [1] 2997.423
callr::r(can_induce_data_race, env = c(MKL_THREADING_LAYER = "GNU"))
#> [1] 249.7852
I was hoping that I could resolve the issue from within my R session, as follows, but it does not seem to work.
callr::r(can_induce_data_race)
#> [1] 2967.369
Sys.setenv(MKL_THREADING_LAYER = "GNU")
Sys.getenv("MKL_THREADING_LAYER")
#> [1] "GNU"
can_induce_data_race()
#> [1] 2997.423
However, using callr::r()
at this point, the data race is eliminated. Further, if I specify MKL_THREADING_LAYER = "GNU"
in my .Renviron
file, the data race is eliminated.
callr::r(can_induce_data_race)
#> [1] 249.7852
callr::r(can_induce_data_race, env = c(MKL_THREADING_LAYER = "GNU"))
#> [1] 249.7852
Why does MKL_THREADING_LAYER = "GNU"
get respected when I specify it in the env
argument to callr::r()
or via .Renviron
, but not when I explicitly set it via Sys.setenv()
?