spla
cpu_mxm.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_MXM_HPP
29 #define SPLA_CPU_MXM_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_mxm_cpu final : public RegistryAlgo {
47  public:
48  ~Algo_mxm_cpu() override = default;
49 
50  std::string get_name() override {
51  return "mxm";
52  }
53 
54  std::string get_description() override {
55  return "sequential sparse matrix sparse matrix product on cpu";
56  }
57 
58  Status execute(const DispatchContext& ctx) override {
59  TIME_PROFILE_SCOPE("cpu/mxm");
60 
61  auto t = ctx.task.template cast_safe<ScheduleTask_mxm>();
62 
63  auto R = t->R.template cast_safe<TMatrix<T>>();
64  auto A = t->A.template cast_safe<TMatrix<T>>();
65  auto B = t->B.template cast_safe<TMatrix<T>>();
66  auto op_multiply = t->op_multiply.template cast_safe<TOpBinary<T, T, T>>();
67  auto op_add = t->op_add.template cast_safe<TOpBinary<T, T, T>>();
68  auto init = t->init.template cast_safe<TScalar<T>>();
69 
70  R->validate_wd(FormatMatrix::CpuLil);
71  A->validate_rw(FormatMatrix::CpuLil);
72  B->validate_rw(FormatMatrix::CpuLil);
73 
74  CpuLil<T>* p_lil_R = R->template get<CpuLil<T>>();
75  const CpuLil<T>* p_lil_A = A->template get<CpuLil<T>>();
76  const CpuLil<T>* p_lil_B = B->template get<CpuLil<T>>();
77 
78  auto& func_multiply = op_multiply->function;
79  auto& func_add = op_add->function;
80 
81  auto DM = R->get_n_rows();
82  auto DN = R->get_n_cols();
83  auto I = init->get_value();
84 
85  std::vector<T> R_tmp(DN, I);
86 
87  for (uint row_R = 0; row_R < DM; row_R++) {
88  const auto& A_lst = p_lil_A->Ar[row_R];
89  auto& R_lst = p_lil_R->Ar[row_R];
90 
91  assert(R_lst.empty());
92 
93  std::fill(R_tmp.begin(), R_tmp.end(), I);
94 
95  for (const typename CpuLil<T>::Entry& entry_A : A_lst) {
96  const uint i = entry_A.first;
97  const T value_A = entry_A.second;
98 
99  const auto& B_lst = p_lil_B->Ar[i];
100 
101  for (const typename CpuLil<T>::Entry& entry_B : B_lst) {
102  const uint j = entry_B.first;
103  const T value_B = entry_B.second;
104 
105  R_tmp[j] = func_add(R_tmp[j], func_multiply(value_A, value_B));
106  }
107  }
108 
109  for (uint col_R = 0; col_R < DN; col_R++) {
110  if (R_tmp[col_R] != I) {
111  R_lst.emplace_back(col_R, R_tmp[col_R]);
112  }
113  }
114  }
115 
116  return Status::Ok;
117  }
118  };
119 
120 }// namespace spla
121 
122 #endif//SPLA_CPU_MXM_HPP
Status of library operation execution.
Definition: cpu_mxm.hpp:46
~Algo_mxm_cpu() override=default
std::string get_name() override
Definition: cpu_mxm.hpp:50
Status execute(const DispatchContext &ctx) override
Definition: cpu_mxm.hpp:58
std::string get_description() override
Definition: cpu_mxm.hpp:54
CPU list-of-list matrix format for fast incremental build.
Definition: cpu_formats.hpp:107
std::pair< uint, T > Entry
Definition: cpu_formats.hpp:113
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