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