spla
Loading...
Searching...
No Matches
cpu_v_assign.hpp
Go to the documentation of this file.
1/**********************************************************************************/
2/* This file is part of spla project */
3/* https://github.com/SparseLinearAlgebra/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_V_ASSIGN_HPP
29#define SPLA_CPU_V_ASSIGN_HPP
30
32
33#include <core/dispatcher.hpp>
34#include <core/registry.hpp>
35#include <core/top.hpp>
36#include <core/tscalar.hpp>
37#include <core/ttype.hpp>
38#include <core/tvector.hpp>
39
40namespace spla {
41
42 template<typename T>
44 public:
45 ~Algo_v_assign_masked_cpu() override = default;
46
47 std::string get_name() override {
48 return "v_assign_masked";
49 }
50
51 std::string get_description() override {
52 return "sequential masked vector assignment";
53 }
54
55 Status execute(const DispatchContext& ctx) override {
56 auto t = ctx.task.template cast_safe<ScheduleTask_v_assign_masked>();
57 ref_ptr<TVector<T>> mask = t->mask.template cast_safe<TVector<T>>();
58
59 if (mask->is_valid(FormatVector::CpuCoo))
60 return execute_sp2dn(ctx);
61 if (mask->is_valid(FormatVector::CpuDense))
62 return execute_dn2dn(ctx);
63
64 return execute_sp2dn(ctx);
65 }
66
67 private:
68 Status execute_sp2dn(const DispatchContext& ctx) {
69 TIME_PROFILE_SCOPE("cpu/vector_assign_sparse2dense");
70
71 auto t = ctx.task.template cast_safe<ScheduleTask_v_assign_masked>();
72
73 auto r = t->r.template cast_safe<TVector<T>>();
74 auto mask = t->mask.template cast_safe<TVector<T>>();
75 auto value = t->value.template cast_safe<TScalar<T>>();
76 auto op_assign = t->op_assign.template cast_safe<TOpBinary<T, T, T>>();
77 auto op_select = t->op_select.template cast_safe<TOpSelect<T>>();
78
79 auto assign_value = value->get_value();
80
81 r->validate_rwd(FormatVector::CpuDense);
82 mask->validate_rw(FormatVector::CpuCoo);
83
84 auto* p_r_dense = r->template get<CpuDenseVec<T>>();
85 const auto* p_mask_sparse = mask->template get<CpuCooVec<T>>();
86 const auto& func_assign = op_assign->function;
87 const auto& func_select = op_select->function;
88
89 uint N = p_mask_sparse->values;
90
91 for (uint idx = 0; idx < N; ++idx) {
92 uint i = p_mask_sparse->Ai[idx];
93 auto x = p_mask_sparse->Ax[idx];
94 if (func_select(x)) {
95 p_r_dense->Ax[i] = func_assign(p_r_dense->Ax[i], assign_value);
96 }
97 }
98
99 return Status::Ok;
100 }
101
102 Status execute_dn2dn(const DispatchContext& ctx) {
103 TIME_PROFILE_SCOPE("cpu/vector_assign_dense2dense");
104
105 auto t = ctx.task.template cast_safe<ScheduleTask_v_assign_masked>();
106
107 auto r = t->r.template cast_safe<TVector<T>>();
108 auto mask = t->mask.template cast_safe<TVector<T>>();
109 auto value = t->value.template cast_safe<TScalar<T>>();
110 auto op_assign = t->op_assign.template cast_safe<TOpBinary<T, T, T>>();
111 auto op_select = t->op_select.template cast_safe<TOpSelect<T>>();
112
113 auto assign_value = value->get_value();
114
115 r->validate_rwd(FormatVector::CpuDense);
116 mask->validate_rw(FormatVector::CpuDense);
117
118 auto* p_r_dense = r->template get<CpuDenseVec<T>>();
119 const auto* p_mask_dense = mask->template get<CpuDenseVec<T>>();
120 const auto& func_assign = op_assign->function;
121 const auto& func_select = op_select->function;
122
123 uint N = r->get_n_rows();
124
125 for (uint i = 0; i < N; ++i) {
126 if (func_select(p_mask_dense->Ax[i])) {
127 p_r_dense->Ax[i] = func_assign(p_r_dense->Ax[i], assign_value);
128 }
129 }
130
131 return Status::Ok;
132 }
133 };
134
135}// namespace spla
136
137#endif//SPLA_CPU_V_ASSIGN_HPP
Status of library operation execution.
Definition cpu_v_assign.hpp:43
std::string get_name() override
Definition cpu_v_assign.hpp:47
~Algo_v_assign_masked_cpu() override=default
Status execute(const DispatchContext &ctx) override
Definition cpu_v_assign.hpp:55
std::string get_description() override
Definition cpu_v_assign.hpp:51
Algorithm suitable to process schedule task based on task string key.
Definition registry.hpp:66
Automates reference counting and behaves as shared smart pointer.
Definition ref.hpp:117
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