Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
DoKLocalMatrixT.h
1/*
2 * Copyright 2020 IFPEN-CEA
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 */
18
19#pragma once
20
21#include <memory>
22
23#include <alien/kernels/dok/DoKLocalMatrixIndexer.h>
24#include <alien/kernels/dok/ILocalMatrixIndexer.h>
25#include <alien/kernels/dok/IReverseIndexer.h>
26
27namespace Alien
28{
29
32template <typename NNZValue>
33class DoKLocalMatrixT
34{
35 public:
36 DoKLocalMatrixT()
37 : m_indexer(new DoKLocalMatrixIndexer())
38 , m_offset(0)
39 , m_values()
40 {}
41
42 virtual ~DoKLocalMatrixT() = default;
43
44 DoKLocalMatrixT& operator=(DoKLocalMatrixT&& src) noexcept
45 {
46 m_indexer = std::move(src.m_indexer);
47 m_r_indexer = std::move(src.m_r_indexer);
48
49 if (&m_values != &src.m_values) {
50 m_values = src.m_values;
51 src.m_values.clear();
52 }
53 return *this;
54 }
55
56 DoKLocalMatrixT& operator=(const DoKLocalMatrixT& src)
57 {
58 m_indexer.reset(src.m_indexer->clone());
59 m_values = src.m_values;
60 m_r_indexer.reset(nullptr);
61
62 return *this;
63 }
64
65 void setMaxNnz(Integer size) { _reallocate(size); }
66
71 void set(Int32 i, Int32 j, const NNZValue& val)
72 {
73 auto offset = findOffset(i, j);
74 m_values[offset] = val;
75 }
76
77 NNZValue add(Int32 i, Int32 j, const NNZValue& val)
78 {
79 auto offset = findOffset(i, j);
80 m_values[offset] += val;
81 return m_values[offset];
82 }
83
85 void compact()
86 {
87 UniqueArray<ILocalMatrixIndexer::Renumbering> perm(m_offset);
88 m_r_indexer.reset(m_indexer->sort(perm));
89 UniqueArray<NNZValue> old_vals = m_values;
90 for (auto curr : perm) {
91 m_values[curr.second] = old_vals[curr.first];
92 }
93 }
94
95 IReverseIndexer* getReverseIndexer() const { return m_r_indexer.get(); }
96
97 ILocalMatrixIndexer* getIndexer() const { return m_indexer.get(); }
98
99 ConstArrayView<NNZValue> getValues() const { return m_values; }
100
101 void dump()
102 {
103 if (m_r_indexer == nullptr)
104 this->compact();
105
106 std::cout << "Number of elements: " << m_values.size() << "\n";
107 for (int i = 0; i < m_r_indexer->size(); ++i) {
108 auto index = (*m_r_indexer)[i];
109 auto offset = m_indexer->find(index.value().first, index.value().second);
110 std::cout
111 << "( " << index.value().first << " , " << index.value().second << " ) "
112 << " = "
113 << m_values[offset.value()]
114 << "\n";
115 }
116 }
117
118 private:
119 void _reallocate(Integer size = 0)
120 {
121 if (size || (size < m_offset + 1))
122 size = m_offset + 1;
123 m_values.resize(size);
124 }
125
126 ILocalMatrixIndexer::Offset findOffset(Int32 i, Int32 j)
127 {
128 auto offset = m_indexer->create(i, j, m_offset);
129 if (offset == (Integer)m_values.size()) {
130 m_values.add(0.);
131 }
132 return offset;
133 }
134
135 private:
136 std::unique_ptr<ILocalMatrixIndexer> m_indexer;
137 ILocalMatrixIndexer::Offset m_offset;
138 UniqueArray<NNZValue> m_values;
139 std::unique_ptr<IReverseIndexer> m_r_indexer;
140};
141
142} // namespace Alien
Local matrix indexer using HashMap.
void compact()
Group non-zeros according to indexer.
void set(Int32 i, Int32 j, const NNZValue &val)
Reverse indexer: associates an Index (i,j) to an offset.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17