All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
blas1.cpp
Go to the documentation of this file.
/* =========================================================================
Copyright (c) 2010-2014, 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 "viennacl.hpp"
#include "viennacl_private.hpp"
#include "init_vector.hpp"
//include basic scalar and vector types of ViennaCL
#include "viennacl/scalar.hpp"
#include "viennacl/vector.hpp"
//include the generic inner product functions of ViennaCL
#include "viennacl/linalg/inner_prod.hpp"
//include the generic norm functions of ViennaCL
#include "viennacl/linalg/norm_1.hpp"
#include "viennacl/linalg/norm_2.hpp"
#include "viennacl/linalg/norm_inf.hpp"
// IxAMAX
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLiamax(ViennaCLInt *index, ViennaCLVector x)
{
viennacl::backend::mem_handle v1_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
*index = static_cast<ViennaCLInt>(viennacl::linalg::index_norm_inf(v1));
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
*index = static_cast<ViennaCLInt>(viennacl::linalg::index_norm_inf(v1));
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
// xASUM
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLasum(ViennaCLHostScalar *alpha, ViennaCLVector x)
{
if ((*alpha)->precision != x->precision)
return ViennaCLGenericFailure;
viennacl::backend::mem_handle v1_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
(*alpha)->value_float = viennacl::linalg::norm_1(v1);
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
(*alpha)->value_double = viennacl::linalg::norm_1(v1);
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
// xAXPY
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLaxpy(ViennaCLHostScalar alpha, ViennaCLVector x, ViennaCLVector y)
{
if (alpha->precision != x->precision)
return ViennaCLGenericFailure;
if (x->precision != y->precision)
return ViennaCLGenericFailure;
viennacl::backend::mem_handle v1_handle;
viennacl::backend::mem_handle v2_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
if (init_vector(v2_handle, y) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
v2 += alpha->value_float * v1;
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
v2 += alpha->value_double * v1;
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
// xCOPY
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLcopy(ViennaCLVector x, ViennaCLVector y)
{
if (x->precision != y->precision)
return ViennaCLGenericFailure;
viennacl::backend::mem_handle v1_handle;
viennacl::backend::mem_handle v2_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
if (init_vector(v2_handle, y) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
v2 = v1;
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
v2 = v1;
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
// xDOT
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLdot(ViennaCLHostScalar *alpha, ViennaCLVector x, ViennaCLVector y)
{
if ((*alpha)->precision != x->precision)
return ViennaCLGenericFailure;
if (x->precision != y->precision)
return ViennaCLGenericFailure;
viennacl::backend::mem_handle v1_handle;
viennacl::backend::mem_handle v2_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
if (init_vector(v2_handle, y) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
(*alpha)->value_float = viennacl::linalg::inner_prod(v1, v2);
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
(*alpha)->value_double = viennacl::linalg::inner_prod(v1, v2);
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
// xNRM2
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLnrm2(ViennaCLHostScalar *alpha, ViennaCLVector x)
{
if ((*alpha)->precision != x->precision)
return ViennaCLGenericFailure;
viennacl::backend::mem_handle v1_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
(*alpha)->value_float = viennacl::linalg::norm_2(v1);
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
(*alpha)->value_double = viennacl::linalg::norm_2(v1);
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
// xROT
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLrot(ViennaCLVector x, ViennaCLVector y,
ViennaCLHostScalar c, ViennaCLHostScalar s)
{
if (c->precision != x->precision)
return ViennaCLGenericFailure;
if (s->precision != x->precision)
return ViennaCLGenericFailure;
if (x->precision != y->precision)
return ViennaCLGenericFailure;
viennacl::backend::mem_handle v1_handle;
viennacl::backend::mem_handle v2_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
if (init_vector(v2_handle, y) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
viennacl::linalg::plane_rotation(v1, v2, c->value_float, s->value_float);
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
viennacl::linalg::plane_rotation(v1, v2, c->value_double, s->value_double);
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
// xSCAL
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLscal(ViennaCLHostScalar alpha, ViennaCLVector x)
{
if (alpha->precision != x->precision)
return ViennaCLGenericFailure;
viennacl::backend::mem_handle v1_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
v1 *= alpha->value_float;
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
v1 *= alpha->value_double;
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
// xSWAP
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLswap(ViennaCLVector x, ViennaCLVector y)
{
if (x->precision != y->precision)
return ViennaCLGenericFailure;
viennacl::backend::mem_handle v1_handle;
viennacl::backend::mem_handle v2_handle;
if (init_vector(v1_handle, x) != ViennaCLSuccess)
return ViennaCLGenericFailure;
if (init_vector(v2_handle, y) != ViennaCLSuccess)
return ViennaCLGenericFailure;
switch (x->precision)
{
case ViennaCLFloat:
{
typedef viennacl::vector_base<float>::size_type difference_type;
viennacl::vector_base<float> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<float> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
viennacl::swap(v1, v2);
return ViennaCLSuccess;
}
case ViennaCLDouble:
{
typedef viennacl::vector_base<double>::size_type difference_type;
viennacl::vector_base<double> v1(v1_handle, static_cast<viennacl::vcl_size_t>(x->size), static_cast<viennacl::vcl_size_t>(x->offset), static_cast<difference_type>(x->inc));
viennacl::vector_base<double> v2(v2_handle, static_cast<viennacl::vcl_size_t>(y->size), static_cast<viennacl::vcl_size_t>(y->offset), static_cast<difference_type>(y->inc));
viennacl::swap(v1, v2);
return ViennaCLSuccess;
}
default:
return ViennaCLGenericFailure;
}
}
viennacl::linalg::norm_2
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
viennacl::linalg::index_norm_inf
vcl_size_t index_norm_inf(vector_base< T > const &vec)
Computes the index of the first entry that is equal to the supremum-norm in modulus.
Definition: vector_operations.hpp:845
norm_2.hpp
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
ViennaCLaxpy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLaxpy(ViennaCLHostScalar alpha, ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:116
ViennaCLVector_impl::precision
ViennaCLPrecision precision
Definition: viennacl_private.hpp:97
ViennaCLHostScalar_impl::value_float
float value_float
Definition: viennacl_private.hpp:72
ViennaCLswap
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLswap(ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:379
ViennaCLnrm2
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLnrm2(ViennaCLHostScalar *alpha, ViennaCLVector x)
Definition: blas1.cpp:252
viennacl::linalg::plane_rotation
void plane_rotation(vector_base< T > &vec1, vector_base< T > &vec2, T alpha, T beta)
Computes a plane rotation of two vectors.
Definition: vector_operations.hpp:1152
ViennaCLscal
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLscal(ViennaCLHostScalar alpha, ViennaCLVector x)
Definition: blas1.cpp:341
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
ViennaCLDouble
Definition: viennacl.hpp:93
ViennaCLStatus
ViennaCLStatus
Definition: viennacl.hpp:97
inner_prod.hpp
Generic interface for the computation of inner products. See viennacl/linalg/vector_operations.hpp for implementations.
ViennaCLVector_impl::size
ViennaCLInt size
Definition: viennacl_private.hpp:110
ViennaCLrot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLrot(ViennaCLVector x, ViennaCLVector y, ViennaCLHostScalar c, ViennaCLHostScalar s)
Definition: blas1.cpp:291
norm_1.hpp
Generic interface for the l^1-norm. See viennacl/linalg/vector_operations.hpp for implementations...
viennacl::swap
void swap(vector_base< T > &vec1, vector_base< T > &vec2)
Swaps the contents of two vectors, data is copied.
Definition: vector.hpp:1648
VIENNACL_EXPORTED_FUNCTION
#define VIENNACL_EXPORTED_FUNCTION
Definition: viennacl.hpp:40
v1
viennacl::vector< float > v1
Definition: global_variables.cpp:60
ViennaCLVector_impl::inc
ViennaCLInt inc
Definition: viennacl_private.hpp:109
ViennaCLVector_impl
Definition: viennacl_private.hpp:94
viennacl_private.hpp
ViennaCLiamax
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLiamax(ViennaCLInt *index, ViennaCLVector x)
Definition: blas1.cpp:40
ViennaCLasum
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLasum(ViennaCLHostScalar *alpha, ViennaCLVector x)
Definition: blas1.cpp:77
viennacl::vector_base< float >
init_vector.hpp
ViennaCLInt
int ViennaCLInt
Definition: viennacl.hpp:48
ViennaCLdot
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLdot(ViennaCLHostScalar *alpha, ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:206
ViennaCLGenericFailure
Definition: viennacl.hpp:100
ViennaCLVector_impl::offset
ViennaCLInt offset
Definition: viennacl_private.hpp:108
ViennaCLHostScalar_impl
Definition: viennacl_private.hpp:67
v2
viennacl::vector< int > v2
Definition: global_variables.cpp:61
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::linalg::norm_1
T norm_1(std::vector< T, A > const &v1)
Definition: norm_1.hpp:61
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
ViennaCLcopy
VIENNACL_EXPORTED_FUNCTION ViennaCLStatus ViennaCLcopy(ViennaCLVector x, ViennaCLVector y)
Definition: blas1.cpp:163
scalar.hpp
Implementation of the ViennaCL scalar class.
viennacl.hpp
norm_inf.hpp
Generic interface for the l^infty-norm. See viennacl/linalg/vector_operations.hpp for implementations...
ViennaCLHostScalar_impl::value_double
double value_double
Definition: viennacl_private.hpp:73
ViennaCLFloat
Definition: viennacl.hpp:92
ViennaCLHostScalar_impl::precision
ViennaCLPrecision precision
Definition: viennacl_private.hpp:69
ViennaCLSuccess
Definition: viennacl.hpp:99
viennacl::vector_base::size_type
SizeT size_type
Definition: vector_def.hpp:112