OpenMP: Get total number of running threads

后端 未结 3 600
囚心锁ツ
囚心锁ツ 2021-02-19 02:38

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 w

3条回答
  •  后悔当初
    2021-02-19 03:17

    I think there isn't any such routine in at least OpenMP 3; and if there was, I'm not sure it would help, as there's obviously a huge race condition in between the counting of the number of threads and the forking. You could end up overshooting your target number of threads by almost a factor of 2 if everyone sees that there's room for one thread left and then everyone spawns a thread.

    If this really is the structure of your program, though, and you just want to limit the total number of threads, there are options (all of these are OpenMP 3.0):

    1. Use the OMP_THREAD_LIMIT environment variable to limit the total number of OpenMP threads
    2. Use OMP_MAX_ACTIVE_LEVELS, or omp_set_max_active_levels(), or test against omp_get_level(), to limit how deeply nested your threads are; if you only want 16 threads, limit to 4 levels of nesting
    3. If you want finer control than powers of two, you can use omp_get_level() to find your level, and call omp_get_ancestor_thread_num(int level) at various levels to find out which thread was your parent, grandparent, etc and from that (using this simple left-right forking) determine a global thread ID. (I think in this case it would go something like ∑l=0..L-1 al 2L-l where l is the level number starting at 0 and a is the ancestor thread number at that level). This would let you (say) allow threads 0-3 to fork but not 4-7, so that you'd end up with 12 rather than 16 threads. I think this only works in such a regular situation; if each parent thread forked a different number of child threads, I don't think you could determine a unique global thread ID because it looks like you can only query your direct ancestors.

提交回复
热议问题