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> : public ConstMatrix<typename NekMatrix<DataType, InnerMatrixType>::NumberType>
52  {
53  public:
56  typedef typename InnerType::NumberType NumberType;
58 
59  // Each inner matrix type can possible return references or value types from GetValue.
60  // Query the type here to find out.
61 
62  typedef typename InnerType::GetValueType GetValueType;
63  typedef typename InnerType::ConstGetValueType ConstGetValueType;
64 
65  public:
66  template<typename MatrixType>
67  class iterator_base
68  {
69  public:
70  typedef typename MatrixType::InnerType IteratorInnerType;
72 
73  public:
74  iterator_base(MatrixType& m, unsigned int curRow, unsigned int curCol) :
75  m_matrix(m),
76  m_curRow(curRow),
77  m_curColumn(curCol)
78  {
79  }
80 
82  m_matrix(m),
83  m_curRow(std::numeric_limits<unsigned int>::max()),
84  m_curColumn(std::numeric_limits<unsigned int>::max())
85  {
86  }
87 
88  iterator_base(const iterator_base<MatrixType>& rhs) :
89  m_matrix(rhs.m_matrix),
90  m_curRow(rhs.m_curRow),
91  m_curColumn(rhs.m_curColumn)
92  {
93  }
94 
95  void operator++()
96  {
97  if( m_curRow != std::numeric_limits<unsigned int>::max() )
98  {
99  std::tie(m_curRow, m_curColumn) = StoragePolicy::Advance(
100  m_matrix.GetRows(), m_matrix.GetColumns(), m_curRow, m_curColumn);
101  }
102  }
103 
105  {
106  return m_matrix(m_curRow, m_curColumn);
107  }
108 
109  bool operator==(const iterator_base<MatrixType>& rhs)
110  {
111  return m_curRow == rhs.m_curRow && m_curColumn == rhs.m_curColumn;
112  }
113 
114  bool operator!=(const iterator_base<MatrixType>& rhs)
115  {
116  return !(*this == rhs);
117  }
118 
119  private:
120  iterator_base<MatrixType>& operator=(const iterator_base<MatrixType>& rhs);
121 
123  unsigned int m_curRow;
124  unsigned int m_curColumn;
125 
126  };
127 
128  typedef iterator_base<ThisType> iterator;
129  typedef iterator_base<const ThisType> const_iterator;
130 
131  public:
133 
134  LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows, unsigned int numberOfBlockColumns,
135  unsigned int rowsPerBlock, unsigned int columnsPerBlock,
136  MatrixStorage type = eFULL);
137 
138  LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows, unsigned int numberOfBlockColumns,
139  const unsigned int* rowsPerBlock, const unsigned int* columnsPerBlock,
140  MatrixStorage type = eFULL);
141 
142  LIB_UTILITIES_EXPORT NekMatrix(unsigned int numberOfBlockRows, unsigned int numberOfBlockColumns,
143  const Array<OneD, const unsigned int>& rowsPerBlock, const Array<OneD, const unsigned int>& columnsPerBlock,
144  MatrixStorage type = eFULL);
145 
147  const Array<OneD, const unsigned int>& columnsPerBlock,
148  MatrixStorage type = eFULL);
149 
151 
152  LIB_UTILITIES_EXPORT unsigned int GetRequiredStorageSize() const;
153 
154  LIB_UTILITIES_EXPORT unsigned int CalculateBlockIndex(unsigned int row, unsigned int column) const;
155 
156  LIB_UTILITIES_EXPORT const InnerType* GetBlockPtr(unsigned int row, unsigned int column) const;
157 
158  LIB_UTILITIES_EXPORT std::shared_ptr<const InnerType> GetBlock(unsigned int row, unsigned int column) const;
159 
160  LIB_UTILITIES_EXPORT std::shared_ptr<InnerType>& GetBlock(unsigned int row, unsigned int column);
161 
162  LIB_UTILITIES_EXPORT void SetBlock(unsigned int row, unsigned int column, std::shared_ptr<InnerType>& m);
163 
164  LIB_UTILITIES_EXPORT ConstGetValueType operator()(unsigned int row, unsigned int col) const;
165 
166  LIB_UTILITIES_EXPORT unsigned int GetStorageSize() const;
167 
168  LIB_UTILITIES_EXPORT unsigned int GetNumberOfBlockRows() const;
169 
170  LIB_UTILITIES_EXPORT unsigned int GetNumberOfBlockColumns() const;
171 
172  LIB_UTILITIES_EXPORT unsigned int GetNumberOfRowsInBlockRow(unsigned int blockRow) const;
173 
174  LIB_UTILITIES_EXPORT unsigned int GetNumberOfColumnsInBlockColumn(unsigned int blockCol) const;
175 
176  LIB_UTILITIES_EXPORT void GetBlockSizes(Array<OneD, unsigned int>& rowSizes,
177  Array<OneD, unsigned int>& colSizes) const;
178 
181  LIB_UTILITIES_EXPORT const_iterator begin() const;
183 
184  public:
186 
187  LIB_UTILITIES_EXPORT static std::shared_ptr<ThisType> CreateWrapper(const std::shared_ptr<ThisType>& rhs);
188 
189  private:
190 
191  LIB_UTILITIES_EXPORT static unsigned int GetNumberOfElementsInBlock(unsigned int block, unsigned int totalBlocks, const Array<OneD, unsigned int>& sizes);
192 
193  LIB_UTILITIES_EXPORT void Initialize(const unsigned int* rowsPerBlock, const unsigned int* columnsPerBlock);
194 
195  LIB_UTILITIES_EXPORT virtual typename boost::call_traits<NumberType>::value_type v_GetValue(unsigned int row, unsigned int column) const;
196 
197  LIB_UTILITIES_EXPORT virtual unsigned int v_GetStorageSize() const;
198 
199  LIB_UTILITIES_EXPORT virtual void v_Transpose();
200 
202  std::shared_ptr<InnerType> m_nullBlockPtr;
205  unsigned int m_storageSize;
206  unsigned int m_numberOfBlockRows;
209  };
210 
211  template<typename DataType, typename InnerMatrixType>
212  typename NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag>::NumberType
213  NekMatrix<NekMatrix<DataType, InnerMatrixType>, BlockMatrixTag>::m_zeroElement(0);
214 
215  template<typename InnerMatrixType>
218  {
220  result.Transpose();
221  return result;
222  }
223 }
224 
225 
226 #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:74
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:1
NekMatrix< InnerMatrixType, BlockMatrixTag > Transpose(NekMatrix< InnerMatrixType, BlockMatrixTag > &rhs)