MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
standaloneTestFastElim.cpp File Reference
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <iomanip>
#include "/usr/include/gsl/gsl_linalg.h"
#include "../basecode/SparseMatrix.h"
+ Include dependency graph for standaloneTestFastElim.cpp:

Go to the source code of this file.

Classes

class  FastElim
 
class  Unroll
 

Functions

void advance (vector< double > &y, const vector< Triplet< double > > &ops, const vector< double > &diagVal)
 
double checkAns (const double *m, unsigned int numCompts, const double *ans, const double *rhs)
 
 main ()
 
void sortByColumn (vector< unsigned int > &col, vector< double > &entry)
 
void testSorting ()
 

Variables

const unsigned int SM_MAX_COLUMNS = 200000
 
const unsigned int SM_MAX_ROWS = 200000
 
const unsigned int SM_RESERVE = 8
 

Function Documentation

void advance ( vector< double > &  y,
const vector< Triplet< double > > &  ops,
const vector< double > &  diagVal 
)

Definition at line 402 of file standaloneTestFastElim.cpp.

Referenced by main(), Gsolve::process(), and Ksolve::process().

405 {
406  for ( vector< Triplet< double > >::const_iterator
407  i = ops.begin(); i != ops.end(); ++i )
408  y[i->c_] -= y[i->b_] * i->a_;
409 
410  assert( y.size() == diagVal.size() );
411  vector< double >::iterator iy = y.begin();
412  for ( vector< double >::const_iterator
413  i = diagVal.begin(); i != diagVal.end(); ++i )
414  *iy++ *= *i;
415 }

+ Here is the caller graph for this function:

double checkAns ( const double *  m,
unsigned int  numCompts,
const double *  ans,
const double *  rhs 
)

Definition at line 417 of file standaloneTestFastElim.cpp.

Referenced by main().

420 {
421  vector< double > check( numCompts, 0.0 );
422  for ( unsigned int i = 0; i < numCompts; ++i ) {
423  for ( unsigned int j = 0; j < numCompts; ++j )
424  check[i] += m[i*numCompts + j] * ans[j];
425  }
426  double ret = 0.0;
427  for ( unsigned int i = 0; i < numCompts; ++i )
428  ret += (check[i] - rhs[i]) * (check[i] - rhs[i] );
429  return ret;
430 }

+ Here is the caller graph for this function:

main ( )

Definition at line 434 of file standaloneTestFastElim.cpp.

References advance(), FastElim::buildBackwardSub(), FastElim::buildForwardElim(), checkAns(), SparseMatrix< T >::get(), FastElim::hinesReorder(), FastElim::makeTestMatrix(), and SparseMatrix< T >::print().

435 {
436 
437 
438 /*
439 2 11
440  1 4
441  3 10
442  9 5
443  6
444  7
445  8
446 
447  1 2 3 4 5 6 7 8 9 10 11
448 1 x x x x
449 2 x x
450 3 x x x x
451 4 x x x x
452 5 x x x x
453 6 x x x x
454 7 x x x
455 8 x x
456 9 x x x x
457 10 x x
458 11 x x
459  static double test[] = {
460  1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0,
461  5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
462  7, 0, 8, 9, 0, 0, 0, 0, 10, 0, 0,
463  11, 0, 12, 13, 0, 0, 0, 0, 0, 0, 14,
464  0, 0, 0, 0, 15, 16, 0, 0, 17, 18, 0,
465  0, 0, 0, 0, 19, 20, 21, 0, 22, 0, 0,
466  0, 0, 0, 0, 0, 23, 24, 25, 0, 0, 0,
467  0, 0, 0, 0, 0, 0, 26, 27, 0, 0, 0,
468  0, 0, 28, 0, 29, 30, 0, 0, 31, 0, 0,
469  0, 0, 0, 0, 32, 0, 0, 0, 0, 33, 0,
470  0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 35,
471  };
472  const unsigned int numCompts = 11;
473 // static unsigned int parents[] = { 3,1,9,3,6,7,8,-1,6,5,4 };
474  static unsigned int parents[] = { 2,0,8,2,5,6,7,-1,5,4,3 };
475 */
476 
477 /*
478 1 3
479  2 4
480  7 5
481  8 6
482  9
483  10
484  11
485 
486  1 2 3 4 5 6 7 8 9 10 11
487 1 x x
488 2 x x x
489 3 x x
490 4 x x x
491 5 x x
492 6 x x x
493 7 x x x x
494 8 x x x
495 9 x x x x
496 10 x x x
497 11 x x
498  static double test[] = {
499  1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
500  3, 4, 0, 0, 0, 0, 5, 0, 0, 0, 0,
501  0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0,
502  0, 0, 8, 9, 0, 0, 10, 0, 0, 0, 0,
503  0, 0, 0, 0, 11, 12, 0, 0, 0, 0, 0,
504  0, 0, 0, 0, 13, 14, 0, 0, 15, 0, 0,
505  0, 16, 0, 17, 0, 0, 18, 19, 0, 0, 0,
506  0, 0, 0, 0, 0, 0, 20, 21, 22, 0, 0,
507  0, 0, 0, 0, 0, 23, 0, 24, 25, 26, 0,
508  0, 0, 0, 0, 0, 0, 0, 0, 27, 28, 29,
509  0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 31,
510  };
511  const unsigned int numCompts = 11;
512  static unsigned int parents[] = { 1,6,3,6,5,8,7,8,9,10,-1};
513 */
514 
515 /*
516 Linear cable, 12 segments.
517 */
518 
519  static double test[] = {
520  1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
521  3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
522  0, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0,
523  0, 0, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0,
524  0, 0, 0, 12, 13, 14, 0, 0, 0, 0, 0, 0,
525  0, 0, 0, 0, 15, 16, 17, 0, 0, 0, 0, 0,
526  0, 0, 0, 0, 0, 18, 19, 20, 0, 0, 0, 0,
527  0, 0, 0, 0, 0, 0, 21, 22, 23, 0, 0, 0,
528  0, 0, 0, 0, 0, 0, 0, 24, 25, 26, 0, 0,
529  0, 0, 0, 0, 0, 0, 0, 0, 27, 28, 29, 0,
530  0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 31, 32,
531  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 34,
532  };
533  const unsigned int numCompts = 12;
534  static unsigned int parents[] = { 1,2,3,4,5,6,7,8,9,10,11,-1};
535 
536  /*
537  static double test[] = {
538  1, 2,
539  3, 4
540  };
541  const unsigned int numCompts = 2;
542  static double test[] = {
543  1, 2, 0, 0,
544  3, 4, 5, 0,
545  0, 6, 7, 8,
546  0, 0, 9, 10
547  };
548  const unsigned int numCompts = 4;
549  static double test[] = {
550  1, 2, 0, 0, 0, 0,
551  3, 4, 5, 0, 0, 0,
552  0, 6, 7, 8, 0, 0,
553  0, 0, 9, 10, 11, 0,
554  0, 0, 0, 12, 13, 14,
555  0, 0, 0, 0, 15, 16,
556  };
557  const unsigned int numCompts = 6;
558  static double test[] = {
559  1, 2, 0, 0, 0, 0,
560  3, 4, 0, 0, 1, 0,
561  0, 0, 7, 8, 0, 0,
562  0, 0, 9, 10, 11, 0,
563  0, 1, 0, 12, 13, 14,
564  0, 0, 0, 0, 15, 16,
565  };
566  const unsigned int numCompts = 6;
567  */
568  // testSorting(); // seems to work fine.
569  FastElim fe;
570  vector< Triplet< double > > fops;
571  vector< Unroll > bops;
572  fe.makeTestMatrix( test, numCompts );
573  fe.print();
574  cout << endl << endl;
575  vector< unsigned int > parentVoxel;
576  parentVoxel.insert( parentVoxel.begin(), &parents[0], &parents[numCompts] );
577  fe.hinesReorder( parentVoxel );
578  /*
579  */
580  /*
581  vector< unsigned int > shuf;
582  for ( unsigned int i = 0; i < numCompts; ++i )
583  shuf.push_back( i );
584  shuf[0] = 1;
585  shuf[1] = 0;
586  fe.shuffleRows( shuf );
587  */
588  fe.print();
589  cout << endl << endl;
590  FastElim foo = fe;
591 
592  vector< unsigned int > diag;
593  vector< double > diagVal;
594  fe.buildForwardElim( diag, fops );
595  fe.print();
596  fe.buildBackwardSub( diag, fops, diagVal );
597  vector< double > y( numCompts, 1.0 );
598  vector< double > ones( numCompts, 1.0 );
599  advance( y, fops, diagVal );
600  for ( int i = 0; i < numCompts; ++i )
601  cout << "y" << i << "]= " << y[i] << endl;
602 
603  // Here we verify the answer
604 
605  vector< double > alle;
606  for( unsigned int i = 0; i < numCompts; ++i ) {
607  for( unsigned int j = 0; j < numCompts; ++j ) {
608  alle.push_back( foo.get( i, j ) );
609  }
610  }
611  cout << "myCode: " <<
612  checkAns( &alle[0], numCompts, &y[0], &ones[0] ) << endl;
613 
614 
615 
617  // Here we do the gsl test.
618  vector< double > temp( &test[0], &test[numCompts*numCompts] );
619  gsl_matrix_view m = gsl_matrix_view_array( &temp[0], numCompts, numCompts );
620 
621  vector< double > z( numCompts, 1.0 );
622  gsl_vector_view b = gsl_vector_view_array( &z[0], numCompts );
623  gsl_vector* x = gsl_vector_alloc( numCompts );
624  int s;
625  gsl_permutation* p = gsl_permutation_alloc( numCompts );
626  gsl_linalg_LU_decomp( &m.matrix, p, &s );
627  gsl_linalg_LU_solve( &m.matrix, p, &b.vector, x);
628  vector< double > gslAns( numCompts );
629  for ( int i = 0; i < numCompts; ++i ) {
630  gslAns[i] = gsl_vector_get( x, i );
631  cout << "x[" << i << "]= " << gslAns[i] << endl;
632  }
633  /*
634  */
635  cout << "GSL: " << checkAns( test, numCompts, &gslAns[0], &ones[0] ) << endl;
636  gsl_vector_free( x );
637 
638 
639 }
T get(unsigned int row, unsigned int column) const
Definition: SparseMatrix.h:253
bool hinesReorder(const vector< unsigned int > &parentVoxel)
void buildBackwardSub(vector< unsigned int > &diag, vector< Triplet< double > > &bops, vector< double > &diagVal)
void advance(vector< double > &y, const vector< Triplet< double > > &ops, const vector< double > &diagVal)
void makeTestMatrix(const double *test, unsigned int numCompts)
double checkAns(const double *m, unsigned int numCompts, const double *ans, const double *rhs)
void buildForwardElim(vector< unsigned int > &diag, vector< Triplet< double > > &fops)
void print() const
Definition: SparseMatrix.h:650

+ Here is the call graph for this function:

void sortByColumn ( vector< unsigned int > &  col,
vector< double > &  entry 
)

Definition at line 162 of file standaloneTestFastElim.cpp.

Referenced by FastElim::shuffleRows(), and testSorting().

163 {
164  unsigned int num = col.size();
165  assert( num == entry.size() );
166  // Stupid bubble sort, as we only have up to 5 entries and need to
167  // sort both the col and reorder the entries by the same sequence.
168  for ( unsigned int i = 0; i < num; ++i ) {
169  for ( unsigned int j = 1; j < num; ++j ) {
170  if ( col[j] < col[j-1] ) {
171  unsigned int temp = col[j];
172  col[j] = col[j-1];
173  col[j-1] = temp;
174  double v = entry[j];
175  entry[j] = entry[j-1];
176  entry[j-1] = v;
177  }
178  }
179  }
180 }

+ Here is the caller graph for this function:

void testSorting ( )

Definition at line 641 of file standaloneTestFastElim.cpp.

References sortByColumn().

642 {
643  static unsigned int k[] = {20,40,60,80,100,10,30,50,70,90};
644  static double d[] = {1,2,3,4,5,6,7,8,9,10};
645  vector< unsigned int > col;
646  col.insert( col.begin(), k, k+10);
647  vector< double > entry;
648  entry.insert( entry.begin(), d, d+10);
649  sortByColumn( col, entry );
650  cout << "testing sorting\n";
651  for ( int i = 0; i < col.size(); ++i ) {
652  cout << "d[" << i << "]= " << k[i] <<
653  ", col[" << i << "]= " << col[i] << ", e=" << entry[i] << endl;
654  }
655  cout << endl;
656 }
void sortByColumn(vector< unsigned int > &col, vector< double > &entry)

+ Here is the call graph for this function:

Variable Documentation

const unsigned int SM_MAX_COLUMNS = 200000

Definition at line 17 of file standaloneTestFastElim.cpp.

Referenced by SparseMatrix< unsigned int >::setSize().

const unsigned int SM_MAX_ROWS = 200000

Template for specialized SparseMatrix. Used both for the Kinetic solver and for handling certain kinds of messages. Speciality is that it can extract entire rows efficiently, for marching through a specified row for a matrix multiplication or for traversing messages.

Requires that type T have an equality operator ==

Definition at line 16 of file standaloneTestFastElim.cpp.

Referenced by SparseMatrix< unsigned int >::setSize().

const unsigned int SM_RESERVE = 8