并行计算--Java--求π并行实现



  1.  public void run()   
  2. {  
  3.     
  4.   int i;  
  5.   step=1.0/(double)num_steps_wy;  
  6.   for(i=start_wy;i<num_steps_wy;i+=2)  
  7.   {  
  8.    x=(i+0.5)*step;  
  9.    sum=sum+4.0/(1.0+x*x);  
  10.   }  
  11.   
  12.  }  
  13.  public void seril_pi()  
  14.  {  
  15.   int i;  
  16.   step=1.0/(double)num_steps_wy;  
  17.   for(i=1;i<num_steps_wy;i++)  
  18.   {  
  19.    x=(i+0.5)*step;  
  20.    sum=sum+4.0/(1.0+x*x);  
  21.   }  
  22.  }  
  23.   
  24.  public static void main(String[] args) throws InterruptedException {  
  25.   
  26.   double pi_wy,sum_wy=0.0,seri_t_wy,para_t_wy;  
  27.   
  28.   
  29.   Pi_thread thread1=new Pi_thread(1);  
  30.   Pi_thread thread2=new Pi_thread(2);  
  31.   
  32.   double t1=System.currentTimeMillis();  
  33.   
  34.   thread1.start();  
  35.   thread2.start();  
  36.   thread1.join();  
  37.   thread2.join();  
  38.   
  39.   double t2=System.currentTimeMillis();  
  40.   
  41.   sum_wy=thread1.sum+thread2.sum;  
  42.   pi_wy=thread1.step*sum_wy;  
  43.   para_t_wy=t2-t1;  
  44.   System.out.println("并行结果: "+pi_wy);  
  45.   System.out.println("并行时间: "+para_t_wy);  
  46.     
  47.   Pi_thread thread3=new Pi_thread(3);  
  48.   
  49.   t1=System.currentTimeMillis();  
  50.   
  51.   thread3.seril_pi();  
  52.   
  53.   t2=System.currentTimeMillis();  
  54.   
  55.   pi_wy=thread3.sum*thread3.step;  
  56.   
  57.   seri_t_wy=t2-t1;  
  58.   System.out.println("窜行结果: "+pi_wy);  
  59.   System.out.println("串行时间: "+seri_t_wy);  
  60.   System.out.println("加速比: "+(seri_t_wy/para_t_wy));  
  61.     
  62.  }  
  63.   
  64. }  

并行结果: 3.1415926135900225
并行时间: 484.0
窜行结果: 3.1415926135904266
串行时间: 1000.0
加速比: 2.0661157024793386
  1.  public void run()   
  2. {  
  3.     
  4.   int i;  
  5.   step=1.0/(double)num_steps_wy;  
  6.   for(i=start_wy;i<num_steps_wy;i+=2)  
  7.   {  
  8.    x=(i+0.5)*step;  
  9.    sum=sum+4.0/(1.0+x*x);  
  10.   }  
  11.   
  12.  }  
  13.  public void seril_pi()  
  14.  {  
  15.   int i;  
  16.   step=1.0/(double)num_steps_wy;  
  17.   for(i=1;i<num_steps_wy;i++)  
  18.   {  
  19.    x=(i+0.5)*step;  
  20.    sum=sum+4.0/(1.0+x*x);  
  21.   }  
  22.  }  
  23.   
  24.  public static void main(String[] args) throws InterruptedException {  
  25.   
  26.   double pi_wy,sum_wy=0.0,seri_t_wy,para_t_wy;  
  27.   
  28.   
  29.   Pi_thread thread1=new Pi_thread(1);  
  30.   Pi_thread thread2=new Pi_thread(2);  
  31.   
  32.   double t1=System.currentTimeMillis();  
  33.   
  34.   thread1.start();  
  35.   thread2.start();  
  36.   thread1.join();  
  37.   thread2.join();  
  38.   
  39.   double t2=System.currentTimeMillis();  
  40.   
  41.   sum_wy=thread1.sum+thread2.sum;  
  42.   pi_wy=thread1.step*sum_wy;  
  43.   para_t_wy=t2-t1;  
  44.   System.out.println("并行结果: "+pi_wy);  
  45.   System.out.println("并行时间: "+para_t_wy);  
  46.     
  47.   Pi_thread thread3=new Pi_thread(3);  
  48.   
  49.   t1=System.currentTimeMillis();  
  50.   
  51.   thread3.seril_pi();  
  52.   
  53.   t2=System.currentTimeMillis();  
  54.   
  55.   pi_wy=thread3.sum*thread3.step;  
  56.   
  57.   seri_t_wy=t2-t1;  
  58.   System.out.println("窜行结果: "+pi_wy);  
  59.   System.out.println("串行时间: "+seri_t_wy);  
  60.   System.out.println("加速比: "+(seri_t_wy/para_t_wy));  
  61.     
  62.  }  
  63.   
  64. }  

并行结果: 3.1415926135900225
并行时间: 484.0
窜行结果: 3.1415926135904266
串行时间: 1000.0
加速比: 2.0661157024793386

你可能感兴趣的:(并行计算--Java--求π并行实现)