<div dir="auto">The short answer here is -- don't do that.<div dir="auto"><br></div><div dir="auto">The behavior you're seeing is expected — TBB is not designed for nested parallelFor calls with externally managed thread counts. Both setThreadOptions and global_control::max_allowed_parallelism constrain the thread pool size, but not oversubscription across nested task arenas, and TBB may spin up additional threads in nested contexts specifically to avoid deadlock. The cleanest fix is to flatten your parallelism into a single parallelFor over the full grid × folds index space, decomposing the combined index inside the worker — this gives TBB a complete view of the work, avoids the nesting issue entirely, and typically improves load balancing as a bonus.</div><div dir="auto"><br></div><div dir="auto">Best,</div><div dir="auto">Kevin</div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Sun, May 24, 2026, 2:56 PM Kumar MS <<a href="mailto:mskb01@gmail.com">mskb01@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I have an Rcpp function that nests RcppParallel workers. ( I am doing<br>
cross-validation. The outer worker parallelizes over a grid of<br>
hyperparameter values, and the inner worker parallelizes over folds ).<br>
<br>
Before calling this function, I do a<br>
`RcppParallel::setThreadOptions(6)` to set a desired number of threads<br>
of six. However, I observe that whenever the nested worker gets<br>
called, the total number of threads goes much higher (~12 or higher)<br>
with CPU usage shooting to ~1200% on an M4 Mac with the code compiled<br>
and linked against Homebrew's intel tbb.<br>
<br>
I have also tried `tbb::global_control<br>
ctll(tbb::global_control::max_allowed_parallelism, 6);` I see the same<br>
behavior.<br>
<br>
It appears nesting ignores the global setting?<br>
<br>
Thank you,<br>
Kumar<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank" rel="noreferrer">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</blockquote></div>