Number of threads used by SparseArray operations
thread-control.RdUse get_SparseArray_nthread or set_SparseArray_nthread
to get or set the number of threads to use by the multithreaded
operations implemented in the SparseArray package.
Arguments
- nthread
The number of threads to use by multithreaded operations implemented in the SparseArray package.
On systems where OpenMP is available, this must be
NULLor an integer value >= 1. WhenNULL(the default), a "reasonable" value will be used that never exceeds one third of the number of logical cpus available on the machine.On systems where OpenMP is not available, the supplied
nthreadis ignored andset_SparseArray_nthread()is a no-op.
Details
Multithreaded operations in the SparseArray package are implemented in C with OpenMP (https://www.openmp.org/).
Note that OpenMP is not available on all systems. On systems where it's
available, get_SparseArray_nthread() is guaranteed to return a
value >= 1. On systems where it's not available (e.g. macOS),
get_SparseArray_nthread() returns 0 and
set_SparseArray_nthread() is a no-op.
IMPORTANT: The portable way to disable multithreading is by calling
set_SparseArray_nthread(1), NOT set_SparseArray_nthread(0)
(the latter returns an error on systems where OpenMP is available).
Value
get_SparseArray_nthread() returns an integer value >= 1 on systems
where OpenMP is available, and 0 on systems where it's not.
set_SparseArray_nthread() returns the previous nthread
value, that is, the value returned by get_SparseArray_nthread()
before the call to set_SparseArray_nthread(). Note that the value
is returned invisibly.
See also
SparseArray_matrixStats for SparseArray col/row summarization methods.
SparseMatrix_mult for SparseMatrix multiplication and cross-product.
SparseArray objects.
Examples
get_SparseArray_nthread()
#> [1] 1
if (get_SparseArray_nthread() != 0) { # multithreading is available
svt1 <- poissonSparseMatrix(77000L, 15000L, density=0.01)
## 'user' time is typically N x 'elapsed' time where N is roughly the
## number of threads that was effectively used:
system.time(cv1 <- colVars(svt1))
svt2 <- poissonSparseMatrix(77000L, 300L, density=0.3) * 0.77
system.time(cp12 <- crossprod(svt1, svt2))
prev_nthread <- set_SparseArray_nthread(1) # disable multithreading
system.time(cv1 <- colVars(svt1))
system.time(cp12 <- crossprod(svt1, svt2))
## Restore previous 'nthread' value:
set_SparseArray_nthread(prev_nthread)
}