Skip to content

Commit 02e60ed

Browse files
committed
refactor a bit
1 parent ae4673a commit 02e60ed

File tree

3 files changed

+50
-30
lines changed

3 files changed

+50
-30
lines changed

inst/NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
RcppParallel 5.0.3 (UNRELEASED)
22
------------------------------------------------------------------------
3+
4+
* setThreadOptions(...) can again be called multiple times per session.
5+
The requested number of threads will be used for invocations to parallelFor()
6+
and parallelReduce() that don't explicitly request a specific number of threads.
37
* The parallelFor() and parallelReduce() functions gain the 'numThreads'
48
argument, allowing one to limit the number of threads used for a
59
particular computation.
610

711
RcppParallel 5.0.2
812
------------------------------------------------------------------------
13+
914
* setThreadOptions(...) can now only be called once per session, to avoid
1015
segfaults when compiling RcppParallel / TBB with gcc 10.1. Subsequent
1116
calls to setThreadOptions(...) are ignored.
1217

1318
RcppParallel 5.0.1
1419
------------------------------------------------------------------------
20+
1521
* Fixed compilation issue on OpenSUSE Tumbleweed with -flto=auto
1622
* Fixed compilation when CPPFLAGS = -I/usr/local/include and a version
1723
of libtbb is installed there
1824

1925
RcppParallel 5.0.0
2026
------------------------------------------------------------------------
27+
2128
* RcppParallel backend can now be customized with RCPP_PARALLEL_BACKEND
2229
environment variable (supported values are 'tbb' and 'tinythread')
2330
* Fixed issue when compiling RcppParallel on macOS Catalina
2431
* Fixed issue when compiling RcppParallel with Rtools40
2532

2633
RcppParallel 4.4.4
2734
------------------------------------------------------------------------
35+
2836
* Fixed an issue when compiling RcppParallel with clang-9 on Fedora
2937

3038
RcppParallel 4.4.3

inst/include/RcppParallel.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,46 @@
2828

2929
namespace RcppParallel {
3030

31+
namespace {
32+
33+
template <typename T, typename U>
34+
int resolveValue(const char* envvar,
35+
T requestedValue,
36+
U defaultValue)
37+
{
38+
// if the requested value is non-zero and not the default, we can use it
39+
if (requestedValue != defaultValue && requestedValue > 0)
40+
return requestedValue;
41+
42+
// otherwise, try reading the default from associated envvar
43+
// if the environment variable is unset, use the default
44+
const char* var = getenv(envvar);
45+
if (var == NULL)
46+
return defaultValue;
47+
48+
// try to convert the string to a number
49+
// if an error occurs during conversion, just use default
50+
errno = 0;
51+
char* end;
52+
long value = strtol(var, &end, 10);
53+
if (errno != 0)
54+
return defaultValue;
55+
56+
// okay, return the parsed environment variable value
57+
return value;
58+
}
59+
60+
} // end anonymous namespace
61+
3162
inline void parallelFor(std::size_t begin,
3263
std::size_t end,
3364
Worker& worker,
3465
std::size_t grainSize = 1,
3566
int numThreads = -1)
3667
{
68+
grainSize = resolveValue("RCPP_PARALLEL_GRAIN_SIZE", grainSize, 1);
69+
numThreads = resolveValue("RCPP_PARALLEL_NUM_THREADS", numThreads, -1);
70+
3771
#if RCPP_PARALLEL_USE_TBB
3872
if (internal::backend() == internal::BACKEND_TBB)
3973
tbbParallelFor(begin, end, worker, grainSize, numThreads);
@@ -51,6 +85,9 @@ inline void parallelReduce(std::size_t begin,
5185
std::size_t grainSize = 1,
5286
int numThreads = -1)
5387
{
88+
grainSize = resolveValue("RCPP_PARALLEL_GRAIN_SIZE", grainSize, 1);
89+
numThreads = resolveValue("RCPP_PARALLEL_NUM_THREADS", numThreads, -1);
90+
5491
#if RCPP_PARALLEL_USE_TBB
5592
if (internal::backend() == internal::BACKEND_TBB)
5693
tbbParallelReduce(begin, end, reducer, grainSize, numThreads);
@@ -61,6 +98,6 @@ inline void parallelReduce(std::size_t begin,
6198
#endif
6299
}
63100

64-
} // namespace RcppParallel
101+
} // end namespace RcppParallel
65102

66103
#endif // __RCPP_PARALLEL__

inst/include/RcppParallel/TBB.h

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -178,31 +178,6 @@ class TBBArenaParallelReduceExecutor
178178
std::size_t end_;
179179
std::size_t grainSize_;
180180
};
181-
182-
int tbbResolveNumThreads(int numThreads)
183-
{
184-
// if the user has explicitly set the value away from the default,
185-
// then we can just honor their request
186-
if (numThreads != -1)
187-
return numThreads;
188-
189-
// otherwise, try reading the default from RCPP_PARALLEL_NUM_THREADS.
190-
// if that is unset, we'll let tbb decide
191-
const char* var = getenv("RCPP_PARALLEL_NUM_THREADS");
192-
if (var == NULL)
193-
return tbb::task_arena::automatic;
194-
195-
// try to convert the string to a number
196-
errno = 0;
197-
char* end;
198-
long threads = strtol(var, &end, 10);
199-
200-
// if an error occurred during conversion, just use default
201-
if (errno != 0)
202-
threads = tbb::task_arena::automatic;
203-
204-
return threads;
205-
}
206181

207182
} // anonymous namespace
208183

@@ -211,9 +186,9 @@ inline void tbbParallelFor(std::size_t begin,
211186
std::size_t end,
212187
Worker& worker,
213188
std::size_t grainSize = 1,
214-
int numThreads = -1)
189+
int numThreads = tbb::task_arena::automatic)
215190
{
216-
tbb::task_arena arena(tbbResolveNumThreads(numThreads));
191+
tbb::task_arena arena(numThreads);
217192
tbb::task_group group;
218193

219194
TBBArenaParallelForExecutor executor(group, worker, begin, end, grainSize);
@@ -225,9 +200,9 @@ inline void tbbParallelReduce(std::size_t begin,
225200
std::size_t end,
226201
Reducer& reducer,
227202
std::size_t grainSize = 1,
228-
int numThreads = -1)
203+
int numThreads = tbb::task_arena::automatic)
229204
{
230-
tbb::task_arena arena(tbbResolveNumThreads(numThreads));
205+
tbb::task_arena arena(numThreads);
231206
tbb::task_group group;
232207

233208
TBBArenaParallelReduceExecutor<Reducer> executor(group, reducer, begin, end, grainSize);

0 commit comments

Comments
 (0)