Nektar++
SparseUtils.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: SparseUtils.hpp
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 <utility>
36 #include <map>
37 
38 #include <boost/core/ignore_unused.hpp>
39 
45 
46 
47 namespace Nektar{
48 
50  const unsigned int blkDim,
51  const COOMatType& cooMat,
52  BCOMatType& bcoMat)
53  {
54  COOMatTypeConstIt entry;
56  int rowcoord, localRow, blkRowCoord;
57  int colcoord, localCol, blkColCoord;
58 
59  for(entry = cooMat.begin(); entry != cooMat.end(); entry++)
60  {
61  rowcoord = (entry->first).first;
62  colcoord = (entry->first).second;
63 
64  blkRowCoord = rowcoord / blkDim;
65  blkColCoord = colcoord / blkDim;
66 
67  CoordType blkCoords = std::make_pair(blkRowCoord,blkColCoord);
68  blk = bcoMat.find(blkCoords);
69  if (blk == bcoMat.end())
70  {
71  BCOEntryType b(blkDim*blkDim, 0.0);
72  bcoMat[blkCoords] = b;
73  }
74 
75  localRow = rowcoord % blkDim;
76  localCol = colcoord % blkDim;
77 
78  // transpose it: NIST SpBLAS expects Fortran ordering
79  // of dense subblocks
80  const unsigned int localoffset = localRow + localCol*blkDim;
81  (bcoMat[blkCoords])[localoffset] = entry->second;
82  }
83  }
84 
85  template<typename SparseStorageType>
86  std::ostream& operator<<(std::ostream& os, const NekSparseMatrix<SparseStorageType>& rhs)
87  {
88  int oswidth = 9;
89  int osprecision = 6;
90 
91  for(unsigned int i = 0; i < rhs.GetRows(); ++i)
92  {
93  os << "[";
94  for(unsigned int j = 0; j < rhs.GetColumns(); ++j)
95  {
96  os.width(oswidth);
97  os.precision(osprecision);
98  os << rhs(i,j);
99  if( j != rhs.GetColumns() - 1 )
100  {
101  os << ", ";
102  }
103  }
104  os << "]";
105  if( i != rhs.GetRows()-1 )
106  {
107  os << std::endl;
108  }
109  }
110  return os;
111  }
112 
113  template<typename SparseStorageType>
114  std::ostream& operator<<(std::ostream& os, const NekSparseDiagBlkMatrix<SparseStorageType>& rhs)
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<<(std::ostream& os, const NekSparseMatrix<StorageSmvBsr<NekDouble> >& rhs);
142  template std::ostream& operator<<(std::ostream& os, const NekSparseDiagBlkMatrix<StorageSmvBsr<NekDouble> >& rhs);
143 
144 } // namespace
std::map< CoordType, NekDouble > COOMatType
COOMatType::const_iterator COOMatTypeConstIt
BCOMatType::const_iterator BCOMatTypeConstIt
std::map< CoordType, BCOEntryType > BCOMatType
void convertCooToBco(const unsigned int blkDim, const COOMatType &cooMat, BCOMatType &bcoMat)
Definition: SparseUtils.cpp:49
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs
std::pair< IndexType, IndexType > CoordType