All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector_operations.hpp
Go to the documentation of this file.
#ifndef VIENNACL_LINALG_OPENCL_VECTOR_OPERATIONS_HPP_
#define VIENNACL_LINALG_OPENCL_VECTOR_OPERATIONS_HPP_
/* =========================================================================
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 manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <cmath>
#include "viennacl/forwards.h"
#include "viennacl/detail/vector_def.hpp"
#include "viennacl/ocl/device.hpp"
#include "viennacl/ocl/handle.hpp"
#include "viennacl/ocl/kernel.hpp"
#include "viennacl/scalar.hpp"
#include "viennacl/tools/tools.hpp"
#include "viennacl/linalg/opencl/common.hpp"
#include "viennacl/linalg/opencl/kernels/vector.hpp"
#include "viennacl/linalg/opencl/kernels/vector_element.hpp"
#include "viennacl/linalg/opencl/kernels/scan.hpp"
#include "viennacl/meta/predicate.hpp"
#include "viennacl/meta/enable_if.hpp"
#include "viennacl/traits/size.hpp"
#include "viennacl/traits/start.hpp"
#include "viennacl/traits/handle.hpp"
#include "viennacl/traits/stride.hpp"
namespace viennacl
{
namespace linalg
{
namespace opencl
{
//
// Introductory note: By convention, all dimensions are already checked in the dispatcher frontend. No need to double-check again in here!
//
template<typename DestNumericT, typename SrcNumericT>
void convert(vector_base<DestNumericT> & dest, vector_base<SrcNumericT> const & src)
{
assert(viennacl::traits::opencl_handle(dest).context() == viennacl::traits::opencl_handle(src).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
std::string kernel_name("convert_");
kernel_name += viennacl::ocl::type_to_string<DestNumericT>::apply();
kernel_name += "_";
kernel_name += viennacl::ocl::type_to_string<SrcNumericT>::apply();
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(dest).context());
viennacl::linalg::opencl::kernels::vector_convert::init(ctx);
viennacl::ocl::kernel& k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector_convert::program_name(), kernel_name);
viennacl::ocl::enqueue(k( dest, cl_uint(dest.start()), cl_uint(dest.stride()), cl_uint(dest.size()),
src, cl_uint( src.start()), cl_uint( src.stride())
) );
}
template <typename T, typename ScalarType1>
void av(vector_base<T> & vec1,
vector_base<T> const & vec2, ScalarType1 const & alpha, vcl_size_t len_alpha, bool reciprocal_alpha, bool flip_sign_alpha)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(vec2).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
cl_uint options_alpha = detail::make_options(len_alpha, reciprocal_alpha, flip_sign_alpha);
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(),
(viennacl::is_cpu_scalar<ScalarType1>::value ? "av_cpu" : "av_gpu"));
k.global_work_size(0, std::min<vcl_size_t>(128 * k.local_work_size(),
viennacl::tools::align_to_multiple<vcl_size_t>(viennacl::traits::size(vec1), k.local_work_size()) ) );
viennacl::ocl::packed_cl_uint size_vec1;
size_vec1.start = cl_uint(viennacl::traits::start(vec1));
size_vec1.stride = cl_uint(viennacl::traits::stride(vec1));
size_vec1.size = cl_uint(viennacl::traits::size(vec1));
size_vec1.internal_size = cl_uint(viennacl::traits::internal_size(vec1));
viennacl::ocl::packed_cl_uint size_vec2;
size_vec2.start = cl_uint(viennacl::traits::start(vec2));
size_vec2.stride = cl_uint(viennacl::traits::stride(vec2));
size_vec2.size = cl_uint(viennacl::traits::size(vec2));
size_vec2.internal_size = cl_uint(viennacl::traits::internal_size(vec2));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
size_vec1,
viennacl::traits::opencl_handle(viennacl::tools::promote_if_host_scalar<T>(alpha)),
options_alpha,
viennacl::traits::opencl_handle(vec2),
size_vec2 )
);
}
template <typename T, typename ScalarType1, typename ScalarType2>
void avbv(vector_base<T> & vec1,
vector_base<T> const & vec2, ScalarType1 const & alpha, vcl_size_t len_alpha, bool reciprocal_alpha, bool flip_sign_alpha,
vector_base<T> const & vec3, ScalarType2 const & beta, vcl_size_t len_beta, bool reciprocal_beta, bool flip_sign_beta)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(vec2).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
assert(viennacl::traits::opencl_handle(vec2).context() == viennacl::traits::opencl_handle(vec3).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
std::string kernel_name;
if (viennacl::is_cpu_scalar<ScalarType1>::value && viennacl::is_cpu_scalar<ScalarType2>::value)
kernel_name = "avbv_cpu_cpu";
else if (viennacl::is_cpu_scalar<ScalarType1>::value && !viennacl::is_cpu_scalar<ScalarType2>::value)
kernel_name = "avbv_cpu_gpu";
else if (!viennacl::is_cpu_scalar<ScalarType1>::value && viennacl::is_cpu_scalar<ScalarType2>::value)
kernel_name = "avbv_gpu_cpu";
else
kernel_name = "avbv_gpu_gpu";
cl_uint options_alpha = detail::make_options(len_alpha, reciprocal_alpha, flip_sign_alpha);
cl_uint options_beta = detail::make_options(len_beta, reciprocal_beta, flip_sign_beta);
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), kernel_name);
k.global_work_size(0, std::min<vcl_size_t>(128 * k.local_work_size(),
viennacl::tools::align_to_multiple<vcl_size_t>(viennacl::traits::size(vec1), k.local_work_size()) ) );
viennacl::ocl::packed_cl_uint size_vec1;
size_vec1.start = cl_uint(viennacl::traits::start(vec1));
size_vec1.stride = cl_uint(viennacl::traits::stride(vec1));
size_vec1.size = cl_uint(viennacl::traits::size(vec1));
size_vec1.internal_size = cl_uint(viennacl::traits::internal_size(vec1));
viennacl::ocl::packed_cl_uint size_vec2;
size_vec2.start = cl_uint(viennacl::traits::start(vec2));
size_vec2.stride = cl_uint(viennacl::traits::stride(vec2));
size_vec2.size = cl_uint(viennacl::traits::size(vec2));
size_vec2.internal_size = cl_uint(viennacl::traits::internal_size(vec2));
viennacl::ocl::packed_cl_uint size_vec3;
size_vec3.start = cl_uint(viennacl::traits::start(vec3));
size_vec3.stride = cl_uint(viennacl::traits::stride(vec3));
size_vec3.size = cl_uint(viennacl::traits::size(vec3));
size_vec3.internal_size = cl_uint(viennacl::traits::internal_size(vec3));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
size_vec1,
viennacl::traits::opencl_handle(viennacl::tools::promote_if_host_scalar<T>(alpha)),
options_alpha,
viennacl::traits::opencl_handle(vec2),
size_vec2,
viennacl::traits::opencl_handle(viennacl::tools::promote_if_host_scalar<T>(beta)),
options_beta,
viennacl::traits::opencl_handle(vec3),
size_vec3 )
);
}
template <typename T, typename ScalarType1, typename ScalarType2>
void avbv_v(vector_base<T> & vec1,
vector_base<T> const & vec2, ScalarType1 const & alpha, vcl_size_t len_alpha, bool reciprocal_alpha, bool flip_sign_alpha,
vector_base<T> const & vec3, ScalarType2 const & beta, vcl_size_t len_beta, bool reciprocal_beta, bool flip_sign_beta)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(vec2).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
assert(viennacl::traits::opencl_handle(vec2).context() == viennacl::traits::opencl_handle(vec3).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
std::string kernel_name;
if (viennacl::is_cpu_scalar<ScalarType1>::value && viennacl::is_cpu_scalar<ScalarType2>::value)
kernel_name = "avbv_v_cpu_cpu";
else if (viennacl::is_cpu_scalar<ScalarType1>::value && !viennacl::is_cpu_scalar<ScalarType2>::value)
kernel_name = "avbv_v_cpu_gpu";
else if (!viennacl::is_cpu_scalar<ScalarType1>::value && viennacl::is_cpu_scalar<ScalarType2>::value)
kernel_name = "avbv_v_gpu_cpu";
else
kernel_name = "avbv_v_gpu_gpu";
cl_uint options_alpha = detail::make_options(len_alpha, reciprocal_alpha, flip_sign_alpha);
cl_uint options_beta = detail::make_options(len_beta, reciprocal_beta, flip_sign_beta);
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), kernel_name);
k.global_work_size(0, std::min<vcl_size_t>(128 * k.local_work_size(),
viennacl::tools::align_to_multiple<vcl_size_t>(viennacl::traits::size(vec1), k.local_work_size()) ) );
viennacl::ocl::packed_cl_uint size_vec1;
size_vec1.start = cl_uint(viennacl::traits::start(vec1));
size_vec1.stride = cl_uint(viennacl::traits::stride(vec1));
size_vec1.size = cl_uint(viennacl::traits::size(vec1));
size_vec1.internal_size = cl_uint(viennacl::traits::internal_size(vec1));
viennacl::ocl::packed_cl_uint size_vec2;
size_vec2.start = cl_uint(viennacl::traits::start(vec2));
size_vec2.stride = cl_uint(viennacl::traits::stride(vec2));
size_vec2.size = cl_uint(viennacl::traits::size(vec2));
size_vec2.internal_size = cl_uint(viennacl::traits::internal_size(vec2));
viennacl::ocl::packed_cl_uint size_vec3;
size_vec3.start = cl_uint(viennacl::traits::start(vec3));
size_vec3.stride = cl_uint(viennacl::traits::stride(vec3));
size_vec3.size = cl_uint(viennacl::traits::size(vec3));
size_vec3.internal_size = cl_uint(viennacl::traits::internal_size(vec3));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
size_vec1,
viennacl::traits::opencl_handle(viennacl::tools::promote_if_host_scalar<T>(alpha)),
options_alpha,
viennacl::traits::opencl_handle(vec2),
size_vec2,
viennacl::traits::opencl_handle(viennacl::tools::promote_if_host_scalar<T>(beta)),
options_beta,
viennacl::traits::opencl_handle(vec3),
size_vec3 )
);
}
template <typename T>
void vector_assign(vector_base<T> & vec1, const T & alpha, bool up_to_internal_size = false)
{
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "assign_cpu");
k.global_work_size(0, std::min<vcl_size_t>(128 * k.local_work_size(),
viennacl::tools::align_to_multiple<vcl_size_t>(viennacl::traits::size(vec1), k.local_work_size()) ) );
cl_uint size = up_to_internal_size ? cl_uint(vec1.internal_size()) : cl_uint(viennacl::traits::size(vec1));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
cl_uint(viennacl::traits::start(vec1)),
cl_uint(viennacl::traits::stride(vec1)),
size,
cl_uint(vec1.internal_size()), //Note: Do NOT use traits::internal_size() here, because vector proxies don't require padding.
viennacl::traits::opencl_handle(T(alpha)) )
);
}
template <typename T>
void vector_swap(vector_base<T> & vec1, vector_base<T> & vec2)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(vec2).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "swap");
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
cl_uint(viennacl::traits::start(vec1)),
cl_uint(viennacl::traits::stride(vec1)),
cl_uint(viennacl::traits::size(vec1)),
viennacl::traits::opencl_handle(vec2),
cl_uint(viennacl::traits::start(vec2)),
cl_uint(viennacl::traits::stride(vec2)),
cl_uint(viennacl::traits::size(vec2)))
);
}
template <typename T, typename OP>
void element_op(vector_base<T> & vec1,
vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<OP> > const & proxy)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(proxy.lhs()).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(proxy.rhs()).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector_element<T>::init(ctx);
std::string kernel_name = "element_pow";
cl_uint op_type = 2; //0: product, 1: division, 2: power
if (viennacl::is_division<OP>::value)
{
op_type = 1;
kernel_name = "element_div";
}
else if (viennacl::is_product<OP>::value)
{
op_type = 0;
kernel_name = "element_prod";
}
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector_element<T>::program_name(), kernel_name);
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
cl_uint(viennacl::traits::start(vec1)),
cl_uint(viennacl::traits::stride(vec1)),
cl_uint(viennacl::traits::size(vec1)),
viennacl::traits::opencl_handle(proxy.lhs()),
cl_uint(viennacl::traits::start(proxy.lhs())),
cl_uint(viennacl::traits::stride(proxy.lhs())),
viennacl::traits::opencl_handle(proxy.rhs()),
cl_uint(viennacl::traits::start(proxy.rhs())),
cl_uint(viennacl::traits::stride(proxy.rhs())),
op_type)
);
}
template <typename T, typename OP>
void element_op(vector_base<T> & vec1,
vector_expression<const vector_base<T>, const vector_base<T>, op_element_unary<OP> > const & proxy)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(proxy.lhs()).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(proxy.rhs()).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector_element<T>::init(ctx);
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector_element<T>::program_name(), detail::op_to_string(OP()) + "_assign");
viennacl::ocl::packed_cl_uint size_vec1;
size_vec1.start = cl_uint(viennacl::traits::start(vec1));
size_vec1.stride = cl_uint(viennacl::traits::stride(vec1));
size_vec1.size = cl_uint(viennacl::traits::size(vec1));
size_vec1.internal_size = cl_uint(viennacl::traits::internal_size(vec1));
viennacl::ocl::packed_cl_uint size_vec2;
size_vec2.start = cl_uint(viennacl::traits::start(proxy.lhs()));
size_vec2.stride = cl_uint(viennacl::traits::stride(proxy.lhs()));
size_vec2.size = cl_uint(viennacl::traits::size(proxy.lhs()));
size_vec2.internal_size = cl_uint(viennacl::traits::internal_size(proxy.lhs()));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
size_vec1,
viennacl::traits::opencl_handle(proxy.lhs()),
size_vec2)
);
}
template <typename T>
void inner_prod_impl(vector_base<T> const & vec1,
vector_base<T> const & vec2,
vector_base<T> & partial_result)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(vec2).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
assert(viennacl::traits::opencl_handle(vec2).context() == viennacl::traits::opencl_handle(partial_result).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
assert( (viennacl::traits::size(vec1) == viennacl::traits::size(vec2))
&& bool("Incompatible vector sizes in inner_prod_impl()!"));
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "inner_prod1");
assert( (k.global_work_size() / k.local_work_size() <= partial_result.size()) && bool("Size mismatch for partial reduction in inner_prod_impl()") );
viennacl::ocl::packed_cl_uint size_vec1;
size_vec1.start = cl_uint(viennacl::traits::start(vec1));
size_vec1.stride = cl_uint(viennacl::traits::stride(vec1));
size_vec1.size = cl_uint(viennacl::traits::size(vec1));
size_vec1.internal_size = cl_uint(viennacl::traits::internal_size(vec1));
viennacl::ocl::packed_cl_uint size_vec2;
size_vec2.start = cl_uint(viennacl::traits::start(vec2));
size_vec2.stride = cl_uint(viennacl::traits::stride(vec2));
size_vec2.size = cl_uint(viennacl::traits::size(vec2));
size_vec2.internal_size = cl_uint(viennacl::traits::internal_size(vec2));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
size_vec1,
viennacl::traits::opencl_handle(vec2),
size_vec2,
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<T>::type) * k.local_work_size()),
viennacl::traits::opencl_handle(partial_result)
)
);
}
//implementation of inner product:
//namespace {
template <typename T>
void inner_prod_impl(vector_base<T> const & vec1,
vector_base<T> const & vec2,
scalar<T> & result)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(vec2).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
vcl_size_t work_groups = 128;
viennacl::vector<T> temp(work_groups, viennacl::traits::context(vec1));
temp.resize(work_groups, ctx); // bring default-constructed vectors to the correct size:
// Step 1: Compute partial inner products for each work group:
inner_prod_impl(vec1, vec2, temp);
// Step 2: Sum partial results:
viennacl::ocl::kernel & ksum = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "sum");
ksum.global_work_size(0, ksum.local_work_size(0));
viennacl::ocl::enqueue(ksum(viennacl::traits::opencl_handle(temp),
cl_uint(viennacl::traits::start(temp)),
cl_uint(viennacl::traits::stride(temp)),
cl_uint(viennacl::traits::size(temp)),
cl_uint(1),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<T>::type) * ksum.local_work_size()),
viennacl::traits::opencl_handle(result) )
);
}
namespace detail
{
template<typename NumericT>
viennacl::ocl::packed_cl_uint make_layout(vector_base<NumericT> const & vec)
{
viennacl::ocl::packed_cl_uint ret;
ret.start = cl_uint(viennacl::traits::start(vec));
ret.stride = cl_uint(viennacl::traits::stride(vec));
ret.size = cl_uint(viennacl::traits::size(vec));
ret.internal_size = cl_uint(viennacl::traits::internal_size(vec));
return ret;
}
}
template <typename NumericT>
void inner_prod_impl(vector_base<NumericT> const & x,
vector_tuple<NumericT> const & vec_tuple,
vector_base<NumericT> & result)
{
assert(viennacl::traits::opencl_handle(x).context() == viennacl::traits::opencl_handle(result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(x).context());
viennacl::linalg::opencl::kernels::vector<NumericT>::init(ctx);
viennacl::linalg::opencl::kernels::vector_multi_inner_prod<NumericT>::init(ctx);
viennacl::ocl::packed_cl_uint layout_x = detail::make_layout(x);
viennacl::ocl::kernel & ksum = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector_multi_inner_prod<NumericT>::program_name(), "sum_inner_prod");
viennacl::ocl::kernel & inner_prod_kernel_1 = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<NumericT>::program_name(), "inner_prod1");
viennacl::ocl::kernel & inner_prod_kernel_2 = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector_multi_inner_prod<NumericT>::program_name(), "inner_prod2");
viennacl::ocl::kernel & inner_prod_kernel_3 = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector_multi_inner_prod<NumericT>::program_name(), "inner_prod3");
viennacl::ocl::kernel & inner_prod_kernel_4 = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector_multi_inner_prod<NumericT>::program_name(), "inner_prod4");
viennacl::ocl::kernel & inner_prod_kernel_8 = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector_multi_inner_prod<NumericT>::program_name(), "inner_prod8");
vcl_size_t work_groups = inner_prod_kernel_8.global_work_size(0) / inner_prod_kernel_8.local_work_size(0);
viennacl::vector<NumericT> temp(8 * work_groups, viennacl::traits::context(x));
vcl_size_t current_index = 0;
while (current_index < vec_tuple.const_size())
{
switch (vec_tuple.const_size() - current_index)
{
case 7:
case 6:
case 5:
case 4:
{
vector_base<NumericT> const & y0 = vec_tuple.const_at(current_index );
vector_base<NumericT> const & y1 = vec_tuple.const_at(current_index + 1);
vector_base<NumericT> const & y2 = vec_tuple.const_at(current_index + 2);
vector_base<NumericT> const & y3 = vec_tuple.const_at(current_index + 3);
viennacl::ocl::enqueue(inner_prod_kernel_4( viennacl::traits::opencl_handle(x), layout_x,
viennacl::traits::opencl_handle(y0), detail::make_layout(y0),
viennacl::traits::opencl_handle(y1), detail::make_layout(y1),
viennacl::traits::opencl_handle(y2), detail::make_layout(y2),
viennacl::traits::opencl_handle(y3), detail::make_layout(y3),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 4 * inner_prod_kernel_4.local_work_size()),
viennacl::traits::opencl_handle(temp)
) );
ksum.global_work_size(0, 4 * ksum.local_work_size(0));
viennacl::ocl::enqueue(ksum(viennacl::traits::opencl_handle(temp),
cl_uint(work_groups),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 4 * ksum.local_work_size()),
viennacl::traits::opencl_handle(result),
cl_uint(viennacl::traits::start(result) + current_index * viennacl::traits::stride(result)),
cl_uint(viennacl::traits::stride(result))
)
);
}
current_index += 4;
break;
case 3:
{
vector_base<NumericT> const & y0 = vec_tuple.const_at(current_index );
vector_base<NumericT> const & y1 = vec_tuple.const_at(current_index + 1);
vector_base<NumericT> const & y2 = vec_tuple.const_at(current_index + 2);
viennacl::ocl::enqueue(inner_prod_kernel_3( viennacl::traits::opencl_handle(x), layout_x,
viennacl::traits::opencl_handle(y0), detail::make_layout(y0),
viennacl::traits::opencl_handle(y1), detail::make_layout(y1),
viennacl::traits::opencl_handle(y2), detail::make_layout(y2),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 3 * inner_prod_kernel_3.local_work_size()),
viennacl::traits::opencl_handle(temp)
) );
ksum.global_work_size(0, 3 * ksum.local_work_size(0));
viennacl::ocl::enqueue(ksum(viennacl::traits::opencl_handle(temp),
cl_uint(work_groups),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 3 * ksum.local_work_size()),
viennacl::traits::opencl_handle(result),
cl_uint(viennacl::traits::start(result) + current_index * viennacl::traits::stride(result)),
cl_uint(viennacl::traits::stride(result))
)
);
}
current_index += 3;
break;
case 2:
{
vector_base<NumericT> const & y0 = vec_tuple.const_at(current_index );
vector_base<NumericT> const & y1 = vec_tuple.const_at(current_index + 1);
viennacl::ocl::enqueue(inner_prod_kernel_2( viennacl::traits::opencl_handle(x), layout_x,
viennacl::traits::opencl_handle(y0), detail::make_layout(y0),
viennacl::traits::opencl_handle(y1), detail::make_layout(y1),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 2 * inner_prod_kernel_2.local_work_size()),
viennacl::traits::opencl_handle(temp)
) );
ksum.global_work_size(0, 2 * ksum.local_work_size(0));
viennacl::ocl::enqueue(ksum(viennacl::traits::opencl_handle(temp),
cl_uint(work_groups),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 2 * ksum.local_work_size()),
viennacl::traits::opencl_handle(result),
cl_uint(viennacl::traits::start(result) + current_index * viennacl::traits::stride(result)),
cl_uint(viennacl::traits::stride(result))
)
);
}
current_index += 2;
break;
case 1:
{
vector_base<NumericT> const & y0 = vec_tuple.const_at(current_index );
viennacl::ocl::enqueue(inner_prod_kernel_1( viennacl::traits::opencl_handle(x), layout_x,
viennacl::traits::opencl_handle(y0), detail::make_layout(y0),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 1 * inner_prod_kernel_1.local_work_size()),
viennacl::traits::opencl_handle(temp)
) );
ksum.global_work_size(0, 1 * ksum.local_work_size(0));
viennacl::ocl::enqueue(ksum(viennacl::traits::opencl_handle(temp),
cl_uint(work_groups),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 1 * ksum.local_work_size()),
viennacl::traits::opencl_handle(result),
cl_uint(viennacl::traits::start(result) + current_index * viennacl::traits::stride(result)),
cl_uint(viennacl::traits::stride(result))
)
);
}
current_index += 1;
break;
default: //8 or more vectors
{
vector_base<NumericT> const & y0 = vec_tuple.const_at(current_index );
vector_base<NumericT> const & y1 = vec_tuple.const_at(current_index + 1);
vector_base<NumericT> const & y2 = vec_tuple.const_at(current_index + 2);
vector_base<NumericT> const & y3 = vec_tuple.const_at(current_index + 3);
vector_base<NumericT> const & y4 = vec_tuple.const_at(current_index + 4);
vector_base<NumericT> const & y5 = vec_tuple.const_at(current_index + 5);
vector_base<NumericT> const & y6 = vec_tuple.const_at(current_index + 6);
vector_base<NumericT> const & y7 = vec_tuple.const_at(current_index + 7);
viennacl::ocl::enqueue(inner_prod_kernel_8( viennacl::traits::opencl_handle(x), layout_x,
viennacl::traits::opencl_handle(y0), detail::make_layout(y0),
viennacl::traits::opencl_handle(y1), detail::make_layout(y1),
viennacl::traits::opencl_handle(y2), detail::make_layout(y2),
viennacl::traits::opencl_handle(y3), detail::make_layout(y3),
viennacl::traits::opencl_handle(y4), detail::make_layout(y4),
viennacl::traits::opencl_handle(y5), detail::make_layout(y5),
viennacl::traits::opencl_handle(y6), detail::make_layout(y6),
viennacl::traits::opencl_handle(y7), detail::make_layout(y7),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 8 * inner_prod_kernel_8.local_work_size()),
viennacl::traits::opencl_handle(temp)
) );
ksum.global_work_size(0, 8 * ksum.local_work_size(0));
viennacl::ocl::enqueue(ksum(viennacl::traits::opencl_handle(temp),
cl_uint(work_groups),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * 8 * ksum.local_work_size()),
viennacl::traits::opencl_handle(result),
cl_uint(viennacl::traits::start(result) + current_index * viennacl::traits::stride(result)),
cl_uint(viennacl::traits::stride(result))
)
);
}
current_index += 8;
break;
}
}
}
//implementation of inner product:
//namespace {
template <typename T>
void inner_prod_cpu(vector_base<T> const & vec1,
vector_base<T> const & vec2,
T & result)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(vec2).context() && bool("Vectors do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
vcl_size_t work_groups = 128;
viennacl::vector<T> temp(work_groups, viennacl::traits::context(vec1));
temp.resize(work_groups, ctx); // bring default-constructed vectors to the correct size:
// Step 1: Compute partial inner products for each work group:
inner_prod_impl(vec1, vec2, temp);
// Step 2: Sum partial results:
// Now copy partial results from GPU back to CPU and run reduction there:
std::vector<T> temp_cpu(work_groups);
viennacl::fast_copy(temp.begin(), temp.end(), temp_cpu.begin());
result = 0;
for (typename std::vector<T>::const_iterator it = temp_cpu.begin(); it != temp_cpu.end(); ++it)
result += *it;
}
template <typename T>
void norm_reduction_impl(vector_base<T> const & vec,
vector_base<T> & partial_result,
cl_uint norm_id)
{
assert(viennacl::traits::opencl_handle(vec).context() == viennacl::traits::opencl_handle(partial_result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "norm");
assert( (k.global_work_size() / k.local_work_size() <= partial_result.size()) && bool("Size mismatch for partial reduction in norm_reduction_impl()") );
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec),
cl_uint(viennacl::traits::start(vec)),
cl_uint(viennacl::traits::stride(vec)),
cl_uint(viennacl::traits::size(vec)),
cl_uint(norm_id),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<T>::type) * k.local_work_size()),
viennacl::traits::opencl_handle(partial_result) )
);
}
template <typename T>
void norm_1_impl(vector_base<T> const & vec,
scalar<T> & result)
{
assert(viennacl::traits::opencl_handle(vec).context() == viennacl::traits::opencl_handle(result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec).context());
vcl_size_t work_groups = 128;
viennacl::vector<T> temp(work_groups, viennacl::traits::context(vec));
// Step 1: Compute the partial work group results
norm_reduction_impl(vec, temp, 1);
// Step 2: Compute the partial reduction using OpenCL
viennacl::ocl::kernel & ksum = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "sum");
ksum.global_work_size(0, ksum.local_work_size(0));
viennacl::ocl::enqueue(ksum(viennacl::traits::opencl_handle(temp),
cl_uint(viennacl::traits::start(temp)),
cl_uint(viennacl::traits::stride(temp)),
cl_uint(viennacl::traits::size(temp)),
cl_uint(1),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<T>::type) * ksum.local_work_size()),
result)
);
}
template <typename T>
void norm_1_cpu(vector_base<T> const & vec,
T & result)
{
vcl_size_t work_groups = 128;
viennacl::vector<T> temp(work_groups, viennacl::traits::context(vec));
// Step 1: Compute the partial work group results
norm_reduction_impl(vec, temp, 1);
// Step 2: Now copy partial results from GPU back to CPU and run reduction there:
typedef std::vector<typename viennacl::result_of::cl_type<T>::type> CPUVectorType;
CPUVectorType temp_cpu(work_groups);
viennacl::fast_copy(temp.begin(), temp.end(), temp_cpu.begin());
result = 0;
for (typename CPUVectorType::const_iterator it = temp_cpu.begin(); it != temp_cpu.end(); ++it)
result += static_cast<T>(*it);
}
template <typename T>
void norm_2_impl(vector_base<T> const & vec,
scalar<T> & result)
{
assert(viennacl::traits::opencl_handle(vec).context() == viennacl::traits::opencl_handle(result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec).context());
vcl_size_t work_groups = 128;
viennacl::vector<T> temp(work_groups, viennacl::traits::context(vec));
// Step 1: Compute the partial work group results
norm_reduction_impl(vec, temp, 2);
// Step 2: Reduction via OpenCL
viennacl::ocl::kernel & ksum = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "sum");
ksum.global_work_size(0, ksum.local_work_size(0));
viennacl::ocl::enqueue( ksum(viennacl::traits::opencl_handle(temp),
cl_uint(viennacl::traits::start(temp)),
cl_uint(viennacl::traits::stride(temp)),
cl_uint(viennacl::traits::size(temp)),
cl_uint(2),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<T>::type) * ksum.local_work_size()),
result)
);
}
template <typename T>
void norm_2_cpu(vector_base<T> const & vec,
T & result)
{
vcl_size_t work_groups = 128;
viennacl::vector<T> temp(work_groups, viennacl::traits::context(vec));
// Step 1: Compute the partial work group results
norm_reduction_impl(vec, temp, 2);
// Step 2: Now copy partial results from GPU back to CPU and run reduction there:
typedef std::vector<typename viennacl::result_of::cl_type<T>::type> CPUVectorType;
CPUVectorType temp_cpu(work_groups);
viennacl::fast_copy(temp.begin(), temp.end(), temp_cpu.begin());
result = 0;
for (typename CPUVectorType::const_iterator it = temp_cpu.begin(); it != temp_cpu.end(); ++it)
result += static_cast<T>(*it);
result = std::sqrt(result);
}
template <typename T>
void norm_inf_impl(vector_base<T> const & vec,
scalar<T> & result)
{
assert(viennacl::traits::opencl_handle(vec).context() == viennacl::traits::opencl_handle(result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec).context());
vcl_size_t work_groups = 128;
viennacl::vector<T> temp(work_groups, viennacl::traits::context(vec));
// Step 1: Compute the partial work group results
norm_reduction_impl(vec, temp, 0);
//part 2: parallel reduction of reduced kernel:
viennacl::ocl::kernel & ksum = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "sum");
ksum.global_work_size(0, ksum.local_work_size(0));
viennacl::ocl::enqueue( ksum(viennacl::traits::opencl_handle(temp),
cl_uint(viennacl::traits::start(temp)),
cl_uint(viennacl::traits::stride(temp)),
cl_uint(viennacl::traits::size(temp)),
cl_uint(0),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<T>::type) * ksum.local_work_size()),
result)
);
}
template <typename T>
void norm_inf_cpu(vector_base<T> const & vec,
T & result)
{
vcl_size_t work_groups = 128;
viennacl::vector<T> temp(work_groups, viennacl::traits::context(vec));
// Step 1: Compute the partial work group results
norm_reduction_impl(vec, temp, 0);
// Step 2: Now copy partial results from GPU back to CPU and run reduction there:
typedef std::vector<typename viennacl::result_of::cl_type<T>::type> CPUVectorType;
CPUVectorType temp_cpu(work_groups);
viennacl::fast_copy(temp.begin(), temp.end(), temp_cpu.begin());
result = 0;
for (typename CPUVectorType::const_iterator it = temp_cpu.begin(); it != temp_cpu.end(); ++it)
result = std::max(result, static_cast<T>(*it));
}
//This function should return a CPU scalar, otherwise statements like
// vcl_rhs[index_norm_inf(vcl_rhs)]
// are ambiguous
template <typename T>
cl_uint index_norm_inf(vector_base<T> const & vec)
{
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
viennacl::ocl::handle<cl_mem> h = ctx.create_memory(CL_MEM_READ_WRITE, sizeof(cl_uint));
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "index_norm_inf");
//cl_uint size = static_cast<cl_uint>(vcl_vec.internal_size());
//TODO: Use multi-group kernel for large vector sizes
k.global_work_size(0, k.local_work_size());
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec),
cl_uint(viennacl::traits::start(vec)),
cl_uint(viennacl::traits::stride(vec)),
cl_uint(viennacl::traits::size(vec)),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<T>::type) * k.local_work_size()),
viennacl::ocl::local_mem(sizeof(cl_uint) * k.local_work_size()), h));
//read value:
cl_uint result;
cl_int err = clEnqueueReadBuffer(ctx.get_queue().handle().get(), h.get(), CL_TRUE, 0, sizeof(cl_uint), &result, 0, NULL, NULL);
VIENNACL_ERR_CHECK(err);
return result;
}
template<typename NumericT>
void max_impl(vector_base<NumericT> const & x,
scalar<NumericT> & result)
{
assert(viennacl::traits::opencl_handle(x).context() == viennacl::traits::opencl_handle(result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(x).context());
viennacl::linalg::opencl::kernels::vector<NumericT>::init(ctx);
vcl_size_t work_groups = 128;
viennacl::vector<NumericT> temp(work_groups, viennacl::traits::context(x));
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<NumericT>::program_name(), "max_kernel");
k.global_work_size(0, work_groups * k.local_work_size(0));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(x),
cl_uint(viennacl::traits::start(x)),
cl_uint(viennacl::traits::stride(x)),
cl_uint(viennacl::traits::size(x)),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * k.local_work_size()),
viennacl::traits::opencl_handle(temp)
));
k.global_work_size(0, k.local_work_size());
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(temp),
cl_uint(viennacl::traits::start(temp)),
cl_uint(viennacl::traits::stride(temp)),
cl_uint(viennacl::traits::size(temp)),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * k.local_work_size()),
viennacl::traits::opencl_handle(result)
));
}
template<typename NumericT>
void max_cpu(vector_base<NumericT> const & x,
NumericT & result)
{
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(x).context());
viennacl::linalg::opencl::kernels::vector<NumericT>::init(ctx);
vcl_size_t work_groups = 128;
viennacl::vector<NumericT> temp(work_groups, viennacl::traits::context(x));
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<NumericT>::program_name(), "max_kernel");
k.global_work_size(0, work_groups * k.local_work_size(0));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(x),
cl_uint(viennacl::traits::start(x)),
cl_uint(viennacl::traits::stride(x)),
cl_uint(viennacl::traits::size(x)),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * k.local_work_size()),
viennacl::traits::opencl_handle(temp)
));
// Step 2: Now copy partial results from GPU back to CPU and run reduction there:
typedef std::vector<typename viennacl::result_of::cl_type<NumericT>::type> CPUVectorType;
CPUVectorType temp_cpu(work_groups);
viennacl::fast_copy(temp.begin(), temp.end(), temp_cpu.begin());
result = static_cast<NumericT>(temp_cpu[0]);
for (typename CPUVectorType::const_iterator it = temp_cpu.begin(); it != temp_cpu.end(); ++it)
result = std::max(result, static_cast<NumericT>(*it));
}
template<typename NumericT>
void min_impl(vector_base<NumericT> const & x,
scalar<NumericT> & result)
{
assert(viennacl::traits::opencl_handle(x).context() == viennacl::traits::opencl_handle(result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(x).context());
viennacl::linalg::opencl::kernels::vector<NumericT>::init(ctx);
vcl_size_t work_groups = 128;
viennacl::vector<NumericT> temp(work_groups, viennacl::traits::context(x));
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<NumericT>::program_name(), "min_kernel");
k.global_work_size(0, work_groups * k.local_work_size(0));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(x),
cl_uint(viennacl::traits::start(x)),
cl_uint(viennacl::traits::stride(x)),
cl_uint(viennacl::traits::size(x)),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * k.local_work_size()),
viennacl::traits::opencl_handle(temp)
));
k.global_work_size(0, k.local_work_size());
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(temp),
cl_uint(viennacl::traits::start(temp)),
cl_uint(viennacl::traits::stride(temp)),
cl_uint(viennacl::traits::size(temp)),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * k.local_work_size()),
viennacl::traits::opencl_handle(result)
));
}
template<typename NumericT>
void min_cpu(vector_base<NumericT> const & x,
NumericT & result)
{
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(x).context());
viennacl::linalg::opencl::kernels::vector<NumericT>::init(ctx);
vcl_size_t work_groups = 128;
viennacl::vector<NumericT> temp(work_groups, viennacl::traits::context(x));
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<NumericT>::program_name(), "min_kernel");
k.global_work_size(0, work_groups * k.local_work_size(0));
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(x),
cl_uint(viennacl::traits::start(x)),
cl_uint(viennacl::traits::stride(x)),
cl_uint(viennacl::traits::size(x)),
viennacl::ocl::local_mem(sizeof(typename viennacl::result_of::cl_type<NumericT>::type) * k.local_work_size()),
viennacl::traits::opencl_handle(temp)
));
// Step 2: Now copy partial results from GPU back to CPU and run reduction there:
typedef std::vector<typename viennacl::result_of::cl_type<NumericT>::type> CPUVectorType;
CPUVectorType temp_cpu(work_groups);
viennacl::fast_copy(temp.begin(), temp.end(), temp_cpu.begin());
result = static_cast<NumericT>(temp_cpu[0]);
for (typename CPUVectorType::const_iterator it = temp_cpu.begin(); it != temp_cpu.end(); ++it)
result = std::min(result, static_cast<NumericT>(*it));
}
template<typename NumericT>
void sum_impl(vector_base<NumericT> const & x,
scalar<NumericT> & result)
{
assert(viennacl::traits::opencl_handle(x).context() == viennacl::traits::opencl_handle(result).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::vector<NumericT> all_ones = viennacl::scalar_vector<NumericT>(x.size(), NumericT(1), viennacl::traits::context(x));
viennacl::linalg::opencl::inner_prod_impl(x, all_ones, result);
}
template<typename NumericT>
void sum_cpu(vector_base<NumericT> const & x, NumericT & result)
{
scalar<NumericT> tmp(0, viennacl::traits::context(x));
sum_impl(x, tmp);
result = tmp;
}
//TODO: Special case vec1 == vec2 allows improvement!!
template <typename T>
void plane_rotation(vector_base<T> & vec1,
vector_base<T> & vec2,
T alpha, T beta)
{
assert(viennacl::traits::opencl_handle(vec1).context() == viennacl::traits::opencl_handle(vec2).context() && bool("Operands do not reside in the same OpenCL context. Automatic migration not yet supported!"));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(vec1).context());
viennacl::linalg::opencl::kernels::vector<T>::init(ctx);
assert(viennacl::traits::size(vec1) == viennacl::traits::size(vec2));
viennacl::ocl::kernel & k = ctx.get_kernel(viennacl::linalg::opencl::kernels::vector<T>::program_name(), "plane_rotation");
viennacl::ocl::enqueue(k(viennacl::traits::opencl_handle(vec1),
cl_uint(viennacl::traits::start(vec1)),
cl_uint(viennacl::traits::stride(vec1)),
cl_uint(viennacl::traits::size(vec1)),
viennacl::traits::opencl_handle(vec2),
cl_uint(viennacl::traits::start(vec2)),
cl_uint(viennacl::traits::stride(vec2)),
cl_uint(viennacl::traits::size(vec2)),
viennacl::traits::opencl_handle(alpha),
viennacl::traits::opencl_handle(beta))
);
}
namespace detail
{
template<typename NumericT>
void scan_impl(vector_base<NumericT> const & input,
vector_base<NumericT> & output,
bool is_inclusive)
{
vcl_size_t local_worksize = 128;
vcl_size_t workgroups = 128;
viennacl::backend::mem_handle opencl_carries;
viennacl::backend::memory_create(opencl_carries, sizeof(NumericT)*workgroups, viennacl::traits::context(input));
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(input).context());
viennacl::linalg::opencl::kernels::scan<NumericT>::init(ctx);
viennacl::ocl::kernel& k1 = ctx.get_kernel(viennacl::linalg::opencl::kernels::scan<NumericT>::program_name(), "scan_1");
viennacl::ocl::kernel& k2 = ctx.get_kernel(viennacl::linalg::opencl::kernels::scan<NumericT>::program_name(), "scan_2");
viennacl::ocl::kernel& k3 = ctx.get_kernel(viennacl::linalg::opencl::kernels::scan<NumericT>::program_name(), "scan_3");
// First step: Scan within each thread group and write carries
k1.local_work_size(0, local_worksize);
k1.global_work_size(0, workgroups * local_worksize);
viennacl::ocl::enqueue(k1( input, cl_uint( input.start()), cl_uint( input.stride()), cl_uint(input.size()),
output, cl_uint(output.start()), cl_uint(output.stride()),
cl_uint(is_inclusive ? 0 : 1), opencl_carries.opencl_handle())
);
// Second step: Compute offset for each thread group (exclusive scan for each thread group)
k2.local_work_size(0, workgroups);
k2.global_work_size(0, workgroups);
viennacl::ocl::enqueue(k2(opencl_carries.opencl_handle()));
// Third step: Offset each thread group accordingly
k3.local_work_size(0, local_worksize);
k3.global_work_size(0, workgroups * local_worksize);
viennacl::ocl::enqueue(k3(output, cl_uint(output.start()), cl_uint(output.stride()), cl_uint(output.size()),
opencl_carries.opencl_handle())
);
}
}
template<typename NumericT>
void inclusive_scan(vector_base<NumericT> const & input,
vector_base<NumericT> & output)
{
detail::scan_impl(input, output, true);
}
template<typename NumericT>
void exclusive_scan(vector_base<NumericT> const & input,
vector_base<NumericT> & output)
{
detail::scan_impl(input, output, false);
}
} //namespace opencl
} //namespace linalg
} //namespace viennacl
#endif
viennacl::linalg::opencl::min_cpu
void min_cpu(vector_base< NumericT > const &x, NumericT &result)
Computes the minimum of a vector, where the result is stored on a CPU scalar.
Definition: vector_operations.hpp:1083
viennacl::ocl::packed_cl_uint::stride
cl_uint stride
Increment between integers.
Definition: kernel.hpp:50
viennacl::vector_tuple::const_size
vcl_size_t const_size() const
Definition: vector.hpp:1143
viennacl::ocl::packed_cl_uint
Helper class for packing four cl_uint numbers into a uint4 type for access inside an OpenCL kernel...
Definition: kernel.hpp:45
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
viennacl::linalg::opencl::avbv
void avbv(vector_base< T > &vec1, vector_base< T > const &vec2, ScalarType1 const &alpha, vcl_size_t len_alpha, bool reciprocal_alpha, bool flip_sign_alpha, vector_base< T > const &vec3, ScalarType2 const &beta, vcl_size_t len_beta, bool reciprocal_beta, bool flip_sign_beta)
Definition: vector_operations.hpp:116
viennacl::linalg::opencl::norm_2_impl
void norm_2_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the l^2-norm of a vector - implementation using OpenCL summation at second step...
Definition: vector_operations.hpp:801
device.hpp
Represents an OpenCL device within ViennaCL.
viennacl::linalg::opencl::norm_1_cpu
void norm_1_cpu(vector_base< T > const &vec, T &result)
Computes the l^1-norm of a vector with final reduction on CPU.
Definition: vector_operations.hpp:770
size.hpp
Generic size and resize functionality for different vector and matrix types.
viennacl::linalg::opencl::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:1156
viennacl::ocl::context::get_queue
viennacl::ocl::command_queue & get_queue()
Definition: context.hpp:266
viennacl::ocl::kernel
Represents an OpenCL kernel within ViennaCL.
Definition: kernel.hpp:58
start.hpp
Extracts the underlying OpenCL start index handle from a vector, a matrix, an expression etc...
viennacl::ocl::packed_cl_uint::start
cl_uint start
Starting value of the integer stride.
Definition: kernel.hpp:48
viennacl::linalg::opencl::kernels::vector_convert::program_name
static std::string program_name()
Definition: vector.hpp:784
tools.hpp
Various little tools used here and there in ViennaCL.
viennacl::ocl::kernel::local_work_size
size_type local_work_size(int index=0) const
Returns the local work size at the respective dimension.
Definition: kernel.hpp:742
viennacl::linalg::opencl::norm_reduction_impl
void norm_reduction_impl(vector_base< T > const &vec, vector_base< T > &partial_result, cl_uint norm_id)
Computes the partial work group results for vector norms.
Definition: vector_operations.hpp:705
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::linalg::opencl::kernels::vector_multi_inner_prod
Main kernel class for generating OpenCL kernels for multiple inner products on/with viennacl::vector<...
Definition: vector.hpp:727
viennacl::linalg::opencl::norm_inf_impl
void norm_inf_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the supremum-norm of a vector.
Definition: vector_operations.hpp:865
viennacl::traits::stride
result_of::size_type< viennacl::vector_base< T > >::type stride(viennacl::vector_base< T > const &s)
Definition: stride.hpp:45
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
stride.hpp
Determines row and column increments for matrices and matrix proxies.
viennacl::linalg::opencl::min_impl
void min_impl(vector_base< NumericT > const &x, scalar< NumericT > &result)
Computes the minimum of a vector, where the result is stored in an OpenCL buffer. ...
Definition: vector_operations.hpp:1045
viennacl::result_of::cl_type::type
T type
Definition: result_of.hpp:590
viennacl::traits::internal_size
vcl_size_t internal_size(vector_base< NumericT > const &vec)
Helper routine for obtaining the buffer length of a ViennaCL vector.
Definition: size.hpp:375
viennacl::linalg::detail::max
T max(const T &lhs, const T &rhs)
Maximum.
Definition: util.hpp:59
viennacl::vector_expression
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
viennacl::linalg::opencl::kernels::scan::init
static void init(viennacl::ocl::context &ctx)
Definition: scan.hpp:162
viennacl::linalg::opencl::avbv_v
void avbv_v(vector_base< T > &vec1, vector_base< T > const &vec2, ScalarType1 const &alpha, vcl_size_t len_alpha, bool reciprocal_alpha, bool flip_sign_alpha, vector_base< T > const &vec3, ScalarType2 const &beta, vcl_size_t len_beta, bool reciprocal_beta, bool flip_sign_beta)
Definition: vector_operations.hpp:178
viennacl::linalg::opencl::kernels::scan
Main kernel class for generating OpenCL kernels for singular value decomposition of dense matrices...
Definition: scan.hpp:155
viennacl::ocl::packed_cl_uint::internal_size
cl_uint internal_size
Internal length of the buffer. Might be larger than 'size' due to padding.
Definition: kernel.hpp:54
common.hpp
Common implementations shared by OpenCL-based operations.
viennacl::ocl::command_queue::handle
viennacl::ocl::handle< cl_command_queue > const & handle() const
Definition: command_queue.hpp:81
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::vector_base::stride
size_type stride() const
Returns the stride within the buffer (in multiples of sizeof(NumericT))
Definition: vector_def.hpp:124
VIENNACL_ERR_CHECK
#define VIENNACL_ERR_CHECK(err)
Definition: error.hpp:681
viennacl::ocl::handle::get
const OCL_TYPE & get() const
Definition: handle.hpp:191
viennacl::linalg::opencl::inner_prod_impl
void inner_prod_impl(vector_base< T > const &vec1, vector_base< T > const &vec2, vector_base< T > &partial_result)
Computes the partial inner product of two vectors - implementation. Library users should call inner_p...
Definition: vector_operations.hpp:388
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::linalg::opencl::sum_impl
void sum_impl(vector_base< NumericT > const &x, scalar< NumericT > &result)
Computes the sum over all entries of a vector.
Definition: vector_operations.hpp:1122
viennacl::ocl::local_mem
A class representing local (shared) OpenCL memory. Typically used as kernel argument.
Definition: local_mem.hpp:33
viennacl::linalg::opencl::max_impl
void max_impl(vector_base< NumericT > const &x, scalar< NumericT > &result)
Computes the maximum value of a vector, where the result is stored in an OpenCL buffer.
Definition: vector_operations.hpp:966
viennacl::is_cpu_scalar
Helper struct for checking whether a type is a host scalar type (e.g. float, double) ...
Definition: forwards.h:448
viennacl::linalg::opencl::max_cpu
void max_cpu(vector_base< NumericT > const &x, NumericT &result)
Computes the maximum value of a vector, where the value is stored in a host value.
Definition: vector_operations.hpp:1004
viennacl::ocl::context::get_kernel
viennacl::ocl::kernel & get_kernel(std::string const &program_name, std::string const &kernel_name)
Convenience function for retrieving the kernel of a program directly from the context.
Definition: context.hpp:605
viennacl::vector_base< NumericT >::begin
iterator begin()
Returns an iterator pointing to the beginning of the vector (STL like)
viennacl::linalg::opencl::norm_2_cpu
void norm_2_cpu(vector_base< T > const &vec, T &result)
Computes the l^1-norm of a vector with final reduction on CPU.
Definition: vector_operations.hpp:834
viennacl::vector_tuple
Tuple class holding pointers to multiple vectors. Mainly used as a temporary object returned from vie...
Definition: forwards.h:269
vector.hpp
OpenCL kernel file for vector operations.
handle.hpp
Implementation of a smart-pointer-like class for handling OpenCL handles.
viennacl::traits::start
result_of::size_type< T >::type start(T const &obj)
Definition: start.hpp:44
viennacl::linalg::opencl::av
void av(vector_base< T > &vec1, vector_base< T > const &vec2, ScalarType1 const &alpha, vcl_size_t len_alpha, bool reciprocal_alpha, bool flip_sign_alpha)
Definition: vector_operations.hpp:76
viennacl::linalg::opencl::detail::make_options
cl_uint make_options(vcl_size_t length, bool reciprocal, bool flip_sign)
Definition: common.hpp:42
viennacl::linalg::opencl::kernels::vector::init
static void init(viennacl::ocl::context &ctx)
Definition: vector.hpp:686
viennacl::linalg::opencl::norm_1_impl
void norm_1_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the l^1-norm of a vector.
Definition: vector_operations.hpp:737
viennacl::vector::resize
void resize(size_type new_size, bool preserve=true)
Resizes the allocated memory for the vector. Pads the memory to be a multiple of 'AlignmentV'.
Definition: vector.hpp:1046
viennacl::vector_base
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::vector
Definition: forwards.h:266
viennacl::linalg::opencl::inclusive_scan
void inclusive_scan(vector_base< NumericT > const &input, vector_base< NumericT > &output)
This function implements an inclusive scan using CUDA.
Definition: vector_operations.hpp:1238
viennacl::is_division
Helper metafunction for checking whether the provided type is viennacl::op_div (for division) ...
Definition: predicate.hpp:466
scan.hpp
OpenCL kernel file for scan operations. To be merged back to vector operations.
viennacl::linalg::opencl::element_op
void element_op(matrix_base< T > &A, matrix_expression< const matrix_base< T >, const matrix_base< T >, op_element_binary< OP > > const &proxy)
Implementation of binary element-wise operations A = OP(B,C)
Definition: matrix_operations.hpp:540
viennacl::linalg::opencl::kernels::vector_multi_inner_prod::init
static void init(viennacl::ocl::context &ctx)
Definition: vector.hpp:734
viennacl::linalg::opencl::kernels::vector_element
Main kernel class for generating OpenCL kernels for elementwise operations other than addition and su...
Definition: vector_element.hpp:97
predicate.hpp
All the predicates used within ViennaCL. Checks for expressions to be vectors, etc.
viennacl::linalg::opencl::convert
void convert(matrix_base< DestNumericT > &dest, matrix_base< SrcNumericT > const &src)
Definition: matrix_operations.hpp:134
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
viennacl::ocl::enqueue
void enqueue(KernelType &k, viennacl::ocl::command_queue const &queue)
Enqueues a kernel in the provided queue.
Definition: enqueue.hpp:50
kernel.hpp
Representation of an OpenCL kernel in ViennaCL.
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::opencl::exclusive_scan
void exclusive_scan(vector_base< NumericT > const &input, vector_base< NumericT > &output)
This function implements an exclusive scan using CUDA.
Definition: vector_operations.hpp:1251
viennacl::linalg::opencl::index_norm_inf
cl_uint 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:930
viennacl::vector_base::size
size_type size() const
Returns the length of the vector (cf. std::vector)
Definition: vector_def.hpp:118
viennacl::linalg::opencl::norm_inf_cpu
void norm_inf_cpu(vector_base< T > const &vec, T &result)
Computes the supremum-norm of a vector.
Definition: vector_operations.hpp:898
viennacl::ocl::kernel::global_work_size
size_type global_work_size(int index=0) const
Returns the global work size at the respective dimension.
Definition: kernel.hpp:751
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::vector_tuple::const_at
VectorType const & const_at(vcl_size_t i) const
Definition: vector.hpp:1146
viennacl::linalg::opencl::detail::make_layout
viennacl::ocl::packed_cl_uint make_layout(vector_base< NumericT > const &vec)
Definition: vector_operations.hpp:470
viennacl::op_element_binary
A tag class representing element-wise binary operations (like multiplication) on vectors or matrices...
Definition: forwards.h:130
vector_element.hpp
OpenCL kernel file for element-wise vector operations.
viennacl::backend::memory_create
void memory_create(mem_handle &handle, vcl_size_t size_in_bytes, viennacl::context const &ctx, const void *host_ptr=NULL)
Creates an array of the specified size. If the second argument is provided, the buffer is initialized...
Definition: memory.hpp:87
vector_def.hpp
Forward declarations of the implicit_vector_base, vector_base class.
viennacl::linalg::detail::min
T min(const T &lhs, const T &rhs)
Minimum.
Definition: util.hpp:45
handle.hpp
Extracts the underlying OpenCL handle from a vector, a matrix, an expression etc. ...
viennacl::vector_base::internal_size
size_type internal_size() const
Returns the internal length of the vector, which is given by size() plus the extra memory due to padd...
Definition: vector_def.hpp:120
viennacl::linalg::opencl::vector_assign
void vector_assign(vector_base< T > &vec1, const T &alpha, bool up_to_internal_size=false)
Assign a constant value to a vector (-range/-slice)
Definition: vector_operations.hpp:246
viennacl::vector_base< NumericT >::end
iterator end()
Returns an iterator pointing to the end of the vector (STL like)
viennacl::is_product
Helper metafunction for checking whether the provided type is viennacl::op_prod (for products/multipl...
Definition: predicate.hpp:436
viennacl::vector_base::start
size_type start() const
Returns the offset within the buffer.
Definition: vector_def.hpp:122
viennacl::linalg::opencl::detail::op_to_string
std::string op_to_string(op_abs)
Definition: common.hpp:78
viennacl::ocl::type_to_string
Helper class for converting a type to its string representation.
Definition: utils.hpp:57
viennacl::linalg::opencl::detail::scan_impl
void scan_impl(vector_base< NumericT > const &input, vector_base< NumericT > &output, bool is_inclusive)
Worker routine for scan routines using OpenCL.
Definition: vector_operations.hpp:1193
viennacl::linalg::opencl::vector_swap
void vector_swap(vector_base< T > &vec1, vector_base< T > &vec2)
Swaps the contents of two vectors, data is copied.
Definition: vector_operations.hpp:272
viennacl::op_element_unary
A tag class representing element-wise unary operations (like sin()) on vectors or matrices...
Definition: forwards.h:134
viennacl::linalg::opencl::inner_prod_cpu
void inner_prod_cpu(vector_base< T > const &vec1, vector_base< T > const &vec2, T &result)
Computes the inner product of two vectors - implementation. Library users should call inner_prod(vec1...
Definition: vector_operations.hpp:669
scalar.hpp
Implementation of the ViennaCL scalar class.
viennacl::linalg::opencl::kernels::vector_convert::init
static void init(viennacl::ocl::context &ctx)
Definition: vector.hpp:789
viennacl::ocl::handle< cl_mem >
viennacl::linalg::opencl::sum_cpu
void sum_cpu(vector_base< NumericT > const &x, NumericT &result)
Computes the sum over all entries of a vector.
Definition: vector_operations.hpp:1137
viennacl::linalg::opencl::kernels::vector
Main kernel class for generating OpenCL kernels for operations on/with viennacl::vector<> without inv...
Definition: vector.hpp:679
enable_if.hpp
Simple enable-if variant that uses the SFINAE pattern.
viennacl::ocl::packed_cl_uint::size
cl_uint size
Number of values in the stride.
Definition: kernel.hpp:52
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)
viennacl::linalg::opencl::kernels::vector_element::init
static void init(viennacl::ocl::context &ctx)
Definition: vector_element.hpp:104
viennacl::ocl::context::create_memory
viennacl::ocl::handle< cl_mem > create_memory(cl_mem_flags flags, unsigned int size, void *ptr=NULL) const
Creates a memory buffer within the context.
Definition: context.hpp:216