How to check how many threads are currently running when using TBB?

谁说我不能喝 提交于 2019-12-10 22:59:46

问题


I am running Intel TBB on clusters. However, I don't know how to check how many threads are active and running. Is there a way to check this?

Let's say I have 16 cores, so I would like to know if all 16 cores are now being used in my TBB code. This would make sure there is no problem with my system.


回答1:


I would like to know if all 16 cores are now being used in my TBB code.

It is right to assume that requesting or expecting a number of threads in TBB is not always results in this number of threads created or then working on a user code. TBB follows optional parallelism paradigm which does not guarantee specific number of threads working concurrently. Moreover, even if TBB created enough threads, it does not mean that all the threads will be able to join a given user code; task_scheduler_init and task_arena specify only the limit for number of worker threads.

You can use task_scheduler_observer to monitor how many worker threads are created and how many are really joined the task arena of your concern.

This blog provides simple code for how to count TBB worker threads which were created:

class concurrency_tracker: public tbb::task_scheduler_observer {
    tbb::atomic<int> num_threads;
public:
    concurrency_tracker() : num_threads() { observe(true); }
    /*override*/ void on_scheduler_entry( bool ) { ++num_threads; }
    /*override*/ void on_scheduler_exit( bool ) { --num_threads; }

    int get_concurrency() { return num_threads; }
};

But it is not much different from an external tool which can show number of live threads in a process. In order to check how many threads are joining your computation region (arena), we can use a preview feature TBB_PREVIEW_LOCAL_OBSERVER:

#define TBB_PREVIEW_LOCAL_OBSERVER 1
#include <tbb/task_scheduler_observer.h>
//...
class concurrency_tracker: public tbb::task_scheduler_observer {
    tbb::atomic<int> num_threads;
    tbb::atomic<int> max_threads;
public:
    concurrency_tracker()
    :   tbb::task_scheduler_observer(true)   // request implicit arena observation
    ,   num_threads(), max_thread()
    {
        observe(true);
    }

    /*override*/ void on_scheduler_entry( bool )
    {
        int current_num = ++num_threads;    // increment instant concurrency counter
        int current_max = max_threads;
        while( current_max < current_num )  // update max concurrency value
            current_max = max_threads.compare_and_swap(current_num, current_max);
    }

    /*override*/ void on_scheduler_exit( bool ) { --num_threads; }

    int get_instant_concurrency() { return num_threads; }
    int get_peak_concurrency()    { return max_threads; }
};

Finally, the same trick can be done directly from inside of your parallel algorithms using TLS (e.g. tbb::enumerable_thread_specific) as it is implemented in src/test/harness_concurrency_tracker.h file from the TBB unit tests sources. It can track how many of the specific task instances were running in parallel.



来源:https://stackoverflow.com/questions/24810370/how-to-check-how-many-threads-are-currently-running-when-using-tbb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!