ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [OpenMP] SOR( Successive Over Relaxation) 병렬화
    랭귀지/OpenMP 2012. 9. 20. 14:36

     SOR( Successive Over Relaxation) 병렬화

     

    #include <stdio.h>
    #include <omp.h>
    
    
    #define width 256
    #define height 256
    
    void Sequential()
    {
    	double before = omp_get_wtime();	
    	float A[width][height], B[width][height];
    	int i,j,t;
    
    	for(i=0; i<width; i++){
    		for(j=0; j<height; j++){
    			B[i][j] = (float) (i+1)/(j+1);
    		}
    	}
    
    	for(t=0; t<1000; t++)
    	{
    		for(i=1; i<width-1; i++)
    		{
    			for(j=1; j<height-1; j++)
    			{
    				A[i][j] = (B[i+1][j] + B[i-1][j] + B[i][j+1] + B[i][j-1])/4;
    			}
    		}
    
    		for(i=1; i<width-1; i++)
    		{
    			for(j=1; j<height-1; j++)
    			{
    				B[i][j] = A[i][j];
    			}
    		}
    	}
    
    		
    	printf("%f sec Sequential Complete.\n",omp_get_wtime()-before);
    
    }
    
    
    void openMP()
    {
    	double before = omp_get_wtime();	
    	float A[width][height], B[width][height];
    	int i,j,t;
    
    	#pragma omp parallel for private(i,j) shared(B)
    	for(i=0; i<width; i++){
    		for(j=0; j<height; j++){
    			B[i][j] = (float) (i+1)/(j+1);
    		}
    	}
    
    	for(t=0; t<1000; t++)
    	{
    		#pragma omp parallel for private(i,j) shared(A,B)
    		for(i=1; i<width-1; i++)
    		{
    			for(j=1; j<height-1; j++)
    			{
    				A[i][j] = (B[i+1][j] + B[i-1][j] + B[i][j+1] + B[i][j-1])/4;
    			}
    		}
    
    		#pragma omp parallel for private(i,j) shared(A,B)
    		for(i=1; i<width-1; i++)
    		{
    			for(j=1; j<height-1; j++)
    			{
    				B[i][j] = A[i][j];
    			}
    		}
    	}
    
    
    	printf("%f sec OpenMP Complete.\n",omp_get_wtime()-before);
    }
    
    int main()
    {
    	openMP();
    	Sequential();
    
    	
    	return 0;
    }
    
    
    

    댓글

Designed by Tistory.