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!
You can use the function omp_get_num_threads() . It will return you the number of threads that are used by your program. One must call omp_get_num_threads() inside a parallel region. Otherwise it always returns 1!
OpenMP is a set of compiler directives as well as an API for programs written in C, C++, or FORTRAN that provides support for parallel programming in shared-memory environments. OpenMP identifies parallel regions as blocks of code that may run in parallel.
OpenMP comes under the shared memory concept. In this, different CPU's (processors) will have access to the same memory location. Since all CPU's connect to the same memory, memory access should be handled carefully. Here, each CPU(processor) will have its own memory location to access and use.
#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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With