Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMT] ROOT::GetThreadPoolSize does not reflect tbb::global_control settings #6363

Closed
eguiraud opened this issue Sep 16, 2020 · 4 comments
Closed

Comments

@eguiraud
Copy link
Member

Describe the bug

Calling tbb::global_control c(tbb::global_control::max_allowed_parallelism, 3); followed by ROOT::GetThreadPoolSize() or TThreadExecutor::GetPoolSize() results in the incorrect number of threads reported (hardware concurrency rather than what was set via tbb::global_control.

Expected behavior

ROOT::GetThreadPoolSize should report the real TBB thread pool size or the real number of threads that ROOT will use.

To Reproduce

This code prints 8 two times on my 8-core workstation, but CPU usage is actually 300%, i.e. TBB's thread pool has size 3 but we ROOT reports 8.

#include <ROOT/TThreadExecutor.hxx>
#include <tbb/global_control.h>
#include <tbb/task_scheduler_init.h>
#include <iostream>
#include <TROOT.h>

int main() {
  tbb::global_control c(tbb::global_control::max_allowed_parallelism, 3);
  ROOT::EnableImplicitMT();
  std::cout << ROOT::GetThreadPoolSize() << std::endl;
  ROOT::TThreadExecutor pool;
  std::cout << pool.GetPoolSize() << std::endl;
  auto busyLoop = [](int x) { int i = 0; for (; i < 1000000000+x; ++i); return i; };
  pool.Foreach(busyLoop, {1,2,3,4,5,6,7,8});
  return 0;
}
@eguiraud
Copy link
Member Author

Maybe we could check tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism) in

RTaskArenaWrapper::RTaskArenaWrapper(unsigned maxConcurrency) : fTBBArena(new tbb::task_arena{})
{
const unsigned tbbDefaultNumberThreads = fTBBArena->max_concurrency(); // not initialized, automatic state
maxConcurrency = maxConcurrency > 0 ? std::min(maxConcurrency, tbbDefaultNumberThreads) : tbbDefaultNumberThreads;
const unsigned bcCpus = LogicalCPUBandwithControl();
if (maxConcurrency > bcCpus) {
Warning("RTaskArenaWrapper", "CPU Bandwith Control Active. Proceeding with %d threads accordingly", bcCpus);
maxConcurrency = bcCpus;
}
fTBBArena->initialize(maxConcurrency);
fNWorkers = maxConcurrency;
ROOT::EnableThreadSafety();
}

@xvallspl
Copy link
Contributor

Makes sense, can do. I would expect tbb::task_arena::max_concurrency() to check that

@eguiraud
Copy link
Member Author

uhm on the other hand tbb::global_control is a RAII kind of thing, so it can come and go. Maybe it's on at the point ROOT's task arena is constructed but not at the point tasks are spawned, and users now get less threads in the arena for "no reason". Not sure.

xvallspl added a commit to xvallspl/root that referenced this issue Jan 12, 2021
    warn when the number of threads set by the user is limited at runtime
    by tbb::global_control.

    Fix for github issue root-project#6363: root-project#6363
xvallspl added a commit to xvallspl/root that referenced this issue Jan 12, 2021
    warn when the number of threads set by the user is limited at runtime
    by tbb::global_control.

    Fix for github issue root-project#6363: root-project#6363
xvallspl added a commit to xvallspl/root that referenced this issue Jan 14, 2021
    warn when the number of threads set by the user is limited at runtime
    by tbb::global_control.

    Fix for github issue root-project#6363: root-project#6363
xvallspl added a commit that referenced this issue Jan 15, 2021
    warn when the number of threads set by the user is limited at runtime
    by tbb::global_control.

    Fix for github issue #6363: #6363
@xvallspl
Copy link
Contributor

Solved in #6814

xvallspl added a commit to xvallspl/root that referenced this issue Jan 20, 2021
    warn when the number of threads set by the user is limited at runtime
    by tbb::global_control.

    Fix for github issue root-project#6363: root-project#6363
xvallspl added a commit to xvallspl/root that referenced this issue Jan 20, 2021
    warn when the number of threads set by the user is limited at runtime
    by tbb::global_control.

    Fix for github issue root-project#6363: root-project#6363
xvallspl added a commit that referenced this issue Jan 21, 2021
    warn when the number of threads set by the user is limited at runtime
    by tbb::global_control.

    Fix for github issue #6363: #6363
mrodozov pushed a commit to cms-sw/root that referenced this issue Jan 21, 2021
    warn when the number of threads set by the user is limited at runtime
    by tbb::global_control.

    Fix for github issue root-project#6363: root-project#6363
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants