view PoliMI2012/examples/Pi.m @ 4:0a1567794b40

[mq]: folder_reorganization
author Carlo de Falco <cdf@users.sourceforge.net>
date Thu, 08 Nov 2012 09:55:34 +0100
parents
children
line wrap: on
line source

  T=clock; 
  MPI_ANY_SOURCE = -1;
  MPI_Init ();  
  MPI_COMM_WORLD = MPI_Comm_Load ("NEWORLD");           
  rnk   = MPI_Comm_rank (MPI_COMM_WORLD);
  siz   = MPI_Comm_size (MPI_COMM_WORLD);
  SLV = logical(rnk);                 
  MST = ~ SLV;                        
  width=1/N; lsum=0;
  i=rnk:siz:N-1;    
  x=(i+0.5)*width;  
  lsum=sum(4./(1+x.^2));
  TAG=7;                
  if SLV                
    MPI_Send (lsum, 0, TAG, MPI_COMM_WORLD);
  else                  
    Sum =lsum;          
    for slv=1:siz-1     
      lsum = MPI_Recv (MPI_ANY_SOURCE, TAG, MPI_COMM_WORLD);
      Sum += lsum;      
    endfor              
  endif
  MPI_Finalize ();