Question

So I am trying to use parallel for each.. I have code where I do:

Source s;..
parallel_for_each(begin(_allocs), end(_allocs), [&s] (Allocs_t::value_type allocation) {
  // cool stuff with allocation
}

This works, and works well. however, I've seen in many posts that I should call tbb:task_scheduler_init before scheduling tasks.

The problem is that I override malloc and calloc and I can't have the init call malloc and calloc(which it does..) So the questions are:

  1. why does it work well? DOES it work well?
  2. is there a way to give intel a specific allocator for all its purposes?

Thanks

Was it helpful?

Solution

Instantiation of tbb:task_scheduler_init object is optional. TBB has lazy auto-initialization mechanism which constructs everything on the first call to TBB algorithms/scheduler. Auto-initialization is equal to construction of a global task_scheduler_init object just before your first call to TBB.

Of course, if you need to override the default number of threads, specify the scope where TBB should be initialized, or specify the size of the stack for workers, explicit initialization is unavoidable.

TBB scheduler uses either its own scalable allocator (tbbmalloc.dll, libtbbmalloc.so ..) if found nearby to tbb binaries, or it falls back to using malloc otherwise. There is no way to explicitly specify any other allocator to be used by the scheduler (unlike TBB containers which have corresponding template argument).

Given all the above, I think you have two [combinable] options:

  1. Ensure that TBB scheduler uses its own allocator, so you don't have to worry about correct replacement of malloc for TBB.
  2. Or/and, ensure that the only task_scheduler_init is created and destroyed at the points (scope) where malloc/free are in consistent state.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top