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
42
43namespace Nektar
44{
45
46void convertCooToBco(const unsigned int blkDim, const COOMatType &cooMat,
47 BCOMatType &bcoMat)
48{
51 int rowcoord, localRow, blkRowCoord;
52 int colcoord, localCol, blkColCoord;
53
54 for (entry = cooMat.begin(); entry != cooMat.end(); entry++)
55 {
56 rowcoord = (entry->first).first;
57 colcoord = (entry->first).second;
58
59 blkRowCoord = rowcoord / blkDim;
60 blkColCoord = colcoord / blkDim;
61
62 CoordType blkCoords = std::make_pair(blkRowCoord, blkColCoord);
63 blk = bcoMat.find(blkCoords);
64 if (blk == bcoMat.end())
65 {
66 BCOEntryType b(blkDim * blkDim, 0.0);
67 bcoMat[blkCoords] = b;
68 }
69
70 localRow = rowcoord % blkDim;
71 localCol = colcoord % blkDim;
72
73 // transpose it: NIST SpBLAS expects Fortran ordering
74 // of dense subblocks
75 const unsigned int localoffset = localRow + localCol * blkDim;
76 (bcoMat[blkCoords])[localoffset] = entry->second;
77 }
78}
79
80template <typename SparseStorageType>
81std::ostream &operator<<(std::ostream &os,
83{
84 int oswidth = 9;
85 int osprecision = 6;
86
87 for (unsigned int i = 0; i < rhs.GetRows(); ++i)
88 {
89 os << "[";
90 for (unsigned int j = 0; j < rhs.GetColumns(); ++j)
91 {
92 os.width(oswidth);
93 os.precision(osprecision);
94 os << rhs(i, j);
95 if (j != rhs.GetColumns() - 1)
96 {
97 os << ", ";
98 }
99 }
100 os << "]";
101 if (i != rhs.GetRows() - 1)
102 {
103 os << std::endl;
104 }
105 }
106 return os;
107}
108
109template <typename SparseStorageType>
110std::ostream &operator<<(std::ostream &os,
112{
113 int oswidth = 9;
114 int osprecision = 6;
115
116 for (unsigned int i = 0; i < rhs.GetRows(); ++i)
117 {
118 os << "[";
119 for (unsigned int j = 0; j < rhs.GetColumns(); ++j)
120 {
121 os.width(oswidth);
122 os.precision(osprecision);
123 os << rhs(i, j);
124 if (j != rhs.GetColumns() - 1)
125 {
126 os << ", ";
127 }
128 }
129 os << "]";
130 if (i != rhs.GetRows() - 1)
131 {
132 os << std::endl;
133 }
134 }
135 return os;
136}
137
138template std::ostream &operator<<(
139 std::ostream &os, const NekSparseMatrix<StorageSmvBsr<NekDouble>> &rhs);
140template std::ostream &operator<<(
141 std::ostream &os,
143
144} // namespace Nektar
IndexType GetRows() const
IndexType GetColumns() const
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:46
std::map< CoordType, NekDouble > COOMatType
std::ostream & operator<<(std::ostream &os, const NekMatrix< DataType, FormType > &rhs)
Definition: NekMatrix.hpp:49
BCOMatType::const_iterator BCOMatTypeConstIt