spla
Loading...
Searching...
No Matches
cpu_m_extract_column.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_COLUMN_HPP
29#define SPLA_CPU_M_EXTRACT_COLUMN_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 <iterator>
43#include <numeric>
44
45namespace spla {
46
47 template<typename T>
49 public:
50 ~Algo_m_extract_column_cpu() override = default;
51
52 std::string get_name() override {
53 return "m_extract_column";
54 }
55
56 std::string get_description() override {
57 return "extract matrix column on cpu sequentially";
58 }
59
60 Status execute(const DispatchContext& ctx) override {
61 auto t = ctx.task.template cast_safe<ScheduleTask_m_extract_column>();
62 auto M = t->M.template cast_safe<TMatrix<T>>();
63
64 if (M->is_valid(FormatMatrix::CpuCsr)) {
65 return execute_csr(ctx);
66 }
67 if (M->is_valid(FormatMatrix::CpuLil)) {
68 return execute_lil(ctx);
69 }
70 if (M->is_valid(FormatMatrix::CpuDok)) {
71 return execute_dok(ctx);
72 }
73
74 return execute_csr(ctx);
75 }
76
77 private:
78 Status execute_dok(const DispatchContext& ctx) {
79 TIME_PROFILE_SCOPE("cpu/m_extract_column_dok");
80
81 auto t = ctx.task.template cast_safe<ScheduleTask_m_extract_column>();
82
83 ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
84 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
85 ref_ptr<TOpUnary<T, T>> op_apply = t->op_apply.template cast_safe<TOpUnary<T, T>>();
86 uint index = t->index;
87
88 r->validate_wd(FormatVector::CpuCoo);
89 M->validate_rw(FormatMatrix::CpuDok);
90
91 CpuCooVec<T>* p_coo_r = r->template get<CpuCooVec<T>>();
92 const CpuDok<T>* p_dok_M = M->template get<CpuDok<T>>();
93 auto& func_apply = op_apply->function;
94
95 for (const auto [key, value] : p_dok_M->Ax) {
96 if (key.second == index) {
97 p_coo_r->values += 1;
98 p_coo_r->Ai.push_back(key.first);
99 p_coo_r->Ax.push_back(func_apply(value));
100 }
101 }
102
103 cpu_coo_vec_sort(*p_coo_r);
104
105 return Status::Ok;
106 }
107
108 Status execute_lil(const DispatchContext& ctx) {
109 TIME_PROFILE_SCOPE("cpu/m_extract_column_lil");
110
111 auto t = ctx.task.template cast_safe<ScheduleTask_m_extract_column>();
112
113 ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
114 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
115 ref_ptr<TOpUnary<T, T>> op_apply = t->op_apply.template cast_safe<TOpUnary<T, T>>();
116 uint index = t->index;
117
118 r->validate_wd(FormatVector::CpuCoo);
119 M->validate_rw(FormatMatrix::CpuLil);
120
121 CpuCooVec<T>* p_coo_r = r->template get<CpuCooVec<T>>();
122 const CpuLil<T>* p_lil_M = M->template get<CpuLil<T>>();
123 auto& func_apply = op_apply->function;
124
125 for (uint i = 0; i < M->get_n_rows(); i++) {
126 const auto& row = p_lil_M->Ar[i];
127
128 typename CpuLil<T>::Entry fake{index, T()};
129 auto query = std::lower_bound(row.begin(), row.end(), fake, [](auto& a, auto& b) { return a.first < b.first; });
130
131 if (query != row.end() && query->first == index) {
132 p_coo_r->values += 1;
133 p_coo_r->Ai.push_back(i);
134 p_coo_r->Ax.push_back(func_apply(query->second));
135 }
136 }
137
138 return Status::Ok;
139 }
140
141 Status execute_csr(const DispatchContext& ctx) {
142 TIME_PROFILE_SCOPE("cpu/m_extract_column_csr");
143
144 auto t = ctx.task.template cast_safe<ScheduleTask_m_extract_column>();
145
146 ref_ptr<TVector<T>> r = t->r.template cast_safe<TVector<T>>();
147 ref_ptr<TMatrix<T>> M = t->M.template cast_safe<TMatrix<T>>();
148 ref_ptr<TOpUnary<T, T>> op_apply = t->op_apply.template cast_safe<TOpUnary<T, T>>();
149 uint index = t->index;
150
151 r->validate_wd(FormatVector::CpuCoo);
152 M->validate_rw(FormatMatrix::CpuCsr);
153
154 CpuCooVec<T>* p_coo_r = r->template get<CpuCooVec<T>>();
155 const CpuCsr<T>* p_csr_M = M->template get<CpuCsr<T>>();
156 auto& func_apply = op_apply->function;
157
158 for (uint i = 0; i < M->get_n_rows(); i++) {
159 const auto row_begin = p_csr_M->Aj.begin() + p_csr_M->Ap[i];
160 const auto row_end = p_csr_M->Aj.begin() + p_csr_M->Ap[i + 1];
161
162 auto query = std::lower_bound(row_begin, row_end, index);
163
164 if (query != row_end && *query == index) {
165 p_coo_r->values += 1;
166 p_coo_r->Ai.push_back(i);
167 p_coo_r->Ax.push_back(func_apply(p_csr_M->Ax[std::distance(p_csr_M->Aj.begin(), query)]));
168 }
169 }
170
171 return Status::Ok;
172 }
173 };
174
175}// namespace spla
176
177#endif//SPLA_CPU_M_EXTRACT_COLUMN_HPP
Status of library operation execution.
Definition cpu_m_extract_column.hpp:48
std::string get_description() override
Definition cpu_m_extract_column.hpp:56
~Algo_m_extract_column_cpu() override=default
Status execute(const DispatchContext &ctx) override
Definition cpu_m_extract_column.hpp:60
std::string get_name() override
Definition cpu_m_extract_column.hpp:52
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
std::pair< uint, T > Entry
Definition cpu_formats.hpp:113
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