What is OpenMP ??
Now days we mostly use computers with more than one processor and CPU having multiple cores. Also with growing technology, as demand of processing power is increasing day by day, the cores in CPU's are also increasing. This is where OpenMP comes into role. OpenMP is an easy way to convert a program which uses one core into a program which is able to use multiple core for performance enhancement. OpenMP is designed only for shared memory systems, its meaning is that, it is designed for applications that run on single computer only. If you want to write a high performance application that will run on a cluster of computers, you should go with MPI.
As per wikis definition OpenMP is an API that supports multi-platform shared memory multiprocessing programming in C, C++, and Fortran.
Concept:
Generally all programs that require high performance have some loops in the program, e.g. loops. Many times, one iteration of a for loop has nothing to do with any of the other iterations. Therefore, it can be parallelized. With OpenMP, this can be easily done by putting a compiler directive before and after the for loop. Wit h OpenMP by simply inserting compiler directives in the right places we can increase performance of our application significantly. If you originally wrote a program to only use one core, it may be easier than you think to convert your program to take advantage of multiple cores in some areas.
Parallel Construct:
openmpBasics.cpp
#include <omp.h> #include<vector> int main() { #pragma omp parallel { std::cout<< "\n Hello World... from thread = " << omp_get_thread_num() <<"\n"; } return 0; }
In above code #pragma omp parallel creates a team of N threads(where N is the number of CPU cores by default). To compile the code from command prompt run:
g++ openmpBasics.cpp -fopenmp -o demo
To run program:
./demo
Output:
Hello World... from thread = 4 Hello World... from thread = 2 Hello World... from thread = 6 Hello World... from thread = 0 Hello World... from thread = 7 Hello World... from thread = 3 Hello World... from thread = 1 Hello World... from thread = 5
Loop Construct:
We can set number of threads using function omp_set_num_threads
ompCoreSet.cpp
#include <iostream>#include <omp.h> int main (int argc, const char * argv[]) { int nCores = omp_get_max_threads(); omp_set_num_threads(nCores); std::cout<< " Number of threads set " << nCores << "\n"; #pragma omp parallel for for(int i = 0; i <nCores ; i++){ std::cout<<"Hello tan, Thread no. "<<omp_get_thread_num() << std::endl; } return 0; }
In above code we can set number threads with #pragma directive itself. e.g. #pragma omp parallel for num_threads(3)
Output:
Number of threads set 8
Hello tan, Thread no. Hello tan, Thread no. 7 Hello tan, Thread no. Hello tan, Thread no. Hello tan, Thread no.
0
4
Hello tan, Thread no. 6
1
Hello tan, Thread no. 3
Hello tan, Thread no. 5
2
Scheduling:
The scheduling algorithm for the for-loop can be explicitly specified using OpenMP directives. e.g.
Ordered clause:
Using ordered clause, we can force certain events within loop run in a predicted order only.
When in our program, we have nested loops, we can use collapse in OpenMP directive.
We have several tasks which can be executed in parallel. But sometimes we want to run each task in its own thread. This is where sections comes into role.
The scheduling algorithm for the for-loop can be explicitly specified using OpenMP directives. e.g.
#pragma omp parallel for schedule(dynamic,3)
We can replace dynamic by static for static scheduling.Ordered clause:
Using ordered clause, we can force certain events within loop run in a predicted order only.
#pragma omp parallel for ordered shcedule(dynamic) for(int n = 0;i < 100;i++) { inFiles[n].compress(); #pragma omp ordered send(outFiles[n]); }Collapse Clause:
When in our program, we have nested loops, we can use collapse in OpenMP directive.
#pragma omp parallel for collapse(2) for(int i = 0;i < 10;i++) { for(int j = 0;j < 10;j++) { doSomething(); } }Section:
We have several tasks which can be executed in parallel. But sometimes we want to run each task in its own thread. This is where sections comes into role.
#pragma omp parallel sections { { task1(); } #pragma omp section { task2(); task3(); } #pragma omp section { task4(); task5(); } }Note:
OpenMP is not supported in the Express or Standard editions of Microsoft's Visual Studio. You need the professional version. but it can be used on open-source compilers such as GCC with no problem.
No comments:
Post a Comment