C – Parallel for loop in OpenMP
Prerequisites:
We have the for loops in the programs and we can parallelize them easily with the help of OpenMP Library. Add #include<omp.h>, as all the prototypes for these parallelization techniques of OpenMP lie in that header file.
Syntax:
#pragma omp parallel for [clause[[,] clause] ...] new-line for-loop
Explanation:
- The Threads split the range of iterations evenly and execute in parallel.
- For Example,
- There are 4 threads
- The for loop goes from 1 to 1000. Total 1000 iterations.
- So, thread 0 executes 1 to 250
- Thread 1 executes 251 to 500
- Thread 2 executes 501 to 750
- Thread 3 executes 751 to 1000
- So in this way, the time taken would reduce as the work is now not done in sequential order. Rather it is executed parallelly.
- This is just an example for visualization, the actual range may vary a bit, but at the most, it divides evenly.
Algorithm:
- Start the program
- There are many for loops in the program.
- Add the for loop construct before all the for loops.
- num_threads( n ) needs to be mentioned to get n threads
- If not mentioned, by default, the no. of processor’s scores threads are formed.
- So therefore parallelized.
Example:
C
#include <omp.h> #include <stdio.h> int main() { #pragma omp parallel for num_threads(4) for ( int i = 1; i <= 10; i++) { int tid = omp_get_thread_num(); printf ( "The thread %d executes i = %d\n" , tid, i); } return 0; } |
Output:
- Here are 10 iterations, and 4 threads. Each thread has 2/3 iterations with it.
- Another proof that parallelization has happened is the order of iteration:
- Here in the sequential or serial program, we have iterations i=1 to i=10 in the order one after another.
- But here you can see that the i=4 comes first and i=1 at 3rd last place.
- As in the above example, thread 1 finished before thread 0
Time Complexity: O(N)
Space Complexity: O(1)
Contact Us