windows - visual studio 2013:OpenMP:omp_set_num_threads() 不工作
windows - visual studio 2013 : OpenMP: omp_set_num_threads() not working
我想要运行这个程序:
#include <iostream>
#include <omp.h>
using namespace std;
int main()
{
int numThread, myId;
cout << "num_procs=" << omp_get_num_procs();
omp_set_num_threads(omp_get_num_procs());
#pragma omp parallel
{
cout << "\nid=" << omp_get_thread_num();
numThread = omp_get_num_threads();
cout << "\nmax-thread=" << omp_get_max_threads();
}
getchar();
}
结果是:
num_procs=4
id=0
max-thread=4
我认为这个结果必须重复打印4次,但我不知道为什么只打印一次。
我 运行 下面的代码来自 this comment in this post 而我的结果不同。
#include <iostream>
#include <omp.h>
int main(int argc, const char * argv[])
{
int nProcessors = omp_get_max_threads();
std::cout << nProcessors << std::endl;
omp_set_num_threads(nProcessors);
std::cout << omp_get_num_threads() << std::endl;
#pragma omp parallel for
for (int i = 0; i<5; i++){
int tid = omp_get_thread_num();
std::cout << tid << "\t tid" << std::endl;
int nThreads = omp_get_num_threads();
std::cout << nThreads << "\t nThreads" << std::endl;
}
exit(0);
}
打印此结果:
4
1
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
我在 cmd 中 运行 这个命令:
set OMP_NUM_THREADS=16
当我 运行 :
set OMP_NUM_THREADS in cmd
打印此结果:OMP_NUM_THREADS=16
但是当我关闭 cmd 并重新打开它并且 运行 设置 OMP_NUM_THREADS
打印此结果:
Environment variable OMP_NUM_THREADS not defined !!!!!!!!Please Help me.
使用 SETX 命令(注意 'x' 后缀)设置在 cmd window 关闭后仍然存在的变量。
setx OMP_NUM_THREADS 16
感谢 doqtor 对我的回答,我在这里学到了两件事,非常好。
我的问题的答案是
1. 使用 setx 命令持久化环境变量
2.enable visual studio 2013 中的 openmp 支持:VIEW->Other Windows->属性 Manager 然后右键单击 属性 管理器和 配置属性 -> C/C++ -> 语言 -> OpenMP 支持 设置是!
我想要运行这个程序:
#include <iostream>
#include <omp.h>
using namespace std;
int main()
{
int numThread, myId;
cout << "num_procs=" << omp_get_num_procs();
omp_set_num_threads(omp_get_num_procs());
#pragma omp parallel
{
cout << "\nid=" << omp_get_thread_num();
numThread = omp_get_num_threads();
cout << "\nmax-thread=" << omp_get_max_threads();
}
getchar();
}
结果是:
num_procs=4
id=0
max-thread=4
我认为这个结果必须重复打印4次,但我不知道为什么只打印一次。
我 运行 下面的代码来自 this comment in this post 而我的结果不同。
#include <iostream>
#include <omp.h>
int main(int argc, const char * argv[])
{
int nProcessors = omp_get_max_threads();
std::cout << nProcessors << std::endl;
omp_set_num_threads(nProcessors);
std::cout << omp_get_num_threads() << std::endl;
#pragma omp parallel for
for (int i = 0; i<5; i++){
int tid = omp_get_thread_num();
std::cout << tid << "\t tid" << std::endl;
int nThreads = omp_get_num_threads();
std::cout << nThreads << "\t nThreads" << std::endl;
}
exit(0);
}
打印此结果:
4
1
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
0 tid
1 nThreads
我在 cmd 中 运行 这个命令:
set OMP_NUM_THREADS=16
当我 运行 :
set OMP_NUM_THREADS in cmd
打印此结果:OMP_NUM_THREADS=16
但是当我关闭 cmd 并重新打开它并且 运行 设置 OMP_NUM_THREADS 打印此结果:
Environment variable OMP_NUM_THREADS not defined !!!!!!!!Please Help me.
使用 SETX 命令(注意 'x' 后缀)设置在 cmd window 关闭后仍然存在的变量。
setx OMP_NUM_THREADS 16
感谢 doqtor 对我的回答,我在这里学到了两件事,非常好。 我的问题的答案是 1. 使用 setx 命令持久化环境变量 2.enable visual studio 2013 中的 openmp 支持:VIEW->Other Windows->属性 Manager 然后右键单击 属性 管理器和 配置属性 -> C/C++ -> 语言 -> OpenMP 支持 设置是!