OpenMP Tutorial Study Notes (8) Synchronous structure of OpenMP commands (synchronization constructs)

Source: Internet
Author: User

OpenMP Tutorial: https://computing.llnl.gov/tutorials/openMP/#Synchronization

Synchronous Construction: synchronization constructs

(1) What is a synchronous structure?

OpenMP is used for parallel programming. Naturally, there will be "data competition" and other related issues. Therefore, OpenMP also provides some synchronization commands ).

(2) master commands for synchronous construction

Command function: the master command specifies that a region will only be executed by the master thread in a team. Other threads in the team will ignore this region. This command does not have an implicit wait.

Command Format:

#pragma omp master  newline   structured_block

Command restrictions: master commands cannot jump out or jump into (goto) Master blocks.

(3) Critical commands for synchronous construction

Command function: The Critical command specifies that the code in a region can only be executed by one thread at a time.

Command Format:

#pragma omp critical [ (name) ]  newline   structured_block

Command restrictions: Critical commands cannot jump out or into (Goto, etc.) Critical blocks.

Note:

If an execution is executing the critical block code and other threads reach the critical block, the thread will block the wait until the first thread finishes executing the critical code. In fact, critical is equivalent to the simplified concept of critical section or lock in Win32 multithreading.

Instance:

#include "stdafx.h"int g;#define ADD_COUNT100000int main(int argc, char *argv[]){printf("Masterthread started\n\n");#pragma omp parallel{for (int i = 0; i < ADD_COUNT;i ++){#pragma omp criticalg = g + 1;// Will cause data races without critical directive.}}// End of parallel regionprintf("g = %d\n",g);printf("Expected g = %d\n",ADD_COUNT*4);// Assume the thread number is 4.printf("Masterthread finished\n");return(0);}

Here, if critical is not used, the result will be incorrect because of data competition.

The following example shows the same data competition. The two examples can also be used to compare the differences between parallel and parallel.

#include "stdafx.h"int g;#define ADD_COUNT1000000int main(int argc, char *argv[]){printf("Masterthread started\n\n");#pragma omp parallel forfor (int i = 0; i < ADD_COUNT;i ++){#pragma omp criticalg = g + 1;// Will cause data races without critical directive.} // End of parallel for regionprintf("g = %d\n",g);printf("Expected g = %d\n",ADD_COUNT);printf("Masterthread finished\n");return(0);}

About name option: in critical, there is also a name parameter. Critical can specify the name of the code segment. The code segment with the same name is treated as the same code segment, all untitled code segments are treated as a code segment.

The following two examples compare to understand the role of Name:

#include "stdafx.h"int g;int g1;#define ADD_COUNT1000000int main(int argc, char *argv[]){printf("Masterthread started\n\n");#pragma omp parallel forfor (int i = 0; i < ADD_COUNT;i ++){#pragma omp critical (namex)g = g + 1;// Will cause data races without critical directive.#pragma omp critical (namey)g1 = g1 + 1;// Will cause data races without critical directive.} // End of parallel for regionprintf("g = %d, g1 = %d\n",g, g1);printf("Expected g = g1 = %d\n",ADD_COUNT);printf("Masterthread finished\n");return(0);}

This example defines two global variables g and G1 for separate operations, so we can use two critical for synchronization. When the first thread operates the code (g) of the namex segment, another thread (the second thread) may be waiting. Then, the first thread enters the namey code segment, so the second thread can execute the code of the namex segment, because the operations are g and G1 respectively, it is completely allowed to be performed at the same time. However, in the following example, using different names of critical will result in an error:

#include "stdafx.h"int g;int g1;#define ADD_COUNT1000000int main(int argc, char *argv[]){printf("Masterthread started\n\n");#pragma omp parallel forfor (int i = 0; i < ADD_COUNT;i ++){#pragma omp critical (namex)g = g + 1;#pragma omp critical (namey)g = g + 1;// Still data races!} // End of parallel for regiong1 = g;printf("g = %d, g1 = %d\n",g, g1);printf("Expected g = g1 = %d\n",ADD_COUNT);printf("Masterthread finished\n");return(0);}

(4) barrier command for synchronous construction

Purpose: barrier is the simplest synchronization command used to synchronize all threads in the team. When a thread reaches barrier, it waits to know that all other threads are executed here.

Command Format:

#pragma omp barrier  newline

(5) taskwait command for synchronous construction

The new commands added by openmp3.0 are related to the task construction commands.

(6) synchronous construction of atomic commands

Command function: indicates that a memory location must be updated in the form of atomic operations. It is equivalent to the simplified use of critical.

Command Format:

#pragma omp atomic  newline   statement_expression

Note: Atomic is not followed by a structed Bock, but an expression. The use of atomic has some format requirements. For details, refer to the OpenMP spec description.

(7) flush command for synchronous construction

Command: Specifies a synchronization point where the thread variables are written into the real memory.

Command Format:

#pragma omp flush (list)  newline

(8) ordered command for synchronous construction

Specify the for loop iteration and serial execution in the same order.

It can only be used to instruct the for command of the ordered clause.

Command Format:

#pragma omp for ordered [clauses...]   (loop region)#pragma omp ordered  newline   structured_block   (endo of loop region)

Note:

For basic usage, Master, critical, and barrier commands must be mastered and understood. Other commands will be gradually understood later.

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.