(Original) Simple OpenMP Parallel Programming Tutorial (1)

Source: Internet
Author: User
(Original) OpenMP parallel programming simple tutorial (1) -- Linux general technology-Linux programming and kernel information, the following is a detailed description. Recently, I have worked as an intern at school. I need to use OpenMP to optimize some SAT algorithms. Then I came into contact with OpenMP. It is a multi-processor-based, parallel programming mode that shares the same memory. For multi-core CPU computers, you only need to add simple statements to achieve immediate performance improvement.
This document describes the basic programming statements of OpenMP through four examples.

Example 1 helloworld. c
01 # include
02 int main (argc, argv)
03 int argc;
04 char ** argv;
05 {
06 # pragma omp parallel
07 printf ("Hello world! \ N ");
08 return 0;
09}
The essence of the program helloworld. c is to print the "Hello world!" string on the screen !".
"# Pragma omp parallel" is an OpenMP standard statement. It means that the subsequent statements are executed in multiple threads. Note that each thread does the same thing.

[Root @ localhost zf] # gcc? Fopenmp helloworld. c
[Root @ localhost zf] #./a. out
Hello world!
Compile and execute the program. "Hello world" is printed on the screen ".
-Fopenmp is a parameter for gcc compilation to support OpenMP programs. OpenMP is supported by gcc4.2 and later versions by default.
Because the default value of NUM_OMP_THREADS in the system environment variable is 1, the program only uses one thread for execution.

[Root @ localhost zf] # NUM_OMP_THREADS = 5
[Root @ localhost zf] # export NUM_OMP_THREADS
[Root @ localhost zf] #./a. out
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Assign a value to the environment variable NUM_OMP_THREADS indicating the number of threads to 5 and export it. Then execute the program and get "Hello world!" five times !", The program executes the print statement in five threads.
Of course, we do not want to be restricted by running system environment variables, so we can also replace the 06 line of code with "# pragma omp parallel num_threads (10)" and then execute the program after compilation, we get "Hello world!" 10 times! ", At this time, regardless of the value of the environment variable NUM_OMP_THREADS, we can only get "Hello world!" 10 times !".

Example 2 RankNum. c
01 # include
02 # include
03 int main (int argc, char ** argv)
04 {
05 int rank, size;
06 # pragma omp parallel private (rank)
07 {
08 rank = omp_get_thread_num ();
09 size = omp_get_num_threads ();
10 printf ("Hello world! I'm % d of % d \ n ", rank, size );
11}
12 return 0;
13}
Import omp. H files to support OpenMP functions.
The function omp_get_threads_num () returns the current thread number. The omp_get_num_threads () function returns the current number of threads. (No longer defines the number of threads in the program. The data in the environment variable is used. The current value is 10)

[Root @ localhost zf] # gcc? Fopenmp RankNum. c
[Root @ localhost zf] #./a. out
Hello world! I'm 8 of 10
Hello world! I'm 0 of 10
Hello world! I'm 4 of 10
Hello world! I'm 6 of 10
Hello world! I'm 3 of 10
Hello world! I'm 2 of 10
Hello world! I'm 7 of 10
Hello world! I'm 1 of 10
Hello world! I'm 9 of 10
Hello world! I'm 5 of 10
Compile, execute, get 10 output records, and execute print statements in 10 threads respectively.
The thread execution sequence is random and irrelevant to the number.

To be continued ......


Note: This article is based on Dr. Robert Roy's teaching plan (in French). If you are interested, you can send an email asking me for the original teaching plan.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.