Learning OpenMP together (2) -- common library functions

Source: Internet
Author: User

I. Introduction

The first section demonstrates OpenMP through a simple example program. This section describes the four common library functions to further understand OpenMP.

 

Ii. Common Database Functions

Function prototype

Int omp_get_num_procs (void) returns the number of currently available processors

Int omp_get_num_threads (void) returns the number of active threads in the current parallel area. If it is called outside the parallel area, 1 is returned.

Int omp_get_thread_num (void) returns the current thread number (which is better than omp_get_thread_id)

Int omp_set_num_threads (void) is used to set the number of threads to be created when entering the parallel area.

 

Iii. Parallel Zone

# Include <iostream> <br/> # include <OMP. h> // header file to be included in OpenMP programming </P> <p> int main () <br/> {<br/> STD: cout <omp_get_num_procs () <STD: Endl; </P> <p> # pragma OMP parallel // specify the parallel area in the braces below <br/>{< br/> STD :: cout <"OpenMP" <STD: Endl; <br/>}</P> <p> return 0; <br/>}

Input result:

 

 

The system is dual-core, so two threads are created by default to execute parallel regions, so two OpenMP is printed.

 

If you set the system environment variables as follows:

 

Restart vs2008, then output

 

This is because by default, several threads are created with several cores, but the number of threads that can be created can be specified through environment variables. In fact, this can also be done by the omp_set_num_threads function, which will be described later.

 

Iv. Examples of common library functions

The following sample code demonstrates four library functions at the same time.

 # Include <iostream> <br/> # include <OMP. h> // header file to be included in OpenMP programming </P> <p> int main () <br/>{< br/> STD: cout <"CPU number: "<omp_get_num_procs () <STD: Endl; </P> <p> STD: cout <" parallel area 1: "<STD: Endl; <br/> # pragma OMP parallel // specify the parallel area in the braces below <br/>{< br/> STD :: cout <"Num of threads is" <omp_get_num_threads (); <br/> STD: cout <"; this thread ID is" <omp_get_thread_num () <STD: Endl; <br/>}</P> <p> STD: cout <"parallel area 2:" <STD: Endl; <br/> omp_set_num_threads (4 ); // create four threads in the parallel area <br/> # pragma OMP parallel // specify the parallel area in the braces below <br/>{< br/> STD :: cout <"Num of threads is" <omp_get_num_threads (); <br/> STD: cout <"; this thread ID is" <omp_get_thread_num () <STD: Endl; <br/>}< br/> return 0; <br/>}

Output:

 

By default, two threads are created in the first parallel area. The thread IDs are 0 and 1 respectively. Due to parallel output, printing is messy, however, we can see that the last 01 represents thread ID 0 and Id 1 respectively.

In the second parallel area, the number of threads to be created is 4, and the thread IDs are 0, 1, 2, and 3, respectively.

 

 

Section 5

Demonstrate how to use common OpenMP library functions. The next section describes the for loop parallelization.

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.