使用TBB时如何检查当前正在运行多少个线程? [英] How to check how many threads are currently running when using TBB?

查看:524
本文介绍了使用TBB时如何检查当前正在运行多少个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在集群上运行Intel TBB.但是,我不知道如何检查活动和正在运行的线程数.有办法检查吗?

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?

比方说,我有16个内核,所以我想知道我的TBB代码中是否现在全部使用了16个内核.这样可以确保我的系统没有问题.

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.

推荐答案

我想知道现在我的TBB代码中是否全部使用了16个内核.

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

正确地假设,在TBB中请求或期望多个线程并不总是导致创建或随后处理用户代码的线程数量如此. TBB遵循可选的并行性范例,该范例不保证特定数量的线程可以同时工作.而且,即使TBB创建了足够的线程,也并不意味着所有线程都可以加入给定的用户代码. task_scheduler_inittask_arena仅指定工作线程数的限制.

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.

您可以使用task_scheduler_observer来监视创建了多少个工作线程以及真正加入了您关注的任务领域的

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; }
};

但是,它与可以显示进程中活动线程数的外部工具没有太大区别.为了检查加入您的计算区域(竞技场)有多少个线程,我们可以使用预览功能TBB_PREVIEW_LOCAL_OBSERVER:

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; }
};

最后,与在TBB单元测试源文件的src/test/harness_concurrency_tracker.h文件中实现的相同,使用TLS(例如tbb::enumerable_thread_specific)可以直接在并行算法内部完成相同的技巧.它可以跟踪有多少特定任务实例是并行运行的.

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.

这篇关于使用TBB时如何检查当前正在运行多少个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆