
Using rjuliabugs in parallel
rjuliabugs_parallel.Rmd
The JuliaBUGS
library in Julia supports parallel
sampling through AbstractMCMC
. While in a native Julia
session this can be easily enabled by launching Julia with the
-t <n_threads>
flag, using multithreaded sampling via
rjuliabugs
requires additional setup. Specifically, you
must define the environment variable
JULIA_NUM_THREADS=n_threads
, where n_threads
is the number of threads (or CPU cores) you wish to use for parallel
computation. This ensures that rjuliabugs
can properly
initialize Julia with multithreading support when running multiple
chains in parallel from within an R session. The instructions on how to
configure JULIA_NUM_THREADS
and enable multithreading in
your environment, are the following:
- Open your terminal or command prompt.
- Check what is set as
JULIA_NUM_THREADS
is set by running:
(On Windows PowerShell use:
echo $Env:JULIA_NUM_THREADS
)
- If empty or incorrectly set, decide how many threads you want Julia to use (e.g., 4), then set the environment variable in your terminal session:
(On Windows PowerShell use:
$Env:JULIA_NUM_THREADS="4"
)
-
To make this setting permanent (applies every time you open R and Julia):
-
On macOS/Linux:
Open your shell configuration file (e.g.,.bashrc
,.zshrc
) with a text editor:or
Add the following line at the end of the file:
Save and exit (
Ctrl+O
,Enter
,Ctrl+X
in nano).
Then reload your shell configuration or restart your terminal:(or
source ~/.zshrc
if using zsh) -
On Windows:
- Search for “Environment Variables” in the Start menu and open “Edit
the system environment variables.”
- Click “Environment Variables.”
- Under “User variables” or “System variables,” click “New…”
- Set the variable name as
JULIA_NUM_THREADS
and the value as the number of threads (e.g.,4
).
- Click OK to save all dialogs.
- Restart your terminal or R session for changes to take effect.
- Search for “Environment Variables” in the Start menu and open “Edit
the system environment variables.”
-
Verify it is set correctly by re-running:
(or in R use:
Sys.getenv("JULIA_NUM_THREADS")
)
Setting JULIA_NUM_THREADS
ensures Julia can execute in
parallel where possible when running models through
rjuliabugs
.