问题描述
我正在使用OpenMP对嵌套循环进行多线程处理.由于这是新手,所以我不确定我是否以正确的方式使用OpenMP,以便它实际上可以执行并行编程.因此,我想知道是否可以衡量使用OpenMP的C ++程序的性能,以便我可以说它确实有效并且我走在正确的轨道上?就像有多少个线程正在并行运行,以及每个线程需要多长时间才能完成.谢谢和问候!
I am using OpenMP to do multithreading with my nested loops. Since new to this stuff, I am not sure if I am using OpenMP in the correct way so that it can actually do the parallel programming. So I like to know if I can measure the performance of my C++ program that uses OpenMP so I can tell it actually works and I am on the right track? Like how many threads are running in parallel and how long it takes for each of them to finish.Thanks and regards!
推荐答案
#include <omp.h>
...
int target_thread_num = 4;
omp_set_num_threads(target_thread_num);
unsigned long times[target_thread_num];
// Initialize all the times
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
times[thread_id] = start_time();
std::cout << "Thread number: " << omp_get_thread_num() << endl;
times[thread_id] = end_time();
}
...
显然,您需要提供两个计时器功能,但这就是要点. OMP功能非常不言自明.另外,请确保您的环境设置正确,并且使用正确的机制进行编译. g ++选项是-fopenmp.在Visual Studio上,转到项目设置,C ++,语言,然后启用"OpenMP支持".
Obviously you need ot provide the two timer functions, but that's the gist. The OMP functions are pretty self-explanatory. Also make sure that your environment is set up properly and that you're compiling with the proper mechanisms. g++ option is -fopenmp. On Visual Studio go to project settings, C++, Language, and enable "OpenMP Support".
这篇关于如何判断OpenMP是否可以在我的C ++程序中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!