miércoles, 2 de julio de 2014

Simpson's rule in parallel

Simpson's rule

 In numerical analysis, Simpson's rule is a method for numerical integration, the numerical approximation of definite integrals.

 

More info

 

for this method of integral we use this solution strategy.



 
we can see the original form of the problem and how the solution will be planted.

in the formula above, each "Y" is a function evaluated at each 'X'. 'H' is the distance between each 'X'.

the simpson code is:

   simpson(a,b,n) {
        n=n*2;
        h=(b-a)/n;
       sum=f(a)+f(b);
       for(i=1;i<=(n-1));i+=2){
                sum=sum+4*f(a+h*i);
       }
       for(i=2;i<=(n-2);i+=2){
               sum=sum+2*f(a+h*i) ;
       }
       sum=sum*h;
       sum=sum/3;
      return sum;
   }

In this code f is a function to evaluate the integral, 'A' is a min value of the integral and 'B' is the max value. 'N' is the number of interval used to calculate the integral.


We can combine both 'FOR' into one.

for(i=1;i<(n-1);i++){
     if(i%2==0) sum=sum+2*f(a+h*i);
     else             sum=sum+4*f(a+h*i);
}
now parallelize this 'FOR'.

we can divide the 'FOR' between 'P' processors. 


for(i=1;i<(n-1);i++){
    if((i%size)==mypro){
              if(i%2==0) sum=sum+2*f(a+h*i);
              else             sum=sum+4*f(a+h*i);
    }
}

ready, the code would end.


simpson(a,b,n) {
       size;//number of cores,
       mypro;//id of core;

        n=n*2;
        h=(b-a)/n;
        if(mypro==0)    sum=f(a)+f(b);
        else   sum=0;
    
     for(i=1;i<(n-1);i++){
           if((i%size)==mypro){
                  if(i%2==0) sum=sum+2*f(a+h*i);
                  else             sum=sum+4*f(a+h*i);
           }
     }
        if(mypro!=0) send(sum,core0);
       else{
             for(i=1;i<size;i++) {
                     recv(ayu,core[i]) ;
                     sum=sum+ayu;
              }
       }
        if(mypro==0){
               sum=sum*h;
               sum=sum/3;
                return sum;
         } 
}
 

 

No hay comentarios:

Publicar un comentario