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
46{
47std::string GlobalMatrix::def =
49 "GlobalMatrixStorageType", "SmvBSR");
50std::string GlobalMatrix::lookupIds[3] = {
52 "GlobalMatrixStorageType", "SmvBSR", MultiRegions::eSmvBSR)};
53
54/**
55 * @class GlobalMatrix
56 * This matrix is essentially a wrapper around a DNekSparseMat.
57 */
58
59/**
60 * Allocates a new DNekSparseMat object from the given specification.
61 * @param rows Number of rows in matrix.
62 * @param columns Number of columns in matrix.
63 * @param cooMat ?
64 */
66 unsigned int rows, unsigned int columns,
67 const COOMatType &cooMat,
68 const MatrixStorage &matStorage)
69 : m_smvbsrmatrix(), m_rows(rows), m_mulCallsCounter(0)
70{
71 MatrixStorageType storageType =
72 pSession->GetSolverInfoAsEnum<MatrixStorageType>(
73 "GlobalMatrixStorageType");
74
75 unsigned int brows, bcols;
76
77 // Size of dense matrix sub-blocks
78 int block_size = 1;
79
80 BCOMatType bcoMat;
81
82 // assuming current sparse format allows
83 // block-sparse data representation
84
85 if (pSession->DefinesParameter("SparseBlockSize"))
86 {
87 pSession->LoadParameter("SparseBlockSize", block_size);
88 ASSERTL1(block_size > 0,
89 "SparseBlockSize parameter must to be positive");
90 }
91
92 brows = rows / block_size + (rows % block_size > 0);
93 bcols = columns / block_size + (columns % block_size > 0);
94
95 if (rows % block_size > 0)
96 {
97 m_copyOp = true;
98 }
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 {
159 m_smvbsrmatrix->Multiply(in, out);
160 }
161 }
162 else
163 {
164 // if block size makes the last row/column bigger, one needs
165 // using temporary storage for rhs and result vectors.
166 Vmath::Vcopy(m_rows, &in[0], 1, &m_tmpin[0], 1);
167
168 if (m_smvbsrmatrix)
169 {
170 m_smvbsrmatrix->Multiply(m_tmpin, m_tmpout);
171 }
172
173 Vmath::Vcopy(m_rows, &m_tmpout[0], 1, &out[0], 1);
174 }
175
177}
178
180{
181 if (m_smvbsrmatrix)
182 {
183 return m_smvbsrmatrix->GetMulCallsCounter();
184 }
185 return -1;
186}
187
189{
190 if (m_smvbsrmatrix)
191 {
192 return m_smvbsrmatrix->GetNumNonZeroEntries();
193 }
194 return -1;
195}
196
197} // namespace Nektar::MultiRegions
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:242
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:80
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:69
Array< OneD, NekDouble > m_tmpout
Definition: GlobalMatrix.h:73
unsigned long GetMulCallsCounter() const
unsigned int GetNumNonZeroEntries() const
Array< OneD, NekDouble > m_tmpin
Definition: GlobalMatrix.h:72
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[]
std::map< CoordType, BCOEntryType > BCOMatType
void convertCooToBco(const unsigned int blkDim, const COOMatType &cooMat, BCOMatType &bcoMat)
Definition: SparseUtils.cpp:46
std::map< CoordType, NekDouble > COOMatType
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:825
STL namespace.