How to use OpenMP attributes in C + + code

Source: Internet
Author: User
Keywords characteristics C++ OPENMP
Tags aliyun code compiler framework gcc gnu how to how to use

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.

Listing 1. Using OpenMP's Hello World program

#include <iostream>int main () {#pragma omp parallel {std::cout << "Hello world!\n";}}

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

tintin$ g++ test1.cpp-fopenmptintin$/a.out Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!

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.

Related Article

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.