spla
Loading...
Searching...
No Matches
cpu_mxmT_masked.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_MXMT_MASKED_HPP
29#define SPLA_CPU_MXMT_MASKED_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
41namespace spla {
42
43 template<typename T>
44 class Algo_mxmT_masked_cpu final : public RegistryAlgo {
45 public:
46 ~Algo_mxmT_masked_cpu() override = default;
47
48 std::string get_name() override {
49 return "mxmT_masked";
50 }
51
52 std::string get_description() override {
53 return "sequential masked matrix matrix-transposed product on cpu";
54 }
55
56 Status execute(const DispatchContext& ctx) override {
57 TIME_PROFILE_SCOPE("cpu/mxmT_masked");
58
59 auto t = ctx.task.template cast_safe<ScheduleTask_mxmT_masked>();
60
61 auto R = t->R.template cast_safe<TMatrix<T>>();
62 auto mask = t->mask.template cast_safe<TMatrix<T>>();
63 auto A = t->A.template cast_safe<TMatrix<T>>();
64 auto B = t->B.template cast_safe<TMatrix<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 R->validate_wd(FormatMatrix::CpuLil);
71 A->validate_rw(FormatMatrix::CpuLil);
72 B->validate_rw(FormatMatrix::CpuLil);
73 mask->validate_rw(FormatMatrix::CpuLil);
74
75 CpuLil<T>* p_lil_R = R->template get<CpuLil<T>>();
76 const CpuLil<T>* p_lil_A = A->template get<CpuLil<T>>();
77 const CpuLil<T>* p_lil_B = B->template get<CpuLil<T>>();
78 const CpuLil<T>* p_lil_mask = mask->template get<CpuLil<T>>();
79
80 auto& func_multiply = op_multiply->function;
81 auto& func_add = op_add->function;
82 auto& func_select = op_select->function;
83
84 auto DM = R->get_n_rows();
85 auto I = init->get_value();
86
87 for (uint row_R = 0; row_R < DM; row_R++) {
88 const auto& mask_lst = p_lil_mask->Ar[row_R];
89 const auto& A_lst = p_lil_A->Ar[row_R];
90 auto& R_lst = p_lil_R->Ar[row_R];
91
92 assert(R_lst.empty());
93
94 for (const typename CpuLil<T>::Entry& entry_mask : mask_lst) {
95 const uint mask_i = entry_mask.first;
96 const T mask_x = entry_mask.second;
97
98 T r = I;
99
100 if (func_select(mask_x)) {
101 const auto& B_lst = p_lil_B->Ar[mask_i];
102
103 auto A_it = A_lst.begin();
104 auto B_it = B_lst.begin();
105 const auto A_end = A_lst.end();
106 const auto B_end = B_lst.end();
107
108 while (A_it != A_end && B_it != B_end) {
109 if (A_it->first == B_it->first) {
110 r = func_add(r, func_multiply(A_it->second, B_it->second));
111 ++A_it;
112 ++B_it;
113 } else if (A_it->first < B_it->first) {
114 ++A_it;
115 } else {
116 ++B_it;
117 }
118 }
119 }
120
121 if (r != I) {
122 R_lst.emplace_back(mask_i, r);
123 }
124 }
125 }
126
127 return Status::Ok;
128 }
129 };
130
131}// namespace spla
132
133#endif//SPLA_CPU_MXMT_MASKED_HPP
Status of library operation execution.
Definition cpu_mxmT_masked.hpp:44
~Algo_mxmT_masked_cpu() override=default
std::string get_name() override
Definition cpu_mxmT_masked.hpp:48
std::string get_description() override
Definition cpu_mxmT_masked.hpp:52
Status execute(const DispatchContext &ctx) override
Definition cpu_mxmT_masked.hpp:56
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