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 
47 
48 namespace Nektar
49 {
50 template <typename DataType, typename InnerMatrixType>
51 class NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag>
52  : public ConstMatrix<
53  typename NekMatrix<DataType, InnerMatrixType>::NumberType>
54 {
55 public:
58  typedef typename InnerType::NumberType NumberType;
60 
61  // Each inner matrix type can possible return references or value types from
62  // GetValue. Query the type here to find out.
63 
64  typedef typename InnerType::GetValueType GetValueType;
65  typedef typename InnerType::ConstGetValueType ConstGetValueType;
66 
67 public:
68  template <typename MatrixType> class iterator_base
69  {
70  public:
71  typedef typename MatrixType::InnerType IteratorInnerType;
73 
74  public:
75  iterator_base(MatrixType &m, unsigned int curRow, unsigned int curCol)
76  : m_matrix(m), m_curRow(curRow), m_curColumn(curCol)
77  {
78  }
79 
81  : m_matrix(m), m_curRow(std::numeric_limits<unsigned int>::max()),
82  m_curColumn(std::numeric_limits<unsigned int>::max())
83  {
84  }
85 
86  iterator_base(const iterator_base<MatrixType> &rhs)
87  : m_matrix(rhs.m_matrix), m_curRow(rhs.m_curRow),
88  m_curColumn(rhs.m_curColumn)
89  {
90  }
91 
92  void operator++()
93  {
94  if (m_curRow != std::numeric_limits<unsigned int>::max())
95  {
96  std::tie(m_curRow, m_curColumn) = StoragePolicy::Advance(
97  m_matrix.GetRows(), m_matrix.GetColumns(), m_curRow,
98  m_curColumn);
99  }
100  }
101 
103  {
104  return m_matrix(m_curRow, m_curColumn);
105  }
106 
107  bool operator==(const iterator_base<MatrixType> &rhs)
108  {
109  return m_curRow == rhs.m_curRow && m_curColumn == rhs.m_curColumn;
110  }
111 
112  bool operator!=(const iterator_base<MatrixType> &rhs)
113  {
114  return !(*this == rhs);
115  }
116 
117  private:
118  iterator_base<MatrixType> &operator=(
119  const iterator_base<MatrixType> &rhs);
120 
122  unsigned int m_curRow;
123  unsigned int m_curColumn;
124  };
125 
126  typedef iterator_base<ThisType> iterator;
127  typedef iterator_base<const ThisType> const_iterator;
128 
129 public:
131 
132  LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows,
133  unsigned int numberOfBlockColumns,
134  unsigned int rowsPerBlock,
135  unsigned int columnsPerBlock,
136  MatrixStorage type = eFULL);
137 
138  LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows,
139  unsigned int numberOfBlockColumns,
140  const unsigned int *rowsPerBlock,
141  const unsigned int *columnsPerBlock,
142  MatrixStorage type = eFULL);
143 
145  unsigned int numberOfBlockRows, unsigned int numberOfBlockColumns,
146  const Array<OneD, const unsigned int> &rowsPerBlock,
147  const Array<OneD, const unsigned int> &columnsPerBlock,
148  MatrixStorage type = eFULL);
149 
151  const Array<OneD, const unsigned int> &rowsPerBlock,
152  const Array<OneD, const unsigned int> &columnsPerBlock,
153  MatrixStorage type = eFULL);
154 
156 
157  LIB_UTILITIES_EXPORT unsigned int GetRequiredStorageSize() const;
158 
159  LIB_UTILITIES_EXPORT unsigned int CalculateBlockIndex(
160  unsigned int row, unsigned int column) const;
161 
162  LIB_UTILITIES_EXPORT const InnerType *GetBlockPtr(
163  unsigned int row, unsigned int column) const;
164 
165  LIB_UTILITIES_EXPORT std::shared_ptr<const InnerType> GetBlock(
166  unsigned int row, unsigned int column) const;
167 
168  LIB_UTILITIES_EXPORT std::shared_ptr<InnerType> &GetBlock(
169  unsigned int row, unsigned int column);
170 
171  LIB_UTILITIES_EXPORT void SetBlock(unsigned int row, unsigned int column,
172  std::shared_ptr<InnerType> &m);
173 
174  LIB_UTILITIES_EXPORT ConstGetValueType operator()(unsigned int row,
175  unsigned int col) const;
176 
177  LIB_UTILITIES_EXPORT unsigned int GetStorageSize() const;
178 
179  LIB_UTILITIES_EXPORT unsigned int GetNumberOfBlockRows() const;
180 
181  LIB_UTILITIES_EXPORT unsigned int GetNumberOfBlockColumns() const;
182 
183  LIB_UTILITIES_EXPORT unsigned int GetNumberOfRowsInBlockRow(
184  unsigned int blockRow) const;
185 
186  LIB_UTILITIES_EXPORT unsigned int GetNumberOfColumnsInBlockColumn(
187  unsigned int blockCol) const;
188 
189  LIB_UTILITIES_EXPORT void GetBlockSizes(
190  Array<OneD, unsigned int> &rowSizes,
191  Array<OneD, unsigned int> &colSizes) const;
192 
195  LIB_UTILITIES_EXPORT const_iterator begin() const;
197 
198 public:
200 
201  LIB_UTILITIES_EXPORT static std::shared_ptr<ThisType> CreateWrapper(
202  const std::shared_ptr<ThisType> &rhs);
203 
204 protected:
205  LIB_UTILITIES_EXPORT virtual
206  typename boost::call_traits<NumberType>::value_type
207  v_GetValue(unsigned int row, unsigned int column) const override;
208 
209  LIB_UTILITIES_EXPORT virtual unsigned int v_GetStorageSize() const override;
210 
211  LIB_UTILITIES_EXPORT virtual void v_Transpose() override;
212 
213 private:
214  LIB_UTILITIES_EXPORT static unsigned int GetNumberOfElementsInBlock(
215  unsigned int block, unsigned int totalBlocks,
216  const Array<OneD, unsigned int> &sizes);
217 
218  LIB_UTILITIES_EXPORT void Initialize(const unsigned int *rowsPerBlock,
219  const unsigned int *columnsPerBlock);
220 
222  std::shared_ptr<InnerType> m_nullBlockPtr;
225  unsigned int m_storageSize;
226  unsigned int m_numberOfBlockRows;
229 };
230 
231 template <typename DataType, typename InnerMatrixType>
232 typename NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag>::
234  BlockMatrixTag>::m_zeroElement(0);
235 
236 template <typename InnerMatrixType>
239 {
241  result.Transpose();
242  return result;
243 }
244 } // namespace Nektar
245 
246 #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:75
iterator_base< MatrixType > & operator=(const iterator_base< MatrixType > &rhs)
static std::shared_ptr< ThisType > CreateWrapper(const std::shared_ptr< ThisType > &rhs)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
NekMatrix< InnerMatrixType, BlockMatrixTag > Transpose(NekMatrix< InnerMatrixType, BlockMatrixTag > &rhs)