ABOUT ME

Today
Yesterday
Total
  • [OpenMP] PI 구하기
    랭귀지/OpenMP 2012. 9. 20. 14:20

     

    PI 구하는 식 병렬화

     

    #include <stdio.h>
    #include <omp.h>
    
    static long num_steps = 1000000;
    double step;
    
    void Sequential()
    {
    	double before = omp_get_wtime();
    
    	int i;
    	double x, pi, sum = 0.0;
    
    	step = 1.0/(double) num_steps;
    	
    	for(i=1; i<=num_steps; i++)
    	{
    		x = (i-0.5) * step;
    		sum = sum + 4.0/(1.0 + x*x);
    	}
    		
    	pi = step * sum;
    	
    	printf("PI : %f\n",pi);
    	printf("%f sec Sequential Complete.\n",omp_get_wtime()-before);
    }
    
    void openMP()
    {
    	double before = omp_get_wtime();
    
    	int i;
    	double x, pi, sum = 0.0;
    
    	step = 1.0/(double) num_steps;
    	#pragma omp parallel shared(sum, num_steps, step) private(i, x)
    	{	
    		#pragma omp for reduction(+:sum)
    		for(i=1; i<=num_steps; i++)
    		{
    			x = (i-0.5) * step;
    			sum = sum + 4.0/(1.0 + x*x);
    		}
    		
    		#pragma omp single
    		pi = step * sum;
    	}
    
    	printf("PI : %f\n",pi);
    	printf("%f sec OpenMP Complete.\n",omp_get_wtime()-before);
    
    }
    
    
    int main()
    {
    	openMP();
    	Sequential();
    
    	return 0;
    }
    
    

     

    댓글

Designed by Tistory.