Monday, October 7, 2019

Getting Started with OpenMP



Getting Started with OpenMP

https://www.geeksforgeeks.org/openmp-introduction-with-installation-guide/
https://medium.com/swlh/openmp-on-ubuntu-1145355eeb2
http://deeplearning.lipingyang.org/2017/01/30/openmp-on-ubuntu/

The above links was very helpful in getting me started really quickly in getting a Hello World code up and running.

Step 0 - Install GCC compiler: I did initially was not aware if my Ubuntu had gcc installed, I read it would at least have a version of it, so I decided to just install it. I opened at terminal (CTRL+ALT+T) and keyed in the command below.


 sudo apt-get install gcc  

Step 1 (Optional) - Install OpenMP library: Once I had the gcc compiler installed, then OpenMP should already be configured in the compiler. I went ahead to install OpenMP library anyway.


 sudo apt-get install libomp-dev  


Step 2 - Setting Number of Threads:
Then next we can set the number of threads using the command below.


 export OMP_NUM_THREADS=3  

Step 3 - Hello World Code:
I have not mastered using VIM. Coming from Windows environment (don't laugh), I was more comfortable with Notepad++. Thank goodness Notepad++ is available as an application download in Ubuntu, so I used it to write a "hello world" code. Saved it in ".c" of course. 


 #include 
 #include 
int main() {
  int i;
  #pragma omp parallel
  {
   for(i=0; i<10 br="" i="">    printf("%d\n",i);
   }
  }
  return 0;
} 


Step 4 - Compile and execute: Compile and execute command. It was running! Hooray!

Compile:
 gcc -fopenmp helloworldmp.c -o helloworldmp

Execute:
time ./helloworldmp




Using another code:
#include 
#include 
int main() {
 #pragma omp parallel
 {
   printf("Helloworld\n");
 }
 return 0;
}




No comments:

Post a Comment

My 2 cents worth of words...