question about enable TBB multi-threading

1, Does it need to set system variable MMGT_OPT=2 if I want to use tbb multi-threading? 

2,I find in code Handle(BRep_TVertex) TV = new BRep_TVertex();  new operator is still provided by C++ lib, not coming from tbb lib, but in the src code  it have used scalable_malloc to replace malloc ,scalable_free replace free. So these two are not be coincident . Is it a problem ? 

Kirill Gavrilov's picture

1, Does it need to set system variable MMGT_OPT=2 if I want to use tbb multi-threading? 

MMGT_OPT=2 means usage of Standard_MMgrTBBalloc redirecting to TBB scalable memory allocator:
https://www.threadingbuildingblocks.org/tutorial-intel-tbb-scalable-memo...

Scalable allocator is NOT mandatory for using TBB, and is not necessary better than system malloc (OCCT default).

2,I find in code Handle(BRep_TVertex) TV = new BRep_TVertex();  new operator is still provided by C++ lib, not coming from tbb lib

OCCT overrides new operator for most classes - see macros DEFINE_STANDARD_ALLOC.

chanson zhao's picture

thank you very much. I view the code, and make it clear: there are two multi-thread mode in OCC, one is TBB, and another is OSD_Parallel. there are three way to manage memory, 1st is C/C++ mode, 2nd is C/C++ with optimization3rd is TBB memory management. Am i right? and are these two (multi-thread and memory management) are independent of each other?

Kirill Gavrilov's picture

and are these two (multi-thread and memory management) are independent of each other?

This is correct. Memory allocation might be a bottleneck within extensive multi-threading algorithm, this is why TBB provides its own allocator optimized for multi-threaded environment.
But nowadays systems have also improved standard malloc() for multi-threaded environment, so that it is no more severe bottleneck (depending on use case).
So that scalable allocator in TBB (MMGT_OPT=2) is a legacy of this library intended to solve severe performance issues on systems of the past, though it still can be beneficial in some scenarios. Note that well-written multi-threaded version of the algorithm might also pre-allocate all necessary memory and work efficiently regardless of memory allocator performance.
At the same time, MMGT_OPT=1 is even older legacy of OCCT for solving performance issues of system allocators on older systems, unrelated to multi-threading.

Different allocators have different properties in performance, alignment, compactness - developer should avoid playing with them as long as there is a severe usage issue.
You can search on the web the topic about memory allocators to understand why it can be severe bottleneck in modern applications creating many threads for utilizing CPU cores.

there are two multi-thread mode in OCC, one is TBB, and another is OSD_Parallel.

This is correct, OCCT provides its own (based on native threads) multi-threading routines and API, which can be found in OSD_Parallel and OSD_ThreadPool classes.
Building OCCT with TBB activates usage of TBB within OSD_Parallel and availability of MMGT_OPT=2 option.