31#ifndef REMORA_KERNELS_CLBLAS_TRSV_HPP
32#define REMORA_KERNELS_CLBLAS_TRSV_HPP
35#include "../../detail/traits.hpp"
36#include "../../proxy_expressions.hpp"
38#include <boost/compute/functional/operator.hpp>
42namespace 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 VecB>
52trsv_kernel createTRSVDiagBlockKernel(
53 matrix_expression<MatA, gpu_tag>
const& A_unreg,
54 vector_expression<VecB, gpu_tag> &b_unreg,
57 typedef typename MatA::value_type value_typeA;
58 typedef typename VecB::value_type value_typeB;
59 boost::compute::multiplies<value_typeB> prod;
61 gpu::detail::meta_kernel k(
"blas_trsv");
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 b = k.register_args(to_functor(b_unreg));
69 k <<
"__local " <<k.decl<value_typeA>(
"Asub")<<
"[TILE_SIZE][TILE_SIZE+2];\n";
70 k <<
"__local " <<k.decl<value_typeB>(
"Bsub")<<
"[TILE_SIZE];\n";
71 k <<
"const ulong numWorkers = get_local_size(0);\n";
73 k <<
"const ulong curTileA = end-start;\n";
76 k <<
"for(ulong i = get_local_id(0); i < TILE_SIZE; i += numWorkers){\n";
77 k <<
" for(ulong j = 0; j < TILE_SIZE; j++){\n";
78 k <<
" Asub[i][j] ="<< A(k.expr<cl_ulong>(
"min(end-1, start + i)"),k.expr<cl_ulong>(
"min(end-1, start + j)"))<<
";\n";
84 k <<
"for(ulong i = get_local_id(0); i < TILE_SIZE; i += numWorkers){\n";
85 k <<
" Bsub[i] ="<< b(k.expr<cl_ulong>(
"min(end-1,start + i)"))<<
";\n";
88 k <<
"barrier(CLK_LOCAL_MEM_FENCE);\n";
93 k <<
" for(ulong i = 0; i < TILE_SIZE && get_local_id(0) == 0; ++i){\n";
94 k <<
" if(!unit){Bsub[i] /= Asub[i][i];}\n";
95 k <<
" for(ulong j = i+1; j < TILE_SIZE; ++j){\n";
96 k <<
" Bsub[j] -= "<< prod(k.expr<value_typeB>(
"Bsub[i]"), k.expr<value_typeA>(
"Asub[j][i]"))<<
";\n";
101 k <<
" for(ulong n = curTileA; n > 0 && get_local_id(0) == 0; --n){\n";
102 k <<
" ulong i = n-1;\n";
103 k <<
" if(!unit ){Bsub[i] /= Asub[i][i];}\n";
104 k <<
" for(ulong j = 0; j < i; j ++){\n";
105 k <<
" Bsub[j] -= "<< prod(k.expr<value_typeB>(
"Bsub[i]"), k.expr<value_typeA>(
"Asub[j][i]"))<<
";\n";
110 k <<
"barrier(CLK_LOCAL_MEM_FENCE);\n";
112 k <<
"for(ulong i = get_local_id(0); i < curTileA; i += numWorkers){\n";
113 k << b(k.expr<cl_ulong>(
"(start+i)"))<<
" = Bsub[i];\n";
116 boost::compute::kernel kernel = k.compile(b_unreg().queue().get_context(), options);
117 return {kernel,start_index,end_index,unit_index,upper_index};
120template <
typename MatA,
typename VecB,
class Triangular>
122 matrix_expression<MatA, gpu_tag>
const& Afull,
123 vector_expression<VecB, gpu_tag> & bfull,
127 std::size_t tileSize,
128 std::size_t numWorkers,
132 std::size_t size = end-start;
134 if(size <= tileSize){
136 kernel.kernel.set_arg(kernel.start_index, start);
137 kernel.kernel.set_arg(kernel.end_index, end);
138 kernel.kernel.set_arg(kernel.unit_index, (std::size_t)Triangular::is_unit);
139 kernel.kernel.set_arg(kernel.upper_index, (std::size_t)Triangular::is_upper);
141 std::size_t global_work_size[2] = {numWorkers,1};
142 std::size_t local_work_size[2] = {numWorkers, 1};
143 bfull().queue().enqueue_nd_range_kernel(kernel.kernel, 2,
nullptr, global_work_size, local_work_size);
146 std::size_t numBlocks = (size+tileSize-1)/tileSize;
147 std::size_t split = numBlocks/2 * tileSize;
148 auto bfront = subrange(bfull,start,start+split);
149 auto bback = subrange(bfull,start+split,end);
152 if(Triangular::is_upper){
153 auto Aur = subrange(Afull,start,start+split,start+split,end);
154 trsv_recursive(Afull, bfull, kernel, start+split,end, tileSize, numWorkers, t);
155 kernels::gemv(Aur, bback, bfront, -1.0);
156 trsv_recursive(Afull, bfull, kernel, start,start+split, tileSize, numWorkers, t);
158 auto All = subrange(Afull,start+split,end,start,start+split);
159 trsv_recursive(Afull, bfull, kernel, start,start+split, tileSize, numWorkers, t);
160 kernels::gemv(All, bfront, bback, -1.0);
161 trsv_recursive(Afull, bfull, kernel, start+split,end, tileSize, numWorkers, t);
165template <
typename MatA,
typename VecB,
class Triangular>
167 matrix_expression<MatA, gpu_tag>
const& A,
168 vector_expression<VecB, gpu_tag>& b,
172 std::size_t
const TileSize = 32;
173 std::size_t
const numWorkers = TileSize;
174 char const* options =
"-DTILE_SIZE=32ul";
175 auto kernel = bindings::createTRSVDiagBlockKernel(A,b,options);
176 trsv_recursive(A,b,kernel,0,A().size1(), TileSize, numWorkers, Triangular());
179template <
typename MatA,
typename VecB,
class Triangular>
181 matrix_expression<MatA, gpu_tag>
const& A,
182 vector_expression<VecB, gpu_tag>& b,
186 trsv_call(trans(A),b,
typename Triangular::transposed_orientation(),left());
191template <
class Triangular,
class S
ide,
typename MatA,
typename VecB>
193 matrix_expression<MatA, gpu_tag>
const& A,
194 vector_expression<VecB, gpu_tag>& b
196 REMORA_SIZE_CHECK(A().size1() == A().size2());
197 REMORA_SIZE_CHECK(A().size2() == b().size());
198 bindings::trsv_call(A,b,Triangular(), Side());