38 template<
typename ScalarType>
42 if (std::fabs(s1 - s2) > 0)
43 return (s1 - s2) /
std::max(std::fabs(s1), std::fabs(s2));
49 template<
typename NumericT,
typename Epsilon >
50 int test(Epsilon
const& epsilon)
52 int retval = EXIT_SUCCESS;
63 if ( std::fabs(
diff(s1, vcl_s1)) > epsilon )
65 std::cout <<
"# Error at operation: vcl_s1 = s1;" << std::endl;
66 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
67 retval = EXIT_FAILURE;
71 if ( std::fabs(
diff(s2, vcl_s2)) > epsilon )
73 std::cout <<
"# Error at operation: vcl_s2 = s2;" << std::endl;
74 std::cout <<
" diff: " << fabs(
diff(s2, vcl_s2)) << std::endl;
75 retval = EXIT_FAILURE;
79 if ( std::fabs(
diff(s3, vcl_s3)) > epsilon )
81 std::cout <<
"# Error at operation: vcl_s3 = s3;" << std::endl;
82 std::cout <<
" diff: " << s3 - vcl_s3 << std::endl;
83 retval = EXIT_FAILURE;
90 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
92 std::cout <<
"# Error at operation: swap " << std::endl;
93 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
94 retval = EXIT_FAILURE;
99 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
101 std::cout <<
"# Error at operation: += " << std::endl;
102 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
103 retval = EXIT_FAILURE;
109 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
111 std::cout <<
"# Error at operation: *= " << std::endl;
112 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
113 retval = EXIT_FAILURE;
118 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
120 std::cout <<
"# Error at operation: -= " << std::endl;
121 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
122 retval = EXIT_FAILURE;
128 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
130 std::cout <<
"# Error at operation: /= " << std::endl;
131 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
132 retval = EXIT_FAILURE;
138 vcl_s1 = vcl_s2 + vcl_s3;
139 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
141 std::cout <<
"# Error at operation: s1 = s2 + s3 " << std::endl;
142 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
143 retval = EXIT_FAILURE;
147 vcl_s1 += vcl_s2 + vcl_s3;
148 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
150 std::cout <<
"# Error at operation: s1 += s2 + s3 " << std::endl;
151 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
152 retval = EXIT_FAILURE;
156 vcl_s1 -= vcl_s2 + vcl_s3;
157 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
159 std::cout <<
"# Error at operation: s1 -= s2 + s3 " << std::endl;
160 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
161 retval = EXIT_FAILURE;
165 vcl_s1 = vcl_s2 - vcl_s3;
166 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
168 std::cout <<
"# Error at operation: s1 = s2 - s3 " << std::endl;
169 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
170 retval = EXIT_FAILURE;
174 vcl_s1 += vcl_s2 - vcl_s3;
175 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
177 std::cout <<
"# Error at operation: s1 += s2 - s3 " << std::endl;
178 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
179 retval = EXIT_FAILURE;
183 vcl_s1 -= vcl_s2 - vcl_s3;
184 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
186 std::cout <<
"# Error at operation: s1 -= s2 - s3 " << std::endl;
187 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
188 retval = EXIT_FAILURE;
192 vcl_s1 = vcl_s2 * vcl_s3;
193 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
195 std::cout <<
"# Error at operation: s1 = s2 * s3 " << std::endl;
196 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
197 retval = EXIT_FAILURE;
201 vcl_s1 += vcl_s2 * vcl_s3;
202 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
204 std::cout <<
"# Error at operation: s1 += s2 * s3 " << std::endl;
205 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
206 retval = EXIT_FAILURE;
210 vcl_s1 -= vcl_s2 * vcl_s3;
211 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
213 std::cout <<
"# Error at operation: s1 -= s2 * s3 " << std::endl;
214 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
215 retval = EXIT_FAILURE;
219 vcl_s1 = vcl_s2 / vcl_s3;
220 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
222 std::cout <<
"# Error at operation: s1 = s2 / s3 " << std::endl;
223 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
224 retval = EXIT_FAILURE;
228 vcl_s1 += vcl_s2 / vcl_s3;
229 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
231 std::cout <<
"# Error at operation: s1 += s2 / s3 " << std::endl;
232 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
233 retval = EXIT_FAILURE;
237 vcl_s1 -= vcl_s2 / vcl_s3;
238 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
240 std::cout <<
"# Error at operation: s1 -= s2 / s3 " << std::endl;
241 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
242 retval = EXIT_FAILURE;
248 s1 = s2 * s2 + s3 * s3;
249 vcl_s1 = vcl_s2 * s2 + vcl_s3 * s3;
250 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
252 std::cout <<
"# Error at operation: s1 = s2 * s2 + s3 * s3 " << std::endl;
253 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
254 retval = EXIT_FAILURE;
256 vcl_s1 = vcl_s2 * vcl_s2 + vcl_s3 * vcl_s3;
257 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
259 std::cout <<
"# Error at operation: s1 = s2 * s2 + s3 * s3, second test " << std::endl;
260 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
261 retval = EXIT_FAILURE;
264 s1 = s2 * s2 + s3 / s3;
265 vcl_s1 = vcl_s2 * s2 + vcl_s3 / s3;
266 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
268 std::cout <<
"# Error at operation: s1 = s2 * s2 + s3 / s3 " << std::endl;
269 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
270 retval = EXIT_FAILURE;
272 vcl_s1 = vcl_s2 * vcl_s2 + vcl_s3 / vcl_s3;
273 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
275 std::cout <<
"# Error at operation: s1 = s2 * s2 + s3 / s3, second test " << std::endl;
276 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
277 retval = EXIT_FAILURE;
280 s1 = s2 / s2 + s3 * s3;
281 vcl_s1 = vcl_s2 / s2 + vcl_s3 * s3;
282 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
284 std::cout <<
"# Error at operation: s1 = s2 / s2 + s3 * s3 " << std::endl;
285 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
286 retval = EXIT_FAILURE;
288 vcl_s1 = vcl_s2 / vcl_s2 + vcl_s3 * vcl_s3;
289 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
291 std::cout <<
"# Error at operation: s1 = s2 / s2 + s3 * s3, second test " << std::endl;
292 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
293 retval = EXIT_FAILURE;
296 s1 = s2 / s2 + s3 / s3;
297 vcl_s1 = vcl_s2 / s2 + vcl_s3 / s3;
298 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
300 std::cout <<
"# Error at operation: s1 = s2 / s2 + s3 / s3 " << std::endl;
301 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
302 retval = EXIT_FAILURE;
304 vcl_s1 = vcl_s2 / vcl_s2 + vcl_s3 / vcl_s3;
305 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
307 std::cout <<
"# Error at operation: s1 = s2 / s2 + s3 / s3, second test " << std::endl;
308 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
309 retval = EXIT_FAILURE;
315 s1 += s2 * s2 + s3 * s3;
316 vcl_s1 += vcl_s2 * s2 + vcl_s3 * s3;
317 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
319 std::cout <<
"# Error at operation: s1 += s2 * s2 + s3 * s3 " << std::endl;
320 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
321 retval = EXIT_FAILURE;
324 s1 += s2 * s2 + s3 / s3;
325 vcl_s1 += vcl_s2 * s2 + vcl_s3 / s3;
326 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
328 std::cout <<
"# Error at operation: s1 += s2 * s2 + s3 / s3 " << std::endl;
329 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
330 retval = EXIT_FAILURE;
333 s1 += s2 / s2 + s3 * s3;
334 vcl_s1 += vcl_s2 / s2 + vcl_s3 * s3;
335 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
337 std::cout <<
"# Error at operation: s1 += s2 / s2 + s3 * s3 " << std::endl;
338 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
339 retval = EXIT_FAILURE;
342 s1 += s2 / s2 + s3 / s3;
343 vcl_s1 += vcl_s2 / s2 + vcl_s3 / s3;
344 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
346 std::cout <<
"# Error at operation: s1 += s2 / s2 + s3 / s3 " << std::endl;
347 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
348 retval = EXIT_FAILURE;
354 s1 -= s2 * s2 + s3 * s3;
355 vcl_s1 -= vcl_s2 * s2 + vcl_s3 * s3;
356 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
358 std::cout <<
"# Error at operation: s1 -= s2 * s2 + s3 * s3 " << std::endl;
359 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
360 retval = EXIT_FAILURE;
363 s1 -= s2 * s2 + s3 / s3;
364 vcl_s1 -= vcl_s2 * s2 + vcl_s3 / s3;
365 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
367 std::cout <<
"# Error at operation: s1 -= s2 * s2 + s3 / s3 " << std::endl;
368 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
369 retval = EXIT_FAILURE;
372 s1 -= s2 / s2 + s3 * s3;
373 vcl_s1 -= vcl_s2 / s2 + vcl_s3 * s3;
374 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
376 std::cout <<
"# Error at operation: s1 -= s2 / s2 + s3 * s3 " << std::endl;
377 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
378 retval = EXIT_FAILURE;
381 s1 -= s2 / s2 + s3 / s3;
382 vcl_s1 -= vcl_s2 / s2 + vcl_s3 / s3;
383 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
385 std::cout <<
"# Error at operation: s1 -= s2 / s2 + s3 / s3 " << std::endl;
386 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
387 retval = EXIT_FAILURE;
393 s1 = s2 + s3 * s2 - s3 /
s1;
394 vcl_s1 = vcl_s2 + vcl_s3 * vcl_s2 - vcl_s3 / vcl_s1;
395 if ( fabs(
diff(s1, vcl_s1)) > epsilon )
397 std::cout <<
"# Error at operation: + * - / " << std::endl;
398 std::cout <<
" diff: " << fabs(
diff(s1, vcl_s1)) << std::endl;
399 retval = EXIT_FAILURE;
409 std::cout << std::endl;
410 std::cout <<
"----------------------------------------------" << std::endl;
411 std::cout <<
"----------------------------------------------" << std::endl;
412 std::cout <<
"## Test :: Scalar" << std::endl;
413 std::cout <<
"----------------------------------------------" << std::endl;
414 std::cout <<
"----------------------------------------------" << std::endl;
415 std::cout << std::endl;
417 int retval = EXIT_SUCCESS;
419 std::cout << std::endl;
420 std::cout <<
"----------------------------------------------" << std::endl;
421 std::cout << std::endl;
424 NumericT epsilon =
NumericT(1.0E-5);
425 std::cout <<
"# Testing setup:" << std::endl;
426 std::cout <<
" eps: " << epsilon << std::endl;
427 std::cout <<
" numeric: float" << std::endl;
428 retval = test<NumericT>(epsilon);
429 if ( retval == EXIT_SUCCESS )
430 std::cout <<
"# Test passed" << std::endl;
434 std::cout << std::endl;
435 std::cout <<
"----------------------------------------------" << std::endl;
436 std::cout << std::endl;
437 #ifdef VIENNACL_WITH_OPENCL
443 NumericT epsilon = 1.0E-10;
444 std::cout <<
"# Testing setup:" << std::endl;
445 std::cout <<
" eps: " << epsilon << std::endl;
446 std::cout <<
" numeric: double" << std::endl;
447 retval = test<NumericT>(epsilon);
448 if ( retval == EXIT_SUCCESS )
449 std::cout <<
"# Test passed" << std::endl;
453 std::cout << std::endl;
456 std::cout << std::endl;
457 std::cout <<
"------- Test completed --------" << std::endl;
458 std::cout << std::endl;
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
viennacl::scalar< int > s2
viennacl::scalar< float > s1
T max(const T &lhs, const T &rhs)
Maximum.
viennacl::ocl::device const & current_device()
Convenience function for returning the active device in the current context.
viennacl::enable_if< viennacl::is_scalar< S1 >::value &&viennacl::is_scalar< S2 >::value >::type swap(S1 &s1, S2 &s2)
Swaps the contents of two scalars.
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
int test(Epsilon const &epsilon)
ScalarType diff(ScalarType &s1, viennacl::scalar< ScalarType > &s2)
Implementation of the ViennaCL scalar class.