spla
Loading...
Searching...
No Matches
cpu_format_dok_vec.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_FORMAT_DOK_VEC_HPP
29#define SPLA_CPU_FORMAT_DOK_VEC_HPP
30
31#include <cpu/cpu_formats.hpp>
32
33#include <algorithm>
34
35namespace spla {
36
42 template<typename T>
44 CpuCooVec<T>& out) {
45 assert(out.Ai.size() == in.values);
46 assert(out.Ax.size() == in.values);
47
48
49 std::vector<std::pair<uint, T>> tmp;
50 tmp.reserve(in.values);
51
52 for (const auto& entry : in.Ax) {
53 tmp.emplace_back(entry.first, entry.second);
54 }
55
56 std::sort(tmp.begin(), tmp.end(), [](const auto& a, const auto& b) { return a.first < b.first; });
57
58 uint k = 0;
59
60 for (const auto& entry : tmp) {
61 const uint i = entry.first;
62 const T x = entry.second;
63 out.Ai[k] = i;
64 out.Ax[k] = x;
65 k += 1;
66 }
67
68 out.values = in.values;
69 }
70
71 template<typename T>
72 void cpu_dok_vec_to_dense(const uint n_rows,
73 const CpuDokVec<T>& in,
74 CpuDenseVec<T>& out) {
75 assert(out.Ax.size() == n_rows);
76
77 for (const auto& entry : in.Ax) {
78 const uint i = entry.first;
79 const T x = entry.second;
80 out.Ax[i] = x;
81 }
82 }
83
84 template<typename T>
86 T element,
87 CpuDokVec<T>& vec) {
88 auto entry = vec.Ax.find(row_id);
89 if (entry != vec.Ax.end()) {
90 entry->second = vec.reduce(entry->second, element);
91 return;
92 }
93
94 vec.Ax[row_id] = element;
95 vec.values += 1;
96 }
97
98 template<typename T>
100 vec.values = 0;
101 vec.Ax.clear();
102 }
103
108}// namespace spla
109
110#endif//SPLA_CPU_FORMAT_DOK_VEC_HPP
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
CPU one-dim array for dense vector representation.
Definition cpu_formats.hpp:74
std::vector< T > Ax
Definition cpu_formats.hpp:80
Definition cpu_formats.hpp:55
robin_hood::unordered_flat_map< uint, T > Ax
Definition cpu_formats.hpp:63
Reduce reduce
Definition cpu_formats.hpp:64
uint values
Definition tdecoration.hpp:58
void cpu_dok_vec_to_dense(const uint n_rows, const CpuDokVec< T > &in, CpuDenseVec< T > &out)
Definition cpu_format_dok_vec.hpp:72
void cpu_dok_vec_clear(CpuDokVec< T > &vec)
Definition cpu_format_dok_vec.hpp:99
void cpu_dok_vec_to_coo(const CpuDokVec< T > &in, CpuCooVec< T > &out)
Definition cpu_format_dok_vec.hpp:43
void cpu_dok_vec_add_element(uint row_id, T element, CpuDokVec< T > &vec)
Definition cpu_format_dok_vec.hpp:85
std::uint32_t uint
Library index and size type.
Definition config.hpp:56
Definition algorithm.hpp:37