OpenMP parallel computing pI value

Source: Internet
Author: User

After creating a project with vs, you must right-click the project and choose Properties -- C/C ++ -- language. In the dialog box on the right, select YES For OpenMP;

ProgramCodeIs:

# Include<Stdio. h>

# Include<Time. h>

# Include<OMP. h>

 

Static long num_steps= 100000;

Double step, PI;

Void main ()

{

Clock_t T1 = clock ();

Int I;

Double X, sum = 0.0;

Step = 1.0/(double) num_steps;

# Pragma OMP parallel for ction (+: Sum)

For (I = 0; I <num_steps; I ++)

{

X = (I + 0.5) * step;

Sum = sum + 4.0/(1.0 + x * X );

Printf ("% d", omp_get_thread_num ());

}

Pi = Step * sum;

Clock_t t2 = clock ();

Printf ("Pi = % F, time: % d \ n", PI, t2-t1 );

}

The above Code uses the Protocol rduction (OP: List); in this way, each thread will have a private list. After the list calculation of all threads is complete, the private list value of each thread is normalized as a list value by op.

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.