OpenMP:获取正在运行的线程的总数 [英] OpenMP: Get total number of running threads

查看:455
本文介绍了OpenMP:获取正在运行的线程的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道我的应用程序通过OpenMP产生的线程总数。不幸的是, omp_get_num_threads()函数在这里工作,因为它只会产生当前团队中的线程数。

I need to know the total number of threads that my application has spawned via OpenMP. Unfortunately, the omp_get_num_threads() function does not work here since it only yields the number of threads in the current team.

但是,我的代码是递归运行的(基本上是分开和征服),只要还有空闲的处理器,我想生成新的线程。

However, my code runs recursively (divide and conquer, basically) and I want to spawn new threads as long as there are still idle processors, but no more.

有没有办法解决 omp_get_num_threads 的局限性,并获得总数的运行线程数量?

Is there a way to get around the limitations of omp_get_num_threads and get the total number of running threads?

如果需要更多详细信息,请考虑以下伪代码,用于对我的工作流程建模非常密切:

If more detail is required, consider the following pseudo-code that models my workflow quite closely:

function divide_and_conquer(Job job, int total_num_threads):
  if job.is_leaf(): # Recurrence base case.
    job.process()
    return

  left, right = job.divide()

  current_num_threads = omp_get_num_threads()
  if current_num_threads < total_num_threads: # (1)
    #pragma omp parallel num_threads(2)
      #pragma omp section
        divide_and_conquer(left, total_num_threads)
      #pragma omp section
        divide_and_conquer(right, total_num_threads)

  else:
    divide_and_conquer(left, total_num_threads)
    divide_and_conquer(right, total_num_threads)

  job = merge(left, right)

如果我使用 total_num_threads 值为4,条件注释为(1)将始终计算 true 因为每个线程团队最多只包含两个线程),因此代码将总是产生两个新线程,无论多少线程已经在较高级别运行。

If I call this code with a total_num_threads value of 4, the conditional annotated with (1) will always evaluate to true (because each thread team will contain at most two threads) and thus the code will always spawn two new threads, no matter how many threads are already running at a higher level.

我正在搜索与平台无关的方式来确定当前在我的应用中运行的主题总数。

I am searching for a platform-independent way of determining the total number of threads that are currently running in my application.

推荐答案

记住你知道创建的线程的确切数量,我想出的最简单的解决方案是保持自己的线程计数器。

Having in mind you know the exact amount of threads being created, the simplest solution I come up with is keeping your own thread counter.

注意我对OpenMP完全是黑暗的,因为我从来没有真正使用它。

Be aware I'm completely in the dark about OpenMP as I've never really used it.

这篇关于OpenMP:获取正在运行的线程的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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