How to tell if OpenMP works in my C++ program

安稳与你 提交于 2019-12-05 09:14:19
#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();
}
...

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".

You could use windows taskmanager (CTRL-SHIFT-ESC) on windows to monitor CPU usage, or top on *nix boxes.

Just check if many cores are used or not

You can use your debugger (Visual Studio if you're on Windows) to:

  • see how may threads are running
  • see which code each of them is running
  • pause some of them while letting others continue
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!