spla
cpu_m_reduce_by_row.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_BY_ROW_HPP
29 #define SPLA_CPU_M_REDUCE_BY_ROW_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 
43 namespace spla {
44 
45  template<typename T>
46  class Algo_m_reduce_by_row_cpu final : public RegistryAlgo {
47  public:
48  ~Algo_m_reduce_by_row_cpu() override = default;
49 
50  std::string get_name() override {
51  return "m_reduce_by_row";
52  }
53 
54  std::string get_description() override {
55  return "reduce matrix by row on cpu sequentially";
56  }
57 
58  Status execute(const DispatchContext& ctx) override {
59  auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce_by_row>();
60  auto M = t->M.template cast_safe<TMatrix<T>>();
61 
62  if (M->is_valid(FormatMatrix::CpuLil)) {
63  return execute_lil(ctx);
64  }
65  if (M->is_valid(FormatMatrix::CpuDok)) {
66  return execute_dok(ctx);
67  }
68 
69  return execute_dok(ctx);
70  }
71 
72  private:
73  Status execute_dok(const DispatchContext& ctx) {
74  auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce_by_row>();
75  auto r = t->r.template cast_safe<TVector<T>>();
76  auto M = t->M.template cast_safe<TMatrix<T>>();
77  auto op_reduce = t->op_reduce.template cast_safe<TOpBinary<T, T, T>>();
78  auto init = t->init.template cast_safe<TScalar<T>>();
79 
80  r->validate_wd(FormatVector::CpuDense);
81  M->validate_rw(FormatMatrix::CpuDok);
82 
83  CpuDenseVec<T>* p_dense_r = r->template get<CpuDenseVec<T>>();
84  const CpuDok<T>* p_dok_M = M->template get<CpuDok<T>>();
85 
86  std::fill(p_dense_r->Ax.begin(), p_dense_r->Ax.end(), init->get_value());
87 
88  auto& func_reduce = op_reduce->function;
89 
90  for (const auto& entry : p_dok_M->Ax) {
91  const uint i = entry.first.first;
92  const T x = entry.second;
93 
94  p_dense_r->Ax[i] = func_reduce(p_dense_r->Ax[i], x);
95  }
96 
97  return Status::Ok;
98  }
99  Status execute_lil(const DispatchContext& ctx) {
100  auto t = ctx.task.template cast_safe<ScheduleTask_m_reduce_by_row>();
101  auto r = t->r.template cast_safe<TVector<T>>();
102  auto M = t->M.template cast_safe<TMatrix<T>>();
103  auto op_reduce = t->op_reduce.template cast_safe<TOpBinary<T, T, T>>();
104  auto init = t->init.template cast_safe<TScalar<T>>();
105 
106  r->validate_wd(FormatVector::CpuDense);
107  M->validate_rw(FormatMatrix::CpuLil);
108 
109  CpuDenseVec<T>* p_dense_r = r->template get<CpuDenseVec<T>>();
110  const CpuLil<T>* p_lil_M = M->template get<CpuLil<T>>();
111 
112  std::fill(p_dense_r->Ax.begin(), p_dense_r->Ax.end(), init->get_value());
113 
114  auto& func_reduce = op_reduce->function;
115 
116  for (std::size_t i = 0; i < p_lil_M->Ar.size(); i++) {
117  for (const auto& entry : p_lil_M->Ar[i]) {
118  const T x = entry.second;
119 
120  p_dense_r->Ax[i] = func_reduce(p_dense_r->Ax[i], x);
121  }
122  }
123 
124  return Status::Ok;
125  }
126  };
127 
128 }// namespace spla
129 
130 #endif//SPLA_CPU_M_REDUCE_BY_ROW_HPP
Status of library operation execution.
Definition: cpu_m_reduce_by_row.hpp:46
std::string get_description() override
Definition: cpu_m_reduce_by_row.hpp:54
std::string get_name() override
Definition: cpu_m_reduce_by_row.hpp:50
Status execute(const DispatchContext &ctx) override
Definition: cpu_m_reduce_by_row.hpp:58
~Algo_m_reduce_by_row_cpu() override=default
CPU one-dim array for dense vector representation.
Definition: cpu_formats.hpp:74
std::vector< T > Ax
Definition: cpu_formats.hpp:80
Dictionary of keys sparse matrix format.
Definition: cpu_formats.hpp:128
Algorithm suitable to process schedule task based on task string key.
Definition: registry.hpp:66
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