Nektar++
GlobalMatrix.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: GlobalMatrix.cpp
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: GlobalMatrix definition
32//
33///////////////////////////////////////////////////////////////////////////////
34
39
40#include <fstream>
41#include <iomanip>
42
43using namespace std;
44
45namespace Nektar
46{
47namespace MultiRegions
48{
49std::string GlobalMatrix::def =
51 "GlobalMatrixStorageType", "SmvBSR");
52std::string GlobalMatrix::lookupIds[3] = {
54 "GlobalMatrixStorageType", "SmvBSR", MultiRegions::eSmvBSR)};
55
56/**
57 * @class GlobalMatrix
58 * This matrix is essentially a wrapper around a DNekSparseMat.
59 */
60
61/**
62 * Allocates a new DNekSparseMat object from the given specification.
63 * @param rows Number of rows in matrix.
64 * @param columns Number of columns in matrix.
65 * @param cooMat ?
66 */
68 unsigned int rows, unsigned int columns,
69 const COOMatType &cooMat,
70 const MatrixStorage &matStorage)
71 : m_smvbsrmatrix(), m_rows(rows), m_mulCallsCounter(0)
72{
73 MatrixStorageType storageType =
74 pSession->GetSolverInfoAsEnum<MatrixStorageType>(
75 "GlobalMatrixStorageType");
76
77 unsigned int brows, bcols;
78
79 // Size of dense matrix sub-blocks
80 int block_size = 1;
81
82 BCOMatType bcoMat;
83
84 // assuming current sparse format allows
85 // block-sparse data representation
86
87 if (pSession->DefinesParameter("SparseBlockSize"))
88 {
89 pSession->LoadParameter("SparseBlockSize", block_size);
90 ASSERTL1(block_size > 0,
91 "SparseBlockSize parameter must to be positive");
92 }
93
94 brows = rows / block_size + (rows % block_size > 0);
95 bcols = columns / block_size + (columns % block_size > 0);
96
97 if (rows % block_size > 0)
98 m_copyOp = true;
99
100 if (m_copyOp)
101 {
102 m_tmpin = Array<OneD, NekDouble>(brows * block_size, 0.0);
103 m_tmpout = Array<OneD, NekDouble>(brows * block_size, 0.0);
104 }
105
106 convertCooToBco(block_size, cooMat, bcoMat);
107
108 size_t matBytes = 0;
109 switch (storageType)
110 {
111 case eSmvBSR:
112 {
113
114 // Create zero-based Smv-multiply BSR sparse storage holder
117 brows, bcols, block_size, bcoMat, matStorage);
118
119 // Create sparse matrix
122
123 matBytes = m_smvbsrmatrix->GetMemoryFootprint();
124 }
125 break;
126
127 default:
129 "Unsupported sparse storage type chosen");
130 }
131
132 cout << "Global matrix storage type: " << MatrixStorageTypeMap[storageType]
133 << endl;
134 std::cout << "Global matrix memory, bytes = " << matBytes;
135 if (matBytes / (1024 * 1024) > 0)
136 {
137 std::cout << " (" << matBytes / (1024 * 1024) << " MB)" << std::endl;
138 }
139 else
140 {
141 std::cout << " (" << matBytes / 1024 << " KB)" << std::endl;
142 }
143 std::cout << "Sparse storage block size = " << block_size << std::endl;
144}
145
146/**
147 * Performs a matrix-vector multiply using the sparse format-specific
148 * multiply routine.
149 * @param in Input vector.
150 * @param out Output vector.
151 */
154{
155 if (!m_copyOp)
156 {
157 if (m_smvbsrmatrix)
158 m_smvbsrmatrix->Multiply(in, out);
159 }
160 else
161 {
162 // if block size makes the last row/column bigger, one needs
163 // using temporary storage for rhs and result vectors.
164 Vmath::Vcopy(m_rows, &in[0], 1, &m_tmpin[0], 1);
165
166 if (m_smvbsrmatrix)
167 m_smvbsrmatrix->Multiply(m_tmpin, m_tmpout);
168
169 Vmath::Vcopy(m_rows, &m_tmpout[0], 1, &out[0], 1);
170 }
171
173}
174
176{
177 if (m_smvbsrmatrix)
178 return m_smvbsrmatrix->GetMulCallsCounter();
179 return -1;
180}
181
183{
184 if (m_smvbsrmatrix)
185 return m_smvbsrmatrix->GetNumNonZeroEntries();
186 return -1;
187}
188
189} // namespace MultiRegions
190} // namespace Nektar
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
static std::string RegisterEnumValue(std::string pEnum, std::string pString, int pEnumValue)
Registers an enumeration value.
static std::string RegisterDefaultSolverInfo(const std::string &pName, const std::string &pValue)
Registers the default string value of a solver info property.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static std::string lookupIds[]
Definition: GlobalMatrix.h:82
GlobalMatrix(const LibUtilities::SessionReaderSharedPtr &pSession, unsigned int rows, unsigned int columns, const COOMatType &cooMat, const MatrixStorage &matStorage=eFULL)
Construct a new matrix.
DNekSmvBsrMatSharedPtr m_smvbsrmatrix
Pointer to a double-precision Nektar++ sparse matrix.
Definition: GlobalMatrix.h:71
Array< OneD, NekDouble > m_tmpout
Definition: GlobalMatrix.h:75
unsigned long GetMulCallsCounter() const
unsigned int GetNumNonZeroEntries() const
Array< OneD, NekDouble > m_tmpin
Definition: GlobalMatrix.h:74
void Multiply(const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out)
Perform a matrix-vector multiply.
std::shared_ptr< SparseStorageType > SparseStorageSharedPtr
std::shared_ptr< SessionReader > SessionReaderSharedPtr
const char *const MatrixStorageTypeMap[]
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
std::map< CoordType, BCOEntryType > BCOMatType
void convertCooToBco(const unsigned int blkDim, const COOMatType &cooMat, BCOMatType &bcoMat)
Definition: SparseUtils.cpp:48
std::map< CoordType, NekDouble > COOMatType
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1191