Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #include"Error.h"
  2. #include"Function.h"
  3.  
  4. #include <iostream>
  5. #include <mpi.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8. #include <sys/time.h>
  9. #include <math.h>
  10.  
  11. using namespace std;
  12.  
  13. const int LEN = 1250;
  14. const int LEN_2 = LEN/2;
  15. const int SIZE = LEN * LEN;
  16. const int SHUFFLE_TIMES = 2*SIZE;
  17. const double MULT = 0.1;
  18. const int COMPLEXITY = 8;
  19.  
  20. double generate( double x, double y, double *x0, double *y0, double *a, double *c, int indexS, int indexE ) {
  21.   double v = 0.0;
  22.   for ( int i = indexS; i < indexE; i++ ) {
  23.      v += Function::value( x, y, x0[i], y0[i], a[i], c[ i ] );
  24.   }
  25.   return v;
  26. }
  27.  
  28. void generate( double *v, double *x0, double *y0, double *a, double *c ) {
  29.   for ( int i = 0; i < LEN; i++ )
  30.    for ( int j = 0; j < LEN; j++ )
  31.      v[ i * LEN + j ] = generate( ( i - LEN_2 ) * MULT, ( j - LEN_2 ) * MULT, x0, y0, a, c, 0, COMPLEXITY );
  32. }
  33.  
  34. void shuffle( double *v ) {
  35.           // losowo przestawiamy polozenia czastek
  36.       srandom( time( NULL ) );   
  37.       int i, j, k;
  38.       double vtmp;
  39.       for ( int k = 0; k < SHUFFLE_TIMES; k++ ) {
  40.          i = random() % SIZE;
  41.          j = random() % SIZE;
  42.      
  43.          vtmp = v[ i ];
  44.  
  45.          v[ i ] = v[ j ];
  46.              v[ i ] = vtmp;
  47.       }
  48. }
  49.  
  50. void test( Error *err, double errorExpected, int rank ) {
  51.   err->calcError();
  52.  
  53.   double error;
  54.   if ( rank == 0 ) {
  55.     error = err->getError();
  56.     cout << endl << "Test blad " << error << endl;
  57.  
  58.     if ( fabs( error - errorExpected ) > ( 0.001 + 0.001 * errorExpected ) ) {
  59.         cerr << "Blad powinno byc " << errorExpected << endl;
  60.         MPI_Finalize();
  61.         exit(0);
  62.     } else {
  63.       cout << " - - - - - - OK" << endl;
  64.     }
  65.   }
  66.  
  67. }
  68.  
  69. void test1( Error *err, int rank ) {
  70.   if ( rank == 0 ) {
  71.      double x0[] = { -3, -2, -2, -1, 1, 2,  2, 3 };
  72.      double y0[] = { -3, -3,  3, -1, 1, -3, 3, 3 };
  73.      double a[] = { 1,2,3,4,5,6,7,8 };
  74.      double c[] = { 0.1, 0.05, 0.02, 0.01, 0.01, 0.01, 0.02, 0.05 };    
  75.      err->setCoefficients( x0, y0, a, c, 8 );
  76.   }
  77.   test( err, 0, rank );
  78. }
  79.  
  80. void test2( Error *err, int rank ) {
  81.   if ( rank == 0 ) {
  82.      double x0[] = { -3, -1, -2, -1, 1, 2,  2, 3, 1, 2, 3, 4, 5, 6, 7 };
  83.      double y0[] = { -3, -3,  3, -1, 1, -3, 1, 3, 1, 2, 3, 4, 1, 2, 3  };
  84.      double a[] = { 1,2,3,4,5,6,7,8, 7, 6, 5, 4, 1, 2, 3 };
  85.      double c[] = { 0.1, 0.05, 0.02, 0.01, 0.02, 0.01, 0.02, 0.05, 1, 1, 1, 1, 1, 2, 3 };  
  86.      err->setCoefficients( x0, y0, a, c, 15 );
  87.   }
  88.   test( err, 357.729, rank );
  89. }
  90.  
  91. void test3( Error *err, int rank ) {
  92.   if ( rank == 0 ) {
  93.      double x0[] = { -3, -1, -2, -1, 1, 2,  2, 3, -3, -1, -2, -1, 1, 2,  2, 3, 1, 2, 3, 4, 5, 6, 7, -3, -1, -2, -1, 1, 2,  2, 3, 1, 2, 3, 4, 5, 6, 7 };
  94.      double y0[] = { -3, -3,  3, -1, 1, -3, 1, 3, -3, -1, -2, -1, 1, 2,  2, 3, 1, 2, 3, 4, 5, 6, 7, -3, -1, -2, -1, 1, 2,  2, 3, 1, 2, 3, 4, 5, 6, 7 };
  95.      double *a = new double[ 38 ];
  96.      double *c = new double[ 38 ];
  97.      for ( int i = 0; i < 38; i++ ) {
  98.        a[i] = 1 + i / 38.0;
  99.        c[i] = 2 + i / 38.0;
  100.      }
  101.      err->setCoefficients( x0, y0, a, c, 38 );
  102.   }
  103.   test( err, 2975.86, rank );
  104. }
  105.  
  106. void test4( Error *err, int rank ) {
  107.   if ( rank == 0 ) {
  108.      double *x0 = new double[ 150 ];
  109.      double *y0 = new double[ 150 ];
  110.      double *a = new double[ 150 ];
  111.      double *c = new double[ 150 ];
  112.      for ( int i = 0; i < 150; i++ ) {
  113.        x0[i] = 5 - i * 0.2;
  114.        a[i] = 1 + i / 8.0;
  115.        y0[i] = 2 - i * 0.22;
  116.        c[i] = 2 + i / 38.0;
  117.      }
  118.      err->setCoefficients( x0, y0, a, c, 150 );
  119.   }
  120.   test( err, 3303.04, rank );
  121. }
  122.  
  123. void testL( Error *err, int rank ) {
  124.  
  125.   double *x0, *y0, *a, *c;
  126.  
  127.   if ( rank == 0 ) {
  128.      cout << "Test pozwalajacy na oszacowanie przyspieszenia" << endl;
  129.      x0 = new double[ 111 ];
  130.      y0 = new double[ 111 ];
  131.      a = new double[ 111 ];
  132.      c = new double[ 111 ];
  133.      for ( int i = 0; i < 111; i++ ) {
  134.        x0[i] = 5 - i * 0.2;
  135.        a[i] = 1 + i / 1.1;
  136.        y0[i] = 2 - i * 0.4;
  137.        c[i] = 2 + i / 38.0;
  138.      }
  139.   }
  140.  
  141.   double error = 0;
  142.  
  143.   for ( int i = 0; i < 20; i++ ) {
  144.  
  145.     if ( rank == 0 ) {
  146.        a[i] = i;
  147.        err->setCoefficients( x0, y0, a, c, 111-i*3 );
  148.     }
  149.  
  150.     err->calcError();
  151.  
  152.     if ( rank == 0 )
  153.       error += err->getError();
  154.   }
  155.  
  156.   if ( rank == 0 )
  157.     cout << "Uwaga: ta wartosc nie moze zalezec od liczby uzytych procesow = " << error << endl;
  158. }
  159.  
  160. int main(int ac, char **av) {
  161.  
  162.     MPI_Init(&ac, &av);
  163.  
  164.     int rank;
  165.  
  166.     MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
  167.  
  168.     Error *err = new Error();
  169.     double *v;
  170.  
  171.     if ( ! rank ) {
  172.       v = new double[ SIZE ];
  173.       double x0[] = { -3, -2, -2, -1, 1, 2,  2, 3 };
  174.       double y0[] = { -3, -3,  3, -1, 1, -3, 3, 3 };
  175.           double a[] = { 1,2,3,4,5,6,7,8 };
  176.           double c[] = { 0.1, 0.05, 0.02, 0.01, 0.01, 0.01, 0.02, 0.05 };  
  177.  
  178.       generate( v, x0, y0, a, c );
  179.       shuffle( v );
  180.      
  181.       // udostepniam dane dla procesu = 0
  182.       err->setValues( v, LEN, MULT );
  183.     }
  184.  
  185.     test1( err, rank );      
  186.     test2( err, rank );      
  187.     test3( err, rank );      
  188.     test4( err, rank );      
  189.     test1( err, rank );      
  190.  
  191.     testL( err, rank );
  192.  
  193.     MPI_Finalize();
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement