Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
SimpleCSRInternal.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8#pragma once
9
10#include <alien/kernels/simple_csr/CSRStructInfo.h>
11#include <alien/kernels/simple_csr/SimpleCSRPrecomp.h>
12
13/*---------------------------------------------------------------------------*/
14
15namespace Alien::SimpleCSRInternal
16{
17
18/*---------------------------------------------------------------------------*/
19
20template <typename ValueT = Real>
21class MatrixInternal
22{
23 public:
24 typedef ValueT ValueType;
25 typedef MatrixInternal<ValueType> ThisType;
26 typedef CSRStructInfo ProfileType;
27
28 public:
29 MatrixInternal(bool is_variable_block = false)
30 : m_profile(new ProfileType(is_variable_block))
31 {}
32
33 ~MatrixInternal() {}
34
35 void setValues(ValueT value)
36 {
37 m_values.fill(value) ;
38 }
39
40 ConstArrayView<ValueType> getValues() const { return m_values; }
41
42 UniqueArray<ValueType>& getValues() { return m_values; }
43
44 ValueType* getDataPtr() { return m_values.data(); }
45
46 ValueType const* getDataPtr() const { return m_values.data(); }
47
48 // Remark: once a profile is associated to a matrix he should not allow profile change
49 CSRStructInfo& getCSRProfile() { return *m_profile; }
50
51 const CSRStructInfo& getCSRProfile() const { return *m_profile; }
52
53 Integer getRowSize(Integer row) const { return m_profile->getRowSize(row); }
54
55 void scal(ValueType const* diag)
56 {
57 auto nrows = m_profile->getNRows() ;
58 auto kcol = m_profile->kcol() ;
59 for(int irow=0;irow<nrows;++irow)
60 {
61 ValueType scal = diag[irow] ;
62 for(int k=kcol[irow];k<kcol[irow+1];++k)
63 m_values[k] *= scal ;
64 }
65 }
66
67 void clear() { m_values.resize(0); }
68
69 MatrixInternal<ValueT>* clone() const { return new MatrixInternal<ValueT>(*this); }
70
71 template <typename T>
72 void copy(const MatrixInternal<T>& internal)
73 {
74 m_values.copy(internal.getValues());
75 m_profile->copy(internal.getCSRProfile());
76 }
77
78 template <typename T>
79 void copy(const MatrixInternal<T>& internal, Integer block_size1, Integer block_size2, Integer nb_blocks)
80 {
81 auto const& values2 = internal.getValues() ;
82 if(block_size1==block_size2)
83 m_values.copy(values2);
84 else if(block_size1==1)
85 {
86 Integer stride2 = block_size2*block_size2 ;
87 Integer offset2 = 0 ;
88 m_values.resize(nb_blocks) ;
89 for(Integer ib=0;ib<nb_blocks;++ib)
90 {
91 m_values[ib] = values2[offset2];
92 offset2 += stride2 ;
93 }
94 }
95 else
96 {
97 Integer stride1 = block_size1*block_size1 ;
98 Integer stride2 = block_size2*block_size2 ;
99 Integer offset1 = 0 ;
100 Integer offset2 = 0 ;
101 m_values.resize(nb_blocks*stride1) ;
102 for(Integer ib=0;ib<nb_blocks;++ib)
103 {
104 for(Integer i=0;i<block_size1;++i)
105 for(Integer j=0;j<block_size1;++j)
106 m_values[offset1 + i*block_size1+j] = values2[offset2+ i*block_size2+j];
107 offset1 += stride1 ;
108 offset2 += stride2 ;
109 }
110 }
111 m_profile->copy(internal.getCSRProfile());
112 }
113
114 bool needUpdate()
115 {
116 return m_is_update != true;
117 }
118
119 void notifyChanges()
120 {
121 m_is_update = false;
122 }
123
124 void endUpdate()
125 {
126 m_is_update = true;
127 }
128
129 bool m_is_update = false;
130 UniqueArray<ValueType> m_values;
131 std::shared_ptr<CSRStructInfo> m_profile;
132};
133
134/*---------------------------------------------------------------------------*/
135
136} // namespace Alien::SimpleCSRInternal
137
138/*---------------------------------------------------------------------------*/