OpenMp:创建具有线程数大小的数组的最佳方法 [英] OpenMp: Best way to create an array with size of the number of threads

查看:259
本文介绍了OpenMp:创建具有线程数大小的数组的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特定于我必须与OpenMp并行计算pi.我只允许使用#omp parallel.所以我想创建一个具有进程数大小的数组,然后部分地并行计算总和,然后一起计算总和.但是不幸的是,在并行版本之前无法获得线程数.那么最好的方法是创建一个非常大的数组并将其初始化为0.0,然后一起计算所有内容,还是有更好的方法呢?我会很感激每一个答案.预先谢谢你!

specific i have to calculate pi parallel with OpenMp. I am just allowed using #omp parallel. So i wanted to create an array with the size of the number of processes and then calculating partially the sum parallel and then calculating the sums together. But it's unfortunately impossible to get the number of threads before the parallel version. So is the best way to create a very large array and initializing it with 0.0 and then calculating everything together or is there a better way? I would appreciate every answer. Thank you in advance!

推荐答案

幸运的是,并非不可能事先获得线程数.在没有程序员和程序用户任何控制的情况下,OpenMP运行时不会简单地启动随机数量的线程.相反,它遵循定义良好的机制来确定该数字,即在OpenMP规范中详细描述.具体来说,除非您为num_threads提供了更高的固定线程数,否则OpenMP启动的线程数受称为<<>内部控制变量(简称ICV)的特殊值的限制. em> nthreads-var .设置此ICV的方法是通过OMP_NUM_THREADS环境变量或通过omp_set_num_threads()调用(后一种方法将覆盖前一种方法).通过调用omp_get_max_threads()可以访问 nthreads-var 的值.有关其他ICV,请参见规范.

Fortunately, it is not impossible to obtain the number of threads in advance. The OpenMP runtime does not simply launch a random number of threads without any control from both the programmer and the program user. On the contrary, it follows a well-defined mechanism to determine that number, which is described in detail in the OpenMP specification. Specifically, unless you've supplied a higher fixed number of threads with the num_threads, the number of threads OpenMP launches is limited by the value of the special internal control variable (ICV for short) called nthreads-var. The way to set this ICV is via the OMP_NUM_THREADS environment variable or via the omp_set_num_threads() call (the latter method overrides the former). The value of nthreads-var is accessible by calling omp_get_max_threads(). For other ICVs see the specification.

所有您需要做的就是调用omp_get_max_threads()并将返回值用作数组的大小,因为线程数量不会超过该值,因为您不会以更大的值调用omp_set_num_threads()之后,并且不会将num_threads子句应用于parallel构造.

All you need to do is call omp_get_max_threads() and use the return value as the size of your array, for the number of threads will not exceed that value, given that you aren't calling omp_set_num_threads() with a larger value afterwards and aren't applying the num_threads clause to the parallel construct.

这篇关于OpenMp:创建具有线程数大小的数组的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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