1*bf2c3715SXin Li //===================================================== 2*bf2c3715SXin Li // File : action_axpby.hh 3*bf2c3715SXin Li // Copyright (C) 2008 Gael Guennebaud <[email protected]> 4*bf2c3715SXin Li //===================================================== 5*bf2c3715SXin Li // 6*bf2c3715SXin Li // This program is free software; you can redistribute it and/or 7*bf2c3715SXin Li // modify it under the terms of the GNU General Public License 8*bf2c3715SXin Li // as published by the Free Software Foundation; either version 2 9*bf2c3715SXin Li // of the License, or (at your option) any later version. 10*bf2c3715SXin Li // 11*bf2c3715SXin Li // This program is distributed in the hope that it will be useful, 12*bf2c3715SXin Li // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*bf2c3715SXin Li // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*bf2c3715SXin Li // GNU General Public License for more details. 15*bf2c3715SXin Li // You should have received a copy of the GNU General Public License 16*bf2c3715SXin Li // along with this program; if not, write to the Free Software 17*bf2c3715SXin Li // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18*bf2c3715SXin Li // 19*bf2c3715SXin Li #ifndef ACTION_AXPBY 20*bf2c3715SXin Li #define ACTION_AXPBY 21*bf2c3715SXin Li #include "utilities.h" 22*bf2c3715SXin Li #include "STL_interface.hh" 23*bf2c3715SXin Li #include <string> 24*bf2c3715SXin Li #include "init/init_function.hh" 25*bf2c3715SXin Li #include "init/init_vector.hh" 26*bf2c3715SXin Li #include "init/init_matrix.hh" 27*bf2c3715SXin Li 28*bf2c3715SXin Li using namespace std; 29*bf2c3715SXin Li 30*bf2c3715SXin Li template<class Interface> 31*bf2c3715SXin Li class Action_axpby { 32*bf2c3715SXin Li 33*bf2c3715SXin Li public : 34*bf2c3715SXin Li 35*bf2c3715SXin Li // Ctor Action_axpby(int size)36*bf2c3715SXin Li Action_axpby( int size ):_alpha(0.5),_beta(0.95),_size(size) 37*bf2c3715SXin Li { 38*bf2c3715SXin Li MESSAGE("Action_axpby Ctor"); 39*bf2c3715SXin Li 40*bf2c3715SXin Li // STL vector initialization 41*bf2c3715SXin Li init_vector<pseudo_random>(X_stl,_size); 42*bf2c3715SXin Li init_vector<pseudo_random>(Y_stl,_size); 43*bf2c3715SXin Li init_vector<null_function>(resu_stl,_size); 44*bf2c3715SXin Li 45*bf2c3715SXin Li // generic matrix and vector initialization 46*bf2c3715SXin Li Interface::vector_from_stl(X_ref,X_stl); 47*bf2c3715SXin Li Interface::vector_from_stl(Y_ref,Y_stl); 48*bf2c3715SXin Li 49*bf2c3715SXin Li Interface::vector_from_stl(X,X_stl); 50*bf2c3715SXin Li Interface::vector_from_stl(Y,Y_stl); 51*bf2c3715SXin Li } 52*bf2c3715SXin Li 53*bf2c3715SXin Li // invalidate copy ctor Action_axpby(const Action_axpby &)54*bf2c3715SXin Li Action_axpby( const Action_axpby & ) 55*bf2c3715SXin Li { 56*bf2c3715SXin Li INFOS("illegal call to Action_axpby Copy Ctor"); 57*bf2c3715SXin Li exit(1); 58*bf2c3715SXin Li } 59*bf2c3715SXin Li 60*bf2c3715SXin Li // Dtor ~Action_axpby(void)61*bf2c3715SXin Li ~Action_axpby( void ){ 62*bf2c3715SXin Li MESSAGE("Action_axpby Dtor"); 63*bf2c3715SXin Li 64*bf2c3715SXin Li // deallocation 65*bf2c3715SXin Li Interface::free_vector(X_ref); 66*bf2c3715SXin Li Interface::free_vector(Y_ref); 67*bf2c3715SXin Li 68*bf2c3715SXin Li Interface::free_vector(X); 69*bf2c3715SXin Li Interface::free_vector(Y); 70*bf2c3715SXin Li } 71*bf2c3715SXin Li 72*bf2c3715SXin Li // action name name(void)73*bf2c3715SXin Li static inline std::string name( void ) 74*bf2c3715SXin Li { 75*bf2c3715SXin Li return "axpby_"+Interface::name(); 76*bf2c3715SXin Li } 77*bf2c3715SXin Li nb_op_base(void)78*bf2c3715SXin Li double nb_op_base( void ){ 79*bf2c3715SXin Li return 3.0*_size; 80*bf2c3715SXin Li } 81*bf2c3715SXin Li initialize(void)82*bf2c3715SXin Li inline void initialize( void ){ 83*bf2c3715SXin Li Interface::copy_vector(X_ref,X,_size); 84*bf2c3715SXin Li Interface::copy_vector(Y_ref,Y,_size); 85*bf2c3715SXin Li } 86*bf2c3715SXin Li calculate(void)87*bf2c3715SXin Li inline void calculate( void ) { 88*bf2c3715SXin Li BTL_ASM_COMMENT("mybegin axpby"); 89*bf2c3715SXin Li Interface::axpby(_alpha,X,_beta,Y,_size); 90*bf2c3715SXin Li BTL_ASM_COMMENT("myend axpby"); 91*bf2c3715SXin Li } 92*bf2c3715SXin Li check_result(void)93*bf2c3715SXin Li void check_result( void ){ 94*bf2c3715SXin Li if (_size>128) return; 95*bf2c3715SXin Li // calculation check 96*bf2c3715SXin Li Interface::vector_to_stl(Y,resu_stl); 97*bf2c3715SXin Li 98*bf2c3715SXin Li STL_interface<typename Interface::real_type>::axpby(_alpha,X_stl,_beta,Y_stl,_size); 99*bf2c3715SXin Li 100*bf2c3715SXin Li typename Interface::real_type error= 101*bf2c3715SXin Li STL_interface<typename Interface::real_type>::norm_diff(Y_stl,resu_stl); 102*bf2c3715SXin Li 103*bf2c3715SXin Li if (error>1.e-6){ 104*bf2c3715SXin Li INFOS("WRONG CALCULATION...residual=" << error); 105*bf2c3715SXin Li exit(2); 106*bf2c3715SXin Li } 107*bf2c3715SXin Li } 108*bf2c3715SXin Li 109*bf2c3715SXin Li private : 110*bf2c3715SXin Li 111*bf2c3715SXin Li typename Interface::stl_vector X_stl; 112*bf2c3715SXin Li typename Interface::stl_vector Y_stl; 113*bf2c3715SXin Li typename Interface::stl_vector resu_stl; 114*bf2c3715SXin Li 115*bf2c3715SXin Li typename Interface::gene_vector X_ref; 116*bf2c3715SXin Li typename Interface::gene_vector Y_ref; 117*bf2c3715SXin Li 118*bf2c3715SXin Li typename Interface::gene_vector X; 119*bf2c3715SXin Li typename Interface::gene_vector Y; 120*bf2c3715SXin Li 121*bf2c3715SXin Li typename Interface::real_type _alpha; 122*bf2c3715SXin Li typename Interface::real_type _beta; 123*bf2c3715SXin Li 124*bf2c3715SXin Li int _size; 125*bf2c3715SXin Li }; 126*bf2c3715SXin Li 127*bf2c3715SXin Li #endif 128