Learning OpenMP (5) together-effective data conventions

Source: Internet
Author: User

I. Introduction

This section describes how to use function.

 

Ii. Example of Function

# Include <iostream> <br/> # include <OMP. h> // header file to be included in OpenMP programming </P> <p> int main () <br/>{< br/> int sum = 0; </P> <p> STD: cout <"before:" <sum <STD: Endl; </P> <p> # pragma OMP parallel for ction (+: Sum) <br/> for (INT I = 0; I <10; ++ I) <br/>{< br/> sum = sum + I; <br/> STD: cout <sum <STD: Endl; <br/>}</P> <p> STD: cout <"after:" <sum <STD: Endl; <br/> return 0; <br/>}< br/>

The output is as follows:

Sum is shared. After callback, each thread calculates its own sum based on the statement of callback (+: Sum), and then sums the sum of each thread.

We can see that the sum value of the first thread is 0, 1, 3, 6, 10, and the sum value of the second thread is 5, 11, 18, 26, 35 at a time; the last 10 + 35 = 45.

 

If the function declaration is removed, the following output is displayed:

 

The calculation procedure is as follows:

Sum = 0 for the first thread; sum = 5 for the second thread

The first thread sum = 1 + 5 = 6; the second thread sum = 6 + 6 = 12

The first thread sum = 2 + 12 = 14; the second thread sum = 7 + 14 = 21

The first thread sum = 3 + 21 = 24; the second thread sum = 8 + 24 = 32

The first thread sum = 4 + 32 = 36; the second thread sum = 9 + 36 = 45

Although the results are correct, the two threads are not sure about the shared sum operation, which will lead to data competition. For example, the calculation steps may be as follows:

Sum = 0 for the first thread; sum = 5 for the second thread

The first thread sum = 1 + 5 = 6; the second thread sum = 6 + 6 = 12

The first thread sum = 2 + 12 = 14; the second thread sum = 7 + 14 = 21

The first thread sum = 3 + 21 = 24; the second thread sum = 8 + 21 = 29 // when the first thread does not change sum to 24, the second thread reads the sum value.

The first thread sum = 4 + 29 = 33; the second thread sum = 9 + 33 = 42 // cause a result error.

 

The function statement can be viewed as follows:

1. The sum principle operation is ensured.

2. The execution results of multiple threads are calculated using the operators declared in callback. The addition operator is used as an example:

Assume that the initial value of sum is 10, and the initial value of sum for each thread in the parallel area declared by 'Operation ction (+: Sum) is 0 (required). After the parallel processing ends, the initialization value 10 of sum and the sum value calculated by each thread are added.

 

III. Statement Form

We have learned about the statement form of function above. The details are as follows:

Ction (OPERATOR: var1, val2 ,...)

The operator and the initial values of the agreed variables are as follows:

Default initial value of the operator Data Type

+ Integer and floating point 0

-Integer, floating point 0

* Integer, floating point 1

& All integers are 1

| Integer 0
^ Integer 0

& Integer 1

| Integer 0

 

Summary

This section describes the usage of function. The following describes thread synchronization.

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.