OpenMP programming study Note 3

Source: Internet
Author: User

I learned about OpenMP sections today. The main function of sections is that you can divide a task into several independent sections, each of which is processed by different threads.

C/C ++ test code:

 

Int a = 2;

Int B = 20;

Int c = 200;

Int d = 2000;

Int sum;

 

Omp_set_num_threads (4 );

# Pragma omp parallel

{

# Pragma omp sections

{

# Pragma omp section

{

A = 1;

Printf ("execute thread ID is % d/n", omp_get_thread_num ());

}

 

# Pragma omp section

{

B = 10;

Printf ("execute thread ID is % d/n", omp_get_thread_num ());

}

 

# Pragma omp section

{

C = 100;

Printf ("execute thread ID is % d/n", omp_get_thread_num ());

}

 

# Pragma omp section

{

D = 1000;

Printf ("execute thread ID is % d/n", omp_get_thread_num ());

}

}

 

Sum = a + B + c + d;

Printf ("sum = % d/n", sum );

 

}

 

Test results:

Execute thread ID is 3

Execute thread ID is 1

Execute thread ID is 2

Execute thread ID is 0

Sums = 1111

Sums = 1111

Sums = 1111

Sums = 1111

 

We can see that the four sections are allocated to four threads for separate execution. Since OpenMP sections automatically synchronizes each section, the sum result is:

Expected. To make it more intuitive to understand that the section is automatically synchronized (before sum is calculated, it will wait until all threads are executed), modify the Code as follows:

Int a = 2;

Int B = 20;

Int c = 200;

Int d = 2000;

Int sum;

 

Omp_set_num_threads (4 );

# Pragma
Omp parallel

{

# Pragma omp sections

{

# Pragma
Omp section

{

A = 1;

Printf ("execute thread ID is
% D/n ", omp_get_thread_num ());

For (int I = 0; I <0x7ffff; I ++)

{

A = 1;

B = 2;

C = 3;

D = 4;

}

}

 

# Pragma omp section

{

 

B = 10;

Printf ("execute thread ID is % d/n", omp_get_thread_num ());

For (int I = 0; I <0x7fffff; I ++)

{

A = 10;

B = 20;

C = 30;

D = 40;

}

}

 

# Pragma omp section

{

C = 100;

Printf ("execute thread ID is % d/n", omp_get_thread_num ());

For (int I = 0; I <0x7ffffff; I ++)

{

A = 100;

B = 200;

C = 300;

D = 400;

}

}

 

# Pragma omp section

{

D = 1000;

Printf ("execute thread ID is % d/n", omp_get_thread_num ());

For (int I = 0; I <0x7fffff; I ++)

{

A = 1000;

B = 2000;

C = 3000;

D = 4000;

}

}

}

Sum = A + B + C + D;

Printf ("sum = % d/N", sum );

 

}

 

Test results:

Execute thread ID is 0

Execute thread ID is 2

Execute thread ID is 1

Execute thread ID is 3

Sums = 10000

Sums = 10000

Sums = 10000

Sums = 10000

The execution time of the fourth section is the longest, so the sum result is calculated after the execution of the fourth section is completed.

An important issue for parallel programs is to divide the issues to be processed into different modules, which are independent and can be executed by distributing the functions to different processors.

Note that sum = 10000 is output four times, because sum = A + B + C + D; the Code is in parallel region, and all threads will execute it once.

 

Excerpt: The binding thread set for a sections region is the current team. A sections region binds to the innermost enclosing

Parallel region. Only the threads of the team executing the binding parallel region particle in the execution of

Structured blocks and (optional) implicit barrier of the sections region.

 

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.