spla
Loading...
Searching...
No Matches
cpu_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_CPU_M_REDUCE_HPP
29#define SPLA_CPU_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#include <core/tvector.hpp>
40
41#include <algorithm>
42
43namespace spla {
44
45 template<typename T>
46 class Algo_m_reduce_cpu final : public RegistryAlgo {
47 public:
48 ~Algo_m_reduce_cpu() override = default;
49
50 std::string get_name() override {
51 return "m_reduce";
52 }
53
54 std::string get_description() override {
55 return "reduce matrix on cpu sequentially";
56 }
57
58 Status execute(const DispatchContext& ctx) override {
59 auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce>();
60 auto M = t->M.template cast_safe<TMatrix<T>>();
61
62 if (M->is_valid(FormatMatrix::CpuCsr)) {
63 return execute_csr(ctx);
64 }
65 if (M->is_valid(FormatMatrix::CpuLil)) {
66 return execute_lil(ctx);
67 }
68 if (M->is_valid(FormatMatrix::CpuDok)) {
69 return execute_dok(ctx);
70 }
71
72 return execute_csr(ctx);
73 }
74
75 private:
76 Status execute_dok(const DispatchContext& ctx) {
77 TIME_PROFILE_SCOPE("cpu/m_reduce_dok");
78
79 auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce>();
80
81 ref_ptr<TScalar<T>> r = t->r.template cast_safe<TScalar<T>>();
82 ref_ptr<TScalar<T>> s = t->s.template cast_safe<TScalar<T>>();
83 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
84 ref_ptr<TOpBinary<T, T, T>> op_reduce = t->op_reduce.template cast_safe<TOpBinary<T, T, T>>();
85
86 M->validate_rw(FormatMatrix::CpuDok);
87
88 const CpuDok<T>* p_dok_M = M->template get<CpuDok<T>>();
89 auto& func_reduce = op_reduce->function;
90
91 T result = s->get_value();
92
93 for (const auto& entry : p_dok_M->Ax) {
94 result = func_reduce(result, entry.second);
95 }
96
97 r->get_value() = result;
98
99 return Status::Ok;
100 }
101
102 Status execute_lil(const DispatchContext& ctx) {
103 TIME_PROFILE_SCOPE("cpu/m_reduce_lil");
104
105 auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce>();
106
107 ref_ptr<TScalar<T>> r = t->r.template cast_safe<TScalar<T>>();
108 ref_ptr<TScalar<T>> s = t->s.template cast_safe<TScalar<T>>();
109 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
110 ref_ptr<TOpBinary<T, T, T>> op_reduce = t->op_reduce.template cast_safe<TOpBinary<T, T, T>>();
111
112 M->validate_rw(FormatMatrix::CpuLil);
113
114 const CpuLil<T>* p_lil_M = M->template get<CpuLil<T>>();
115 auto& func_reduce = op_reduce->function;
116
117 T result = s->get_value();
118
119 for (const auto& row : p_lil_M->Ar) {
120 for (const auto& entry : row) {
121 result = func_reduce(result, entry.second);
122 }
123 }
124
125 r->get_value() = result;
126
127 return Status::Ok;
128 }
129
130 Status execute_csr(const DispatchContext& ctx) {
131 TIME_PROFILE_SCOPE("cpu/m_reduce_csr");
132
133 auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce>();
134
135 ref_ptr<TScalar<T>> r = t->r.template cast_safe<TScalar<T>>();
136 ref_ptr<TScalar<T>> s = t->s.template cast_safe<TScalar<T>>();
137 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
138 ref_ptr<TOpBinary<T, T, T>> op_reduce = t->op_reduce.template cast_safe<TOpBinary<T, T, T>>();
139
140 M->validate_rw(FormatMatrix::CpuCsr);
141
142 const CpuCsr<T>* p_csr_M = M->template get<CpuCsr<T>>();
143 auto& func_reduce = op_reduce->function;
144
145 T result = s->get_value();
146
147 for (const auto v : p_csr_M->Ax) {
148 result = func_reduce(result, v);
149 }
150
151 r->get_value() = result;
152
153 return Status::Ok;
154 }
155 };
156
157}// namespace spla
158
159#endif//SPLA_CPU_M_REDUCE_HPP
Status of library operation execution.
Definition cpu_m_reduce.hpp:46
std::string get_description() override
Definition cpu_m_reduce.hpp:54
~Algo_m_reduce_cpu() override=default
std::string get_name() override
Definition cpu_m_reduce.hpp:50
Status execute(const DispatchContext &ctx) override
Definition cpu_m_reduce.hpp:58
Dictionary of keys sparse matrix format.
Definition cpu_formats.hpp:128
robin_hood::unordered_flat_map< Key, T, pair_hash > Ax
Definition cpu_formats.hpp:137
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
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