spla
Loading...
Searching...
No Matches
cpu_v_map.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_V_MAP_HPP
29#define SPLA_CPU_V_MAP_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
40#include <cstring>
41
42namespace spla {
43
44 template<typename T>
45 class Algo_v_map_cpu final : public RegistryAlgo {
46 public:
47 ~Algo_v_map_cpu() override = default;
48
49 std::string get_name() override {
50 return "v_map";
51 }
52
53 std::string get_description() override {
54 return "sequential vector map on cpu";
55 }
56
57 Status execute(const DispatchContext& ctx) override {
58 auto t = ctx.task.template cast_safe<ScheduleTask_v_map>();
59 ref_ptr<TVector<T>> v = t->v.template cast_safe<TVector<T>>();
60
61 if (v->is_valid(FormatVector::CpuCoo)) {
62 return execute_sp(ctx);
63 }
64 if (v->is_valid(FormatVector::CpuDense)) {
65 return execute_dn(ctx);
66 }
67
68 return execute_sp(ctx);
69 }
70
71 private:
72 Status execute_sp(const DispatchContext& ctx) {
73 TIME_PROFILE_SCOPE("cpu/vector_map_sparse");
74
75 auto t = ctx.task.template cast_safe<ScheduleTask_v_map>();
76
77 auto r = t->r.template cast_safe<TVector<T>>();
78 auto v = t->v.template cast_safe<TVector<T>>();
79 auto op = t->op.template cast_safe<TOpUnary<T, T>>();
80
81 r->validate_wd(FormatVector::CpuCoo);
82 v->validate_rw(FormatVector::CpuCoo);
83 auto* p_sparse_r = r->template get<CpuCooVec<T>>();
84 const auto* p_sparse_v = v->template get<CpuCooVec<T>>();
85 const auto& function = op->function;
86
87 const uint N = p_sparse_v->values;
88 cpu_coo_vec_resize(N, *p_sparse_r);
89
90 if (N > 0) {
91 std::memcpy(p_sparse_r->Ai.data(), p_sparse_v->Ai.data(), sizeof(uint) * N);
92
93 for (uint i = 0; i < N; i += 1) {
94 p_sparse_r->Ax[i] = function(p_sparse_v->Ax[i]);
95 }
96 }
97
98 return Status::Ok;
99 }
100
101 Status execute_dn(const DispatchContext& ctx) {
102 TIME_PROFILE_SCOPE("cpu/vector_map_dense");
103
104 auto t = ctx.task.template cast_safe<ScheduleTask_v_map>();
105
106 auto r = t->r.template cast_safe<TVector<T>>();
107 auto v = t->v.template cast_safe<TVector<T>>();
108 auto op = t->op.template cast_safe<TOpUnary<T, T>>();
109
110 r->validate_wd(FormatVector::CpuDense);
111 v->validate_rw(FormatVector::CpuDense);
112 auto* p_dense_r = r->template get<CpuDenseVec<T>>();
113 const auto* p_dense_v = v->template get<CpuDenseVec<T>>();
114 const auto& function = op->function;
115
116 const uint N = r->get_n_rows();
117
118 for (uint i = 0; i < N; i += 1) {
119 p_dense_r->Ax[i] = function(p_dense_v->Ax[i]);
120 }
121
122 return Status::Ok;
123 }
124 };
125
126}// namespace spla
127
128#endif//SPLA_CPU_V_MAP_HPP
Status of library operation execution.
Definition cpu_v_map.hpp:45
~Algo_v_map_cpu() override=default
Status execute(const DispatchContext &ctx) override
Definition cpu_v_map.hpp:57
std::string get_description() override
Definition cpu_v_map.hpp:53
std::string get_name() override
Definition cpu_v_map.hpp:49
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
void cpu_coo_vec_resize(const uint n_values, CpuCooVec< T > &vec)
Definition cpu_format_coo_vec.hpp:61
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