Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. <% main.cpp>
  2.  
  3. #include"Simulation.h"
  4.  
  5. #include <iostream>
  6. #include <mpi.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9. #include <sys/time.h>
  10.  
  11. using namespace std;
  12.  
  13. const int LEN = 50;
  14. const int SIZE = LEN * LEN * LEN;
  15. const int REPETITIONS = 5;
  16. const int NUMBER_OF_PARTICLES_TO_REMOVE_ONCE = 2;
  17. const int SHUFFLE_TIMES = 2*SIZE;
  18. const double BOX_SIZE = 10.0;
  19. const int ALL_PARTICLES = SIZE + REPETITIONS * NUMBER_OF_PARTICLES_TO_REMOVE_ONCE;
  20.  
  21. void generate( double *x, double *y, double *z ) {
  22.       int l = 0;
  23.       for ( int k = 0; k < LEN; k++ )
  24.        for ( int j = 0; j < LEN; j++ )
  25.          for ( int i = 0; i < LEN; i++ ) {
  26. //      cout << " l[ " << i << ", " << j << ", " << k << " ] = " << l << endl;
  27.         x[l] = i * BOX_SIZE;
  28.         y[l] = j * BOX_SIZE;
  29.         z[l] = k * BOX_SIZE;
  30.         l++;
  31.          }
  32.          
  33.       // dodajemy troche czastek w poblizu [0,0,0]
  34.       double size = BOX_SIZE * 0.9;
  35.       double pos = size;
  36.       for ( int i = SIZE; i < ALL_PARTICLES; i++ ) {
  37.         size *= 0.5;
  38.         pos -= size;
  39.         x[i] = 0.0;
  40.         y[i] = 0.0;
  41.         z[i] = pos;
  42.         cout << "z [ " << i << " ] = " << pos << endl;
  43.       }              
  44.  
  45.  
  46.       srandom( time( NULL ) );   
  47.       int i, j, k;
  48.       double xtmp, ytmp, ztmp;
  49.       for ( int k = 0; k < SHUFFLE_TIMES; k++ ) {
  50.          i = random() % ALL_PARTICLES;
  51.          j = random() % ALL_PARTICLES;
  52.      
  53.          xtmp = x[ i ];
  54.          ytmp = y[ i ];
  55.          ztmp = z[ i ];
  56.          
  57.          x[ i ] = x[ j ];
  58.          y[ i ] = y[ j ];
  59.          z[ i ] = z[ j ];
  60.          
  61.          x[ j ] = xtmp;
  62.          y[ j ] = ytmp;
  63.          z[ j ] = ztmp;
  64.       }
  65. }
  66.  
  67. int main(int ac, char **av) {
  68.  
  69.     MPI_Init(&ac, &av);
  70.  
  71.     int rank;
  72.  
  73.     MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
  74.  
  75.     Simulation *sim = new Simulation();
  76.  
  77.     if ( ! rank ) {
  78.       double *x = new double[ ALL_PARTICLES ];
  79.       double *y = new double[ ALL_PARTICLES ];
  80.       double *z = new double[ ALL_PARTICLES ];
  81.    
  82.       // data is generated for process rank ==0 only
  83.       generate( x, y, z );
  84.      
  85.       // sharing particles with process rank==0
  86.       sim->setParticles( x, y, z, ALL_PARTICLES );
  87.     }
  88.  
  89.     struct timeval tf;
  90.     gettimeofday( &tf, NULL );
  91.     double t0 = tf.tv_sec + tf.tv_usec * 0.000001;
  92.    
  93.     for (int i = 0; i < REPETITIONS; i++) {
  94.         sim->remove( NUMBER_OF_PARTICLES_TO_REMOVE_ONCE );
  95.         sim->calcAvgMinDistance();
  96.  
  97.         if (!rank) {
  98.            cout << "Krok " << ( i + 1 ) << " -> " <<
  99.                 sim->getAvgMinDistance() << endl;
  100.         }
  101.     } // REPETITIONS
  102.  
  103.     gettimeofday( &tf, NULL );
  104.     double tk = tf.tv_sec + tf.tv_usec * 0.000001;
  105.  
  106.         cout << "Pomiar czasu " << ( tk - t0 ) << " sekund" <<  endl;
  107.  
  108.     MPI_Finalize();
  109.     return 0;
  110. }
  111.  
  112. <% Helper.h>
  113.  
  114. #ifndef HELPER_H_
  115. #define HELPER_H_
  116.  
  117. #include<math.h>
  118.  
  119. class Helper {
  120.    public:
  121.       static double getDistanceSQ( double *x, double *y, double *z, int i, int j );
  122.      
  123.       static double getDistance( double *x, double *y, double *z, int i, int j );
  124.      
  125.       static double middle( double *x, int i, int j );
  126.  
  127. };
  128.  
  129. #endif
  130.  
  131. <% Helper.cpp>
  132.  
  133. #include"Helper.h"
  134.  
  135. double Helper::getDistanceSQ( double *x, double *y, double *z, int i, int j ) {
  136.    double dx = x[ i ] - x[ j ];
  137.    double dy = y[ i ] - y[ j ];
  138.    double dz = z[ i ] - z[ j ];
  139.    return dx*dx + dy*dy + dz*dz;
  140. }
  141.      
  142. double Helper::getDistance( double *x, double *y, double *z, int i, int j ) {
  143.    return sqrt( getDistanceSQ( x, y, z, i, j ) );
  144. }
  145.  
  146. double Helper::middle( double *x, int i, int j ) {
  147.   return ( x[ i ] + x[ j ] ) * 0.5;
  148. }
  149.  
  150. <% Simulation.h>
  151.  
  152. /*
  153.  * Simulation.h
  154.  *
  155.  */
  156.  
  157. #ifndef SIMULATION_H_
  158. #define SIMULATION_H_
  159.  
  160. class Simulation {
  161. public:
  162.     Simulation();
  163.     virtual ~Simulation();
  164.  
  165. // those two methods are only called for process with rank==0
  166.     void setParticles( double *x, double *y, double *z, int numberOfParticles );
  167.     double getAvgMinDistance( void );
  168.  
  169. //those two methods are called for all processes
  170.     void remove( int numberOfPairsToRemove );
  171.     void calcAvgMinDistance( void );
  172. };
  173.  
  174. #endif /* SIMULATION_H_ */
  175.  
  176. <% Simulation.cpp>
  177.  
  178. #include"Simulation.h"
  179.  
  180. Simulation::Simulation() {}
  181. Simulation::~Simulation() {}
  182.  
  183. void Simulation::setParticles( double *x, double *y, double *z, int numberOfParticles ) {}
  184. double Simulation::getAvgMinDistance( void ) { return 0.0; }
  185. void Simulation::remove( int numberOfPairsToRemove ) {}
  186. void Simulation::calcAvgMinDistance( void ) {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement