First dip into Parallel Programming

Parallel Computing , words itself seem very intriguing. The reason behind Parallel Computing was the increase in power in our present systems and chance to exploit that power.

So I was very eager to dive into some Parallel Programming.What is the first program every computer science engineer writes in a new Environment,Hello world Program.

#include 
#include "omp.h"
int main(){
#pragma omp parallel for
for(int i=0;i<5;i++){
    printf(" Hello World %d ....",omp_get_thread_num());
}

}

Output I got were pretty Interesting
Test 1:

Hello World 2
3rd iteration in Thread 2
Hello World 1
2nd iteration in Thread 1
Hello World 0
0th iteration in Thread 0
Hello World 0
1st iteration in Thread 0
Hello World 3
4th iteration in Thread 3

Test 2:

 Hello World 0 
0th iteration in Thread 0 
 Hello World 0 
1st iteration in Thread 0 
 Hello World 2 
3rd iteration in Thread 2 
 Hello World 1 
2nd iteration in Thread 1 
 Hello World 3 
4th iteration in Thread 3 

#Pragma OpenMP for ,this flag tells the compiler that this for loop can be executed in parallel.(YES , as simple as that)
It created 4 threads on my machine.Which is running an Intel i5 Processor,two physical cores and two virtual cores.Which add up to 4 threads.

The outputs show an Interesting Behaviour!The for loop iterations may occur in any order when executed.
With that I found my first learning in Parallel Computing If the output of a program must remain consistent throughout multiple tests we must make sure the tasks we flag parallel are well and truly independent of the other iterations.

Advertisements