spla
Loading...
Searching...
No Matches
cl_m_reduce.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_M_REDUCE_HPP
29#define SPLA_CL_M_REDUCE_HPP
30
32
33#include <core/dispatcher.hpp>
34#include <core/registry.hpp>
35#include <core/tmatrix.hpp>
36#include <core/top.hpp>
37#include <core/tscalar.hpp>
38#include <core/ttype.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_m_reduce_cl final : public RegistryAlgo {
49 public:
50 ~Algo_m_reduce_cl() override = default;
51
52 std::string get_name() override {
53 return "m_reduce";
54 }
55
56 std::string get_description() override {
57 return "parallel matrix reduction on opencl device";
58 }
59
60 Status execute(const DispatchContext& ctx) override {
61 auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce>();
62 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
63
64 if (M->is_valid(FormatMatrix::AccCsr)) {
65 return execute_csr(ctx);
66 }
67 if (M->is_valid(FormatMatrix::CpuCsr)) {
68 return execute_csr(ctx);
69 }
70
71 return execute_csr(ctx);
72 }
73
74 private:
75 Status execute_csr(const DispatchContext& ctx) {
76 TIME_PROFILE_SCOPE("opencl/m_reduce_csr");
77
78 auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce>();
79
80 auto r = t->r.template cast_safe<TScalar<T>>();
81 auto s = t->s.template cast_safe<TScalar<T>>();
82 auto M = t->M.template cast_safe<TMatrix<T>>();
83 auto op_reduce = t->op_reduce.template cast_safe<TOpBinary<T, T, T>>();
84
85 M->validate_rw(FormatMatrix::AccCsr);
86
87 const auto* p_cl_csr = M->template get<CLCsr<T>>();
88 auto* p_cl_acc = get_acc_cl();
89 auto& queue = p_cl_acc->get_queue_default();
90
91 cl_reduce<T>(queue, p_cl_csr->Ax, p_cl_csr->values, s->get_value(), op_reduce, r->get_value());
92
93 return Status::Ok;
94 }
95 };
96
97}// namespace spla
98
99#endif//SPLA_CL_M_REDUCE_HPP
Status of library operation execution.
Definition cl_m_reduce.hpp:48
std::string get_description() override
Definition cl_m_reduce.hpp:56
~Algo_m_reduce_cl() override=default
std::string get_name() override
Definition cl_m_reduce.hpp:52
Status execute(const DispatchContext &ctx) override
Definition cl_m_reduce.hpp:60
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