32#ifndef REMORA_KERNELS_CLBLAS_TRMV_HPP
33#define REMORA_KERNELS_CLBLAS_TRMV_HPP
36#include "../../expression_types.hpp"
37#include "../../detail/traits.hpp"
38#include <boost/compute/functional/operator.hpp>
41namespace remora {
namespace bindings {
44 boost::compute::kernel kernel;
45 std::size_t start_index;
46 std::size_t end_index;
47 std::size_t unit_index;
48 std::size_t upper_index;
51template<
class MatA,
class VecV>
52trmv_kernel createTRMVBlockKernel(
53 matrix_expression<MatA, gpu_tag>
const& A_unreg,
54 vector_expression<VecV, gpu_tag>& v_unreg,
57 typedef typename MatA::value_type value_typeA;
58 typedef typename VecV::value_type value_typeV;
59 boost::compute::multiplies<value_typeV> prod;
61 gpu::detail::meta_kernel k(
"blas_trmv");
62 std::size_t start_index = k.add_arg<std::size_t>(
"start");
63 std::size_t end_index = k.add_arg<std::size_t>(
"end");
64 std::size_t unit_index = k.add_arg<std::size_t>(
"unit");
65 std::size_t upper_index = k.add_arg<std::size_t>(
"upper");
66 auto A = k.register_args(to_functor(A_unreg));
67 auto v = k.register_args(to_functor(v_unreg));
71 k <<
"__local " <<k.decl<value_typeA>(
"Asub")<<
"[TILE_SIZE][TILE_SIZE+2];\n";
72 k <<
"__local " <<k.decl<value_typeV>(
"Bsub")<<
"[TILE_SIZE];\n";
73 k <<
"__local " <<k.decl<value_typeV>(
"BResult")<<
"[TILE_SIZE];\n";
74 k <<
" const ulong numWorkers = get_local_size(0);\n";
77 k <<
"const ulong curTileA = end-start;\n";
78 k <<
"for(ulong i = 0; i < curTileA; ++i){\n";
79 k <<
" for(ulong j = get_local_id(0); j < curTileA; j += numWorkers){\n";
80 k <<
" Asub[i][j] ="<< A(k.expr<cl_ulong>(
"(i+start)"),k.expr<cl_ulong>(
"(j+start)"))<<
";\n";
86 k <<
"for(ulong i = get_local_id(0); i < curTileA; i += numWorkers){\n";
87 k <<
" Bsub[i] = "<< v(k.expr<cl_ulong>(
"(start+i)"))<<
";\n";
90 k <<
"barrier(CLK_LOCAL_MEM_FENCE);\n";
96 k <<
" for(ulong i = get_local_id(0); i < TILE_SIZE; i += numWorkers){\n";
97 k <<
" BResult[i] = Bsub[i];\n";
98 k <<
" if(!unit){BResult[i] *= Asub[i][i];}\n";
99 k <<
" for(ulong j = 0; j < i; ++j){\n";
100 k <<
" BResult[i] +="<< prod(k.expr<value_typeV>(
"Bsub[j]"), k.expr<value_typeA>(
"Asub[i][j]"))<<
";\n";
105 k <<
" for(ulong i = get_local_id(0); i < curTileA; i += numWorkers){\n";
106 k <<
" BResult[i] = Bsub[i];\n";
107 k <<
" if(!unit){BResult[i] *= Asub[i][i];}\n";
108 k <<
" for(ulong j = i+1; j < curTileA; ++j){\n";
109 k <<
" BResult[i] +="<< prod(k.expr<value_typeV>(
"Bsub[j]"), k.expr<value_typeA>(
"Asub[i][j]"))<<
";\n";
114 k <<
"barrier(CLK_LOCAL_MEM_FENCE);\n";
116 k <<
"for(ulong i = get_local_id(0); i < curTileA; i += numWorkers){\n";
117 k << v(k.expr<cl_ulong>(
"(start+i)"))<<
" = BResult[i];\n";
120 boost::compute::kernel kernel = k.compile(v_unreg().queue().get_context(), options);
121 return {kernel,start_index,end_index,unit_index,upper_index};
124template <
typename MatA,
typename VecV,
typename Triangular>
126 matrix_expression<MatA, gpu_tag>
const& Afull,
127 vector_expression<VecV, gpu_tag> & vfull,
131 std::size_t tileSizeA,
134 std::size_t size = end-start;
137 if(size <= tileSizeA){
141 kernel.kernel.set_arg(kernel.start_index, start);
142 kernel.kernel.set_arg(kernel.end_index, end);
143 kernel.kernel.set_arg(kernel.unit_index, (std::size_t)Triangular::is_unit);
144 kernel.kernel.set_arg(kernel.upper_index, (std::size_t)Triangular::is_upper);
146 std::size_t global_work_size[2] = {
150 vfull().queue().enqueue_nd_range_kernel(kernel.kernel, 2,
nullptr, global_work_size, global_work_size);
154 std::size_t split = ((size+tileSizeA-1)/tileSizeA)/2 * tileSizeA;
155 auto vfront = subrange(vfull,start,start+split);
156 auto vback = subrange(vfull,start+split,end);
158 if(Triangular::is_upper){
159 auto Aur = subrange(Afull,start,start+split,start+split,end);
160 trmv_recursive(Afull, vfull, kernel, start, start+split, tileSizeA, t);
161 kernels::gemv(Aur, vback, vfront, 1.0);
162 trmv_recursive(Afull, vfull, kernel, start+split, end, tileSizeA, t);
164 auto All = subrange(Afull,start+split,end,start,start+split);
165 trmv_recursive(Afull, vfull, kernel, start+split, end, tileSizeA, t);
166 kernels::gemv(All, vfront, vback, 1.0);
167 trmv_recursive(Afull, vfull, kernel, start, start+split, tileSizeA, t);
174template <
bool Upper,
bool Unit,
typename MatA,
typename VecV>
176 matrix_expression<MatA, gpu_tag>
const& A,
177 vector_expression<VecV, gpu_tag>& v
179 REMORA_SIZE_CHECK(A().size1() == A().size2());
180 REMORA_SIZE_CHECK(A().size2() == v().size());
182 std::size_t
const TileSizeA = 32;
183 char const* options =
"-DTILE_SIZE=32ul";
184 auto kernel = bindings::createTRMVBlockKernel(A,v,options);
186 bindings::trmv_recursive(A,v,kernel,0,A().size1(), TileSizeA, triangular_tag<Upper,Unit>());