- Notifications
You must be signed in to change notification settings - Fork6
R-friendly threading in C++
License
tnagler/RcppThread
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Provides R-friendly threading functionality:
- thread safe versions ofRcpp's
checkUserInterrupt(),Rcout, andRcerr, - an interruptible thread class that otherwise behaves like
std::thread, - classes for thethread poolpattern and parallel for loopsfor easy and flexible parallelism,
- thread safe progress tracking,
- state-of-the art speed, seebenchmarks.
The library is header-only, platform-independent, and onlyrequires aC++11-compatible compiler.
For a detailed description of its functionality and examples, see the associatedJSS paperor theAPI documentation.
Since then, the followingnew features have been added:
Printing to the error stream with
Rcerr.Free-standing functions like
parallelFor()now dispatchto a global thread pool that persists for the entire session. Thissignificantly speeds up programs that repeatedly call these functions.Faster runtimes due to lock-free work stealing queue and loops (fromquickpool).
Option to resize a thread pool.
An R function
RcppThread::detectCores()to determine the number of (logical)cores on your machine.C++ classes
ProgressCounterandProgressBarfor tracking progress inlong-running loops.
Example usage:// 20 iterations in loop, update progress every 1 sec RcppThread::ProgressBarbar(20,1);RcppThread::parallelFor(0,20, [&] (int i) {std::this_thread::sleep_for(std::chrono::milliseconds(200)); bar++; });
Output: (just one line that is continuously updated)
...Computing: [========================== ] 65% (~1s remaining) ...Computing: [========================================] 100% (done)
Release version from CRAN:
install.packages("RcppThread")Latest development version from github:
# install.packages("devtools")devtools::install_github("tnagler/RcppThread")
Pass"RcppThread" to thedepends argument and"cpp11" to thepluginsargument. For example:
Rcpp::cppFunction('void func() { /* actual code here */ }',depends="RcppThread",plugins="cpp11")
Add
// [[Rcpp::plugins(cpp11)]]// [[Rcpp::depends(RcppThread)]]
before including any headers in your source code.
- Add the line
CXX_STD = CXX11to thesrc/Makevars(.win)files of your package. - Add
RcppThreadto theLinkingTofield of yourDESCRIPTIONfile.
For optimal portability, you might also want to add
PKG_LIBS = `"$(R_HOME)/bin/Rscript" -e "RcppThread::LdFlags()"`to yoursrc/Makevars (not.win). This adds-latomic/-lpthread flags asnecessary and available.
There are preprocessor options to replace all occurrences ofstd::cout,std::cerr, andstd::thread with calls toRcppThread::Rcout,RcppThread::Rcerr, andRcppThread::Thread(provided that the RcppThread headers are included first). To enable this, use
#define RCPPTHREAD_OVERRIDE_COUT 1 // std::cout override#define RCPPTHREAD_OVERRIDE_CERR 1 // std::cerr override#define RCPPTHREAD_OVERRIDE_THREAD 1 // std::thread overridebefore including the RcppThread headers.
Nagler, T. (2021). "R-Friendly Multi-Threading in C++."Journal of StatisticalSoftware, Code Snippets,97(1), 1-18.doi: 10.18637/jss.v097.c01
About
R-friendly threading in C++
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.