Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"Error.h"
- #include"Function.h"
- #include <iostream>
- #include <mpi.h>
- #include <time.h>
- #include <stdlib.h>
- #include <sys/time.h>
- #include <math.h>
- using namespace std;
- const int LEN = 1250;
- const int LEN_2 = LEN/2;
- const int SIZE = LEN * LEN;
- const int SHUFFLE_TIMES = 2*SIZE;
- const double MULT = 0.1;
- const int COMPLEXITY = 8;
- double generate( double x, double y, double *x0, double *y0, double *a, double *c, int indexS, int indexE ) {
- double v = 0.0;
- for ( int i = indexS; i < indexE; i++ ) {
- v += Function::value( x, y, x0[i], y0[i], a[i], c[ i ] );
- }
- return v;
- }
- void generate( double *v, double *x0, double *y0, double *a, double *c ) {
- for ( int i = 0; i < LEN; i++ )
- for ( int j = 0; j < LEN; j++ )
- v[ i * LEN + j ] = generate( ( i - LEN_2 ) * MULT, ( j - LEN_2 ) * MULT, x0, y0, a, c, 0, COMPLEXITY );
- }
- void shuffle( double *v ) {
- // losowo przestawiamy polozenia czastek
- srandom( time( NULL ) );
- int i, j, k;
- double vtmp;
- for ( int k = 0; k < SHUFFLE_TIMES; k++ ) {
- i = random() % SIZE;
- j = random() % SIZE;
- vtmp = v[ i ];
- v[ i ] = v[ j ];
- v[ i ] = vtmp;
- }
- }
- void test( Error *err, double errorExpected, int rank ) {
- err->calcError();
- double error;
- if ( rank == 0 ) {
- error = err->getError();
- cout << endl << "Test blad " << error << endl;
- if ( fabs( error - errorExpected ) > ( 0.001 + 0.001 * errorExpected ) ) {
- cerr << "Blad powinno byc " << errorExpected << endl;
- MPI_Finalize();
- exit(0);
- } else {
- cout << " - - - - - - OK" << endl;
- }
- }
- }
- void test1( Error *err, int rank ) {
- if ( rank == 0 ) {
- double x0[] = { -3, -2, -2, -1, 1, 2, 2, 3 };
- double y0[] = { -3, -3, 3, -1, 1, -3, 3, 3 };
- double a[] = { 1,2,3,4,5,6,7,8 };
- double c[] = { 0.1, 0.05, 0.02, 0.01, 0.01, 0.01, 0.02, 0.05 };
- err->setCoefficients( x0, y0, a, c, 8 );
- }
- test( err, 0, rank );
- }
- void test2( Error *err, int rank ) {
- if ( rank == 0 ) {
- double x0[] = { -3, -1, -2, -1, 1, 2, 2, 3, 1, 2, 3, 4, 5, 6, 7 };
- double y0[] = { -3, -3, 3, -1, 1, -3, 1, 3, 1, 2, 3, 4, 1, 2, 3 };
- double a[] = { 1,2,3,4,5,6,7,8, 7, 6, 5, 4, 1, 2, 3 };
- 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 };
- err->setCoefficients( x0, y0, a, c, 15 );
- }
- test( err, 357.729, rank );
- }
- void test3( Error *err, int rank ) {
- if ( rank == 0 ) {
- 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 };
- 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 };
- double *a = new double[ 38 ];
- double *c = new double[ 38 ];
- for ( int i = 0; i < 38; i++ ) {
- a[i] = 1 + i / 38.0;
- c[i] = 2 + i / 38.0;
- }
- err->setCoefficients( x0, y0, a, c, 38 );
- }
- test( err, 2975.86, rank );
- }
- void test4( Error *err, int rank ) {
- if ( rank == 0 ) {
- double *x0 = new double[ 150 ];
- double *y0 = new double[ 150 ];
- double *a = new double[ 150 ];
- double *c = new double[ 150 ];
- for ( int i = 0; i < 150; i++ ) {
- x0[i] = 5 - i * 0.2;
- a[i] = 1 + i / 8.0;
- y0[i] = 2 - i * 0.22;
- c[i] = 2 + i / 38.0;
- }
- err->setCoefficients( x0, y0, a, c, 150 );
- }
- test( err, 3303.04, rank );
- }
- void testL( Error *err, int rank ) {
- double *x0, *y0, *a, *c;
- if ( rank == 0 ) {
- cout << "Test pozwalajacy na oszacowanie przyspieszenia" << endl;
- x0 = new double[ 111 ];
- y0 = new double[ 111 ];
- a = new double[ 111 ];
- c = new double[ 111 ];
- for ( int i = 0; i < 111; i++ ) {
- x0[i] = 5 - i * 0.2;
- a[i] = 1 + i / 1.1;
- y0[i] = 2 - i * 0.4;
- c[i] = 2 + i / 38.0;
- }
- }
- double error = 0;
- for ( int i = 0; i < 20; i++ ) {
- if ( rank == 0 ) {
- a[i] = i;
- err->setCoefficients( x0, y0, a, c, 111-i*3 );
- }
- err->calcError();
- if ( rank == 0 )
- error += err->getError();
- }
- if ( rank == 0 )
- cout << "Uwaga: ta wartosc nie moze zalezec od liczby uzytych procesow = " << error << endl;
- }
- int main(int ac, char **av) {
- MPI_Init(&ac, &av);
- int rank;
- MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
- Error *err = new Error();
- double *v;
- if ( ! rank ) {
- v = new double[ SIZE ];
- double x0[] = { -3, -2, -2, -1, 1, 2, 2, 3 };
- double y0[] = { -3, -3, 3, -1, 1, -3, 3, 3 };
- double a[] = { 1,2,3,4,5,6,7,8 };
- double c[] = { 0.1, 0.05, 0.02, 0.01, 0.01, 0.01, 0.02, 0.05 };
- generate( v, x0, y0, a, c );
- shuffle( v );
- // udostepniam dane dla procesu = 0
- err->setValues( v, LEN, MULT );
- }
- test1( err, rank );
- test2( err, rank );
- test3( err, rank );
- test4( err, rank );
- test1( err, rank );
- testL( err, rank );
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement