ViennaCL - The Vienna Computing Library  1.6.0
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
generator_blas2.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2012, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17 
18 //
19 // *** System
20 //
21 #include <iostream>
22 
23 //
24 // *** Boost
25 //
26 #include <boost/numeric/ublas/io.hpp>
27 #include <boost/numeric/ublas/vector.hpp>
28 
29 //
30 // *** ViennaCL
31 //
32 #define VIENNACL_WITH_UBLAS 1
33 
34 //#define VIENNACL_DEBUG_ALL
35 //#define VIENNACL_DEBUG_BUILD
36 #include "viennacl/vector.hpp"
37 #include "viennacl/matrix.hpp"
38 
39 #include "viennacl/linalg/prod.hpp"
41 #include "viennacl/device_specific/code_generator.hpp"
43 
44 #define CHECK_RESULT(cpu,gpu, op) \
45  if ( double delta = fabs ( diff ( cpu, gpu) ) > epsilon ) {\
46  std::cout << "# Error at operation: " #op << std::endl;\
47  std::cout << " diff: " << delta << std::endl;\
48  retval = EXIT_FAILURE;\
49  }\
50 
51 
52 using namespace boost::numeric;
53 using namespace viennacl;
54 
55 template<typename ScalarType, typename VCLMatrixType>
56 ScalarType diff(ublas::matrix<ScalarType> & mat1, VCLMatrixType & mat2)
57 {
58  ublas::matrix<ScalarType> mat2_cpu(mat2.size1(), mat2.size2());
59  viennacl::backend::finish(); //workaround for a bug in APP SDK 2.7 on Trinity APUs (with Catalyst 12.8)
60  viennacl::copy(mat2, mat2_cpu);
61  double ret = 0;
62  double act = 0;
63 
64  for (unsigned int i = 0; i < mat2_cpu.size1(); ++i)
65  {
66  for (unsigned int j = 0; j < mat2_cpu.size2(); ++j)
67  {
68  act = std::fabs(mat2_cpu(i,j) - mat1(i,j)) / std::max<ScalarType>( std::fabs(mat2_cpu(i, j)), std::fabs(mat1(i,j)) );
69  if (act > ret)
70  ret = act;
71  }
72  }
73  //std::cout << ret << std::endl;
74  return ret;
75 }
76 
77 template<typename ScalarType, unsigned int Alignment>
78 ScalarType diff ( ublas::vector<ScalarType> & v1, viennacl::vector<ScalarType,Alignment> & v2 ) {
79  ublas::vector<ScalarType> v2_cpu ( v2.size() );
80  viennacl::copy( v2.begin(), v2.end(), v2_cpu.begin() );
81  for ( unsigned int i=0; i<v1.size(); ++i ) {
82  if ( std::max ( std::fabs ( v2_cpu[i] ), std::fabs ( v1[i] ) ) > 0 )
83  v2_cpu[i] = std::fabs ( v2_cpu[i] - v1[i] ) / std::max<ScalarType>( std::fabs ( v2_cpu[i] ), std::fabs ( v1[i] ) );
84  else
85  v2_cpu[i] = 0.0;
86  }
87  return norm_inf ( v2_cpu );
88 }
89 
90 
91 template< typename NumericT, class Layout, typename Epsilon >
92 int test( Epsilon const& epsilon) {
93  int retval = EXIT_SUCCESS;
94 
95  ublas::vector<NumericT> cx;
96  ublas::vector<NumericT> cy;
97 
98  ublas::matrix<NumericT> cA;
99  ublas::matrix<NumericT> cB;
100  ublas::matrix<NumericT> cC;
101  ublas::matrix<NumericT> cD;
102 
103  unsigned int size1 = 762;
104  unsigned int size2 = 663;
105 
106  cA.resize(size1,size2);
107  cx.resize(size2);
108  cy.resize(size1);
109 
110  srand(0);
111 
112  for (unsigned int i=0; i<size1; ++i){
113  for (unsigned int j=0; j<size2; ++j){
114  cA(i,j)=j;
115  }
116  }
117 
118  for (unsigned int i=0; i<size2; ++i){
119  cx(i) = i;
120  }
121 
122  for (unsigned int i=0; i<size1; ++i){
123  cy(i) = i;
124  }
125 
126 // std::cout << "Running tests for matrix of size " << cA.size1() << "," << cA.size2() << std::endl;
127 
128  viennacl::matrix<NumericT,Layout> A (size1, size2);
129  viennacl::matrix<NumericT,Layout> B (size1, size2);
130  viennacl::matrix<NumericT,Layout> C (size1, size2);
131  viennacl::matrix<NumericT,Layout> D (size1, size2);
132 
135 
136 
137  cB = cA;
138  cC = cA;
139  cD = cA;
140  viennacl::copy(cA,A);
141  viennacl::copy(cB,B);
142  viennacl::copy(cC,C);
143  viennacl::copy(cD,D);
144 
145  viennacl::copy(cx,x);
146  viennacl::copy(cy,y);
147 
148 
149  // --------------------------------------------------------------------------
150  {
151  std::cout << "y = A*x..." << std::endl;
152  cy = ublas::prod(cA,cx);
154  //std::cout << statement << std::endl;
155  device_specific::generate_enqueue_statement(statement, statement.array()[0]);
157  CHECK_RESULT(cy,y,y=A*x)
158  }
159 
160  {
161  std::cout << "x = trans(A)*y..." << std::endl;
162  cx = ublas::prod(trans(cA),cy);
164  device_specific::generate_enqueue_statement(statement, statement.array()[0]);
166  CHECK_RESULT(cx,x,x=trans(A)*y)
167  }
168 
169  {
170  std::cout << "y = reduce_rows<add>(A)..." << std::endl;
171  for (unsigned int i = 0; i < size1; ++i){
172  NumericT acc = cA(i,0);
173  for (unsigned int j = 1; j < size2; ++j){
174  acc += cA(i,j);
175  }
176  cy(i) = acc;
177  }
178  viennacl::scheduler::statement statement(y, viennacl::op_assign(), viennacl::linalg::reduce_rows<viennacl::op_add>(A));
179  //std::cout << statement << std::endl;
180 
181  device_specific::generate_enqueue_statement(statement, statement.array()[0]);
183  CHECK_RESULT(cy,y,y = reduce_rows<max>(A))
184  }
185 
186  {
187  std::cout << "x = reduce_columns<add>(A)..." << std::endl;
188  for (unsigned int j = 0; j < size2; ++j){
189  NumericT acc = cA(0,j);
190  for (unsigned int i = 1; i < size1; ++i){
191  acc += cA(i,j);
192  }
193  cx(j) = acc;
194  }
195  viennacl::scheduler::statement statement(x, viennacl::op_assign(), viennacl::linalg::reduce_columns<viennacl::op_add>(A));
196  device_specific::generate_enqueue_statement(statement, statement.array()[0]);
198  CHECK_RESULT(cx,x,x = reduce_columns<max>(A))
199  }
200 
201 
202  return retval;
203 }
204 
205 
206 int main() {
207  std::cout << std::endl;
208  std::cout << "----------------------------------------------" << std::endl;
209  std::cout << "----------------------------------------------" << std::endl;
210  std::cout << "## Test :: Generated BLAS2" << std::endl;
211  std::cout << "----------------------------------------------" << std::endl;
212  std::cout << "----------------------------------------------" << std::endl;
213  std::cout << std::endl;
214 
215  int retval = EXIT_SUCCESS;
216 
217  std::cout << std::endl;
218  std::cout << "----------------------------------------------" << std::endl;
219  std::cout << std::endl;
220  {
221  double epsilon = 1.0E-4;
222  std::cout << "# Testing setup:" << std::endl;
223  std::cout << " numeric: float" << std::endl;
224  std::cout << " --------------" << std::endl;
225  std::cout << " Row-Major" << std::endl;
226  std::cout << " --------------" << std::endl;
227  retval = test<float, viennacl::row_major> (epsilon);
228  std::cout << " --------------" << std::endl;
229  std::cout << " Column-Major" << std::endl;
230  std::cout << " --------------" << std::endl;
231  retval &= test<float, viennacl::column_major> (epsilon);
232 
233  if ( retval == EXIT_SUCCESS )
234  std::cout << "# Test passed" << std::endl;
235  else
236  return retval;
237  }
238 
239 // std::cout << std::endl;
240 // std::cout << "----------------------------------------------" << std::endl;
241 // std::cout << std::endl;
242 //#ifdef VIENNACL_WITH_OPENCL
243 // if ( viennacl::ocl::current_device().double_support() )
244 //#endif
245 // {
246 // double epsilon = 1.0E-4;
247 // std::cout << "# Testing setup:" << std::endl;
248 // std::cout << " numeric: double" << std::endl;
249 // std::cout << " --------------" << std::endl;
250 // std::cout << " Row-Major" << std::endl;
251 // std::cout << " --------------" << std::endl;
252 // retval = test<double, viennacl::row_major> (epsilon);
253 // std::cout << " --------------" << std::endl;
254 // std::cout << " Column-Major" << std::endl;
255 // std::cout << " --------------" << std::endl;
256 // retval &= test<double, viennacl::column_major> (epsilon);
257 
258 // if ( retval == EXIT_SUCCESS )
259 // std::cout << "# Test passed" << std::endl;
260 // else
261 // return retval;
262 // }
263 }
void trans(matrix_expression< const matrix_base< NumericT, SizeT, DistanceT >, const matrix_base< NumericT, SizeT, DistanceT >, op_trans > const &proxy, matrix_base< NumericT > &temp_trans)
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Implementation of the dense matrix class.
ScalarType diff(ublas::matrix< ScalarType > &mat1, VCLMatrixType &mat2)
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:216
Some helper routines for reading/writing/printing scheduler expressions.
A tag class representing assignment.
Definition: forwards.h:80
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
Definition: memory.hpp:54
A dense matrix class.
Definition: forwards.h:374
container_type const & array() const
Definition: forwards.h:530
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
T max(const T &lhs, const T &rhs)
Maximum.
Definition: util.hpp:59
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
Definition: size.hpp:245
int main()
viennacl::vector< float > v1
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:91
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
int test(Epsilon const &epsilon)
void prod(const MatrixT1 &A, bool transposed_A, const MatrixT2 &B, bool transposed_B, MatrixT3 &C, ScalarT alpha, ScalarT beta)
viennacl::vector< int > v2
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
T norm_inf(std::vector< T, A > const &v1)
Definition: norm_inf.hpp:60
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
float ScalarType
Definition: fft_1d.cpp:42
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:504
iterator end()
Returns an iterator pointing to the end of the vector (STL like)
#define CHECK_RESULT(cpu, gpu, op)