Chapter 6 Multithreading Support
kaltoa currently supports multithreading for the function fit_clock_drift.
To make use of this, you need to (i) set a precompile flag in your user Makevars, and (ii), specify the desired number of threads to use before running the function.
First, find where Makevars is located with tools::makevars_user().
If you currently don’t have one, you may manually have to create it.
On linux, this is typically found in ~/.R.Makevars.
On windows, you may additionally have to set the environment variable R_MAKEVARS_USER.
Navigate to System Properties > Advanced > Environment Variables > New ... User Variable, and define R_MAKEVARS_USER as C:\Users\<user_name>\.R\Makevars, where you’ve substituted in your correct user name.
Be sure to close and reopen RStudio before trying to install the package again.
Makevars is simply an empty text document you can modify with any text editor.
Add the line TMB_FLAGS += -D TMB_MULTITHREAD.
This will enable the multithreading features while compiling.
When building the package, you will know this worked if you see the flag -D TMB_MULTITHREAD in your compiler output.
See the example output below.
====== Building TMB clocksync models ======
cd ./tmb_clocksync && rm -f *o || true;\
/usr/lib/R/bin/Rscript --vanilla -e "TMB::compile('tmb_clocksync.cpp', '-O0 -g -I.././shared -D TMB_MULTITHREAD')" && \
...
== Finished building TMB clocksync models =
Before running fit_clock_drift, the number of threads needs to manually be set with TMB::openmp().
See the example below.
# Use 10 threads
TMB::openmp(n = 10)
# Run model
model_drift <- fit_clock_drift(
sync_model,
sigma_det = 0.1,# (s) Standard deviation of direct detection error
sigma_drift = 0.1)# (s/h) Standard deviation of hourly clock drift
# Show results
model_drift
plot(model_drift)For large datasets, this can greatly improve processing times.