The OpenMP framework is a powerful way to use C + +, C + +, and http://www.aliyun.com/zixun/aggregation/29818.html ">fortran" for concurrent programming. The GNU Compiler Collection (GCC) V4.2 supports the OpenMP 2.5 standard, while GCC 4.4 supports the latest OpenMP 3 standard. Other compilers, including Microsoft®visual Studio, also support OpenMP. In this article, you can learn to use OpenMP compilation instructions (pragma) to find support for some of the application programming interfaces (APIs) that OpenMP provides, and to test OpenMP with some parallel algorithms. This article will use GCC 4.2 as the preferred compiler.
Your first OpenMP program
Let's start with a Hello, world!. The print application begins, and it includes an additional compilation instruction. Listing 1 shows the relevant code.
When compiling and running the code in Listing 1 using g++, the console should display a Hello, world!. Now recompile your code with the-FOPENMP option. Listing 2 shows the output.
Listing 2. Compiling and running code using the-FOPENMP command
What happened? #pragma omp parallel only works if you specify-FOPENMP compiler options. During compilation, GCC generates code at run time based on hardware and operating system configuration, creating as many threads as possible. The starting routine for each thread is the code in the code block that is behind the directive. This behavior is implicit parallelism, and OpenMP consists essentially of a powerful set of compilation instructions that save you the effort to write a large number of sample files. (For comparison purposes, you need to know how to implement your previous program using Portable keyboard-based System Interface (POSIX) threads [Pthreads].) The computer I use runs the Intel®core i7 processor with four physical cores, each with two logical cores, so the output from Listing 2 looks reasonable (8 threads = 8 logical cores).
Next, let's Learn more about parallel compilation instructions.
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.