spla
cpu_v_eadd_fdb.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_V_EADD_FDB_HPP
29 #define SPLA_CPU_V_EADD_FDB_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 namespace spla {
41 
42  template<typename T>
43  class Algo_v_eadd_fdb_cpu final : public RegistryAlgo {
44  public:
45  ~Algo_v_eadd_fdb_cpu() override = default;
46 
47  std::string get_name() override {
48  return "v_eadd_fdb";
49  }
50 
51  std::string get_description() override {
52  return "sequential element-wise add vector operation";
53  }
54 
55  Status execute(const DispatchContext& ctx) override {
56  auto t = ctx.task.template cast_safe<ScheduleTask_v_eadd_fdb>();
57  ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
58  ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
59 
60  if (r->is_valid(FormatVector::CpuDense) && v->is_valid(FormatVector::CpuCoo)) {
61  return execute_sp2dn(ctx);
62  }
63  if (r->is_valid(FormatVector::CpuDense) && v->is_valid(FormatVector::CpuDense)) {
64  return execute_dn2dn(ctx);
65  }
66 
67  return execute_sp2dn(ctx);
68  }
69 
70  private:
71  Status execute_sp2dn(const DispatchContext& ctx) {
72  TIME_PROFILE_SCOPE("cpu/vector_eadd_fdb_sp2dn");
73 
74  auto t = ctx.task.template cast_safe<ScheduleTask_v_eadd_fdb>();
75  ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
76  ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
77  ref_ptr<TVector<T>> fdb = t->fdb.template cast_safe<TVector<T>>();
78  ref_ptr<TOpBinary<T, T, T>> op = t->op.template cast_safe<TOpBinary<T, T, T>>();
79 
80  r->validate_rwd(FormatVector::CpuDense);
81  v->validate_rw(FormatVector::CpuCoo);
82  fdb->validate_wd(FormatVector::CpuCoo);
83 
84  auto* p_r = r->template get<CpuDenseVec<T>>();
85  const auto* p_v = v->template get<CpuCooVec<T>>();
86  auto* p_fdb = fdb->template get<CpuCooVec<T>>();
87  const auto& function = op->function;
88 
89  assert(p_fdb->values == 0);
90 
91  for (uint k = 0; k < p_v->values; k++) {
92  uint i = p_v->Ai[k];
93  T prev = p_r->Ax[i];
94  p_r->Ax[i] = function(prev, p_v->Ax[k]);
95 
96  if (prev != p_r->Ax[i]) {
97  p_fdb->values += 1;
98  p_fdb->Ai.push_back(i);
99  p_fdb->Ax.push_back(p_r->Ax[i]);
100  }
101  }
102 
103  return Status::Ok;
104  }
105 
106  Status execute_dn2dn(const DispatchContext& ctx) {
107  TIME_PROFILE_SCOPE("cpu/vector_eadd_fdb_dn2dn");
108 
109  auto t = ctx.task.template cast_safe<ScheduleTask_v_eadd_fdb>();
110  ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
111  ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
112  ref_ptr<TVector<T>> fdb = t->fdb.template cast_safe<TVector<T>>();
113  ref_ptr<TOpBinary<T, T, T>> op = t->op.template cast_safe<TOpBinary<T, T, T>>();
114 
115  r->validate_rwd(FormatVector::CpuDense);
116  v->validate_rw(FormatVector::CpuDense);
117  fdb->validate_wd(FormatVector::CpuDense);
118 
119  auto* p_r = r->template get<CpuDenseVec<T>>();
120  const auto* p_v = v->template get<CpuDenseVec<T>>();
121  auto* p_fdb = fdb->template get<CpuDenseVec<T>>();
122  const auto& function = op->function;
123 
124  const uint N = v->get_n_rows();
125  const T fill_value = fdb->get_fill_value();
126 
127  cpu_dense_vec_fill(fdb->get_fill_value(), *p_fdb);
128 
129  for (uint i = 0; i < N; i++) {
130  T prev = p_r->Ax[i];
131  p_r->Ax[i] = function(prev, p_v->Ax[i]);
132 
133  if (prev != p_r->Ax[i]) {
134  p_fdb->Ax[i] = p_r->Ax[i];
135  }
136  }
137 
138  return Status::Ok;
139  }
140  };
141 
142 }// namespace spla
143 
144 #endif//SPLA_CPU_V_EADD_FDB_HPP
Status of library operation execution.
Definition: cpu_v_eadd_fdb.hpp:43
std::string get_description() override
Definition: cpu_v_eadd_fdb.hpp:51
Status execute(const DispatchContext &ctx) override
Definition: cpu_v_eadd_fdb.hpp:55
~Algo_v_eadd_fdb_cpu() override=default
std::string get_name() override
Definition: cpu_v_eadd_fdb.hpp:47
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
void cpu_dense_vec_fill(const T fill_value, CpuDenseVec< T > &vec)
Definition: cpu_format_dense_vec.hpp:48
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
#define TIME_PROFILE_SCOPE(name)
Definition: time_profiler.hpp:92