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
scan.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2014, 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 
24 /*
25 *
26 * Test file for inclusive and exclusive scan
27 *
28 */
29 
30 
31 #ifndef NDEBUG
32  #define NDEBUG
33 #endif
34 
35 //include basic scalar and vector types of ViennaCL
36 #include "viennacl/scalar.hpp"
37 #include "viennacl/vector.hpp"
40 
41 // Some helper functions for this tutorial:
42 #include <iostream>
43 #include <limits>
44 #include <string>
45 #include <iomanip>
46 
47 
48 typedef float ScalarType;
49 
50 #define EPS 0.0001
51 
52 
54 
56 {
57  for (unsigned int i = 0; i < v.size(); i++)
58  std::cout << std::setprecision(0) << std::fixed << v(i) << "\t";
59  std::cout << "\n";
60 }
61 
63 
65 {
66  std::vector<ScalarType> v(vcl_v.size());
67  for (std::size_t i = 0; i < v.size(); ++i)
68  v[i] = ScalarType(i);
69  viennacl::copy(v, vcl_v);
70 }
71 
73 
75 {
76  std::vector<ScalarType> vec(vcl_vec.size());
77  viennacl::copy(vcl_vec, vec);
78  for(viennacl::vcl_size_t i = 1; i < vec.size(); i++)
79  {
80  double abs_error = std::fabs(static_cast<double>(i) * (static_cast<double>(i) + 1.) / 2. - static_cast<double>(vec[i]));
81  if (abs_error > static_cast<double>(EPS) * static_cast<double>(vec[i]))
82  {
83  std::cout << "Fail at vector index " << i << " Absolute error: " << abs_error;
84  std::cout << "\t Relative error: " << std::setprecision(7) << abs_error / vec[i] * 100 << "%"<< std::endl;
85  std::cout << "vec[" << i << "] = " << vec[i] << std::endl;
86  exit(EXIT_FAILURE);
87  }
88  }
89 
90 }
91 
93 
95 {
96  std::vector<ScalarType> vec(vcl_vec.size());
97  viennacl::copy(vcl_vec, vec);
98  for(viennacl::vcl_size_t i = 1; i < vec.size() - 1; i++)
99  {
100  double abs_error = std::fabs(static_cast<double>(i) * (static_cast<double>(i) + 1.) / 2. - static_cast<double>(vec[i + 1]));
101  if (abs_error > static_cast<double>(EPS) * static_cast<double>(vec[i]))
102  {
103  std::cout << "Fail at vector index " << i << " Absolute error: " << abs_error;
104  std::cout << "\t Relative error: " << std::setprecision(7) << abs_error / vec[i] * 100 << "%"<< std::endl;
105  std::cout << "vec[" << i << "] = " << vec[i] << std::endl;
106  exit(EXIT_FAILURE);
107  }
108  }
109 
110 }
111 
112 
113 void test_scans();
114 
116 {
117  unsigned int sz = 100000;
118  viennacl::vector<ScalarType> vec1(sz), vec2(sz);
119 
120 
121  std::cout << "Initialize vector..." << std::endl;
122  init_vector(vec1);
123 
124 
125  // INCLUSIVE SCAN
126  std::cout << "Inclusive scan started!" << std::endl;
128  std::cout << "Inclusive scan finished!" << std::endl;
129  //vector_print(vec2);
130  std::cout << "Testing inclusive scan results..." << std::endl;
132  std::cout << "Inclusive scan tested successfully!" << std::endl << std::endl;
133 
134 
135 
136  std::cout << "Initialize vector..." << std::endl;
137  init_vector(vec1);
138 
139  // EXCLUSIVE SCAN
140  std::cout << "Exlusive scan started!" << std::endl;
142  std::cout << "Exclusive scan finished!" << std::endl;
143  //vector_print(vec2);
144  std::cout << "Testing exclusive scan results..." << std::endl;
146  std::cout << "Exclusive scan tested successfully!" << std::endl << std::endl;
147 
148 }
149 
150 int main()
151 {
152 
153  std::cout << std::endl << "----TEST INCLUSIVE and EXCLUSIVE SCAN----" << std::endl << std::endl;
154  test_scans();
155 
156  std::cout << std::endl <<"--------TEST SUCCESSFULLY COMPLETED----------" << std::endl;
157 }
Implementations of dense matrix related operations including matrix-vector products.
#define EPS
Definition: scan.cpp:50
Implementation of the compressed_matrix class.
std::size_t vcl_size_t
Definition: forwards.h:74
void inclusive_scan(vector_base< NumericT > &vec1, vector_base< NumericT > &vec2)
This function implements an inclusive scan.
void test_inclusive_scan_values(viennacl::vector< ScalarType > &vcl_vec)
Definition: scan.cpp:74
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
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) ...
void test_exclusive_scan_values(viennacl::vector< ScalarType > &vcl_vec)
Definition: scan.cpp:94
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
void vector_print(viennacl::vector< ScalarType > &v)
Definition: scan.cpp:55
void exclusive_scan(vector_base< NumericT > &vec1, vector_base< NumericT > &vec2)
This function implements an exclusive scan.
int main()
Definition: scan.cpp:150
void test_scans()
Definition: scan.cpp:115
void init_vector(viennacl::vector< ScalarType > &vcl_v)
Definition: scan.cpp:64
Implementation of the ViennaCL scalar class.
float ScalarType
Definition: scan.cpp:48