You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.5 KiB

  1. //=====================================================
  2. // File : action_lu_solve.hh
  3. // Author : L. Plagne <laurent.plagne@edf.fr)>
  4. // Copyright (C) EDF R&D, lun sep 30 14:23:19 CEST 2002
  5. //=====================================================
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. #ifndef ACTION_LU_SOLVE
  21. #define ACTION_LU_SOLVE
  22. #include "utilities.h"
  23. #include "STL_interface.hh"
  24. #include <string>
  25. #include "init/init_function.hh"
  26. #include "init/init_vector.hh"
  27. #include "init/init_matrix.hh"
  28. using namespace std;
  29. template<class Interface>
  30. class Action_lu_solve
  31. {
  32. public :
  33. static inline std::string name( void )
  34. {
  35. return "lu_solve_"+Interface::name();
  36. }
  37. static double nb_op_base(int size){
  38. return 2.0*size*size*size/3.0; // questionable but not really important
  39. }
  40. static double calculate( int nb_calc, int size ) {
  41. // STL matrix and vector initialization
  42. typename Interface::stl_matrix A_stl;
  43. typename Interface::stl_vector B_stl;
  44. typename Interface::stl_vector X_stl;
  45. init_matrix<pseudo_random>(A_stl,size);
  46. init_vector<pseudo_random>(B_stl,size);
  47. init_vector<null_function>(X_stl,size);
  48. // generic matrix and vector initialization
  49. typename Interface::gene_matrix A;
  50. typename Interface::gene_vector B;
  51. typename Interface::gene_vector X;
  52. typename Interface::gene_matrix LU;
  53. Interface::matrix_from_stl(A,A_stl);
  54. Interface::vector_from_stl(B,B_stl);
  55. Interface::vector_from_stl(X,X_stl);
  56. Interface::matrix_from_stl(LU,A_stl);
  57. // local variable :
  58. typename Interface::Pivot_Vector pivot; // pivot vector
  59. Interface::new_Pivot_Vector(pivot,size);
  60. // timer utilities
  61. Portable_Timer chronos;
  62. // time measurement
  63. chronos.start();
  64. for (int ii=0;ii<nb_calc;ii++){
  65. // LU factorization
  66. Interface::copy_matrix(A,LU,size);
  67. Interface::LU_factor(LU,pivot,size);
  68. // LU solve
  69. Interface::LU_solve(LU,pivot,B,X,size);
  70. }
  71. // Time stop
  72. chronos.stop();
  73. double time=chronos.user_time();
  74. // check result :
  75. typename Interface::stl_vector B_new_stl(size);
  76. Interface::vector_to_stl(X,X_stl);
  77. STL_interface<typename Interface::real_type>::matrix_vector_product(A_stl,X_stl,B_new_stl,size);
  78. typename Interface::real_type error=
  79. STL_interface<typename Interface::real_type>::norm_diff(B_stl,B_new_stl);
  80. if (error>1.e-5){
  81. INFOS("WRONG CALCULATION...residual=" << error);
  82. STL_interface<typename Interface::real_type>::display_vector(B_stl);
  83. STL_interface<typename Interface::real_type>::display_vector(B_new_stl);
  84. exit(0);
  85. }
  86. // deallocation and return time
  87. Interface::free_matrix(A,size);
  88. Interface::free_vector(B);
  89. Interface::free_vector(X);
  90. Interface::free_Pivot_Vector(pivot);
  91. return time;
  92. }
  93. };
  94. #endif