spla
Loading...
Searching...
No Matches
cl_v_count_mf.hpp
Go to the documentation of this file.
1/**********************************************************************************/
2/* This file is part of spla project */
3/* https://github.com/JetBrains-Research/spla */
4/**********************************************************************************/
5/* MIT License */
6/* */
7/* Copyright (c) 2023 SparseLinearAlgebra */
8/* */
9/* Permission is hereby granted, free of charge, to any person obtaining a copy */
10/* of this software and associated documentation files (the "Software"), to deal */
11/* in the Software without restriction, including without limitation the rights */
12/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13/* copies of the Software, and to permit persons to whom the Software is */
14/* furnished to do so, subject to the following conditions: */
15/* */
16/* The above copyright notice and this permission notice shall be included in all */
17/* copies or substantial portions of the Software. */
18/* */
19/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25/* SOFTWARE. */
26/**********************************************************************************/
27
28#ifndef SPLA_CL_V_COUNT_MF_HPP
29#define SPLA_CL_V_COUNT_MF_HPP
30
32
33#include <core/dispatcher.hpp>
34#include <core/registry.hpp>
35#include <core/top.hpp>
36#include <core/tscalar.hpp>
37#include <core/ttype.hpp>
38#include <core/tvector.hpp>
39
40#include <opencl/cl_counter.hpp>
41#include <opencl/cl_formats.hpp>
44
45#include <sstream>
46
47namespace spla {
48
49 template<typename T>
50 class Algo_v_count_mf_cl final : public RegistryAlgo {
51 public:
52 ~Algo_v_count_mf_cl() override = default;
53
54 std::string get_name() override {
55 return "v_count_mf";
56 }
57
58 std::string get_description() override {
59 return "parallel opencl vector count mf";
60 }
61
62 Status execute(const DispatchContext& ctx) override {
63 auto t = ctx.task.template cast_safe<ScheduleTask_v_count_mf>();
64 ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
65
66 if (v->is_valid(FormatVector::AccCoo))
67 return execute_sp(ctx);
68 if (v->is_valid(FormatVector::AccDense))
69 return execute_dn(ctx);
70
71 return execute_sp(ctx);
72 }
73
74 private:
75 Status execute_sp(const DispatchContext& ctx) {
76 auto t = ctx.task.template cast_safe<ScheduleTask_v_count_mf>();
77 ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
78 v->validate_rw(FormatVector::AccCoo);
79 CLCooVec<T>* dec_v = v->template get<CLCooVec<T>>();
80
81 t->r->set_uint(dec_v->values);
82
83 return Status::Ok;
84 }
85
86 Status execute_dn(const DispatchContext& ctx) {
87 auto t = ctx.task.template cast_safe<ScheduleTask_v_count_mf>();
88 ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
89 v->validate_rw(FormatVector::AccDense);
90 CLDenseVec<T>* dec_v = v->template get<CLDenseVec<T>>();
91
92 std::shared_ptr<CLProgram> program;
93 if (!ensure_kernel(program)) return Status::CompilationError;
94
95 auto* cl_acc = get_acc_cl();
96 auto& queue = cl_acc->get_queue_default();
97
98 CLCounterWrapper cl_count;
99 cl_count.set(queue, 0);
100
101 auto kernel = program->make_kernel("count_mf");
102 kernel.setArg(0, dec_v->Ax);
103 kernel.setArg(1, cl_count.buffer());
104 kernel.setArg(2, v->get_n_rows());
105 kernel.setArg(3, v->get_fill_value());
106
107 const uint n_groups = div_up_clamp(v->get_n_rows(), m_block_size, 1, 1024);
108
109 cl::NDRange global(m_block_size * n_groups);
110 cl::NDRange local(m_block_size);
111 queue.enqueueNDRangeKernel(kernel, cl::NullRange, global, local);
112
113 t->r->set_uint(cl_count.get(queue));
114
115 return Status::Ok;
116 }
117
118 bool ensure_kernel(std::shared_ptr<CLProgram>& program) {
119
120 m_block_size = get_acc_cl()->get_default_wgs();
121
122 CLProgramBuilder program_builder;
123 program_builder
124 .set_name("count")
125 .add_type("TYPE", get_ttype<T>().template as<Type>())
126 .set_source(source_count)
127 .acquire();
128
129 program = program_builder.get_program();
130
131 return true;
132 }
133
134 private:
135 uint m_block_size = 0;
136 };
137
138}// namespace spla
139
140#endif//SPLA_CL_V_COUNT_MF_HPP
Status of library operation execution.
Definition cl_v_count_mf.hpp:50
~Algo_v_count_mf_cl() override=default
std::string get_name() override
Definition cl_v_count_mf.hpp:54
Status execute(const DispatchContext &ctx) override
Definition cl_v_count_mf.hpp:62
std::string get_description() override
Definition cl_v_count_mf.hpp:58
OpenCL list-of-coordinates sparse vector representation.
Definition cl_formats.hpp:77
Algorithm suitable to process schedule task based on task string key.
Definition registry.hpp:66
uint values
Definition tdecoration.hpp:58
Automates reference counting and behaves as shared smart pointer.
Definition ref.hpp:117
std::uint32_t uint
Library index and size type.
Definition config.hpp:56
Definition algorithm.hpp:37
Execution context of a single task.
Definition dispatcher.hpp:46
ref_ptr< ScheduleTask > task
Definition dispatcher.hpp:48