spla
Loading...
Searching...
No Matches
cpu_m_extract_row.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_M_EXTRACT_ROW_HPP
29#define SPLA_CPU_M_EXTRACT_ROW_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#include <numeric>
43
44namespace spla {
45
46 template<typename T>
47 class Algo_m_extract_row_cpu final : public RegistryAlgo {
48 public:
49 ~Algo_m_extract_row_cpu() override = default;
50
51 std::string get_name() override {
52 return "m_extract_row";
53 }
54
55 std::string get_description() override {
56 return "extract matrix row on cpu sequentially";
57 }
58
59 Status execute(const DispatchContext& ctx) override {
60 auto t = ctx.task.template cast_safe<ScheduleTask_m_extract_row>();
61 auto M = t->M.template cast_safe<TMatrix<T>>();
62
63 if (M->is_valid(FormatMatrix::CpuCsr)) {
64 return execute_csr(ctx);
65 }
66 if (M->is_valid(FormatMatrix::CpuLil)) {
67 return execute_lil(ctx);
68 }
69 if (M->is_valid(FormatMatrix::CpuDok)) {
70 return execute_dok(ctx);
71 }
72
73 return execute_csr(ctx);
74 }
75
76 private:
77 Status execute_dok(const DispatchContext& ctx) {
78 TIME_PROFILE_SCOPE("cpu/m_extract_row_dok");
79
80 auto t = ctx.task.template cast_safe<ScheduleTask_m_extract_row>();
81
82 ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
83 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
84 ref_ptr<TOpUnary<T, T>> op_apply = t->op_apply.template cast_safe<TOpUnary<T, T>>();
85 uint index = t->index;
86
87 r->validate_wd(FormatVector::CpuCoo);
88 M->validate_rw(FormatMatrix::CpuDok);
89
90 CpuCooVec<T>* p_coo_r = r->template get<CpuCooVec<T>>();
91 const CpuDok<T>* p_dok_M = M->template get<CpuDok<T>>();
92 auto& func_apply = op_apply->function;
93
94 for (const auto [key, value] : p_dok_M->Ax) {
95 if (key.first == index) {
96 p_coo_r->values += 1;
97 p_coo_r->Ai.push_back(key.second);
98 p_coo_r->Ax.push_back(func_apply(value));
99 }
100 }
101
102 cpu_coo_vec_sort(*p_coo_r);
103
104 return Status::Ok;
105 }
106
107 Status execute_lil(const DispatchContext& ctx) {
108 TIME_PROFILE_SCOPE("cpu/m_extract_row_lil");
109
110 auto t = ctx.task.template cast_safe<ScheduleTask_m_extract_row>();
111
112 ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
113 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
114 ref_ptr<TOpUnary<T, T>> op_apply = t->op_apply.template cast_safe<TOpUnary<T, T>>();
115 uint index = t->index;
116
117 r->validate_wd(FormatVector::CpuCoo);
118 M->validate_rw(FormatMatrix::CpuLil);
119
120 CpuCooVec<T>* p_coo_r = r->template get<CpuCooVec<T>>();
121 const CpuLil<T>* p_lil_M = M->template get<CpuLil<T>>();
122 auto& func_apply = op_apply->function;
123
124 assert(index < M->get_n_rows());
125
126 p_coo_r->Ai.reserve(p_lil_M->Ar[index].size());
127 p_coo_r->Ax.reserve(p_lil_M->Ar[index].size());
128
129 for (const auto [key, value] : p_lil_M->Ar[index]) {
130 p_coo_r->values += 1;
131 p_coo_r->Ai.push_back(key);
132 p_coo_r->Ax.push_back(func_apply(value));
133 }
134
135 return Status::Ok;
136 }
137
138 Status execute_csr(const DispatchContext& ctx) {
139 TIME_PROFILE_SCOPE("cpu/m_extract_row_csr");
140
141 auto t = ctx.task.template cast_safe<ScheduleTask_m_extract_row>();
142
143 ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
144 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
145 ref_ptr<TOpUnary<T, T>> op_apply = t->op_apply.template cast_safe<TOpUnary<T, T>>();
146 uint index = t->index;
147
148 r->validate_wd(FormatVector::CpuCoo);
149 M->validate_rw(FormatMatrix::CpuCsr);
150
151 CpuCooVec<T>* p_coo_r = r->template get<CpuCooVec<T>>();
152 const CpuCsr<T>* p_csr_M = M->template get<CpuCsr<T>>();
153 auto& func_apply = op_apply->function;
154
155 assert(index < M->get_n_rows());
156
157 const uint start = p_csr_M->Ap[index];
158 const uint end = p_csr_M->Ap[index + 1];
159 const uint count = end - start;
160
161 p_coo_r->Ai.reserve(count);
162 p_coo_r->Ax.reserve(count);
163
164 for (uint k = start; k < end; k++) {
165 p_coo_r->values += 1;
166 p_coo_r->Ai.push_back(p_csr_M->Aj[k]);
167 p_coo_r->Ax.push_back(func_apply(p_csr_M->Ax[k]));
168 }
169
170 return Status::Ok;
171 }
172 };
173
174}// namespace spla
175
176#endif//SPLA_CPU_M_EXTRACT_ROW_HPP
Status of library operation execution.
Definition cpu_m_extract_row.hpp:47
~Algo_m_extract_row_cpu() override=default
std::string get_name() override
Definition cpu_m_extract_row.hpp:51
std::string get_description() override
Definition cpu_m_extract_row.hpp:55
Status execute(const DispatchContext &ctx) override
Definition cpu_m_extract_row.hpp:59
CPU list-of-coordinates sparse vector representation.
Definition cpu_formats.hpp:90
std::vector< uint > Ai
Definition cpu_formats.hpp:96
std::vector< T > Ax
Definition cpu_formats.hpp:97
Dictionary of keys sparse matrix format.
Definition cpu_formats.hpp:128
Algorithm suitable to process schedule task based on task string key.
Definition registry.hpp:66
uint values
Definition tdecoration.hpp:58
Automates reference counting and behaves as shared smart pointer.
Definition ref.hpp:117
void cpu_coo_vec_sort(CpuCooVec< T > &vec)
Definition cpu_format_coo_vec.hpp:44
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