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.