The Open MP calculates the value of π

Source: Internet
Author: User

After creating a new project with VS, First right-click the project, the attribute--c/c++--language, and the OpenMP support option in the right dialog box is;

The program code is:

#include <stdio.h>#include<time.h>#include<omp.h>Static LongNum_steps =100000;DoubleStep,pi;voidMain () {clock_t T1=clock (); inti; DoubleX,sum =0.0; Step=1.0/(Double) num_steps;#pragmaOMP parallel for Reduction (+: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, spents:%d\n", pi,t2-t1);}

The code above uses the protocol rduction (op:list), so that each thread will have a private list, and when all the threads ' list calculation is complete, the private list value of each thread will be the OP protocol as a list value. If you do not make a return, it may result in a different run.

The Open MP calculates the value of π

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.