spla
Loading...
Searching...
No Matches
cl_v_reduce.hpp
Go to the documentation of this file.
1/**********************************************************************************/
2/* This file is part of spla project */
3/* https://github.com/SparseLinearAlgebra/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_REDUCE_HPP
29#define SPLA_CL_V_REDUCE_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_formats.hpp>
41#include <opencl/cl_reduce.hpp>
42
43#include <sstream>
44
45namespace spla {
46
47 template<typename T>
48 class Algo_v_reduce_cl final : public RegistryAlgo {
49 public:
50 ~Algo_v_reduce_cl() override = default;
51
52 std::string get_name() override {
53 return "v_reduce";
54 }
55
56 std::string get_description() override {
57 return "parallel vector reduction on opencl device";
58 }
59
60 Status execute(const DispatchContext& ctx) override {
61 auto t = ctx.task.template cast_safe<ScheduleTask_v_reduce>();
62 ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
63
64 if (v->is_valid(FormatVector::AccCoo)) {
65 return execute_sp(ctx);
66 }
67 if (v->is_valid(FormatVector::AccDense)) {
68 return execute_dn(ctx);
69 }
70 if (v->is_valid(FormatVector::CpuCoo)) {
71 return execute_sp(ctx);
72 }
73 if (v->is_valid(FormatVector::CpuDense)) {
74 return execute_dn(ctx);
75 }
76
77 return execute_sp(ctx);
78 }
79
80 private:
81 Status execute_dn(const DispatchContext& ctx) {
82 TIME_PROFILE_SCOPE("opencl/vector_reduce_dense");
83
84 auto t = ctx.task.template cast_safe<ScheduleTask_v_reduce>();
85
86 auto r = t->r.template cast_safe<TScalar<T>>();
87 auto s = t->s.template cast_safe<TScalar<T>>();
88 auto v = t->v.template cast_safe<TVector<T>>();
89 auto op_reduce = t->op_reduce.template cast_safe<TOpBinary<T, T, T>>();
90
91 v->validate_rw(FormatVector::AccDense);
92
93 const auto* p_cl_dense_vec = v->template get<CLDenseVec<T>>();
94 auto* p_cl_acc = get_acc_cl();
95 auto& queue = p_cl_acc->get_queue_default();
96
97 cl_reduce<T>(queue, p_cl_dense_vec->Ax, v->get_n_rows(), s->get_value(), op_reduce, r->get_value());
98
99 return Status::Ok;
100 }
101
102 Status execute_sp(const DispatchContext& ctx) {
103 TIME_PROFILE_SCOPE("opencl/vector_reduce_sparse");
104
105 auto t = ctx.task.template cast_safe<ScheduleTask_v_reduce>();
106
107 auto r = t->r.template cast_safe<TScalar<T>>();
108 auto s = t->s.template cast_safe<TScalar<T>>();
109 auto v = t->v.template cast_safe<TVector<T>>();
110 auto op_reduce = t->op_reduce.template cast_safe<TOpBinary<T, T, T>>();
111
112 v->validate_rw(FormatVector::AccCoo);
113
114 const auto* p_cl_coo_vec = v->template get<CLCooVec<T>>();
115 auto* p_cl_acc = get_acc_cl();
116 auto& queue = p_cl_acc->get_queue_default();
117
118 cl_reduce<T>(queue, p_cl_coo_vec->Ax, p_cl_coo_vec->values, s->get_value(), op_reduce, r->get_value());
119
120 return Status::Ok;
121 }
122 };
123
124}// namespace spla
125
126#endif//SPLA_CL_V_REDUCE_HPP
Status of library operation execution.
Definition cl_v_reduce.hpp:48
std::string get_description() override
Definition cl_v_reduce.hpp:56
Status execute(const DispatchContext &ctx) override
Definition cl_v_reduce.hpp:60
~Algo_v_reduce_cl() override=default
std::string get_name() override
Definition cl_v_reduce.hpp:52
Algorithm suitable to process schedule task based on task string key.
Definition registry.hpp:66
Automates reference counting and behaves as shared smart pointer.
Definition ref.hpp:117
Definition algorithm.hpp:37
void cl_reduce(cl::CommandQueue &queue, const cl::Buffer &values, uint n, T init, const ref_ptr< TOpBinary< T, T, T > > &op_reduce, T &result)
Definition cl_reduce.hpp:38
Execution context of a single task.
Definition dispatcher.hpp:46
ref_ptr< ScheduleTask > task
Definition dispatcher.hpp:48
#define TIME_PROFILE_SCOPE(name)
Definition time_profiler.hpp:92