- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 OpenMP 用 C 编写了并行程序。
我想控制程序正在使用的线程数。
我正在使用系统:
我运行的程序:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
double t1[TABLE_SIZE];
double t2[TABLE_SIZE];
int main(int argc, char** argv) {
omp_set_dynamic(0);
omp_set_nested(0);
omp_set_num_threads(NUM_OF_THREADS);
#pragma omp parallel for default(none) shared(t1, t2) private(i)
for(i=0; i<TABLE_SIZE; i++) {
t1[i] = rand();
t2[i] = rand();
}
for(i=0; i<NUM_OF_REPETITION; i++) {
test1(t1, t2);
}
}
void test1(double t1[], double t2[]) {
int i;
double result;
#pragma omp parallel for default(none) shared(t1, t2) private(i) reduction(+:result)
for(i=0; i<TABLE_SIZE; i++) {
result += t1[i]*t2[i];
}
}
我正在运行在编译时设置 TABLE_SIZE(2500, 5000, 100000, 1000000), NUM_OF_THREADS(1-24), NUM_OF_REPETITION(50000 为 50k, 100000 为 100k, 1000000 为 1M) 的脚本。问题是计算机没有利用一直提供的所有线程。看来问题取决于 TABLE_SIZE。
例如,当我使用 TABLE_SIZE=2500 编译代码时,一切正常,直到 NUM_OF_THREADS=20。然后会发生一些奇怪的事情。当我设置 NUM_OF_THREADS=21 时,程序仅使用 18 个线程(我观察 htop 以查看有多少线程正在运行)。当我设置 NUM_OF_THREADS=23 和 NUM_OF_REPETITION=100k 时,它使用 18 个线程,但如果我在 NUM_OF_THREADS=23 处将 NUM_OF_REPETITION 更改为 1M,它使用 19 个线程。
当我将 TABLE_SIZE 更改为 5000 时,异常从 18 个线程开始。我设置 NUM_OF_THREADS=18 并且在 NUM_OF_REPETITION=1M 时程序只使用 17 个线程。当我设置 NUM_OF_THREADS=19 和 NUM_OF_REPETITION=100k 或 1M 时,它只使用 17 个线程。如果我将 NUM_OF_THREADS 更改为 24,则程序将在 NUM_OF_REPETITION=50k 使用 20 个线程,在 NUM_OF_REPETITION=100k 使用 22 个线程,在 NUM_OF_REPETITION=1M 使用 23 个线程。
这种不一致会随着 TABLE_SIZE 的增加而不断发生。 TABLE_SIZE 越大(在较低的 NUM_OF_THREADS 下)不一致发生得越快。
在这篇(OpenMP set_num_threads() is not working)文章中,我读到 omp_set_num_threads() 设置了程序可以使用的线程的上限。如您所见,我已禁用动态团队,程序仍未使用所有线程。如果我设置环境变量 OMP_NUM_THREADS 和 OMP_DYNAMIC 也没有帮助。
所以我去阅读了一些 OpenMP 规范 3.1。它说程序应该使用它由 omp_set_num_threads() 设置的线程数。 omp_get_max_threads() 函数也返回 24 个可用线程。
如有任何帮助,我们将不胜感激。
最佳答案
我终于找到了解决办法。我设置了 KMP_AFFINITY 环境变量。我将变量设置为“紧凑”或“分散”并不重要(我现在只对使用所有线程感兴趣)。
这是文档必须说的(https://software.intel.com/en-us/articles/openmp-thread-affinity-control):
There are 2 considerations for OpenMP threading and affinity: First, determine the number of threads to utilize, and secondly, how to bind threads to specific processor cores.
If you do not set a value for KMP_AFFINITY, the OpenMP runtime is allowed to choose affinity for you. The value chosen depends on the CPU architecture and may change depending on what affinity is deemed most efficient FOR A VARIETY OF APPLICATIONS for that architecture.
另一个来源(https://software.intel.com/en-us/node/522691):
Affinity Types:
type = none (default)
Does not bind OpenMP* threads to particular thread contexts; however, if the operating system supports affinity, the compiler still uses the OpenMP* thread affinity interface to determine machine topology.
所以我猜是因为我没有设置 KMP_AFFINITY,所以 OpenMP 运行时根据其知识设置了最有效的亲和性。如果我错了,请纠正我。
关于c - openmp 没有利用所有线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29579985/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!