All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
libviennacl_blas1.cpp
Go to the documentation of this file.
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the PDF manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
// include necessary system headers
#include <iostream>
#include <vector>
#include <cmath>
// Some helper functions for this tutorial:
#include "viennacl.hpp"
#include "viennacl/vector.hpp"
template<typename ScalarType>
ScalarType diff(ScalarType const & s1, ScalarType const & s2)
{
if (s1 > s2 || s1 < s2)
return (s1 - s2) / std::max(static_cast<ScalarType>(std::fabs(static_cast<double>(s1))),
static_cast<ScalarType>(std::fabs(static_cast<double>(s2))));
return ScalarType(0);
}
template<typename ScalarType, typename ViennaCLVectorType>
ScalarType diff(std::vector<ScalarType> const & v1, ViennaCLVectorType const & vcl_vec)
{
std::vector<ScalarType> v2_cpu(vcl_vec.size());
viennacl::backend::finish();
viennacl::copy(vcl_vec, v2_cpu);
ScalarType inf_norm = 0;
for (unsigned int i=0;i<v1.size(); ++i)
{
if ( std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) ) > 0 )
v2_cpu[i] = std::fabs(v2_cpu[i] - v1[i]) / std::max( std::fabs(v2_cpu[i]), std::fabs(v1[i]) );
else
v2_cpu[i] = 0.0;
if (v2_cpu[i] > inf_norm)
inf_norm = v2_cpu[i];
}
return inf_norm;
}
template<typename T, typename U, typename EpsilonT>
void check(T const & t, U const & u, EpsilonT eps)
{
EpsilonT rel_error = std::fabs(static_cast<EpsilonT>(diff(t,u)));
if (rel_error > eps)
{
std::cerr << "Relative error: " << rel_error << std::endl;
std::cerr << "Aborting!" << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "SUCCESS ";
}
int main()
{
std::size_t size = 10; // at least 7
float eps_float = 1e-5f;
double eps_double = 1e-12;
float ref_float_alpha;
double ref_double_alpha;
std::vector<float> ref_float_x(size, 1.0f);
std::vector<float> ref_float_y(size, 2.0f);
std::vector<double> ref_double_x(size, 1.0);
std::vector<double> ref_double_y(size, 2.0);
ViennaCLBackend my_backend;
ViennaCLBackendCreate(&my_backend);
// Host setup
float host_float_alpha = 0;
viennacl::vector<float> host_float_x = viennacl::scalar_vector<float>(size, 1.0f, viennacl::context(viennacl::MAIN_MEMORY));
viennacl::vector<float> host_float_y = viennacl::scalar_vector<float>(size, 2.0f, viennacl::context(viennacl::MAIN_MEMORY));
double host_double_alpha = 0;
viennacl::vector<double> host_double_x = viennacl::scalar_vector<double>(size, 1.0, viennacl::context(viennacl::MAIN_MEMORY));
viennacl::vector<double> host_double_y = viennacl::scalar_vector<double>(size, 2.0, viennacl::context(viennacl::MAIN_MEMORY));
// CUDA setup
#ifdef VIENNACL_WITH_CUDA
float cuda_float_alpha = 0;
viennacl::vector<float> cuda_float_x = viennacl::scalar_vector<float>(size, 1.0f, viennacl::context(viennacl::CUDA_MEMORY));
viennacl::vector<float> cuda_float_y = viennacl::scalar_vector<float>(size, 2.0f, viennacl::context(viennacl::CUDA_MEMORY));
double cuda_double_alpha = 0;
viennacl::vector<double> cuda_double_x = viennacl::scalar_vector<double>(size, 1.0, viennacl::context(viennacl::CUDA_MEMORY));
viennacl::vector<double> cuda_double_y = viennacl::scalar_vector<double>(size, 2.0, viennacl::context(viennacl::CUDA_MEMORY));
#endif
// OpenCL setup
#ifdef VIENNACL_WITH_OPENCL
ViennaCLInt context_id = 0;
float opencl_float_alpha = 0;
viennacl::vector<float> opencl_float_x = viennacl::scalar_vector<float>(size, 1.0f, viennacl::context(viennacl::ocl::get_context(context_id)));
viennacl::vector<float> opencl_float_y = viennacl::scalar_vector<float>(size, 2.0f, viennacl::context(viennacl::ocl::get_context(context_id)));
double opencl_double_alpha = 0;
viennacl::vector<double> *opencl_double_x = NULL;
viennacl::vector<double> *opencl_double_y = NULL;
if ( viennacl::ocl::current_device().double_support() )
{
opencl_double_x = new viennacl::vector<double>(viennacl::scalar_vector<double>(size, 1.0, viennacl::context(viennacl::ocl::get_context(context_id))));
opencl_double_y = new viennacl::vector<double>(viennacl::scalar_vector<double>(size, 2.0, viennacl::context(viennacl::ocl::get_context(context_id))));
}
ViennaCLBackendSetOpenCLContextID(my_backend, context_id);
#endif
// consistency checks:
check(ref_float_x, host_float_x, eps_float);
check(ref_float_y, host_float_y, eps_float);
check(ref_double_x, host_double_x, eps_double);
check(ref_double_y, host_double_y, eps_double);
#ifdef VIENNACL_WITH_CUDA
check(ref_float_x, cuda_float_x, eps_float);
check(ref_float_y, cuda_float_y, eps_float);
check(ref_double_x, cuda_double_x, eps_double);
check(ref_double_y, cuda_double_y, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
check(ref_float_x, opencl_float_x, eps_float);
check(ref_float_y, opencl_float_y, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
check(ref_double_x, *opencl_double_x, eps_double);
check(ref_double_y, *opencl_double_y, eps_double);
}
#endif
// ASUM
std::cout << std::endl << "-- Testing xASUM...";
ref_float_alpha = 0;
ref_double_alpha = 0;
for (std::size_t i=0; i<size/4; ++i)
{
ref_float_alpha += std::fabs(ref_float_x[2 + 3*i]);
ref_double_alpha += std::fabs(ref_double_x[2 + 3*i]);
}
std::cout << std::endl << "Host: ";
ViennaCLHostSasum(my_backend, ViennaCLInt(size/4),
&host_float_alpha,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 2, 3);
check(ref_float_alpha, host_float_alpha, eps_float);
ViennaCLHostDasum(my_backend, ViennaCLInt(size/4),
&host_double_alpha,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 2, 3);
check(ref_double_alpha, host_double_alpha, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
ViennaCLCUDASasum(my_backend, ViennaCLInt(size/4),
&cuda_float_alpha,
viennacl::cuda_arg(cuda_float_x), 2, 3);
check(ref_float_alpha, cuda_float_alpha, eps_float);
ViennaCLCUDADasum(my_backend, ViennaCLInt(size/4),
&cuda_double_alpha,
viennacl::cuda_arg(cuda_double_x), 2, 3);
check(ref_double_alpha, cuda_double_alpha, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
ViennaCLOpenCLSasum(my_backend, ViennaCLInt(size/4),
&opencl_float_alpha,
viennacl::traits::opencl_handle(opencl_float_x).get(), 2, 3);
check(ref_float_alpha, opencl_float_alpha, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLDasum(my_backend, ViennaCLInt(size/4),
&opencl_double_alpha,
viennacl::traits::opencl_handle(*opencl_double_x).get(), 2, 3);
check(ref_double_alpha, opencl_double_alpha, eps_double);
}
#endif
// AXPY
std::cout << std::endl << "-- Testing xAXPY...";
for (std::size_t i=0; i<size/3; ++i)
{
ref_float_y[1 + 2*i] += 2.0f * ref_float_x[0 + 2*i];
ref_double_y[1 + 2*i] += 2.0 * ref_double_x[0 + 2*i];
}
std::cout << std::endl << "Host: ";
ViennaCLHostSaxpy(my_backend, ViennaCLInt(size/3),
2.0f,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 0, 2,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_y), 1, 2);
check(ref_float_x, host_float_x, eps_float);
check(ref_float_y, host_float_y, eps_float);
ViennaCLHostDaxpy(my_backend, ViennaCLInt(size/3),
2.0,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 0, 2,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_y), 1, 2);
check(ref_double_x, host_double_x, eps_double);
check(ref_double_y, host_double_y, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
ViennaCLCUDASaxpy(my_backend, ViennaCLInt(size/3),
2.0f,
viennacl::cuda_arg(cuda_float_x), 0, 2,
viennacl::cuda_arg(cuda_float_y), 1, 2);
check(ref_float_x, cuda_float_x, eps_float);
check(ref_float_y, cuda_float_y, eps_float);
ViennaCLCUDADaxpy(my_backend, ViennaCLInt(size/3),
2.0,
viennacl::cuda_arg(cuda_double_x), 0, 2,
viennacl::cuda_arg(cuda_double_y), 1, 2);
check(ref_double_x, cuda_double_x, eps_double);
check(ref_double_y, cuda_double_y, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
ViennaCLOpenCLSaxpy(my_backend, ViennaCLInt(size/3),
2.0f,
viennacl::traits::opencl_handle(opencl_float_x).get(), 0, 2,
viennacl::traits::opencl_handle(opencl_float_y).get(), 1, 2);
check(ref_float_x, opencl_float_x, eps_float);
check(ref_float_y, opencl_float_y, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLDaxpy(my_backend, ViennaCLInt(size/3),
2.0,
viennacl::traits::opencl_handle(*opencl_double_x).get(), 0, 2,
viennacl::traits::opencl_handle(*opencl_double_y).get(), 1, 2);
check(ref_double_x, *opencl_double_x, eps_double);
check(ref_double_y, *opencl_double_y, eps_double);
}
#endif
// COPY
std::cout << std::endl << "-- Testing xCOPY...";
for (std::size_t i=0; i<size/3; ++i)
{
ref_float_y[0 + 2*i] = ref_float_x[1 + 2*i];
ref_double_y[0 + 2*i] = ref_double_x[1 + 2*i];
}
std::cout << std::endl << "Host: ";
ViennaCLHostScopy(my_backend, ViennaCLInt(size/3),
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 1, 2,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_y), 0, 2);
check(ref_float_x, host_float_x, eps_float);
check(ref_float_y, host_float_y, eps_float);
ViennaCLHostDcopy(my_backend, ViennaCLInt(size/3),
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 1, 2,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_y), 0, 2);
check(ref_double_x, host_double_x, eps_double);
check(ref_double_y, host_double_y, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
ViennaCLCUDAScopy(my_backend, ViennaCLInt(size/3),
viennacl::cuda_arg(cuda_float_x), 1, 2,
viennacl::cuda_arg(cuda_float_y), 0, 2);
check(ref_float_x, cuda_float_x, eps_float);
check(ref_float_y, cuda_float_y, eps_float);
ViennaCLCUDADcopy(my_backend, ViennaCLInt(size/3),
viennacl::cuda_arg(cuda_double_x), 1, 2,
viennacl::cuda_arg(cuda_double_y), 0, 2);
check(ref_double_x, cuda_double_x, eps_double);
check(ref_double_y, cuda_double_y, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
ViennaCLOpenCLScopy(my_backend, ViennaCLInt(size/3),
viennacl::traits::opencl_handle(opencl_float_x).get(), 1, 2,
viennacl::traits::opencl_handle(opencl_float_y).get(), 0, 2);
check(ref_float_x, opencl_float_x, eps_float);
check(ref_float_y, opencl_float_y, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLDcopy(my_backend, ViennaCLInt(size/3),
viennacl::traits::opencl_handle(*opencl_double_x).get(), 1, 2,
viennacl::traits::opencl_handle(*opencl_double_y).get(), 0, 2);
check(ref_double_x, *opencl_double_x, eps_double);
check(ref_double_y, *opencl_double_y, eps_double);
}
#endif
// DOT
std::cout << std::endl << "-- Testing xDOT...";
ref_float_alpha = 0;
ref_double_alpha = 0;
for (std::size_t i=0; i<size/2; ++i)
{
ref_float_alpha += ref_float_y[3 + i] * ref_float_x[2 + i];
ref_double_alpha += ref_double_y[3 + i] * ref_double_x[2 + i];
}
std::cout << std::endl << "Host: ";
ViennaCLHostSdot(my_backend, ViennaCLInt(size/2),
&host_float_alpha,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 2, 1,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_y), 3, 1);
check(ref_float_alpha, host_float_alpha, eps_float);
ViennaCLHostDdot(my_backend, ViennaCLInt(size/2),
&host_double_alpha,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 2, 1,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_y), 3, 1);
check(ref_double_alpha, host_double_alpha, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
ViennaCLCUDASdot(my_backend, ViennaCLInt(size/2),
&cuda_float_alpha,
viennacl::cuda_arg(cuda_float_x), 2, 1,
viennacl::cuda_arg(cuda_float_y), 3, 1);
check(ref_float_alpha, cuda_float_alpha, eps_float);
ViennaCLCUDADdot(my_backend, ViennaCLInt(size/2),
&cuda_double_alpha,
viennacl::cuda_arg(cuda_double_x), 2, 1,
viennacl::cuda_arg(cuda_double_y), 3, 1);
check(ref_double_alpha, cuda_double_alpha, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
ViennaCLOpenCLSdot(my_backend, ViennaCLInt(size/2),
&opencl_float_alpha,
viennacl::traits::opencl_handle(opencl_float_x).get(), 2, 1,
viennacl::traits::opencl_handle(opencl_float_y).get(), 3, 1);
check(ref_float_alpha, opencl_float_alpha, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLDdot(my_backend, ViennaCLInt(size/2),
&opencl_double_alpha,
viennacl::traits::opencl_handle(*opencl_double_x).get(), 2, 1,
viennacl::traits::opencl_handle(*opencl_double_y).get(), 3, 1);
check(ref_double_alpha, opencl_double_alpha, eps_double);
}
#endif
// NRM2
std::cout << std::endl << "-- Testing xNRM2...";
ref_float_alpha = 0;
ref_double_alpha = 0;
for (std::size_t i=0; i<size/3; ++i)
{
ref_float_alpha += ref_float_x[1 + 2*i] * ref_float_x[1 + 2*i];
ref_double_alpha += ref_double_x[1 + 2*i] * ref_double_x[1 + 2*i];
}
ref_float_alpha = std::sqrt(ref_float_alpha);
ref_double_alpha = std::sqrt(ref_double_alpha);
std::cout << std::endl << "Host: ";
ViennaCLHostSnrm2(my_backend, ViennaCLInt(size/3),
&host_float_alpha,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 1, 2);
check(ref_float_alpha, host_float_alpha, eps_float);
ViennaCLHostDnrm2(my_backend, ViennaCLInt(size/3),
&host_double_alpha,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 1, 2);
check(ref_double_alpha, host_double_alpha, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
ViennaCLCUDASnrm2(my_backend, ViennaCLInt(size/3),
&cuda_float_alpha,
viennacl::cuda_arg(cuda_float_x), 1, 2);
check(ref_float_alpha, cuda_float_alpha, eps_float);
ViennaCLCUDADnrm2(my_backend, ViennaCLInt(size/3),
&cuda_double_alpha,
viennacl::cuda_arg(cuda_double_x), 1, 2);
check(ref_double_alpha, cuda_double_alpha, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
ViennaCLOpenCLSnrm2(my_backend, ViennaCLInt(size/3),
&opencl_float_alpha,
viennacl::traits::opencl_handle(opencl_float_x).get(), 1, 2);
check(ref_float_alpha, opencl_float_alpha, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLDnrm2(my_backend, ViennaCLInt(size/3),
&opencl_double_alpha,
viennacl::traits::opencl_handle(*opencl_double_x).get(), 1, 2);
check(ref_double_alpha, opencl_double_alpha, eps_double);
}
#endif
// ROT
std::cout << std::endl << "-- Testing xROT...";
for (std::size_t i=0; i<size/4; ++i)
{
float tmp = 0.6f * ref_float_x[2 + 3*i] + 0.8f * ref_float_y[1 + 2*i];
ref_float_y[1 + 2*i] = -0.8f * ref_float_x[2 + 3*i] + 0.6f * ref_float_y[1 + 2*i];;
ref_float_x[2 + 3*i] = tmp;
double tmp2 = 0.6 * ref_double_x[2 + 3*i] + 0.8 * ref_double_y[1 + 2*i];
ref_double_y[1 + 2*i] = -0.8 * ref_double_x[2 + 3*i] + 0.6 * ref_double_y[1 + 2*i];;
ref_double_x[2 + 3*i] = tmp2;
}
std::cout << std::endl << "Host: ";
ViennaCLHostSrot(my_backend, ViennaCLInt(size/4),
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 2, 3,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_y), 1, 2,
0.6f, 0.8f);
check(ref_float_x, host_float_x, eps_float);
check(ref_float_y, host_float_y, eps_float);
ViennaCLHostDrot(my_backend, ViennaCLInt(size/4),
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 2, 3,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_y), 1, 2,
0.6, 0.8);
check(ref_double_x, host_double_x, eps_double);
check(ref_double_y, host_double_y, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
ViennaCLCUDASrot(my_backend, ViennaCLInt(size/4),
viennacl::cuda_arg(cuda_float_x), 2, 3,
viennacl::cuda_arg(cuda_float_y), 1, 2,
0.6f, 0.8f);
check(ref_float_x, cuda_float_x, eps_float);
check(ref_float_y, cuda_float_y, eps_float);
ViennaCLCUDADrot(my_backend, ViennaCLInt(size/4),
viennacl::cuda_arg(cuda_double_x), 2, 3,
viennacl::cuda_arg(cuda_double_y), 1, 2,
0.6, 0.8);
check(ref_double_x, cuda_double_x, eps_double);
check(ref_double_y, cuda_double_y, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
ViennaCLOpenCLSrot(my_backend, ViennaCLInt(size/4),
viennacl::traits::opencl_handle(opencl_float_x).get(), 2, 3,
viennacl::traits::opencl_handle(opencl_float_y).get(), 1, 2,
0.6f, 0.8f);
check(ref_float_x, opencl_float_x, eps_float);
check(ref_float_y, opencl_float_y, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLDrot(my_backend, ViennaCLInt(size/4),
viennacl::traits::opencl_handle(*opencl_double_x).get(), 2, 3,
viennacl::traits::opencl_handle(*opencl_double_y).get(), 1, 2,
0.6, 0.8);
check(ref_double_x, *opencl_double_x, eps_double);
check(ref_double_y, *opencl_double_y, eps_double);
}
#endif
// SCAL
std::cout << std::endl << "-- Testing xSCAL...";
for (std::size_t i=0; i<size/4; ++i)
{
ref_float_x[1 + 3*i] *= 2.0f;
ref_double_x[1 + 3*i] *= 2.0;
}
std::cout << std::endl << "Host: ";
ViennaCLHostSscal(my_backend, ViennaCLInt(size/4),
2.0f,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 1, 3);
check(ref_float_x, host_float_x, eps_float);
ViennaCLHostDscal(my_backend, ViennaCLInt(size/4),
2.0,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 1, 3);
check(ref_double_x, host_double_x, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
ViennaCLCUDASscal(my_backend, ViennaCLInt(size/4),
2.0f,
viennacl::cuda_arg(cuda_float_x), 1, 3);
check(ref_float_x, cuda_float_x, eps_float);
ViennaCLCUDADscal(my_backend, ViennaCLInt(size/4),
2.0,
viennacl::cuda_arg(cuda_double_x), 1, 3);
check(ref_double_x, cuda_double_x, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
ViennaCLOpenCLSscal(my_backend, ViennaCLInt(size/4),
2.0f,
viennacl::traits::opencl_handle(opencl_float_x).get(), 1, 3);
check(ref_float_x, opencl_float_x, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLDscal(my_backend, ViennaCLInt(size/4),
2.0,
viennacl::traits::opencl_handle(*opencl_double_x).get(), 1, 3);
check(ref_double_x, *opencl_double_x, eps_double);
}
#endif
// SWAP
std::cout << std::endl << "-- Testing xSWAP...";
for (std::size_t i=0; i<size/3; ++i)
{
float tmp = ref_float_x[2 + 2*i];
ref_float_x[2 + 2*i] = ref_float_y[1 + 2*i];
ref_float_y[1 + 2*i] = tmp;
double tmp2 = ref_double_x[2 + 2*i];
ref_double_x[2 + 2*i] = ref_double_y[1 + 2*i];
ref_double_y[1 + 2*i] = tmp2;
}
std::cout << std::endl << "Host: ";
ViennaCLHostSswap(my_backend, ViennaCLInt(size/3),
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 2, 2,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_y), 1, 2);
check(ref_float_y, host_float_y, eps_float);
ViennaCLHostDswap(my_backend, ViennaCLInt(size/3),
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 2, 2,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_y), 1, 2);
check(ref_double_y, host_double_y, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
ViennaCLCUDASswap(my_backend, ViennaCLInt(size/3),
viennacl::cuda_arg(cuda_float_x), 2, 2,
viennacl::cuda_arg(cuda_float_y), 1, 2);
check(ref_float_y, cuda_float_y, eps_float);
ViennaCLCUDADswap(my_backend, ViennaCLInt(size/3),
viennacl::cuda_arg(cuda_double_x), 2, 2,
viennacl::cuda_arg(cuda_double_y), 1, 2);
check(ref_double_y, cuda_double_y, eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
ViennaCLOpenCLSswap(my_backend, ViennaCLInt(size/3),
viennacl::traits::opencl_handle(opencl_float_x).get(), 2, 2,
viennacl::traits::opencl_handle(opencl_float_y).get(), 1, 2);
check(ref_float_y, opencl_float_y, eps_float);
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLDswap(my_backend, ViennaCLInt(size/3),
viennacl::traits::opencl_handle(*opencl_double_x).get(), 2, 2,
viennacl::traits::opencl_handle(*opencl_double_y).get(), 1, 2);
check(ref_double_y, *opencl_double_y, eps_double);
}
#endif
// IAMAX
std::cout << std::endl << "-- Testing IxASUM...";
ViennaCLInt ref_index = 0;
ref_float_alpha = 0;
for (std::size_t i=0; i<size/3; ++i)
{
if (ref_float_x[0 + 2*i] > std::fabs(ref_float_alpha))
{
ref_index = ViennaCLInt(i);
ref_float_alpha = std::fabs(ref_float_x[0 + 2*i]);
}
}
std::cout << std::endl << "Host: ";
ViennaCLInt idx = 0;
ViennaCLHostiSamax(my_backend, ViennaCLInt(size/3),
&idx,
viennacl::linalg::host_based::detail::extract_raw_pointer<float>(host_float_x), 0, 2);
check(static_cast<float>(ref_index), static_cast<float>(idx), eps_float);
idx = 0;
ViennaCLHostiDamax(my_backend, ViennaCLInt(size/3),
&idx,
viennacl::linalg::host_based::detail::extract_raw_pointer<double>(host_double_x), 0, 2);
check(ref_index, idx, eps_double);
#ifdef VIENNACL_WITH_CUDA
std::cout << std::endl << "CUDA: ";
idx = 0;
ViennaCLCUDAiSamax(my_backend, ViennaCLInt(size/3),
&idx,
viennacl::cuda_arg(cuda_float_x), 0, 2);
check(ref_float_x[2*ref_index], ref_float_x[2*idx], eps_float);
idx = 0;
ViennaCLCUDAiDamax(my_backend, ViennaCLInt(size/3),
&idx,
viennacl::cuda_arg(cuda_double_x), 0, 2);
check(ref_double_x[2*ref_index], ref_double_x[2*idx], eps_double);
#endif
#ifdef VIENNACL_WITH_OPENCL
std::cout << std::endl << "OpenCL: ";
idx = 0;
ViennaCLOpenCLiSamax(my_backend, ViennaCLInt(size/3),
&idx,
viennacl::traits::opencl_handle(opencl_float_x).get(), 0, 2);
check(ref_float_x[2*static_cast<std::size_t>(ref_index)], ref_float_x[2*static_cast<std::size_t>(idx)], eps_float);
idx = 0;
if ( viennacl::ocl::current_device().double_support() )
{
ViennaCLOpenCLiDamax(my_backend, ViennaCLInt(size/3),
&idx,
viennacl::traits::opencl_handle(*opencl_double_x).get(), 0, 2);
check(ref_double_x[2*static_cast<std::size_t>(ref_index)], ref_double_x[2*static_cast<std::size_t>(idx)], eps_double);
}
#endif
#ifdef VIENNACL_WITH_OPENCL
//cleanup
if ( viennacl::ocl::current_device().double_support() )
{
delete opencl_double_x;
delete opencl_double_y;
}
#endif
ViennaCLBackendDestroy(&my_backend);
//
// That's it.
//
std::cout << std::endl << "!!!! TEST COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}
ViennaCLHostDaxpy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostDaxpy(ViennaCLBackend backend, ViennaCLInt n, double alpha, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy)
Definition: blas1_host.cpp:109
ViennaCLHostSscal
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostSscal(ViennaCLBackend backend, ViennaCLInt n, float alpha, float *x, ViennaCLInt offx, ViennaCLInt incx)
Definition: blas1_host.cpp:243
ViennaCLBackendCreate
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLBackendCreate(ViennaCLBackend *backend)
Definition: backend.cpp:25
ViennaCLHostScopy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostScopy(ViennaCLBackend backend, ViennaCLInt n, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy)
Definition: blas1_host.cpp:126
ViennaCLBackend_impl
Generic backend for CUDA, OpenCL, host-based stuff.
Definition: viennacl_private.hpp:54
ViennaCLHostDrot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostDrot(ViennaCLBackend backend, ViennaCLInt n, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy, double c, double s)
Definition: blas1_host.cpp:225
ViennaCLHostDdot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostDdot(ViennaCLBackend backend, ViennaCLInt n, double *alpha, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy)
Definition: blas1_host.cpp:168
ViennaCLCUDADscal
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADscal(ViennaCLBackend backend, ViennaCLInt n, double alpha, double *x, ViennaCLInt offx, ViennaCLInt incx)
ViennaCLCUDADdot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADdot(ViennaCLBackend backend, ViennaCLInt n, double *alpha, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy)
ViennaCLCUDADnrm2
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADnrm2(ViennaCLBackend backend, ViennaCLInt n, double *alpha, double *x, ViennaCLInt offx, ViennaCLInt incx)
ViennaCLHostDscal
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostDscal(ViennaCLBackend backend, ViennaCLInt n, double alpha, double *x, ViennaCLInt offx, ViennaCLInt incx)
Definition: blas1_host.cpp:255
viennacl::backend::finish
void finish()
Synchronizes the execution. finish() will only return after all compute kernels (CUDA, OpenCL) have completed.
Definition: memory.hpp:54
ViennaCLBackendSetOpenCLContextID
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLBackendSetOpenCLContextID(ViennaCLBackend backend, ViennaCLInt context_id)
Definition: backend.cpp:32
ViennaCLHostDnrm2
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostDnrm2(ViennaCLBackend backend, ViennaCLInt n, double *alpha, double *x, ViennaCLInt offx, ViennaCLInt incx)
Definition: blas1_host.cpp:196
ViennaCLHostSswap
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostSswap(ViennaCLBackend backend, ViennaCLInt n, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy)
Definition: blas1_host.cpp:269
ViennaCLHostDcopy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostDcopy(ViennaCLBackend backend, ViennaCLInt n, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy)
Definition: blas1_host.cpp:139
ViennaCLHostSdot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostSdot(ViennaCLBackend backend, ViennaCLInt n, float *alpha, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy)
Definition: blas1_host.cpp:154
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
ViennaCLCUDASasum
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASasum(ViennaCLBackend backend, ViennaCLInt n, float *alpha, float *x, ViennaCLInt offx, ViennaCLInt incx)
ViennaCLCUDASnrm2
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASnrm2(ViennaCLBackend backend, ViennaCLInt n, float *alpha, float *x, ViennaCLInt offx, ViennaCLInt incx)
ViennaCLHostDswap
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostDswap(ViennaCLBackend backend, ViennaCLInt n, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy)
Definition: blas1_host.cpp:282
v1
viennacl::vector< float > v1
Definition: global_variables.cpp:60
ViennaCLHostiDamax
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostiDamax(ViennaCLBackend backend, ViennaCLInt n, ViennaCLInt *alpha, double *x, ViennaCLInt offx, ViennaCLInt incx)
Definition: blas1_host.cpp:51
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
ViennaCLCUDASscal
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASscal(ViennaCLBackend backend, ViennaCLInt n, float alpha, float *x, ViennaCLInt offx, ViennaCLInt incx)
viennacl::CUDA_MEMORY
Definition: forwards.h:350
ViennaCLHostSrot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostSrot(ViennaCLBackend backend, ViennaCLInt n, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy, float c, float s)
Definition: blas1_host.cpp:211
ViennaCLBackendDestroy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLBackendDestroy(ViennaCLBackend *backend)
Definition: backend.cpp:39
ViennaCLCUDASswap
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASswap(ViennaCLBackend backend, ViennaCLInt n, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy)
ViennaCLCUDASaxpy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASaxpy(ViennaCLBackend backend, ViennaCLInt n, float alpha, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy)
main
int main()
Definition: libviennacl_blas1.cpp:78
viennacl::ocl::device::double_support
bool double_support() const
ViennaCL convenience function: Returns true if the device supports double precision.
Definition: device.hpp:956
ViennaCLInt
int ViennaCLInt
Definition: viennacl.hpp:48
viennacl::vector
Definition: forwards.h:266
ViennaCLCUDASrot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASrot(ViennaCLBackend backend, ViennaCLInt n, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy, float c, float s)
ViennaCLCUDADaxpy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADaxpy(ViennaCLBackend backend, ViennaCLInt n, double alpha, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy)
ViennaCLCUDADswap
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADswap(ViennaCLBackend backend, ViennaCLInt n, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy)
viennacl::MAIN_MEMORY
Definition: forwards.h:348
ViennaCLCUDASdot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDASdot(ViennaCLBackend backend, ViennaCLInt n, float *alpha, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy)
viennacl::traits::context
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:40
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::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
ViennaCLHostSasum
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostSasum(ViennaCLBackend backend, ViennaCLInt n, float *alpha, float *x, ViennaCLInt offx, ViennaCLInt incx)
Definition: blas1_host.cpp:67
ViennaCLCUDADasum
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADasum(ViennaCLBackend backend, ViennaCLInt n, double *alpha, double *x, ViennaCLInt offx, ViennaCLInt incx)
ViennaCLHostSaxpy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostSaxpy(ViennaCLBackend backend, ViennaCLInt n, float alpha, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy)
Definition: blas1_host.cpp:95
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
ViennaCLHostSnrm2
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostSnrm2(ViennaCLBackend backend, ViennaCLInt n, float *alpha, float *x, ViennaCLInt offx, ViennaCLInt incx)
Definition: blas1_host.cpp:184
ViennaCLCUDADrot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADrot(ViennaCLBackend backend, ViennaCLInt n, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy, double c, double s)
check
void check(T const &t, U const &u, EpsilonT eps)
Definition: libviennacl_blas1.cpp:66
ViennaCLCUDAiSamax
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDAiSamax(ViennaCLBackend backend, ViennaCLInt n, ViennaCLInt *alpha, float *x, ViennaCLInt offx, ViennaCLInt incx)
viennacl::cuda_arg
NumericT * cuda_arg(scalar< NumericT > &obj)
Convenience helper function for extracting the CUDA handle from a ViennaCL scalar. Non-const version.
Definition: common.hpp:39
viennacl::ocl::get_context
viennacl::ocl::context & get_context(long i)
Convenience function for returning the current context.
Definition: backend.hpp:225
ViennaCLHostiSamax
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostiSamax(ViennaCLBackend backend, ViennaCLInt n, ViennaCLInt *alpha, float *x, ViennaCLInt offx, ViennaCLInt incx)
Definition: blas1_host.cpp:39
ViennaCLCUDAScopy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDAScopy(ViennaCLBackend backend, ViennaCLInt n, float *x, ViennaCLInt offx, ViennaCLInt incx, float *y, ViennaCLInt offy, ViennaCLInt incy)
ViennaCLHostDasum
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLHostDasum(ViennaCLBackend backend, ViennaCLInt n, double *alpha, double *x, ViennaCLInt offx, ViennaCLInt incx)
Definition: blas1_host.cpp:79
viennacl.hpp
ViennaCLCUDAiDamax
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDAiDamax(ViennaCLBackend backend, ViennaCLInt n, ViennaCLInt *alpha, double *x, ViennaCLInt offx, ViennaCLInt incx)
ViennaCLCUDADcopy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLCUDADcopy(ViennaCLBackend backend, ViennaCLInt n, double *x, ViennaCLInt offx, ViennaCLInt incx, double *y, ViennaCLInt offy, ViennaCLInt incy)
diff
ScalarType diff(ScalarType const &s1, ScalarType const &s2)
Definition: libviennacl_blas1.cpp:35