Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
StreamMatrixBuilderInserterT.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 <arccore/collections/Array2.h>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
26namespace Alien
27{
28
29/*---------------------------------------------------------------------------*/
30
31template <typename ValueT>
32StreamMatrixBuilderT<ValueT>::BaseInserter::BaseInserter()
33: m_parent(NULL)
35 throw FatalErrorException(A_FUNCINFO, "Virtual Ctor never called");
37
38/*---------------------------------------------------------------------------*/
39
40template <typename ValueT>
41StreamMatrixBuilderT<ValueT>::BaseInserter::BaseInserter(
42StreamMatrixBuilderT<ValueT>* parent, Integer id)
43: m_id(id)
44, m_index(0)
45, m_current_size(0)
46, m_current_k(NULL)
47, m_values(NULL)
48, m_count(0)
49, m_size(0)
50, m_block_size(1)
51, m_parent(parent)
52{}
53
54/*---------------------------------------------------------------------------*/
55
56template <typename ValueT>
57StreamMatrixBuilderT<ValueT>::BaseInserter::~BaseInserter() {}
58
59/*---------------------------------------------------------------------------*/
60
61template <typename ValueT>
63{
64 m_index = 0;
65 m_current_size = 0;
66 m_current_k = NULL;
67 m_values = NULL;
68 m_count = 0;
69 m_size = 0;
70}
71
72/*---------------------------------------------------------------------------*/
73
74template <typename ValueT>
75Integer
80
81/*---------------------------------------------------------------------------*/
82
83template <typename ValueT>
85{
86 init();
87 m_n.resize(0);
88 m_row_index.resize(0);
89 m_col_index.resize(0);
90 m_data_index.resize(0);
91}
92
93/*---------------------------------------------------------------------------*/
94
95template <typename ValueT>
96void StreamMatrixBuilderT<ValueT>::BaseInserter::setMatrixValues(
97ValueT* matrix_values, Integer block_size)
98{
99 // ALIEN_ASSERT((this->m_parent->m_state == ePrepared),("Inconsistent state"));
100 m_values = matrix_values;
101 m_index = 0;
102 m_current_size = m_n[0];
103 m_current_k = m_data_index.data();
104 m_block_size = block_size;
105}
106
107/*---------------------------------------------------------------------------*/
108
109template <typename ValueT>
110void StreamMatrixBuilderT<ValueT>::Profiler::reserve(Integer capacity)
111{
112 // ALIEN_ASSERT((this->m_parent->m_state == ePrepared),("Inconsistent state"));
113 this->m_row_index.reserve(capacity);
114 this->m_col_index.reserve(capacity);
116
117/*---------------------------------------------------------------------------*/
118
119template <typename ValueT>
120Integer
122{
123 return m_size;
124}
126/*---------------------------------------------------------------------------*/
127
128template <typename ValueT>
129Integer
134
135/*---------------------------------------------------------------------------*/
136
137template <typename ValueT>
139{
140 ALIEN_ASSERT((this->m_parent->m_state == eStart), ("Inconsistent state"));
141 ALIEN_ASSERT((this->m_values != NULL), ("Inserter is not ready for filling"));
142 this->m_index = 0;
143 this->m_current_size = this->m_n[0];
144 this->m_current_k = this->m_data_index.data();
145}
146
147/*---------------------------------------------------------------------------*/
148
149template <typename ValueT>
151{
152 this->m_index = this->m_size;
153}
154
155/*---------------------------------------------------------------------------*/
156
157template <typename ValueT>
159{
160 return (this->m_index == 0);
161}
162
163/*---------------------------------------------------------------------------*/
164
165template <typename ValueT>
167{
168 return (this->m_index == this->m_size);
169}
170
171/*---------------------------------------------------------------------------*/
172
173template <typename ValueT>
174Integer
176{
177 return this->m_current_size;
178}
179
180/*---------------------------------------------------------------------------*/
181
182template <typename ValueT>
183Integer
185{
186 return this->m_index;
187}
188
189/*---------------------------------------------------------------------------*/
190/*---------------------------------------------------------------------------*/
191
192template <typename ValueT>
193void StreamMatrixBuilderT<ValueT>::Profiler::addMatrixEntries(
194ConstArrayView<Integer> row_index, ConstArrayView<Integer> col_index)
195{
196 this->_startTimer();
197 // ALIEN_ASSERT((this->m_parent->m_state == ePrepared),("Inconsistent state"));
198 // ALIEN_ASSERT((row_index.size()==col_index.size()),("row and col with different
199 // sizes")) ;
200 Integer n = col_index.size();
201 this->m_n.add(n);
202 this->m_count += n;
203 for (Integer i = 0; i < n; ++i) {
204 this->m_row_index.add(row_index[i]);
205 this->m_col_index.add(col_index[i]);
206 }
207 ++this->m_size;
208 this->_stopTimer();
209}
210
211/*---------------------------------------------------------------------------*/
212
213template <typename ValueT>
214void StreamMatrixBuilderT<ValueT>::Profiler::addMatrixEntries(
215ConstArrayView<Integer> row_indexes,
216const UniqueArray<ConstArrayView<Integer>>& col_indexes)
217{
218 this->_startTimer();
219 // ALIEN_ASSERT((this->m_parent->m_state == ePrepared),("Inconsistent state"));
220 // ALIEN_ASSERT((row_indexes.size()==col_indexes.size()),("row and col with different
221 // sizes")) ;
222 Integer n = 0;
223 for (Integer i = 0; i < row_indexes.size(); ++i)
224 n += col_indexes[i].size();
225 this->m_n.add(n);
226 this->m_count += n;
227 for (Integer i = 0; i < row_indexes.size(); ++i) {
228 Integer row = row_indexes[i];
229 for (Integer j = 0; j < col_indexes[i].size(); ++j) {
230 this->m_row_index.add(row);
231 this->m_col_index.add(col_indexes[i][j]);
232 }
233 }
234 ++this->m_size;
235 this->_stopTimer();
236}
237
238/*---------------------------------------------------------------------------*/
239
240template <typename ValueT>
242ConstArrayView<Integer> row_indexes, UniqueArray2<Integer> col_indexes,
243ConstArrayView<Integer> stencil_lids, Integer size)
244{
245 this->_startTimer();
246 // ALIEN_ASSERT((this->m_parent->m_state == ePrepared),("Inconsistent state"));
247 // ALIEN_ASSERT((row_indexes.size()==size),("row and col with different sizes")) ;
248 Integer n = 0;
249 for (Integer i = 0; i < stencil_lids.size(); ++i)
250 n += size;
251 this->m_n.add(n);
252 this->m_count += n;
253 for (Integer j = 0; j < stencil_lids.size(); ++j) {
254 ArrayView<Integer> cols = col_indexes[stencil_lids[j]];
255 for (Integer i = 0; i < size; ++i) {
256 this->m_row_index.add(row_indexes[i]);
257 this->m_col_index.add(cols[i]);
258 }
259 }
260 ++this->m_size;
261 this->_stopTimer();
262}
263
264/*---------------------------------------------------------------------------*/
265
266template <typename ValueT>
268const Integer row_index, ConstArrayView<Integer> col_index)
269{
270 this->_startTimer();
271 // ALIEN_ASSERT((this->m_parent->m_state == ePrepared),("Inconsistent state"));
272 Integer n = col_index.size();
273 this->m_n.add(n);
274 this->m_count += n;
275 for (Integer i = 0; i < n; ++i) {
276 this->m_row_index.add(row_index);
277 this->m_col_index.add(col_index[i]);
278 }
279 ++this->m_size;
280 this->_stopTimer();
281}
282
283/*---------------------------------------------------------------------------*/
284
285template <typename ValueT>
287Integer row_index, Integer col_index)
288{
289 this->_startTimer();
290 ALIEN_ASSERT((this->m_parent->m_state == ePrepared), ("Inconsistent state"));
291 this->m_n.add(1);
292 ++this->m_count;
293 this->m_row_index.add(row_index);
294 this->m_col_index.add(col_index);
295 ++this->m_size;
296 this->_stopTimer();
297}
298
299/*---------------------------------------------------------------------------*/
300
301template <typename ValueT>
302void StreamMatrixBuilderT<ValueT>::Filler::setData(ConstArrayView<ValueT> values)
303{
304 this->_startTimer();
305 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
306 // ALIEN_ASSERT((values.size()==this->m_current_size),("Incompatible size")) ;
307 for (Integer i = 0; i < this->m_current_size; ++i)
308 this->m_values[this->m_current_k[i]] = values[i];
309
310 this->_stopTimer();
311}
312
313/*---------------------------------------------------------------------------*/
314
315template <typename ValueT>
317{
318 this->_startTimer();
319 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
320 // ALIEN_ASSERT((1==this->m_current_size),("Incompatible size")) ;
321 this->m_values[*this->m_current_k] = values;
322 this->_stopTimer();
323}
324
325/*---------------------------------------------------------------------------*/
326
327template <typename ValueT>
328void StreamMatrixBuilderT<ValueT>::Filler::addData(ConstArrayView<ValueT> values)
329{
330 this->_startTimer();
331 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
332 // ALIEN_ASSERT((values.size()==this->m_current_size),("Incompatible size")) ;
333 for (Integer i = 0; i < this->m_current_size; ++i)
334 this->m_values[this->m_current_k[i]] += values[i];
335 this->_stopTimer();
336}
337
338/*---------------------------------------------------------------------------*/
339
340template <typename ValueT>
342ConstArrayView<ValueT> values, ValueT factor)
343{
344 this->_startTimer();
345 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
346 // ALIEN_ASSERT((values.size()==this->m_current_size),("Incompatible size")) ;
347 for (Integer i = 0; i < this->m_current_size; ++i)
348 this->m_values[this->m_current_k[i]] += values[i] * factor;
349 this->_stopTimer();
350}
351
352/*---------------------------------------------------------------------------*/
353
354template <typename ValueT>
356{
357 this->_startTimer();
358 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
359 // ALIEN_ASSERT((1==this->m_current_size),("Incompatible size")) ;
360 this->m_values[*this->m_current_k] += values;
361 this->_stopTimer();
362}
363
364/*---------------------------------------------------------------------------*/
365
366template <typename ValueT>
368{
369 this->_startTimer();
370 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
371 for (Integer i = 0; i < this->m_current_size; ++i)
372 this->m_values[this->m_current_k[i]] += value;
373 this->_stopTimer();
374}
375
376/*---------------------------------------------------------------------------*/
377
378template <typename ValueT>
379void StreamMatrixBuilderT<ValueT>::Filler::addBlockData(ConstArrayView<ValueT> values)
380{
381 this->_startTimer();
382 ALIEN_ASSERT((this->m_parent->m_state == eStart), ("Inconsistent state"));
383 ALIEN_ASSERT((values.size() == this->m_current_size * this->m_block_size),
384 ("Incompatible size %d vs %d * %d ", values.size(), this->m_current_size,
385 this->m_block_size));
386 for (Integer i = 0; i < this->m_current_size; ++i)
387 for (Integer k = 0; k < this->m_block_size; ++k)
388 this->m_values[this->m_current_k[i] * this->m_block_size + k] +=
389 values[i * this->m_block_size + k];
390 this->_stopTimer();
391}
392
393/*---------------------------------------------------------------------------*/
394
395template <typename ValueT>
397ConstArrayView<ValueT> values, ValueT factor)
398{
399 this->_startTimer();
400 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
401 // ALIEN_ASSERT((values.size()==this->m_current_size*this->m_block_size),("Incompatible
402 // size")) ;
403 for (Integer i = 0; i < this->m_current_size; ++i)
404 for (Integer k = 0; k < this->m_block_size; ++k)
405 this->m_values[this->m_current_k[i] * this->m_block_size + k] +=
406 factor * values[i * this->m_block_size + k];
407 this->_stopTimer();
408}
409
410/*---------------------------------------------------------------------------*/
411
412template <typename ValueT>
414ConstArrayView<ValueT> values, Integer size)
415{
416 this->_startTimer();
417 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
418 // ALIEN_ASSERT((values.size()*size==this->m_current_size),("Incompatible size")) ;
419
420 Integer icount = 0;
421 for (Integer i = 0; i < values.size(); ++i)
422 for (Integer k = 0; k < size; ++k)
423 this->m_values[this->m_current_k[icount++]] += values[i];
424 this->_stopTimer();
425}
426
427/*---------------------------------------------------------------------------*/
428
429template <typename ValueT>
431ConstArrayView<ValueT> values, ValueT factor, Integer size)
432{
433 this->_startTimer();
434 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
435 // ALIEN_ASSERT((values.size()*size==this->m_current_size),("Incompatible size")) ;
436 Integer icount = 0;
437 for (Integer i = 0; i < values.size(); ++i)
438 for (Integer k = 0; k < size; ++k)
439 this->m_values[this->m_current_k[icount++]] += factor * values[i];
440 this->_stopTimer();
441}
442
443/*---------------------------------------------------------------------------*/
444
445template <typename ValueT>
448{
449 this->_startTimer();
450 // ALIEN_ASSERT((this->m_parent->m_state == eStart),("Inconsistent state"));
451 ++this->m_index;
452 this->m_current_k += this->m_current_size;
453 this->m_current_size = this->m_n[this->m_index];
454 this->_stopTimer();
455 return *this;
456}
457
458/*---------------------------------------------------------------------------*/
459
460} // namespace Alien
461
462/*---------------------------------------------------------------------------*/
463/*---------------------------------------------------------------------------*/
Integer count()
Nombre de données dans l'inserter.
Integer size()
Nombre d'itération dans l'inserter.
UniqueArray< Integer > m_data_index
positon of entry in the Matrix CSR structure
Integer getId() const
Identifiant de l'inserter dans son StreamMatrixBuilder.
void init()
@ Méthodes visibles de l'extérieur
void end()
Termine l'inserter (déallocation des données).
bool isBegin()
Retourne true si le filler est au début.
bool isEnd()
Retourn true, le filler est à la fin.
Integer index()
Retourne l'index d'itération courante de l'inserter.
void setEnd()
Positionne le filler est à la fin.
void start()
Redémarre le filler au début.
Integer currentSize()
Taille du bloc courant à insérer.
StreamMatrixBuilderT(Matrix &matrix, bool init_and_start=true)
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17