vector_assign.hpp
Go to the documentation of this file.
1/*!
2 * \brief Assignment kernels for vector expressions
3 *
4 * \author O. Krause
5 * \date 2016
6 *
7 *
8 * \par Copyright 1995-2015 Shark Development Team
9 *
10 * <BR><HR>
11 * This file is part of Shark.
12 * <http://image.diku.dk/shark/>
13 *
14 * Shark is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published
16 * by the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Shark is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28#ifndef REMORA_KERNELS_CLBLAS_VECTOR_ASSIGN_HPP
29#define REMORA_KERNELS_CLBLAS_VECTOR_ASSIGN_HPP
30
31#include "../../expression_types.hpp"
32#include "../../detail/traits.hpp"
33
34namespace remora{namespace bindings{
35
36template<class F, class V>
37void apply(vector_expression<V, gpu_tag>& v_unreg, F const& f_unreg) {
38 if(v_unreg().size() == 0) return;
39 gpu::detail::meta_kernel k("blas_vector_apply_dense");
40
41 auto v = k.register_args(to_functor(v_unreg));
42 auto f = k.register_args(f_unreg);
43
44 //create source
45 k<<v(k.get_global_id(0))<<" = " << f(v(k.get_global_id(0)))<<";";
46 boost::compute::kernel kernel = k.compile(v_unreg().queue().get_context());
47 //enqueue kernel
48 std::size_t global_work_size[1] = {v_unreg().size()};
49 v_unreg().queue().enqueue_nd_range_kernel(kernel, 1,nullptr, global_work_size, nullptr);
50}
51
52template<class F, class V>
53void assign(vector_expression<V, gpu_tag>& v, typename V::value_type t) {
54 static_assert(std::is_base_of<dense_tag, typename V::storage_type::storage_tag>::value, "target must have dense storage for assignment");
55 auto f = device_traits<gpu_tag>::make_bind_second(F(), t);
56 apply(v,f);
57}
58
59////////////////////////////////////////////
60//assignment with functor
61////////////////////////////////////////////
62
63// Dense-Dense case
64template<class V, class E, class F>
65void vector_assign_functor(
66 vector_expression<V, gpu_tag>& v_unreg,
67 vector_expression<E, gpu_tag> const& e_unreg,
68 F f_unreg,
69 dense_tag, dense_tag
70) {
71 if(v_unreg().size() == 0) return;
72
73 gpu::detail::meta_kernel k("blas_vector_assign_functor_dense");
74
75 auto v = k.register_args(to_functor(v_unreg));
76 auto e = k.register_args(to_functor(e_unreg));
77 auto f = k.register_args(f_unreg);
78
79 //create source
80 k<<v(k.get_global_id(0))<<" = " << f(v(k.get_global_id(0)), e(k.get_global_id(0)))<<";";
81 boost::compute::kernel kernel = k.compile(v_unreg().queue().get_context());
82 //enqueue kernel
83 std::size_t global_work_size[1] = {v_unreg().size()};
84 v_unreg().queue().enqueue_nd_range_kernel(kernel, 1,nullptr, global_work_size, nullptr);
85}
86
87/////////////////////////////////////////////////////////
88//direct assignment of two vectors
89////////////////////////////////////////////////////////
90
91// Dense-Dense case
92template< class V, class E>
93void vector_assign(
94 vector_expression<V, gpu_tag>& v, vector_expression<E, gpu_tag> const& e,
95 dense_tag t, dense_tag
96) {
97 vector_assign_functor(v, e, device_traits<gpu_tag>::right_arg<typename E::value_type>(), t, t);
98}
99
100
101
102
103}}
104#endif