ViennaCL - The Vienna Computing Library: tests/src/vector_multi_inner_prod.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
vector_multi_inner_prod.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 // *** System
26 //
27 #include <iostream>
28 #include <iomanip>
29 #include <iterator>
30
31 //
32 // *** ViennaCL
33 //
34 #include "viennacl/vector.hpp"
35 #include "viennacl/vector_proxy.hpp"
36 #include "viennacl/linalg/inner_prod.hpp"
37 #include "viennacl/linalg/norm_1.hpp"
38 #include "viennacl/linalg/norm_2.hpp"
39 #include "viennacl/linalg/norm_inf.hpp"
40 #include "viennacl/tools/random.hpp"
41
42 //
43 // -------------------------------------------------------------
44 //
45 template<typename ScalarType>
46 ScalarType diff(ScalarType const & s1, ScalarType const & s2)
47 {
48 viennacl::backend::finish();
49 if (s1 != s2)
50 return (s1 - s2) / std::max(std::fabs(s1), std::fabs(s2));
51 return 0;
52 }
53 //
54 // -------------------------------------------------------------
55 //
56 template<typename ScalarType>
57 ScalarType diff(ScalarType const & s1, viennacl::scalar<ScalarType> const & s2)
58 {
59 viennacl::backend::finish();
60 if (s1 != s2)
61 return (s1 - s2) / std::max(std::fabs(s1), std::fabs(s2));
62 return 0;
63 }
64 //
65 // -------------------------------------------------------------
66 //
67 template<typename ScalarType>
68 ScalarType diff(ScalarType const & s1, viennacl::entry_proxy<ScalarType> const & s2)
69 {
70 viennacl::backend::finish();
71 if (s1 != s2)
72 return (s1 - s2) / std::max(std::fabs(s1), std::fabs(s2));
73 return 0;
74 }
75 //
76 // -------------------------------------------------------------
77 //
78 template<typename ScalarType, typename ViennaCLVectorType>
79 ScalarType diff(std::vector<ScalarType> const & v1, ViennaCLVectorType const & vcl_vec)
80 {
81 std::vector<ScalarType> v2_cpu(vcl_vec.size());
82 viennacl::backend::finish();
83 viennacl::copy(vcl_vec, v2_cpu);
84
85 for (unsigned int i=0;i<v1.size(); ++i)
86 {
87 if ( std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) > 0 )
88 v2_cpu[i] = std::fabs(v2_cpu[i] - v1[i]) / std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) );
89 else
90 v2_cpu[i] = 0.0;
91 }
92
93 ScalarType norm_inf = 0;
94 for (std::size_t i=0; i<v2_cpu.size(); ++i)
95 norm_inf = std::max<ScalarType>(norm_inf, std::fabs(v2_cpu[i]));
96
97 return norm_inf;
98 }
99
100
101
102 template<typename T1, typename T2>
103 int check(T1 const & t1, T2 const & t2, double epsilon)
104 {
105 int retval = EXIT_SUCCESS;
106
107 double temp = std::fabs(diff(t1, t2));
108 if (temp > epsilon)
109 {
110 std::cout << "# Error! Relative difference: " << temp << std::endl;
111 retval = EXIT_FAILURE;
112 }
113 return retval;
114 }
115
116
117 //
118 // -------------------------------------------------------------
119 //
120 template< typename NumericT, typename Epsilon,
121 typename STLVectorType1, typename STLVectorType2, typename STLVectorType3, typename STLVectorType4,
122 typename ViennaCLVectorType1, typename ViennaCLVectorType2, typename ViennaCLVectorType3, typename ViennaCLVectorType4 >
123 int test(Epsilon const& epsilon,
124 STLVectorType1 & std_v1, STLVectorType2 & std_v2, STLVectorType3 & std_v3, STLVectorType4 & std_v4,
125 ViennaCLVectorType1 & vcl_v1, ViennaCLVectorType2 & vcl_v2, ViennaCLVectorType3 & vcl_v3, ViennaCLVectorType4 & vcl_v4)
126 {
127 int retval = EXIT_SUCCESS;
128
129 viennacl::tools::uniform_random_numbers<NumericT> randomNumber;
130
131 for (std::size_t i=0; i<std_v1.size(); ++i)
132 {
133 std_v1[i] = NumericT(1.0) + randomNumber();
134 std_v2[i] = NumericT(1.0) + randomNumber();
135 std_v3[i] = NumericT(1.0) + randomNumber();
136 std_v4[i] = NumericT(1.0) + randomNumber();
137 }
138
139 viennacl::copy(std_v1.begin(), std_v1.end(), vcl_v1.begin()); //resync
140 viennacl::copy(std_v2.begin(), std_v2.end(), vcl_v2.begin());
141 viennacl::copy(std_v3.begin(), std_v3.end(), vcl_v3.begin());
142 viennacl::copy(std_v4.begin(), std_v4.end(), vcl_v4.begin());
143
144 std::cout << "Checking for successful copy..." << std::endl;
145 if (check(std_v1, vcl_v1, epsilon) != EXIT_SUCCESS)
146 return EXIT_FAILURE;
147 if (check(std_v2, vcl_v2, epsilon) != EXIT_SUCCESS)
148 return EXIT_FAILURE;
149 if (check(std_v3, vcl_v3, epsilon) != EXIT_SUCCESS)
150 return EXIT_FAILURE;
151 if (check(std_v4, vcl_v4, epsilon) != EXIT_SUCCESS)
152 return EXIT_FAILURE;
153
154 std::vector<NumericT> ref_result(40, 0.0);
155 viennacl::vector<NumericT> result = viennacl::scalar_vector<NumericT>(40, 0.0);
156
157 std::cout << "Testing inner_prod with two vectors..." << std::endl;
158 ref_result[2] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[2] += std_v1[i] * std_v1[i];
159 ref_result[5] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[5] += std_v1[i] * std_v2[i];
160 viennacl::project(result, viennacl::slice(2, 3, 2)) = viennacl::linalg::inner_prod(vcl_v1, viennacl::tie(vcl_v1, vcl_v2));
161 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
162 {
163 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
164 std::cout << result << std::endl;
165 return EXIT_FAILURE;
166 }
167
168 ref_result[3] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[3] += std_v1[i] * std_v3[i];
169 ref_result[7] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[7] += std_v1[i] * std_v4[i];
170 viennacl::project(result, viennacl::slice(3, 4, 2)) = viennacl::linalg::inner_prod(vcl_v1, viennacl::tie(vcl_v3, vcl_v4));
171 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
172 {
173 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
174 std::cout << result << std::endl;
175 return EXIT_FAILURE;
176 }
177
178
179 std::cout << "Testing inner_prod with three vectors..." << std::endl;
180 ref_result[1] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[1] += std_v1[i] * std_v1[i];
181 ref_result[3] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[3] += std_v1[i] * std_v2[i];
182 ref_result[5] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[5] += std_v1[i] * std_v3[i];
183 viennacl::project(result, viennacl::slice(1, 2, 3)) = viennacl::linalg::inner_prod(vcl_v1, viennacl::tie(vcl_v1, vcl_v2, vcl_v3));
184 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
185 {
186 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
187 std::cout << result << std::endl;
188 return EXIT_FAILURE;
189 }
190
191 ref_result[2] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[2] += std_v1[i] * std_v3[i];
192 ref_result[6] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[6] += std_v1[i] * std_v2[i];
193 ref_result[10] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[10] += std_v1[i] * std_v4[i];
194 viennacl::project(result, viennacl::slice(2, 4, 3)) = viennacl::linalg::inner_prod(vcl_v1, viennacl::tie(vcl_v3, vcl_v2, vcl_v4));
195 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
196 {
197 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
198 std::cout << result << std::endl;
199 return EXIT_FAILURE;
200 }
201
202 std::cout << "Testing inner_prod with four vectors..." << std::endl;
203 ref_result[4] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[4] += std_v1[i] * std_v1[i];
204 ref_result[5] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[5] += std_v1[i] * std_v2[i];
205 ref_result[6] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[6] += std_v1[i] * std_v3[i];
206 ref_result[7] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[7] += std_v1[i] * std_v4[i];
207 viennacl::project(result, viennacl::slice(4, 1, 4)) = viennacl::linalg::inner_prod(vcl_v1, viennacl::tie(vcl_v1, vcl_v2, vcl_v3, vcl_v4));
208 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
209 {
210 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
211 std::cout << result << std::endl;
212 return EXIT_FAILURE;
213 }
214
215 ref_result[3] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[3] += std_v1[i] * std_v3[i];
216 ref_result[6] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[6] += std_v1[i] * std_v2[i];
217 ref_result[9] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[9] += std_v1[i] * std_v4[i];
218 ref_result[12] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[12] += std_v1[i] * std_v1[i];
219 viennacl::project(result, viennacl::slice(3, 3, 4)) = viennacl::linalg::inner_prod(vcl_v1, viennacl::tie(vcl_v3, vcl_v2, vcl_v4, vcl_v1));
220 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
221 {
222 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
223 std::cout << result << std::endl;
224 return EXIT_FAILURE;
225 }
226
227 std::cout << "Testing inner_prod with five vectors..." << std::endl;
228 ref_result[1] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[1] += std_v1[i] * std_v1[i];
229 ref_result[3] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[3] += std_v1[i] * std_v2[i];
230 ref_result[5] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[5] += std_v1[i] * std_v3[i];
231 ref_result[7] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[7] += std_v1[i] * std_v4[i];
232 ref_result[9] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[9] += std_v1[i] * std_v2[i];
233 viennacl::project(result, viennacl::slice(1, 2, 5)) = viennacl::linalg::inner_prod(vcl_v1, viennacl::tie(vcl_v1, vcl_v2, vcl_v3, vcl_v4, vcl_v2));
234 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
235 {
236 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
237 std::cout << result << std::endl;
238 return EXIT_FAILURE;
239 }
240
241 ref_result[2] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[2] += std_v1[i] * std_v3[i];
242 ref_result[4] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[4] += std_v1[i] * std_v2[i];
243 ref_result[6] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[6] += std_v1[i] * std_v4[i];
244 ref_result[8] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[8] += std_v1[i] * std_v1[i];
245 ref_result[10] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[10] += std_v1[i] * std_v2[i];
246 viennacl::project(result, viennacl::slice(2, 2, 5)) = viennacl::linalg::inner_prod(vcl_v1, viennacl::tie(vcl_v3, vcl_v2, vcl_v4, vcl_v1, vcl_v2));
247 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
248 {
249 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
250 std::cout << result << std::endl;
251 return EXIT_FAILURE;
252 }
253
254
255 std::cout << "Testing inner_prod with eight vectors..." << std::endl;
256 ref_result[1] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[1] += std_v1[i] * std_v1[i];
257 ref_result[5] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[5] += std_v1[i] * std_v2[i];
258 ref_result[9] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[9] += std_v1[i] * std_v3[i];
259 ref_result[13] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[13] += std_v1[i] * std_v4[i];
260 ref_result[17] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[17] += std_v1[i] * std_v3[i];
261 ref_result[21] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[21] += std_v1[i] * std_v2[i];
262 ref_result[25] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[25] += std_v1[i] * std_v1[i];
263 ref_result[29] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[29] += std_v1[i] * std_v2[i];
264 std::vector<viennacl::vector_base<NumericT> const *> vecs1(8);
265 vecs1[0] = &vcl_v1;
266 vecs1[1] = &vcl_v2;
267 vecs1[2] = &vcl_v3;
268 vecs1[3] = &vcl_v4;
269 vecs1[4] = &vcl_v3;
270 vecs1[5] = &vcl_v2;
271 vecs1[6] = &vcl_v1;
272 vecs1[7] = &vcl_v2;
273 viennacl::vector_tuple<NumericT> tuple1(vecs1);
274 viennacl::project(result, viennacl::slice(1, 4, 8)) = viennacl::linalg::inner_prod(vcl_v1, tuple1);
275 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
276 {
277 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
278 std::cout << result << std::endl;
279 return EXIT_FAILURE;
280 }
281
282 ref_result[3] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[3] += std_v1[i] * std_v2[i];
283 ref_result[5] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[5] += std_v1[i] * std_v4[i];
284 ref_result[7] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[7] += std_v1[i] * std_v1[i];
285 ref_result[9] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[9] += std_v1[i] * std_v2[i];
286 ref_result[11] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[11] += std_v1[i] * std_v2[i];
287 ref_result[13] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[13] += std_v1[i] * std_v1[i];
288 ref_result[15] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[15] += std_v1[i] * std_v4[i];
289 ref_result[17] = 0; for (std::size_t i=0; i<std_v1.size(); ++i) ref_result[17] += std_v1[i] * std_v2[i];
290 std::vector<viennacl::vector_base<NumericT> const *> vecs2(8);
291 vecs2[0] = &vcl_v2;
292 vecs2[1] = &vcl_v4;
293 vecs2[2] = &vcl_v1;
294 vecs2[3] = &vcl_v2;
295 vecs2[4] = &vcl_v2;
296 vecs2[5] = &vcl_v1;
297 vecs2[6] = &vcl_v4;
298 vecs2[7] = &vcl_v2;
299 viennacl::vector_tuple<NumericT> tuple2(vecs2);
300 viennacl::project(result, viennacl::slice(3, 2, 8)) = viennacl::linalg::inner_prod(vcl_v1, tuple2);
301 if (check(ref_result, result, epsilon) != EXIT_SUCCESS)
302 {
303 std::copy(ref_result.begin(), ref_result.end(), std::ostream_iterator<NumericT>(std::cout, " ")); std::cout << std::endl;
304 std::cout << result << std::endl;
305 return EXIT_FAILURE;
306 }
307
308
309 // --------------------------------------------------------------------------
310 return retval;
311 }
312
313
314 template< typename NumericT, typename Epsilon >
315 int test(Epsilon const& epsilon)
316 {
317 viennacl::tools::uniform_random_numbers<NumericT> randomNumber;
318
319 int retval = EXIT_SUCCESS;
320 std::size_t size = 8 * 1337;
321
322 std::cout << "Running tests for vector of size " << size << std::endl;
323
324 //
325 // Set up STL objects
326 //
327 std::vector<NumericT> std_full_vec1(size);
328 std::vector<NumericT> std_full_vec2(std_full_vec1.size());
329
330 for (std::size_t i=0; i<std_full_vec1.size(); ++i)
331 {
332 std_full_vec1[i] = NumericT(1.0) + randomNumber();
333 std_full_vec2[i] = NumericT(1.0) + randomNumber();
334 }
335
336 std::vector<NumericT> std_slice_vec1(std_full_vec1.size() / 8); for (std::size_t i=0; i<std_slice_vec1.size(); ++i) std_slice_vec1[i] = std_full_vec1[ std_full_vec1.size() / 8 + i * 3];
337 std::vector<NumericT> std_slice_vec2(std_full_vec2.size() / 8); for (std::size_t i=0; i<std_slice_vec2.size(); ++i) std_slice_vec2[i] = std_full_vec2[2 * std_full_vec2.size() / 8 + i * 1];
338 std::vector<NumericT> std_slice_vec3(std_full_vec1.size() / 8); for (std::size_t i=0; i<std_slice_vec3.size(); ++i) std_slice_vec3[i] = std_full_vec1[4 * std_full_vec1.size() / 8 + i * 2];
339 std::vector<NumericT> std_slice_vec4(std_full_vec2.size() / 8); for (std::size_t i=0; i<std_slice_vec4.size(); ++i) std_slice_vec4[i] = std_full_vec2[3 * std_full_vec2.size() / 8 + i * 4];
340
341 //
342 // Set up ViennaCL objects
343 //
344 viennacl::vector<NumericT> vcl_full_vec1(std_full_vec1.size());
345 viennacl::vector<NumericT> vcl_full_vec2(std_full_vec2.size());
346
347 viennacl::fast_copy(std_full_vec1.begin(), std_full_vec1.end(), vcl_full_vec1.begin());
348 viennacl::copy (std_full_vec2.begin(), std_full_vec2.end(), vcl_full_vec2.begin());
349
350 viennacl::slice vcl_s1( vcl_full_vec1.size() / 8, 3, vcl_full_vec1.size() / 8);
351 viennacl::slice vcl_s2(2 * vcl_full_vec2.size() / 8, 1, vcl_full_vec2.size() / 8);
352 viennacl::slice vcl_s3(4 * vcl_full_vec1.size() / 8, 2, vcl_full_vec1.size() / 8);
353 viennacl::slice vcl_s4(3 * vcl_full_vec2.size() / 8, 4, vcl_full_vec2.size() / 8);
354 viennacl::vector_slice< viennacl::vector<NumericT> > vcl_slice_vec1(vcl_full_vec1, vcl_s1);
355 viennacl::vector_slice< viennacl::vector<NumericT> > vcl_slice_vec2(vcl_full_vec2, vcl_s2);
356 viennacl::vector_slice< viennacl::vector<NumericT> > vcl_slice_vec3(vcl_full_vec1, vcl_s3);
357 viennacl::vector_slice< viennacl::vector<NumericT> > vcl_slice_vec4(vcl_full_vec2, vcl_s4);
358
359 viennacl::vector<NumericT> vcl_short_vec1(vcl_slice_vec1);
360 viennacl::vector<NumericT> vcl_short_vec2 = vcl_slice_vec2;
361 viennacl::vector<NumericT> vcl_short_vec3 = vcl_slice_vec2 + vcl_slice_vec1;
362 viennacl::vector<NumericT> vcl_short_vec4 = vcl_short_vec1 + vcl_slice_vec2;
363
364 std::vector<NumericT> std_short_vec1(std_slice_vec1);
365 std::vector<NumericT> std_short_vec2(std_slice_vec2);
366 std::vector<NumericT> std_short_vec3(std_slice_vec2.size()); for (std::size_t i=0; i<std_short_vec3.size(); ++i) std_short_vec3[i] = std_slice_vec2[i] + std_slice_vec1[i];
367 std::vector<NumericT> std_short_vec4(std_slice_vec2.size()); for (std::size_t i=0; i<std_short_vec4.size(); ++i) std_short_vec4[i] = std_slice_vec1[i] + std_slice_vec2[i];
368
369 std::cout << "Testing creation of vectors from slice..." << std::endl;
370 if (check(std_short_vec1, vcl_short_vec1, epsilon) != EXIT_SUCCESS)
371 return EXIT_FAILURE;
372 if (check(std_short_vec2, vcl_short_vec2, epsilon) != EXIT_SUCCESS)
373 return EXIT_FAILURE;
374 if (check(std_short_vec3, vcl_short_vec3, epsilon) != EXIT_SUCCESS)
375 return EXIT_FAILURE;
376 if (check(std_short_vec4, vcl_short_vec4, epsilon) != EXIT_SUCCESS)
377 return EXIT_FAILURE;
378
379
380 //
381 // Now start running tests for vectors, ranges and slices:
382 //
383
384 std::cout << " ** [vector|vector|vector|vector] **" << std::endl;
385 retval = test<NumericT>(epsilon,
386 std_short_vec1, std_short_vec2, std_short_vec2, std_short_vec2,
387 vcl_short_vec1, vcl_short_vec2, vcl_short_vec3, vcl_short_vec4);
388 if (retval != EXIT_SUCCESS)
389 return EXIT_FAILURE;
390
391 std::cout << " ** [vector|vector|vector|slice] **" << std::endl;
392 retval = test<NumericT>(epsilon,
393 std_short_vec1, std_short_vec2, std_short_vec2, std_slice_vec2,
394 vcl_short_vec1, vcl_short_vec2, vcl_short_vec3, vcl_slice_vec4);
395 if (retval != EXIT_SUCCESS)
396 return EXIT_FAILURE;
397
398 std::cout << " ** [vector|vector|slice|vector] **" << std::endl;
399 retval = test<NumericT>(epsilon,
400 std_short_vec1, std_short_vec2, std_slice_vec2, std_short_vec2,
401 vcl_short_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_short_vec4);
402 if (retval != EXIT_SUCCESS)
403 return EXIT_FAILURE;
404
405 std::cout << " ** [vector|vector|slice|slice] **" << std::endl;
406 retval = test<NumericT>(epsilon,
407 std_short_vec1, std_short_vec2, std_slice_vec2, std_slice_vec2,
408 vcl_short_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_slice_vec4);
409 if (retval != EXIT_SUCCESS)
410 return EXIT_FAILURE;
411
412 std::cout << " ** [vector|slice|vector|vector] **" << std::endl;
413 retval = test<NumericT>(epsilon,
414 std_short_vec1, std_slice_vec2, std_short_vec2, std_short_vec2,
415 vcl_short_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_short_vec4);
416 if (retval != EXIT_SUCCESS)
417 return EXIT_FAILURE;
418
419 std::cout << " ** [vector|slice|vector|slice] **" << std::endl;
420 retval = test<NumericT>(epsilon,
421 std_short_vec1, std_slice_vec2, std_short_vec2, std_slice_vec2,
422 vcl_short_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_slice_vec4);
423 if (retval != EXIT_SUCCESS)
424 return EXIT_FAILURE;
425
426 std::cout << " ** [vector|slice|slice|vector] **" << std::endl;
427 retval = test<NumericT>(epsilon,
428 std_short_vec1, std_slice_vec2, std_slice_vec2, std_short_vec2,
429 vcl_short_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_short_vec4);
430 if (retval != EXIT_SUCCESS)
431 return EXIT_FAILURE;
432
433 std::cout << " ** [vector|slice|slice|slice] **" << std::endl;
434 retval = test<NumericT>(epsilon,
435 std_short_vec1, std_slice_vec2, std_slice_vec2, std_slice_vec2,
436 vcl_short_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_slice_vec4);
437 if (retval != EXIT_SUCCESS)
438 return EXIT_FAILURE;
439
440
442
443
444 std::cout << " ** [slice|vector|vector|vector] **" << std::endl;
445 retval = test<NumericT>(epsilon,
446 std_slice_vec1, std_short_vec2, std_short_vec2, std_short_vec2,
447 vcl_slice_vec1, vcl_short_vec2, vcl_short_vec3, vcl_short_vec4);
448 if (retval != EXIT_SUCCESS)
449 return EXIT_FAILURE;
450
451 std::cout << " ** [slice|vector|vector|slice] **" << std::endl;
452 retval = test<NumericT>(epsilon,
453 std_slice_vec1, std_short_vec2, std_short_vec2, std_slice_vec2,
454 vcl_slice_vec1, vcl_short_vec2, vcl_short_vec3, vcl_slice_vec4);
455 if (retval != EXIT_SUCCESS)
456 return EXIT_FAILURE;
457
458 std::cout << " ** [slice|vector|slice|vector] **" << std::endl;
459 retval = test<NumericT>(epsilon,
460 std_slice_vec1, std_short_vec2, std_slice_vec2, std_short_vec2,
461 vcl_slice_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_short_vec4);
462 if (retval != EXIT_SUCCESS)
463 return EXIT_FAILURE;
464
465 std::cout << " ** [slice|vector|slice|slice] **" << std::endl;
466 retval = test<NumericT>(epsilon,
467 std_slice_vec1, std_short_vec2, std_slice_vec2, std_slice_vec2,
468 vcl_slice_vec1, vcl_short_vec2, vcl_slice_vec3, vcl_slice_vec4);
469 if (retval != EXIT_SUCCESS)
470 return EXIT_FAILURE;
471
472 std::cout << " ** [slice|slice|vector|vector] **" << std::endl;
473 retval = test<NumericT>(epsilon,
474 std_slice_vec1, std_slice_vec2, std_short_vec2, std_short_vec2,
475 vcl_slice_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_short_vec4);
476 if (retval != EXIT_SUCCESS)
477 return EXIT_FAILURE;
478
479 std::cout << " ** [slice|slice|vector|slice] **" << std::endl;
480 retval = test<NumericT>(epsilon,
481 std_slice_vec1, std_slice_vec2, std_short_vec2, std_slice_vec2,
482 vcl_slice_vec1, vcl_slice_vec2, vcl_short_vec3, vcl_slice_vec4);
483 if (retval != EXIT_SUCCESS)
484 return EXIT_FAILURE;
485
486 std::cout << " ** [slice|slice|slice|vector] **" << std::endl;
487 retval = test<NumericT>(epsilon,
488 std_slice_vec1, std_slice_vec2, std_slice_vec2, std_short_vec2,
489 vcl_slice_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_short_vec4);
490 if (retval != EXIT_SUCCESS)
491 return EXIT_FAILURE;
492
493 std::cout << " ** [slice|slice|slice|slice] **" << std::endl;
494 retval = test<NumericT>(epsilon,
495 std_slice_vec1, std_slice_vec2, std_slice_vec2, std_slice_vec2,
496 vcl_slice_vec1, vcl_slice_vec2, vcl_slice_vec3, vcl_slice_vec4);
497 if (retval != EXIT_SUCCESS)
498 return EXIT_FAILURE;
499
500 return EXIT_SUCCESS;
501 }
502
503
504
505 //
506 // -------------------------------------------------------------
507 //
508 int main()
509 {
510 std::cout << std::endl;
511 std::cout << "----------------------------------------------" << std::endl;
512 std::cout << "----------------------------------------------" << std::endl;
513 std::cout << "## Test :: Vector multiple inner products" << std::endl;
514 std::cout << "----------------------------------------------" << std::endl;
515 std::cout << "----------------------------------------------" << std::endl;
516 std::cout << std::endl;
517
518 int retval = EXIT_SUCCESS;
519
520 std::cout << std::endl;
521 std::cout << "----------------------------------------------" << std::endl;
522 std::cout << std::endl;
523 {
524 typedef float NumericT;
525 NumericT epsilon = static_cast<NumericT>(1.0E-4);
526 std::cout << "# Testing setup:" << std::endl;
527 std::cout << " eps: " << epsilon << std::endl;
528 std::cout << " numeric: float" << std::endl;
529 retval = test<NumericT>(epsilon);
530 if ( retval == EXIT_SUCCESS )
531 std::cout << "# Test passed" << std::endl;
532 else
533 return retval;
534 }
535 std::cout << std::endl;
536 std::cout << "----------------------------------------------" << std::endl;
537 std::cout << std::endl;
538 #ifdef VIENNACL_WITH_OPENCL
539 if ( viennacl::ocl::current_device().double_support() )
540 #endif
541 {
542 {
543 typedef double NumericT;
544 NumericT epsilon = 1.0E-12;
545 std::cout << "# Testing setup:" << std::endl;
546 std::cout << " eps: " << epsilon << std::endl;
547 std::cout << " numeric: double" << std::endl;
548 retval = test<NumericT>(epsilon);
549 if ( retval == EXIT_SUCCESS )
550 std::cout << "# Test passed" << std::endl;
551 else
552 return retval;
553 }
554 std::cout << std::endl;
555 std::cout << "----------------------------------------------" << std::endl;
556 std::cout << std::endl;
557 }
558
559 std::cout << std::endl;
560 std::cout << "------- Test completed --------" << std::endl;
561 std::cout << std::endl;
562
563
564 return retval;
565 }
viennacl::scalar
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
norm_2.hpp
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
main
int main()
Definition: vector_multi_inner_prod.cpp:508
viennacl::backend::finish
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
Definition: memory.hpp:54
viennacl::linalg::inner_prod
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
Definition: inner_prod.hpp:100
s2
viennacl::scalar< int > s2
Definition: global_variables.cpp:58
s1
viennacl::scalar< float > s1
Definition: global_variables.cpp:57
viennacl::linalg::detail::max
T max(const T &lhs, const T &rhs)
Maximum.
Definition: util.hpp:59
viennacl::ocl::current_device
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
Definition: backend.hpp:351
inner_prod.hpp
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
norm_1.hpp
Generic interface for the l^1-norm. See viennacl/linalg/vector_operations.hpp for implementations...
NumericT
float NumericT
Definition: bisect.cpp:40
v1
viennacl::vector< float > v1
Definition: global_variables.cpp:60
viennacl::traits::size
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:239
viennacl::tools::uniform_random_numbers
Random number generator for returning uniformly distributed values in the closed interval [0...
Definition: random.hpp:44
viennacl::vector_tuple
Tuple class holding pointers to multiple vectors. Mainly used as a temporary object returned from vie...
Definition: forwards.h:269
viennacl::vector_slice
Class for representing strided subvectors of a bigger vector x.
Definition: forwards.h:437
test
int test(Epsilon const &epsilon, STLVectorType1 &std_v1, STLVectorType2 &std_v2, STLVectorType3 &std_v3, STLVectorType4 &std_v4, ViennaCLVectorType1 &vcl_v1, ViennaCLVectorType2 &vcl_v2, ViennaCLVectorType3 &vcl_v3, ViennaCLVectorType4 &vcl_v4)
Definition: vector_multi_inner_prod.cpp:123
viennacl::ocl::device::double_support
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Definition: device.hpp:956
viennacl::project
matrix_range< MatrixType > project(MatrixType const &A, viennacl::range const &r1, viennacl::range const &r2)
Definition: matrix_proxy.hpp:326
viennacl::tie
vector_tuple< ScalarT > tie(vector_base< ScalarT > const &v0, vector_base< ScalarT > const &v1)
Definition: vector.hpp:1155
viennacl::vector< NumericT >
vector_proxy.hpp
Proxy classes for vectors.
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::scalar_vector
Represents a vector consisting of scalars 's' only, i.e. v[i] = s for all i. To be used as an initial...
Definition: vector_def.hpp:87
viennacl::linalg::norm_inf
T norm_inf(std::vector< T, A > const &v1)
Definition: norm_inf.hpp:60
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
random.hpp
A small collection of sequential random number generators.
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
diff
ScalarType diff(ScalarType const &s1, ScalarType const &s2)
Definition: vector_multi_inner_prod.cpp:46
check
int check(T1 const &t1, T2 const &t2, double epsilon)
Definition: vector_multi_inner_prod.cpp:103
viennacl::basic_slice
A slice class that refers to an interval [start, stop), where 'start' is included, and 'stop' is excluded.
Definition: forwards.h:429
viennacl::entry_proxy
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:233
norm_inf.hpp
Generic interface for the l^infty-norm. See viennacl/linalg/vector_operations.hpp for implementations...
viennacl::fast_copy
void fast_copy(const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_begin, const const_vector_iterator< SCALARTYPE, ALIGNMENT > &gpu_end, CPU_ITERATOR cpu_begin)