ViennaCL - The Vienna Computing Library: tests/src/scan.cpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 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-2016, 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 //include basic scalar and vector types of ViennaCL
31 #include "viennacl/scalar.hpp"
32 #include "viennacl/vector.hpp"
33
34 // Some helper functions for this tutorial:
35 #include <iostream>
36 #include <limits>
37 #include <string>
38 #include <iomanip>
39
40
41 typedef int ScalarType;
42
43 static void init_vector(viennacl::vector<ScalarType>& vcl_v)
44 {
45 std::vector<ScalarType> v(vcl_v.size());
46 for (std::size_t i = 0; i < v.size(); ++i)
47 v[i] = ScalarType(i % 7 + 1);
48 viennacl::copy(v, vcl_v);
49 }
50
51 static void test_scan_values(viennacl::vector<ScalarType> const & input,
52 viennacl::vector<ScalarType> & result,
53 bool is_inclusive_scan)
54 {
55 std::vector<ScalarType> host_input(input.size());
56 std::vector<ScalarType> host_result(result.size());
57
58 viennacl::copy(input, host_input);
59 viennacl::copy(result, host_result);
60
61 ScalarType sum = 0;
62 if (is_inclusive_scan)
63 {
64 for(viennacl::vcl_size_t i = 0; i < input.size(); i++)
65 {
66 sum += host_input[i];
67 host_input[i] = sum;
68 }
69 }
70 else
71 {
72 for(viennacl::vcl_size_t i = 0; i < input.size(); i++)
73 {
74 ScalarType tmp = host_input[i];
75 host_input[i] = sum;
76 sum += tmp;
77 }
78 }
79
80
81 for(viennacl::vcl_size_t i = 0; i < input.size(); i++)
82 {
83 if (host_input[i] != host_result[i])
84 {
85 std::cout << "Fail at vector index " << i << std::endl;
86 std::cout << " result[" << i << "] = " << host_result[i] << std::endl;
87 std::cout << " Reference = " << host_input[i] << std::endl;
88 if (i > 0)
89 {
90 std::cout << " previous result[" << i-1 << "] = " << host_result[i-1] << std::endl;
91 std::cout << " previous Reference = " << host_input[i-1] << std::endl;
92 }
93 exit(EXIT_FAILURE);
94 }
95 }
96 std::cout << "PASSED!" << std::endl;
97
98 }
99
100
101 static void test_scans(unsigned int sz)
102 {
103 viennacl::vector<ScalarType> vec1(sz), vec2(sz);
104
105 std::cout << "Initialize vector..." << std::endl;
106 init_vector(vec1);
107
108
109 // INCLUSIVE SCAN
110 std::cout << " --- Inclusive scan ---" << std::endl;
111 std::cout << "Separate vectors: ";
112 viennacl::linalg::inclusive_scan(vec1, vec2);
113 test_scan_values(vec1, vec2, true);
114
115 std::cout << "In-place: ";
116 vec2 = vec1;
117 viennacl::linalg::inclusive_scan(vec2);
118 test_scan_values(vec1, vec2, true);
119 std::cout << "Inclusive scan tested successfully!" << std::endl << std::endl;
120
121
122
123 std::cout << "Initialize vector..." << std::endl;
124 init_vector(vec1);
125
126 // EXCLUSIVE SCAN
127 std::cout << " --- Exclusive scan ---" << std::endl;
128 std::cout << "Separate vectors: ";
129 viennacl::linalg::exclusive_scan(vec1, vec2);
130 test_scan_values(vec1, vec2, false);
131
132 std::cout << "In-place: ";
133 vec2 = vec1;
134 viennacl::linalg::exclusive_scan(vec2);
135 test_scan_values(vec1, vec2, false);
136 std::cout << "Exclusive scan tested successfully!" << std::endl << std::endl;
137
138 }
139
140 int main()
141 {
142
143 std::cout << std::endl << "----TEST INCLUSIVE and EXCLUSIVE SCAN----" << std::endl << std::endl;
144 std::cout << " //// Tiny vectors ////" << std::endl;
145 test_scans(27);
146 std::cout << " //// Small vectors ////" << std::endl;
147 test_scans(298);
148 std::cout << " //// Medium vectors ////" << std::endl;
149 test_scans(12345);
150 std::cout << " //// Large vectors ////" << std::endl;
151 test_scans(123456);
152
153 std::cout << std::endl <<"--------TEST SUCCESSFULLY COMPLETED----------" << std::endl;
154 }
ScalarType
int ScalarType
Definition: scan.cpp:41
viennacl::linalg::sum
viennacl::scalar_expression< const viennacl::vector_base< NumericT >, const viennacl::vector_base< NumericT >, viennacl::op_sum > sum(viennacl::vector_base< NumericT > const &x)
User interface function for computing the sum of all elements of a vector.
Definition: sum.hpp:45
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::vector< ScalarType >
viennacl::linalg::inclusive_scan
void inclusive_scan(vector_base< NumericT > &vec1, vector_base< NumericT > &vec2)
This function implements an inclusive scan.
Definition: vector_operations.hpp:1190
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::copy
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) ...
Definition: circulant_matrix.hpp:150
viennacl::vector_base::size
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::linalg::exclusive_scan
void exclusive_scan(vector_base< NumericT > &vec1, vector_base< NumericT > &vec2)
This function implements an exclusive scan.
Definition: vector_operations.hpp:1240
main
int main()
Definition: scan.cpp:140
scalar.hpp
Implementation of the ViennaCL scalar class.