/
threads.cpp
59 lines (47 loc) · 1.17 KB
/
threads.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Rth.h"
/*
PRIORITY (highest to lowest):
RTH_NUM_THREADS
OMP_NUM_THREADS (for OpenMP backends)
# cores
*/
extern "C" SEXP Rth_get_backend()
{
SEXP backend;
PROTECT(backend = allocVector(STRSXP, 1));
#if RTH_OMP
SET_STRING_ELT(backend, 0, mkChar("omp"));
#elif RTH_TBB
SET_STRING_ELT(backend, 0, mkChar("tbb"));
#elif RTH_CUDA
SET_STRING_ELT(backend, 0, mkChar("cuda"));
#else
SET_STRING_ELT(backend, 0, mkChar("unknown"));
#endif
UNPROTECT(1);
return backend;
}
extern "C" SEXP Rth_get_num_threads()
{
char *rth_nthreads = getenv("RTH_NUM_THREADS");
char *omp_nthreads = getenv("OMP_NUM_THREADS");
SEXP nthreads;
PROTECT(nthreads = allocVector(INTSXP, 1));
#if RTH_OMP
if (rth_nthreads != NULL)
INT(nthreads) = atoi(rth_nthreads);
else if (omp_nthreads != NULL)
INT(nthreads) = atoi(omp_nthreads);
else
INT(nthreads) = omp_get_max_threads(); // omp_get_max_threads
#elif RTH_TBB
if (rth_nthreads != NULL)
INT(nthreads) = atoi(rth_nthreads);
else
INT(nthreads) = tbb::task_scheduler_init::automatic;
#else
INT(nthreads) = RTH_ERROR;
#endif
UNPROTECT(1);
return nthreads;
}