spla
cpu_mxv.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_CPU_MXV_HPP
29 #define SPLA_CPU_MXV_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 namespace spla {
42 
43  template<typename T>
44  class Algo_mxv_masked_cpu final : public RegistryAlgo {
45  public:
46  ~Algo_mxv_masked_cpu() override = default;
47 
48  std::string get_name() override {
49  return "mxv_masked";
50  }
51 
52  std::string get_description() override {
53  return "sequential masked matrix-vector product on cpu";
54  }
55 
56  Status execute(const DispatchContext& ctx) override {
57  TIME_PROFILE_SCOPE("cpu/mxv");
58 
59  auto t = ctx.task.template cast_safe<ScheduleTask_mxv_masked>();
60 
61  auto r = t->r.template cast_safe<TVector<T>>();
62  auto mask = t->mask.template cast_safe<TVector<T>>();
63  auto M = t->M.template cast_safe<TMatrix<T>>();
64  auto v = t->v.template cast_safe<TVector<T>>();
65  auto op_multiply = t->op_multiply.template cast_safe<TOpBinary<T, T, T>>();
66  auto op_add = t->op_add.template cast_safe<TOpBinary<T, T, T>>();
67  auto op_select = t->op_select.template cast_safe<TOpSelect<T>>();
68  auto init = t->init.template cast_safe<TScalar<T>>();
69 
70  const uint DM = M->get_n_rows();
71  const T sum_init = init->get_value();
72 
73  r->validate_wd(FormatVector::CpuDense);
74  mask->validate_rw(FormatVector::CpuDense);
75  v->validate_rw(FormatVector::CpuDense);
76  M->validate_rw(FormatMatrix::CpuLil);
77 
78  CpuDenseVec<T>* p_dense_r = r->template get<CpuDenseVec<T>>();
79  const CpuDenseVec<T>* p_dense_mask = mask->template get<CpuDenseVec<T>>();
80  const CpuDenseVec<T>* p_dense_v = v->template get<CpuDenseVec<T>>();
81  const CpuLil<T>* p_lil_M = M->template get<CpuLil<T>>();
82  auto early_exit = t->get_desc_or_default()->get_early_exit();
83 
84  auto& func_multiply = op_multiply->function;
85  auto& func_add = op_add->function;
86  auto& func_select = op_select->function;
87 
88  for (uint i = 0; i < DM; ++i) {
89  T sum = sum_init;
90 
91  if (func_select(p_dense_mask->Ax[i])) {
92  const auto& row = p_lil_M->Ar[i];
93 
94  for (const auto& j_x : row) {
95  const uint j = j_x.first;
96  sum = func_add(sum, func_multiply(j_x.second, p_dense_v->Ax[j]));
97 
98  if ((sum != sum_init) && early_exit) break;
99  }
100  }
101 
102  p_dense_r->Ax[i] = sum;
103  }
104 
105  return Status::Ok;
106  }
107  };
108 
109 }// namespace spla
110 
111 #endif//SPLA_CPU_MXV_HPP
Status of library operation execution.
Definition: cpu_mxv.hpp:44
std::string get_description() override
Definition: cpu_mxv.hpp:52
std::string get_name() override
Definition: cpu_mxv.hpp:48
Status execute(const DispatchContext &ctx) override
Definition: cpu_mxv.hpp:56
~Algo_mxv_masked_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
CPU list-of-list matrix format for fast incremental build.
Definition: cpu_formats.hpp:107
std::vector< Row > Ar
Definition: cpu_formats.hpp:117
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
#define TIME_PROFILE_SCOPE(name)
Definition: time_profiler.hpp:92