Nektar++
BlockMatrix.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: BlockMatrix.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLOCK_MATRIX_HPP
36#define NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLOCK_MATRIX_HPP
37
38#include <memory>
39#include <tuple>
40
42
45
46namespace Nektar
47{
48template <typename DataType, typename InnerMatrixType>
49class NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag>
50 : public ConstMatrix<
51 typename NekMatrix<DataType, InnerMatrixType>::NumberType>
52{
53public:
56 typedef typename InnerType::NumberType NumberType;
58
59 // Each inner matrix type can possible return references or value types from
60 // GetValue. Query the type here to find out.
61
62 typedef typename InnerType::GetValueType GetValueType;
63 typedef typename InnerType::ConstGetValueType ConstGetValueType;
64
65public:
66 template <typename MatrixType> class iterator_base
67 {
68 public:
69 typedef typename MatrixType::InnerType IteratorInnerType;
71
72 public:
73 iterator_base(MatrixType &m, unsigned int curRow, unsigned int curCol)
74 : m_matrix(m), m_curRow(curRow), m_curColumn(curCol)
75 {
76 }
77
79 : m_matrix(m), m_curRow(std::numeric_limits<unsigned int>::max()),
80 m_curColumn(std::numeric_limits<unsigned int>::max())
81 {
82 }
83
84 iterator_base(const iterator_base<MatrixType> &rhs)
85 : m_matrix(rhs.m_matrix), m_curRow(rhs.m_curRow),
86 m_curColumn(rhs.m_curColumn)
87 {
88 }
89
91 {
92 if (m_curRow != std::numeric_limits<unsigned int>::max())
93 {
94 std::tie(m_curRow, m_curColumn) = StoragePolicy::Advance(
95 m_matrix.GetRows(), m_matrix.GetColumns(), m_curRow,
96 m_curColumn);
97 }
98 }
99
101 {
102 return m_matrix(m_curRow, m_curColumn);
103 }
104
105 bool operator==(const iterator_base<MatrixType> &rhs)
106 {
107 return m_curRow == rhs.m_curRow && m_curColumn == rhs.m_curColumn;
108 }
109
110 bool operator!=(const iterator_base<MatrixType> &rhs)
111 {
112 return !(*this == rhs);
113 }
114
115 private:
116 iterator_base<MatrixType> &operator=(
117 const iterator_base<MatrixType> &rhs);
118
120 unsigned int m_curRow;
121 unsigned int m_curColumn;
122 };
123
124 typedef iterator_base<ThisType> iterator;
125 typedef iterator_base<const ThisType> const_iterator;
126
127public:
129
130 LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows,
131 unsigned int numberOfBlockColumns,
132 unsigned int rowsPerBlock,
133 unsigned int columnsPerBlock,
134 MatrixStorage type = eFULL);
135
136 LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows,
137 unsigned int numberOfBlockColumns,
138 const unsigned int *rowsPerBlock,
139 const unsigned int *columnsPerBlock,
140 MatrixStorage type = eFULL);
141
143 unsigned int numberOfBlockRows, unsigned int numberOfBlockColumns,
144 const Array<OneD, const unsigned int> &rowsPerBlock,
145 const Array<OneD, const unsigned int> &columnsPerBlock,
146 MatrixStorage type = eFULL);
147
149 const Array<OneD, const unsigned int> &rowsPerBlock,
150 const Array<OneD, const unsigned int> &columnsPerBlock,
151 MatrixStorage type = eFULL);
152
154
155 LIB_UTILITIES_EXPORT unsigned int GetRequiredStorageSize() const;
156
157 LIB_UTILITIES_EXPORT unsigned int CalculateBlockIndex(
158 unsigned int row, unsigned int column) const;
159
160 LIB_UTILITIES_EXPORT const InnerType *GetBlockPtr(
161 unsigned int row, unsigned int column) const;
162
163 LIB_UTILITIES_EXPORT std::shared_ptr<const InnerType> GetBlock(
164 unsigned int row, unsigned int column) const;
165
166 LIB_UTILITIES_EXPORT std::shared_ptr<InnerType> &GetBlock(
167 unsigned int row, unsigned int column);
168
169 LIB_UTILITIES_EXPORT void SetBlock(unsigned int row, unsigned int column,
170 std::shared_ptr<InnerType> &m);
171
172 LIB_UTILITIES_EXPORT ConstGetValueType operator()(unsigned int row,
173 unsigned int col) const;
174
175 LIB_UTILITIES_EXPORT unsigned int GetStorageSize() const;
176
177 LIB_UTILITIES_EXPORT unsigned int GetNumberOfBlockRows() const;
178
179 LIB_UTILITIES_EXPORT unsigned int GetNumberOfBlockColumns() const;
180
181 LIB_UTILITIES_EXPORT unsigned int GetNumberOfRowsInBlockRow(
182 unsigned int blockRow) const;
183
184 LIB_UTILITIES_EXPORT unsigned int GetNumberOfColumnsInBlockColumn(
185 unsigned int blockCol) const;
186
187 LIB_UTILITIES_EXPORT void GetBlockSizes(
189 Array<OneD, unsigned int> &colSizes) const;
190
195
196public:
198
199 LIB_UTILITIES_EXPORT static std::shared_ptr<ThisType> CreateWrapper(
200 const std::shared_ptr<ThisType> &rhs);
201
202protected:
204 typename boost::call_traits<NumberType>::value_type v_GetValue(
205 unsigned int row, unsigned int column) const override;
206
207 LIB_UTILITIES_EXPORT unsigned int v_GetStorageSize() const override;
208
209 LIB_UTILITIES_EXPORT void v_Transpose() override;
210
211private:
212 LIB_UTILITIES_EXPORT static unsigned int GetNumberOfElementsInBlock(
213 unsigned int block, unsigned int totalBlocks,
214 const Array<OneD, unsigned int> &sizes);
215
216 LIB_UTILITIES_EXPORT void Initialize(const unsigned int *rowsPerBlock,
217 const unsigned int *columnsPerBlock);
218
220 std::shared_ptr<InnerType> m_nullBlockPtr;
223 unsigned int m_storageSize;
227};
228
229template <typename DataType, typename InnerMatrixType>
230typename NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag>::
232 BlockMatrixTag>::m_zeroElement(0);
233
234template <typename InnerMatrixType>
237{
239 result.Transpose();
240 return result;
241}
242} // namespace Nektar
243
244#endif // NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_BLOCK_MATRIX_HPP
#define LIB_UTILITIES_EXPORT
iterator_base(MatrixType &m, unsigned int curRow, unsigned int curCol)
Definition: BlockMatrix.hpp:73
iterator_base< MatrixType > & operator=(const iterator_base< MatrixType > &rhs)
static std::shared_ptr< ThisType > CreateWrapper(const std::shared_ptr< ThisType > &rhs)
NekMatrix< InnerMatrixType, BlockMatrixTag > Transpose(NekMatrix< InnerMatrixType, BlockMatrixTag > &rhs)