High level functions for doing parallel programming with Rcpp. For example, the parallelFor function can be used to convert the work of a standard serial "for" loop into a parallel one and the parallelReduce function can be used for accumulating aggregate or other values.
The high level interface enables safe and robust parallel programming without direct manipulation of operating system threads. The underlying implementation differs by platform: on Linux and Mac systems the Intel TBB (Threading Building Blocks) are used while on Windows systems the TinyThread library is used.
Here are links to some examples that illustrate using RcppParallel. Performance benchmarks were executed on a 2.6GHz Haswell MacBook Pro with 4 cores (8 with hyperthreading).
Parallel Matrix Transform --- Demonstrates using parallelFor to transform a matrix (take the square root of each element) in parallel. In this example the parallel version performs about 2.5x faster than the serial version.
Parallel Vector Sum --- Demonstrates using parallelReduce to take the sum of a vector in parallel. In this example the parallel version performs 4.5x faster than the serial version.
Parallel Distance Matrix --- Demonstrates using parallelFor to compute pairwise distances for each row in an input data matrix. In this example the parallel version performs 5.5x faster than the serial version.
Parallel Inner Product --- Demonstrates using parallelReduce to compute the inner product of two vectors in parallel. In this example the parallel version performs 2.5x faster than the serial version.
Note that the benchmark times above are for the TBB back-end (Posix systems only). Performance on Windows will be about 30-50% slower as a result of less sophisticated thread scheduling.
You can install the RcppParallel package from CRAN as follows:
install.packages("RcppParallel")You can use the RcppParallel library from within a standalone C++ source file as follows:
// [[Rcpp::depends(RcppParallel)]] #include <RcppParallel.h>If you want to use RcppParallel from within an R package you add the following to your DESCRIPTION file:
Imports: RcppParallel LinkingTo: RcppParallelAnd the following to your NAMESPACE file:
import(RcppParallel)The RcppParallel package is made available under the GPLv2.
The TinyThread library is licensed under the zlib/libpng license as described here.
The Intel TBB Library is licensed under the GPLv2 (as described at https://www.threadingbuildingblocks.org/Licensing):
TBB and other open-source software available from this site is licensed under GPLv2 with the (libstdc++) runtime exception. Specifically, the TBB open-source license is the same license used by the GNU libstdc++ library in gcc 4.2.1 (and earlier). For a complete description of the license, please visit the official GNU website for GPLv2 and for the runtime exception.
Runtime Exception
As a special exception, you may use this file as part of a free software library without restriction. Specifically, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other files to produce an executable, this file does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License.