Nektar++
SparseUtils.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: SparseUtils.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: common utility functions for sparse matrices
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <map>
36#include <utility>
37
38#include <boost/core/ignore_unused.hpp>
39
44
45namespace Nektar
46{
47
48void convertCooToBco(const unsigned int blkDim, const COOMatType &cooMat,
49 BCOMatType &bcoMat)
50{
53 int rowcoord, localRow, blkRowCoord;
54 int colcoord, localCol, blkColCoord;
55
56 for (entry = cooMat.begin(); entry != cooMat.end(); entry++)
57 {
58 rowcoord = (entry->first).first;
59 colcoord = (entry->first).second;
60
61 blkRowCoord = rowcoord / blkDim;
62 blkColCoord = colcoord / blkDim;
63
64 CoordType blkCoords = std::make_pair(blkRowCoord, blkColCoord);
65 blk = bcoMat.find(blkCoords);
66 if (blk == bcoMat.end())
67 {
68 BCOEntryType b(blkDim * blkDim, 0.0);
69 bcoMat[blkCoords] = b;
70 }
71
72 localRow = rowcoord % blkDim;
73 localCol = colcoord % blkDim;
74
75 // transpose it: NIST SpBLAS expects Fortran ordering
76 // of dense subblocks
77 const unsigned int localoffset = localRow + localCol * blkDim;
78 (bcoMat[blkCoords])[localoffset] = entry->second;
79 }
80}
81
82template <typename SparseStorageType>
83std::ostream &operator<<(std::ostream &os,
85{
86 int oswidth = 9;
87 int osprecision = 6;
88
89 for (unsigned int i = 0; i < rhs.GetRows(); ++i)
90 {
91 os << "[";
92 for (unsigned int j = 0; j < rhs.GetColumns(); ++j)
93 {
94 os.width(oswidth);
95 os.precision(osprecision);
96 os << rhs(i, j);
97 if (j != rhs.GetColumns() - 1)
98 {
99 os << ", ";
100 }
101 }
102 os << "]";
103 if (i != rhs.GetRows() - 1)
104 {
105 os << std::endl;
106 }
107 }
108 return os;
109}
110
111template <typename SparseStorageType>
112std::ostream &operator<<(std::ostream &os,
114{
115 int oswidth = 9;
116 int osprecision = 6;
117
118 for (unsigned int i = 0; i < rhs.GetRows(); ++i)
119 {
120 os << "[";
121 for (unsigned int j = 0; j < rhs.GetColumns(); ++j)
122 {
123 os.width(oswidth);
124 os.precision(osprecision);
125 os << rhs(i, j);
126 if (j != rhs.GetColumns() - 1)
127 {
128 os << ", ";
129 }
130 }
131 os << "]";
132 if (i != rhs.GetRows() - 1)
133 {
134 os << std::endl;
135 }
136 }
137 return os;
138}
139
140template std::ostream &operator<<(
141 std::ostream &os, const NekSparseMatrix<StorageSmvBsr<NekDouble>> &rhs);
142template std::ostream &operator<<(
143 std::ostream &os,
145
146} // namespace Nektar
IndexType GetRows() const
IndexType GetColumns() const
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
COOMatType::const_iterator COOMatTypeConstIt
std::map< CoordType, BCOEntryType > BCOMatType
std::pair< IndexType, IndexType > CoordType
void convertCooToBco(const unsigned int blkDim, const COOMatType &cooMat, BCOMatType &bcoMat)
Definition: SparseUtils.cpp:48
std::map< CoordType, NekDouble > COOMatType
std::ostream & operator<<(std::ostream &os, const NekMatrix< DataType, FormType > &rhs)
Definition: NekMatrix.hpp:49
BCOMatType::const_iterator BCOMatTypeConstIt