Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Julia TBB detection to instantiation time #213

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading