Skip to content

Commit

Permalink
Move Julia TBB detection to instantiation time (#213)
Browse files Browse the repository at this point in the history
* Move Julia TBB detection to instantiation time

* R fix
  • Loading branch information
WardBrian authored Feb 20, 2024
1 parent 39d0d8e commit c789e48
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
2 changes: 1 addition & 1 deletion R/R/bridgestan.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ StanModel <- R6::R6Class("StanModel",
}

if (.Platform$OS.type == "windows"){
windows_path_setup()
windows_dll_path_setup()
lib_old <- lib
lib <- paste0(tools::file_path_sans_ext(lib), ".dll")
file.copy(from=lib_old, to=lib)
Expand Down
18 changes: 14 additions & 4 deletions R/R/compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,23 @@ compile_model <- function(stan_file, stanc_args = NULL, make_args = NULL) {
return(output)
}

windows_path_setup <- function() {
if (.Platform$OS.type == "windows") {
suppressWarnings(out <- system2("where.exe", "tbb.dll", stdout = NULL, stderr = NULL))
if (out != 0) {
tbb_found <- function() {
suppressWarnings(out <- system2("where.exe", "tbb.dll", stdout = NULL, stderr = NULL))
return(out == 0)
}

WINDOWS_PATH_SET <- FALSE

windows_dll_path_setup <- function() {
if (.Platform$OS.type == "windows" && !WINDOWS_PATH_SET) {

if (tbb_found()) {
assign("WINDOWS_PATH_SET", TRUE, envir = .GlobalEnv)
} else {
tbb_path <- file.path(get_bridgestan_path(), "stan", "lib", "stan_math",
"lib", "tbb")
Sys.setenv(PATH = paste(tbb_path, Sys.getenv("PATH"), sep = ";"))
assign("WINDOWS_PATH_SET", tbb_found(), envir = .GlobalEnv)
}
}
}
16 changes: 0 additions & 16 deletions julia/src/BridgeStan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,4 @@ function StanModel(;
StanModel(stan_file, data, seed; stanc_args, make_args)
end


function __init__()
# On Windows, we may need to add TBB to %PATH%
if Sys.iswindows()
try
run(pipeline(`where.exe tbb.dll`, stdout = devnull, stderr = devnull))
catch
# add TBB to %PATH%
ENV["PATH"] =
joinpath(get_bridgestan_path(), "stan", "lib", "stan_math", "lib", "tbb") *
";" *
ENV["PATH"]
end
end
end

end
26 changes: 26 additions & 0 deletions julia/src/compile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,29 @@ function compile_model(
end
return output_file
end

WINDOWS_PATH_SET = Ref{Bool}(false)

function tbb_found()
try
run(pipeline(`where.exe tbb.dll`, stdout = devnull, stderr = devnull))
catch
return false
end
return true
end

function windows_dll_path_setup()
if Sys.iswindows() && !(WINDOWS_PATH_SET[])
if tbb_found()
WINDOWS_PATH_SET[] = true
else
# add TBB to %PATH%
ENV["PATH"] =
joinpath(get_bridgestan_path(), "stan", "lib", "stan_math", "lib", "tbb") *
";" *
ENV["PATH"]
WINDOWS_PATH_SET[] = tbb_found()
end
end
end
2 changes: 2 additions & 0 deletions julia/src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ mutable struct StanModel
end
end

windows_dll_path_setup()

lib = Libc.Libdl.dlopen(lib)

err = Ref{Cstring}()
Expand Down

0 comments on commit c789e48

Please sign in to comment.