Hello World of Parallel Programming in Ubuntu With OpenMP
by halim930112 in Circuits > Linux
15571 Views, 22 Favorites, 0 Comments
Hello World of Parallel Programming in Ubuntu With OpenMP
Hello EVERYBODY!!
Hope you are having a nice day and continue so. In my second instructables, I would like to share a little titbits i had learned in ubuntu. I would like to share about basic of parallel programming in ubuntu.. Any comments or feedbacks throughout this instructable is appreciated. Stay tuned and follow this instructable..LET'S GO!!!
Open Terminal
Let's get started, shall we?.First and foremost, we must open the terminal in ubuntu. as shown in image
Open a Text Editor
There are many text editors in ubuntu. I prefer using pico.. to open your text editor simply type sudo "text editor name" for an example, when i want to open pico, i type this in my terminal:
sudo pico
user password will be asked, enter your password and you are in the text editor..Easy right? now let's go to the third step..
Write,save and Compile the Code
We are going to write the code in C language. Writing the hello world code in C is the same everywhere. There are only minor differences in the code. Example of the code:
#include<stdio.h>
int main( int ac, char **av)
{
#pragma omp parallel // specify the code between the curly brackets is part of an OpenMP parallel section.
{
printf("Hello World!!!\n");
}
return 0;
}
Now, save the code as anyname.c and exit the text editor . Now lets compile the code first to see if there is any mistake.
To compile your code, simply type this in the terminal
gcc anyname.c -o anyname.out
if there is any problem in the code, you must fix it and compile again.Now to next step.
Number of Threads and Running in OpenMp
Now that the code is correct when compiled, we can run it parallel form using OpenMp. firstly we determine number of threads we are going to use. type this in your terminal:
export OMP_NUM_THREADS=4
you can try changing the number of threads into numbers you like.
Then, we are going to compile it using the OpenMP. It is similar to normal compiling but with addition of a few words. The format is like this:
gcc -fopenmp anyname.c -o anyname.out
after that, you can run the program. To run the program, type this into your terminal:
./anyname.out
you will find out that the hello world prints out as many times as your thread number. Well, test with more numbers and see what happens!! That's all for now, I will try to share with all of you whatever knowledge i have next time. Happy PROGRAMMING!!