Article Title: OpenMP multi-thread programming analysis. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Today, I read a little bit about multithreading, extract the content, and share it with me:
OpenMP
OpenMP is a multi-threaded programming technology for shared memory. It is defined by some internationally influential software and hardware vendors. it is an application programming interface (API) that guides the compilation of multi-thread and shared memory parallel statements)
OpenMP is a multi-processor multi-thread parallel programming language for shared memory and distributed shared memory. OpenMP is an application programming interface that can be used to display and guide multithreading and shared memory parallel. its specification is initiated by SGI. openMP is highly portable and supports multiple programming languages. openMP supports multiple platforms, including most UNIX-like and WindowsNT systems. openMP originally designed parallel programming methods for the system structure of shared memory multi-processing, which is quite different from the model of parallel programming through message transmission. this is used to handle the situation where multiple processors share a memory device. multiple Processors use the same memory addressing space when accessing the memory. SMP is a shared memory architecture, and the distributed shared memory system also belongs to the shared memory multi-processor architecture, distributed Shared Memory virtualizes the memory resources of multiple machines to form an agreed memory space for the use of processors on multiple machines. OpenMP also provides support for such machines.
OpenMP
The OpenMP programming model is based on threads and provides guidance on parallelism by compiling guidance statements. It provides programmers with complete control over parallelism. A new statement is introduced for programming. the execution model of OpenMP adopts the form of Fork-Join. When the Execution Mode of Fork-Join starts, only one running thread called "main thread" exists. during the running process of the main thread, when parallel computing is required, the derived thread is used to execute parallel characters. During parallel execution, the main thread and the derived thread work together, after the parallel code ends, the derived thread exits or suspends and stops working. The control process is returned to the separate main thread. The OpenMP function is provided in two ways: Compile the instruction statement and Runtime library function, and control program running flexibly through environment variables.
Compile the instruction statement:
It indicates that the compiler will recognize specific comments when compiling the program, and these specific comments contain some statements of the OpenMP program. in the C/C ++ program, OpenMP starts with # pragma omp, followed by specific functional instructions. it has the following forms:
# Pragma omp [Clause [[,] clause]...]
The directive section contains specific compilation instructions, including parallel, for, parallel for, section, sections, single, mater,
Critical, flush, ordered, and atomic. these Compilation instruction statements are used for task allocation or synchronization. the optional clause provides the relevant parameters for compiling a known statement. The clause can affect the specific behavior of the compiled guiding statement. Each compiled guiding statement has a series of clauses suitable for it. five guidance statements cannot be followed by other clauses:
Master, critical, flush, ordered, atomic.
Runtime library functions:
The OpenMP Runtime library function is used to set and obtain information about the execution environment. it also contains a series of APIS for synchronization. to use the functions included in the runtime function library, you should include the OpenMP header file (omp) in the corresponding source file. h. openMP Runtime library functions are called using functions similar to those in the corresponding programming language.
It can be seen from the Compilation instruction statement and Runtime library function. OpenMP combines two parallel programming methods at the same time. By compiling instruction statements, you can gradually transform a serial program into a parallel program, to achieve the purpose of incremental update programs, thus reducing the burden on programmers. At the same time, this method can save the serial program and parallel program in the same source code file, reducing the maintenance burden. when running OpenMP, you need to run the function library and obtain some environment variables to control the running process. The environment variables mentioned here are some parameters used to control function running in the dynamic function library.
In the above two aspects, the Compilation instruction is the most important part in the composition of OpenMP and also the key to compiling OpenMP programs.