spla
cpu_kron.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_KRON_HPP
29 #define SPLA_CPU_KRON_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_kron_cpu final : public RegistryAlgo {
45  public:
46  ~Algo_kron_cpu() override = default;
47 
48  std::string get_name() override {
49  return "kron";
50  }
51 
52  std::string get_description() override {
53  return "sequential sparse matrix kronecker product on cpu";
54  }
55 
56  Status execute(const DispatchContext& ctx) override {
57  TIME_PROFILE_SCOPE("cpu/kron");
58 
59  auto t = ctx.task.template cast_safe<ScheduleTask_kron>();
60 
61  auto R = t->R.template cast_safe<TMatrix<T>>();
62  auto A = t->A.template cast_safe<TMatrix<T>>();
63  auto B = t->B.template cast_safe<TMatrix<T>>();
64  auto op_multiply = t->op_multiply.template cast_safe<TOpBinary<T, T, T>>();
65 
66  R->validate_wd(FormatMatrix::CpuLil);
67  A->validate_rw(FormatMatrix::CpuLil);
68  B->validate_rw(FormatMatrix::CpuLil);
69 
70  CpuLil<T>* p_lil_R = R->template get<CpuLil<T>>();
71  const CpuLil<T>* p_lil_A = A->template get<CpuLil<T>>();
72  const CpuLil<T>* p_lil_B = B->template get<CpuLil<T>>();
73 
74  auto& func_multiply = op_multiply->function;
75 
76  auto D_AM = A->get_n_rows();
77  auto D_AN = A->get_n_cols();
78  auto D_BM = B->get_n_rows();
79  auto D_BN = B->get_n_cols();
80 
81  assert(R->get_n_rows() == D_AM * D_BM);
82  assert(R->get_n_cols() == D_AN * D_BN);
83 
84  assert(p_lil_R->Ar.size() == D_AM * D_BM);
85 
86  for (uint i_A = 0; i_A < D_AM; i_A++) {
87  const auto& row_A = p_lil_A->Ar[i_A];
88 
89  for (const auto [j_A, x_A] : row_A) {
90 
91  for (uint i_B = 0; i_B < D_BM; i_B++) {
92  const auto& row_B = p_lil_B->Ar[i_B];
93 
94  auto& row_R = p_lil_R->Ar[i_A * D_BM + i_B];
95  row_R.reserve(row_A.size() * row_B.size());
96 
97  for (const auto [j_B, x_B] : row_B) {
98  row_R.emplace_back(j_A * D_BN + j_B, func_multiply(x_A, x_B));
99  }
100  }
101  }
102  }
103 
104  p_lil_R->values = p_lil_A->values * p_lil_B->values;
105 
106  return Status::Ok;
107  }
108  };
109 
110 }// namespace spla
111 
112 #endif//SPLA_CPU_KRON_HPP
Status of library operation execution.
Definition: cpu_kron.hpp:44
std::string get_name() override
Definition: cpu_kron.hpp:48
Status execute(const DispatchContext &ctx) override
Definition: cpu_kron.hpp:56
std::string get_description() override
Definition: cpu_kron.hpp:52
~Algo_kron_cpu() override=default
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
uint values
Definition: tdecoration.hpp:58
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