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