3 #ifndef DUNE_ISTL_MATRIXMARKET_HH
4 #define DUNE_ISTL_MATRIXMARKET_HH
15 #include <dune/common/fmatrix.hh>
16 #include <dune/common/tuples.hh>
17 #include <dune/common/unused.hh>
47 namespace MatrixMarketImpl
78 static std::string
str()
94 static std::string
str()
159 template<
typename T,
typename A,
int i,
int j>
164 os<<
"%%MatrixMarket matrix coordinate ";
165 os<<mm_numeric_type<T>::str()<<
" general"<<std::endl;
169 template<
typename B,
typename A>
174 os<<
"%%MatrixMarket matrix array ";
175 os<<mm_numeric_type<typename B::field_type>::str()<<
" general"<<std::endl;
179 template<
typename T,
int j>
184 os<<
"%%MatrixMarket matrix array ";
185 os<<mm_numeric_type<T>::str()<<
" general"<<std::endl;
189 template<
typename T,
int i,
int j>
194 os<<
"%%MatrixMarket matrix array ";
195 os<<mm_numeric_type<T>::str()<<
" general"<<std::endl;
210 template<
typename T,
typename A,
int i>
215 static void print(std::ostream& os,
const M&)
217 os<<
"% ISTL_STRUCT blocked ";
218 os<<i<<
" "<<1<<std::endl;
222 template<
typename T,
typename A,
int i,
int j>
227 static void print(std::ostream& os,
const M&)
229 os<<
"% ISTL_STRUCT blocked ";
230 os<<i<<
" "<<j<<std::endl;
235 template<
typename T,
int i,
int j>
240 static void print(std::ostream& os,
const M& m)
244 template<
typename T,
int i>
247 typedef FieldVector<T,i>
M;
249 static void print(std::ostream& os,
const M& m)
304 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
319 std::cout<<buffer<<std::endl;
321 if(buffer!=
"%%MatrixMarket") {
323 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
334 if(buffer !=
"matrix")
337 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
351 std::transform(buffer.begin(), buffer.end(), buffer.begin(),
358 if(buffer !=
"array")
360 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
367 if(buffer !=
"coordinate")
369 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
375 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
389 std::transform(buffer.begin(), buffer.end(), buffer.begin(),
395 if(buffer !=
"integer")
397 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
406 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
413 if(buffer !=
"complex")
415 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
422 if(buffer !=
"pattern")
424 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
430 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
439 std::transform(buffer.begin(), buffer.end(), buffer.begin(),
445 if(buffer !=
"general")
447 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
454 if(buffer !=
"hermitian")
456 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
462 if(buffer.size()==1) {
463 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
471 if(buffer !=
"symmetric")
473 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
480 if(buffer !=
"skew-symmetric")
482 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
488 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
492 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
495 file.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
501 template<std::
size_t brows, std::
size_t bcols>
502 Dune::tuple<std::size_t, std::size_t, std::size_t>
505 std::size_t blockrows=rows/brows;
506 std::size_t blockcols=cols/bcols;
507 std::size_t blocksize=brows*bcols;
508 std::size_t blockentries=0;
513 blockentries = entries/blocksize;
break;
515 blockentries = 2*entries/blocksize;
break;
517 blockentries = (2*entries-rows)/blocksize;
break;
519 blockentries = (2*entries-rows)/blocksize;
break;
521 throw Dune::NotImplemented();
523 return Dune::make_tuple(blockrows, blockcols, blockentries);
577 DUNE_UNUSED_PARAMETER(num);
589 return i1.
index<i2.index;
603 return is>>data.number;
612 template<
typename D,
int brows,
int bcols>
624 for(
typename M::RowIterator iter=matrix.begin();
625 iter!= matrix.end(); ++iter)
627 for(
typename M::size_type brow=iter.index()*brows,
628 browend=iter.index()*brows+brows;
629 brow<browend; ++brow)
631 typedef typename std::set<IndexData<D> >::const_iterator Siter;
632 for(Siter siter=rows[brow].begin(), send=rows[brow].end();
633 siter != send; ++siter)
634 (*iter)[siter->index/bcols][brow%brows][siter->index%bcols]=siter->number;
640 template<
int brows,
int bcols>
649 template<
typename T,
typename A,
int brows,
int bcols,
typename D>
651 std::istream& file, std::size_t entries,
658 std::vector<std::set<IndexData<D> > > rows(matrix.N()*brows);
660 for(; entries>0; --entries) {
666 assert(row/bcols<matrix.N());
668 assert(data.index/bcols<matrix.M());
669 rows[
row].insert(data);
674 DUNE_THROW(Dune::NotImplemented,
"Only general is supported right now!");
678 for(
typename Matrix::CreateIterator iter=matrix.createbegin();
679 iter!= matrix.createend(); ++iter)
681 for(std::size_t brow=iter.index()*brows, browend=iter.index()*brows+brows;
682 brow<browend; ++brow)
684 typedef typename std::set<IndexData<D> >::const_iterator Siter;
685 for(Siter siter=rows[brow].begin(), send=rows[brow].end();
686 siter != send; ++siter, ++nnz)
687 iter.insert(siter->index/bcols);
696 Setter(rows, matrix);
708 using namespace MatrixMarketImpl;
711 std::cerr <<
"First line was not a correct Matrix Market banner. Using default:\n"
712 <<
"%%MatrixMarket matrix coordinate real general"<<std::endl;
715 istr.seekg(0, std::ios::beg);
732 template<
typename T,
typename A,
int entries>
737 for(
int i=0; size>0; ++i, --size) {
740 vector[i/entries][i%entries]=val;
751 template<
typename T,
typename A,
int entries>
755 using namespace MatrixMarketImpl;
758 std::size_t rows, cols;
766 std::size_t size=rows/entries;
767 if(size*entries!=rows)
772 istr.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
784 template<
typename T,
typename A,
int brows,
int bcols>
788 using namespace MatrixMarketImpl;
792 std::cerr <<
"First line was not a correct Matrix Market banner. Using default:\n"
793 <<
"%%MatrixMarket matrix coordinate real general"<<std::endl;
796 istr.seekg(0, std::ios::beg);
800 std::size_t rows, cols, entries;
816 std::size_t nnz, blockrows, blockcols;
818 Dune::tie(blockrows, blockcols, nnz) = calculateNNZ<brows, bcols>(rows, cols, entries, header);
820 istr.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
823 matrix.setSize(blockrows, blockcols);
827 DUNE_THROW(Dune::NotImplemented,
"Array format currently not supported for matrices!");
836 template<
typename B,
int i,
int j,
typename A>
845 template<
typename B,
int i,
int j>
853 for(riterator
row=entry.begin();
row != entry.end(); ++
row, ++rowidx) {
856 ostr<< rowidx<<
" "<<coli<<
" "<<*
col<<std::endl;
863 const integral_constant<int,1>&)
865 ostr<<entry<<std::endl;
871 const integral_constant<int,0>&)
873 using namespace MatrixMarketImpl;
876 const int isnumeric = mm_numeric_type<typename V::block_type>::is_numeric;
877 typedef typename V::const_iterator VIter;
879 for(VIter i=vector.begin(); i != vector.end(); ++i)
882 integral_constant<int,isnumeric>());
885 template<
typename T,
typename A,
int i>
888 return vector.size()*i;
894 const integral_constant<int,0>&)
896 using namespace MatrixMarketImpl;
899 const int isnumeric = mm_numeric_type<typename V::block_type>::is_numeric;
907 const integral_constant<int,1>&)
913 typedef typename M::const_iterator riterator;
914 typedef typename M::ConstColIterator citerator;
915 for(riterator
row=matrix.begin();
row != matrix.end(); ++
row)
930 using namespace MatrixMarketImpl;
933 mm_header_printer<M>::print(ostr);
934 mm_block_structure_header<M>::print(ostr,matrix);
952 std::string filename)
954 std::ofstream file(filename.c_str());
955 file.setf(std::ios::scientific,std::ios::floatfield);
974 template<
typename M,
typename G,
typename L>
976 std::string filename,
978 bool storeIndices=
true)
983 std::ostringstream rfilename;
984 rfilename<<filename <<
"_"<<rank<<
".mm";
985 std::cout<< rfilename.str()<<std::endl;
986 std::ofstream file(rfilename.str().c_str());
987 file.setf(std::ios::scientific,std::ios::floatfield);
996 rfilename<<filename<<
"_"<<rank<<
".idx";
997 file.open(rfilename.str().c_str());
998 file.setf(std::ios::scientific,std::ios::floatfield);
1000 typedef typename IndexSet::const_iterator Iterator;
1001 for(Iterator iter = comm.
indexSet().begin();
1002 iter != comm.
indexSet().end(); ++iter) {
1003 file << iter->global()<<
" "<<(std::size_t)iter->local()<<
" "
1004 <<(int)iter->local().attribute()<<
" "<<(int)iter->local().isPublic()<<std::endl;
1007 file<<
"neighbours:";
1008 const std::set<int>& neighbours=comm.
remoteIndices().getNeighbours();
1009 typedef std::set<int>::const_iterator SIter;
1010 for(SIter neighbour=neighbours.begin(); neighbour != neighbours.end(); ++neighbour) {
1011 file<<
" "<< *neighbour;
1030 template<
typename M,
typename G,
typename L>
1032 const std::string& filename,
1034 bool readIndices=
true)
1036 using namespace MatrixMarketImpl;
1039 typedef typename LocalIndex::Attribute Attribute;
1043 std::ostringstream rfilename;
1044 rfilename<<filename <<
"_"<<rank<<
".mm";
1046 file.open(rfilename.str().c_str(), std::ios::in);
1048 DUNE_THROW(IOError,
"Could not open file" << rfilename.str().c_str());
1059 IndexSet& pis=comm.pis;
1061 rfilename<<filename<<
"_"<<rank<<
".idx";
1062 file.open(rfilename.str().c_str());
1064 DUNE_THROW(InvalidIndexSetState,
"Index set is not empty!");
1067 while(!file.eof() && file.peek()!=
'n') {
1076 pis.add(g,LocalIndex(l,Attribute(c),b));
1084 if(s!=
"neighbours:")
1087 while(!file.eof()) {
1093 comm.ri.setNeighbours(nb);
1095 comm.ri.template rebuild<false>();
1110 template<
typename M>
1112 const std::string& filename)
1115 file.open(filename.c_str(), std::ios::in);
1117 DUNE_THROW(IOError,
"Could not open file" << filename);
Definition: matrixmarket.hh:566
FieldVector< T, i > M
Definition: matrixmarket.hh:247
Functor to the data values of the matrix.
Definition: matrixmarket.hh:613
A class setting up standard communication for a two-valued attribute set with owner/overlap/copy sema...
Definition: owneroverlapcopy.hh:172
static void print(std::ostream &os, const M &m)
Definition: matrixmarket.hh:249
Implementation of the BCRSMatrix class.
Test whether a type is an ISTL Matrix.
Definition: matrixutils.hh:451
std::size_t countEntries(const BlockVector< FieldVector< T, i >, A > &vector)
Definition: matrixmarket.hh:886
Row row
Definition: matrixmatrix.hh:345
Definition: matrixmarket.hh:253
Utility class for marking the pattern type of the MatrixMarket matrices.
Definition: matrixmarket.hh:562
Definition: basearray.hh:19
Definition: matrixmarket.hh:533
MM_CTYPE ctype
Definition: matrixmarket.hh:268
MMHeader()
Definition: matrixmarket.hh:264
BCRSMatrix< FieldMatrix< T, i, j >, A > M
Definition: matrixmarket.hh:225
Meta program to write the correct Matrix Market header.
Definition: matrixmarket.hh:157
void mm_read_header(std::size_t &rows, std::size_t &cols, MatrixMarketImpl::MMHeader &header, std::istream &istr, bool isVector)
Definition: matrixmarket.hh:704
int countNonZeros(const M &matrix)
Get the number of nonzero fields in the matrix.
Definition: matrixutils.hh:154
const ParallelIndexSet & indexSet() const
Get the underlying parallel index set.
Definition: owneroverlapcopy.hh:464
std::size_t index
Definition: matrixmarket.hh:535
static void print(std::ostream &os, const M &)
Definition: matrixmarket.hh:215
Definition: matrixmarket.hh:258
a wrapper class of numeric values.
Definition: matrixmarket.hh:550
Dune::tuple< std::size_t, std::size_t, std::size_t > calculateNNZ(std::size_t rows, std::size_t cols, std::size_t entries, const MMHeader &header)
Definition: matrixmarket.hh:503
A vector of blocks with memory management.
Definition: bvector.hh:252
static void print(std::ostream &os)
Definition: matrixmarket.hh:172
Metaprogram for writing the ISTL block structure header.
Definition: matrixmarket.hh:208
Col col
Definition: matrixmatrix.hh:347
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:412
Definition: matrixmarket.hh:260
const CollectiveCommunication< MPI_Comm > & communicator() const
Definition: owneroverlapcopy.hh:304
Definition: matrixmarket.hh:833
Matrix & A
Definition: matrixmatrix.hh:216
static void print(std::ostream &os)
Definition: matrixmarket.hh:182
Definition: matrixmarket.hh:258
Definition: matrixmarket.hh:700
BlockVector< FieldVector< T, i >, A > M
Definition: matrixmarket.hh:213
T number
Definition: matrixmarket.hh:552
static std::string str()
Definition: matrixmarket.hh:78
Definition: matrixmarket.hh:260
MM_TYPE type
Definition: matrixmarket.hh:267
LineType
Definition: matrixmarket.hh:253
bool readMatrixMarketBanner(std::istream &file, MMHeader &mmHeader)
Definition: matrixmarket.hh:310
Definition: matrixmarket.hh:256
std::istream & operator>>(std::istream &is, NumericWrapper< T > &num)
Definition: matrixmarket.hh:570
const RemoteIndices & remoteIndices() const
Get the underlying remote indices.
Definition: owneroverlapcopy.hh:473
void skipComments(std::istream &file)
Definition: matrixmarket.hh:296
Definition: matrixmarket.hh:254
Definition: matrixmarket.hh:262
Whether T is a supported numeric type.
Definition: matrixmarket.hh:64
Definition: matrixmarket.hh:258
void readMatrixMarket(Dune::BlockVector< Dune::FieldVector< T, entries >, A > &vector, std::istream &istr)
Reads a BlockVector from a matrix market file.
Definition: matrixmarket.hh:752
void operator()(const std::vector< std::set< IndexData< D > > > &rows, M &matrix)
Sets the matrixvalues.
Definition: matrixmarket.hh:621
static std::string str()
Definition: matrixmarket.hh:126
Definition: matrixmarket.hh:258
void storeMatrixMarket(const M &matrix, std::string filename)
Stores a parallel matrix/vector in matrix market format in a file.
Definition: matrixmarket.hh:951
bool lineFeed(std::istream &file)
Definition: matrixmarket.hh:272
MM_CTYPE
Definition: matrixmarket.hh:258
Definition: matrixutils.hh:24
MM_TYPE
Definition: matrixmarket.hh:256
void readSparseEntries(Dune::BCRSMatrix< Dune::FieldMatrix< T, brows, bcols >, A > &matrix, std::istream &file, std::size_t entries, const MMHeader &mmHeader, const D &)
Definition: matrixmarket.hh:650
Dune::ParallelIndexSet< GlobalIdType, LI, 512 > ParallelIndexSet
The type of the parallel index set.
Definition: owneroverlapcopy.hh:451
Definition: matrixmarket.hh:260
Definition: matrixmarket.hh:253
void mm_read_vector_entries(Dune::BlockVector< Dune::FieldVector< T, entries >, A > &vector, std::size_t size, std::istream &istr)
Definition: matrixmarket.hh:733
void writeMatrixMarket(const V &vector, std::ostream &ostr, const integral_constant< int, 0 > &)
Definition: matrixmarket.hh:893
MM_STRUCTURE
Definition: matrixmarket.hh:260
Definition: matrixmarket.hh:258
static std::string str()
Definition: matrixmarket.hh:142
static void print(std::ostream &os)
Definition: matrixmarket.hh:192
void loadMatrixMarket(M &matrix, const std::string &filename, OwnerOverlapCopyCommunication< G, L > &comm, bool readIndices=true)
Load a parallel matrix/vector stored in matrix market format.
Definition: matrixmarket.hh:1031
static std::string str()
Definition: matrixmarket.hh:94
A generic dynamic dense matrix.
Definition: matrix.hh:24
Helper metaprogram to get the matrix market string representation of the numeric type.
Definition: matrixmarket.hh:59
static void print(std::ostream &os, const M &)
Definition: matrixmarket.hh:227
void mm_print_vector_entry(const V &entry, std::ostream &ostr, const integral_constant< int, 1 > &)
Definition: matrixmarket.hh:862
Definition: matrixmarket.hh:256
static void print(std::ostream &os, const M &m)
Definition: matrixmarket.hh:240
Classes providing communication interfaces for overlapping Schwarz methods.
Definition: matrixmarket.hh:253
Definition: matrixmarket.hh:260
void mm_print_entry(const FieldMatrix< B, i, j > &entry, typename FieldMatrix< B, i, j >::size_type rowidx, typename FieldMatrix< B, i, j >::size_type colidx, std::ostream &ostr)
Definition: matrixmarket.hh:846
void operator()(const std::vector< std::set< IndexData< PatternDummy > > > &rows, M &matrix)
Definition: matrixmarket.hh:644
static std::string str()
Definition: matrixmarket.hh:110
MM_STRUCTURE structure
Definition: matrixmarket.hh:269
static void print(std::ostream &os)
Definition: matrixmarket.hh:162
Definition: matrixmarket.hh:256
Some handy generic functions for ISTL matrices.
FieldMatrix< T, i, j > M
Definition: matrixmarket.hh:238
Definition: matrixmarket.hh:260