// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_ISTL_MATRIXMARKET_HH #define DUNE_ISTL_MATRIXMARKET_HH #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // countNonZeros() #include namespace Dune { /** * @defgroup ISTL_IO IO for matrices and vectors. * @ingroup ISTL_SPMV * @brief Provides methods for reading and writing matrices and vectors * in various formats. * * * Routine printmatrix prints a (sparse matrix with all entries (even zeroes). * Function printvector prints a vector to a stream. * PrintSparseMatrix prints a sparse matrix omitting all nonzeroes. * With writeMatrixToMatlab one can write a matrix in a Matlab readable format. * Using storeMartrixMarket and loadMatrixMarket one can store and load a parallel ISTL * matrix in MatrixMarket format. The latter can even read a matrix written with * writeMatrixToMatlab. * * * @addtogroup ISTL_IO * @{ */ /** @file * @author Markus Blatt * @brief Provides classes for reading and writing MatrixMarket Files with * an extension for parallel matrices. */ namespace MatrixMarketImpl { /** * @brief Helper metaprogram to get * the matrix market string representation * of the numeric type. * * Member function mm_numeric_type::str() * returns the string representation of * the type. */ template struct mm_numeric_type { enum { /** * @brief Whether T is a supported numeric type. */ is_numeric=false }; static std::string str() { return "unknown"; } }; template<> struct mm_numeric_type { enum { /** * @brief Whether T is a supported numeric type. */ is_numeric=true }; static std::string str() { return "integer"; } }; template<> struct mm_numeric_type { enum { /** * @brief Whether T is a supported numeric type. */ is_numeric=true }; static std::string str() { return "real"; } }; template<> struct mm_numeric_type { enum { /** * @brief Whether T is a supported numeric type. */ is_numeric=true }; static std::string str() { return "real"; } }; #if HAVE_GMP template struct mm_numeric_type> { enum { /** * @brief Whether T is a supported numeric type. */ is_numeric=true }; static std::string str() { return "real"; } }; #endif #if HAVE_QUADMATH template<> struct mm_numeric_type { enum { /** * @brief Whether T is a supported numeric type. */ is_numeric=true }; static std::string str() { return "real"; } }; #endif template<> struct mm_numeric_type > { enum { /** * @brief Whether T is a supported numeric type. */ is_numeric=true }; static std::string str() { return "complex"; } }; template<> struct mm_numeric_type > { enum { /** * @brief Whether T is a supported numeric type. */ is_numeric=true }; static std::string str() { return "complex"; } }; /** * @brief Meta program to write the correct Matrix Market * header. * * Member function mm_header::operator() writes the header. * * @tparam M The matrix type. */ template struct mm_header_printer; template struct mm_header_printer > { static void print(std::ostream& os) { os<<"%%MatrixMarket matrix coordinate "; os<::field_type>>::str()<<" general"< struct mm_header_printer > { static void print(std::ostream& os) { os<<"%%MatrixMarket matrix array "; os<::field_type>>::str()<<" general"< struct mm_header_printer > { static void print(std::ostream& os) { os<<"%%MatrixMarket matrix array "; os<::str()<<" general"< struct mm_header_printer > { static void print(std::ostream& os) { os<<"%%MatrixMarket matrix array "; os<::str()<<" general"< struct mm_block_structure_header; template struct mm_block_structure_header > { typedef BlockVector M; static_assert(IsNumber::value, "Only scalar entries are expected here!"); static void print(std::ostream& os, const M&) { os<<"% ISTL_STRUCT blocked "; os<<"1 1"< struct mm_block_structure_header,A> > { typedef BlockVector,A> M; static void print(std::ostream& os, const M&) { os<<"% ISTL_STRUCT blocked "; os< struct mm_block_structure_header > { typedef BCRSMatrix M; static_assert(IsNumber::value, "Only scalar entries are expected here!"); static void print(std::ostream& os, const M&) { os<<"% ISTL_STRUCT blocked "; os<<"1 1"< struct mm_block_structure_header,A> > { typedef BCRSMatrix,A> M; static void print(std::ostream& os, const M&) { os<<"% ISTL_STRUCT blocked "; os< struct mm_block_structure_header > { typedef FieldMatrix M; static void print(std::ostream& /*os*/, const M& /*m*/) {} }; template struct mm_block_structure_header > { typedef FieldVector M; static void print(std::ostream& /*os*/, const M& /*m*/) {} }; enum LineType { MM_HEADER, MM_ISTLSTRUCT, DATA }; enum { MM_MAX_LINE_LENGTH=1025 }; enum MM_TYPE { coordinate_type, array_type, unknown_type }; enum MM_CTYPE { integer_type, double_type, complex_type, pattern, unknown_ctype }; enum MM_STRUCTURE { general, symmetric, skew_symmetric, hermitian, unknown_structure }; struct MMHeader { MMHeader() : type(coordinate_type), ctype(double_type), structure(general) {} MM_TYPE type; MM_CTYPE ctype; MM_STRUCTURE structure; }; inline bool lineFeed(std::istream& file) { char c; if(!file.eof()) c=file.peek(); else return false; // ignore whitespace while(c==' ') { file.get(); if(file.eof()) return false; c=file.peek(); } if(c=='\n') { /* eat the line feed */ file.get(); return true; } return false; } inline void skipComments(std::istream& file) { lineFeed(file); char c=file.peek(); // ignore comment lines while(c=='%') { /* discard the rest of the line */ file.ignore(std::numeric_limits::max(),'\n'); c=file.peek(); } } inline bool readMatrixMarketBanner(std::istream& file, MMHeader& mmHeader) { std::string buffer; char c; file >> buffer; c=buffer[0]; mmHeader=MMHeader(); if(c!='%') return false; dverb<::max(),'\n'); return false; } if(lineFeed(file)) /* premature end of line */ return false; /* read the matrix_type */ file >> buffer; if(buffer != "matrix") { /* discard the rest of the line */ file.ignore(std::numeric_limits::max(),'\n'); return false; } if(lineFeed(file)) /* premature end of line */ return false; /* The type of the matrix */ file >> buffer; if(buffer.empty()) return false; std::transform(buffer.begin(), buffer.end(), buffer.begin(), ::tolower); switch(buffer[0]) { case 'a' : /* sanity check */ if(buffer != "array") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.type=array_type; break; case 'c' : /* sanity check */ if(buffer != "coordinate") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.type=coordinate_type; break; default : file.ignore(std::numeric_limits::max(),'\n'); return false; } if(lineFeed(file)) /* premature end of line */ return false; /* The numeric type used. */ file >> buffer; if(buffer.empty()) return false; std::transform(buffer.begin(), buffer.end(), buffer.begin(), ::tolower); switch(buffer[0]) { case 'i' : /* sanity check */ if(buffer != "integer") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.ctype=integer_type; break; case 'r' : /* sanity check */ if(buffer != "real") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.ctype=double_type; break; case 'c' : /* sanity check */ if(buffer != "complex") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.ctype=complex_type; break; case 'p' : /* sanity check */ if(buffer != "pattern") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.ctype=pattern; break; default : file.ignore(std::numeric_limits::max(),'\n'); return false; } if(lineFeed(file)) return false; file >> buffer; std::transform(buffer.begin(), buffer.end(), buffer.begin(), ::tolower); switch(buffer[0]) { case 'g' : /* sanity check */ if(buffer != "general") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.structure=general; break; case 'h' : /* sanity check */ if(buffer != "hermitian") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.structure=hermitian; break; case 's' : if(buffer.size()==1) { file.ignore(std::numeric_limits::max(),'\n'); return false; } switch(buffer[1]) { case 'y' : /* sanity check */ if(buffer != "symmetric") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.structure=symmetric; break; case 'k' : /* sanity check */ if(buffer != "skew-symmetric") { file.ignore(std::numeric_limits::max(),'\n'); return false; } mmHeader.structure=skew_symmetric; break; default : file.ignore(std::numeric_limits::max(),'\n'); return false; } break; default : file.ignore(std::numeric_limits::max(),'\n'); return false; } file.ignore(std::numeric_limits::max(),'\n'); c=file.peek(); return true; } template std::tuple calculateNNZ(std::size_t rows, std::size_t cols, std::size_t entries, const MMHeader& header) { std::size_t blockrows=rows/brows; std::size_t blockcols=cols/bcols; std::size_t blocksize=brows*bcols; std::size_t blockentries=0; switch(header.structure) { case general : blockentries = entries/blocksize; break; case skew_symmetric : blockentries = 2*entries/blocksize; break; case symmetric : blockentries = (2*entries-rows)/blocksize; break; case hermitian : blockentries = (2*entries-rows)/blocksize; break; default : throw Dune::NotImplemented(); } return std::make_tuple(blockrows, blockcols, blockentries); } /* * @brief Storage class for the column index and the numeric value. * * \tparam T Either a NumericWrapper of the numeric type or PatternDummy * for MatrixMarket pattern case. */ template struct IndexData : public T { std::size_t index = {}; }; /** * @brief a wrapper class of numeric values. * * Use for template specialization to catch the pattern * type MatrixMarket matrices. Uses Empty base class optimization * in the pattern case. * * @tparam T Either a NumericWrapper of the numeric type or PatternDummy * for MatrixMarket pattern case. */ template struct NumericWrapper { T number = {}; operator T&() { return number; } }; /** * @brief Utility class for marking the pattern type of the MatrixMarket matrices. */ struct PatternDummy {}; template<> struct NumericWrapper {}; template std::istream& operator>>(std::istream& is, NumericWrapper& num) { return is>>num.number; } inline std::istream& operator>>(std::istream& is, [[maybe_unused]] NumericWrapper& num) { return is; } /** * @brief LessThan operator. * * It simply compares the index. */ template bool operator<(const IndexData& i1, const IndexData& i2) { return i1.index std::istream& operator>>(std::istream& is, IndexData& data) { is>>data.index; /* MatrixMarket indices are one based. Decrement for C++ */ --data.index; return is>>data.number; } /** * @brief Read IndexData from a stream. Specialization for std::complex * @param is The input stream we read. * @param data Where to store the read data. */ template std::istream& operator>>(std::istream& is, IndexData>>& data) { is>>data.index; /* MatrixMarket indices are one based. Decrement for C++ */ --data.index; // real and imaginary part needs to be read separately as // complex numbers are not provided in pair form. (x,y) NumericWrapper real, imag; is>>real; is>>imag; data.number = {real.number, imag.number}; return is; } /** * @brief Functor to the data values of the matrix. * * This is specialized for PatternDummy. The specialization does not * set anything. */ template struct MatrixValuesSetter { /** * @brief Sets the matrix values. * @param rows The row data as read from file. * @param matrix The matrix whose data we set. */ template void operator()(const std::vector > >& rows, BCRSMatrix& matrix) { static_assert(IsNumber::value && brows==1 && bcols==1, "Only scalar entries are expected here!"); for (auto iter=matrix.begin(); iter!= matrix.end(); ++iter) { auto brow=iter.index(); for (auto siter=rows[brow].begin(); siter != rows[brow].end(); ++siter) (*iter)[siter->index] = siter->number; } } /** * @brief Sets the matrix values. * @param rows The row data as read from file. * @param matrix The matrix whose data we set. */ template void operator()(const std::vector > >& rows, BCRSMatrix >& matrix) { for (auto iter=matrix.begin(); iter!= matrix.end(); ++iter) { for (auto brow=iter.index()*brows, browend=iter.index()*brows+brows; browindex/bcols][brow%brows][siter->index%bcols]=siter->number; } } } }; template struct MatrixValuesSetter { template void operator()(const std::vector > >& /*rows*/, M& /*matrix*/) {} }; template struct is_complex : std::false_type {}; template struct is_complex> : std::true_type {}; // wrapper for std::conj. Returns T if T is not complex. template std::enable_if_t::value, T> conj(const T& r){ return r; } template std::enable_if_t::value, T> conj(const T& r){ return std::conj(r); } template struct mm_multipliers {}; template struct mm_multipliers > { enum { rows = 1, cols = 1 }; }; template struct mm_multipliers,A> > { enum { rows = i, cols = j }; }; template void readSparseEntries(Dune::BCRSMatrix& matrix, std::istream& file, std::size_t entries, const MMHeader& mmHeader, const D&) { typedef Dune::BCRSMatrix Matrix; // Number of rows and columns of T, if it is a matrix (1x1 otherwise) constexpr int brows = mm_multipliers::rows; constexpr int bcols = mm_multipliers::cols; // First path // store entries together with column index in a separate // data structure std::vector > > rows(matrix.N()*brows); auto readloop = [&] (auto symmetryFixup) { for(std::size_t i = 0; i < entries; ++i) { std::size_t row; IndexData data; skipComments(file); file>>row; --row; // Index was 1 based. assert(row/bcols>data; assert(data.index/bcols data_sym(data); data_sym.index = row; rows[data.index].insert(data_sym); }); break; case skew_symmetric : readloop([&](auto row, auto data) { IndexData data_sym; data_sym.number = -data.number; data_sym.index = row; rows[data.index].insert(data_sym); }); break; case hermitian : readloop([&](auto row, auto data) { IndexData data_sym; data_sym.number = conj(data.number); data_sym.index = row; rows[data.index].insert(data_sym); }); break; default: DUNE_THROW(Dune::NotImplemented, "Only general, symmetric, skew-symmetric and hermitian is supported right now!"); } // Setup the matrix sparsity pattern int nnz=0; for(typename Matrix::CreateIterator iter=matrix.createbegin(); iter!= matrix.createend(); ++iter) { for(std::size_t brow=iter.index()*brows, browend=iter.index()*brows+brows; brow >::const_iterator Siter; for(Siter siter=rows[brow].begin(), send=rows[brow].end(); siter != send; ++siter, ++nnz) iter.insert(siter->index/bcols); } } //Set the matrix values matrix=0; MatrixValuesSetter Setter; Setter(rows, matrix); } inline std::tuple splitFilename(const std::string& filename) { std::size_t lastdot = filename.find_last_of("."); if(lastdot == std::string::npos) return std::make_tuple(filename, ""); else { std::string potentialFileExtension = filename.substr(lastdot); if (potentialFileExtension == ".mm" || potentialFileExtension == ".mtx") return std::make_tuple(filename.substr(0, lastdot), potentialFileExtension); else return std::make_tuple(filename, ""); } } } // end namespace MatrixMarketImpl class MatrixMarketFormatError : public Dune::Exception {}; inline void mm_read_header(std::size_t& rows, std::size_t& cols, MatrixMarketImpl::MMHeader& header, std::istream& istr, bool isVector) { using namespace MatrixMarketImpl; if(!readMatrixMarketBanner(istr, header)) { std::cerr << "First line was not a correct Matrix Market banner. Using default:\n" << "%%MatrixMarket matrix coordinate real general"<> rows; if(lineFeed(istr)) throw MatrixMarketFormatError(); istr >> cols; } template void mm_read_vector_entries(Dune::BlockVector& vector, std::size_t size, std::istream& istr, size_t lane) { for (int i=0; size>0; ++i, --size) istr>>Simd::lane(lane,vector[i]); } template void mm_read_vector_entries(Dune::BlockVector,A>& vector, std::size_t size, std::istream& istr, size_t lane) { for(int i=0; size>0; ++i, --size) { Simd::Scalar val; istr>>val; Simd::lane(lane, vector[i/entries][i%entries])=val; } } /** * @brief Reads a BlockVector from a matrix market file. * @param vector The vector to store the data in. * @param istr The input stream to read the data from. * @warning Not all formats are supported! */ template void readMatrixMarket(Dune::BlockVector& vector, std::istream& istr) { typedef typename Dune::BlockVector::field_type field_type; using namespace MatrixMarketImpl; MMHeader header; std::size_t rows, cols; mm_read_header(rows,cols,header,istr, true); if(cols!=Simd::lanes()) { if(Simd::lanes() == 1) DUNE_THROW(MatrixMarketFormatError, "cols!=1, therefore this is no vector!"); else DUNE_THROW(MatrixMarketFormatError, "cols does not match the number of lanes in the field_type!"); } if(header.type!=array_type) DUNE_THROW(MatrixMarketFormatError, "Vectors have to be stored in array format!"); if constexpr (Dune::IsNumber()) vector.resize(rows); else { T dummy; auto blocksize = dummy.size(); std::size_t size=rows/blocksize; if(size*blocksize!=rows) DUNE_THROW(MatrixMarketFormatError, "Block size of vector is not correct!"); vector.resize(size); } istr.ignore(std::numeric_limits::max(),'\n'); for(size_t l=0;l();++l){ mm_read_vector_entries(vector, rows, istr, l); } } /** * @brief Reads a sparse matrix from a matrix market file. * @param matrix The matrix to store the data in. * @param istr The input stream to read the data from. * @warning Not all formats are supported! */ template void readMatrixMarket(Dune::BCRSMatrix& matrix, std::istream& istr) { using namespace MatrixMarketImpl; using Matrix = Dune::BCRSMatrix; MMHeader header; if(!readMatrixMarketBanner(istr, header)) { std::cerr << "First line was not a correct Matrix Market banner. Using default:\n" << "%%MatrixMarket matrix coordinate real general"<> rows; if(lineFeed(istr)) throw MatrixMarketFormatError(); istr >> cols; if(lineFeed(istr)) throw MatrixMarketFormatError(); istr >>entries; std::size_t nnz, blockrows, blockcols; // Number of rows and columns of T, if it is a matrix (1x1 otherwise) constexpr int brows = mm_multipliers::rows; constexpr int bcols = mm_multipliers::cols; std::tie(blockrows, blockcols, nnz) = calculateNNZ(rows, cols, entries, header); istr.ignore(std::numeric_limits::max(),'\n'); matrix.setSize(blockrows, blockcols, nnz); matrix.setBuildMode(Dune::BCRSMatrix::row_wise); if(header.type==array_type) DUNE_THROW(Dune::NotImplemented, "Array format currently not supported for matrices!"); readSparseEntries(matrix, istr, entries, header, NumericWrapper()); } // Print a scalar entry template void mm_print_entry(const B& entry, std::size_t rowidx, std::size_t colidx, std::ostream& ostr) { if constexpr (IsNumber()) ostr << rowidx << " " << colidx << " " << entry << std::endl; else { for (auto row=entry.begin(); row != entry.end(); ++row, ++rowidx) { int coli=colidx; for (auto col = row->begin(); col != row->end(); ++col, ++coli) ostr<< rowidx<<" "< void mm_print_vector_entry(const V& entry, std::ostream& ostr, const std::integral_constant&, size_t lane) { ostr< void mm_print_vector_entry(const V& vector, std::ostream& ostr, const std::integral_constant&, size_t lane) { using namespace MatrixMarketImpl; // Is the entry a supported numeric type? const int isnumeric = mm_numeric_type>::is_numeric; typedef typename V::const_iterator VIter; for(VIter i=vector.begin(); i != vector.end(); ++i) mm_print_vector_entry(*i, ostr, std::integral_constant(), lane); } template std::size_t countEntries(const BlockVector& vector) { return vector.size(); } template std::size_t countEntries(const BlockVector,A>& vector) { return vector.size()*i; } // Version for writing vectors. template void writeMatrixMarket(const V& vector, std::ostream& ostr, const std::integral_constant&) { using namespace MatrixMarketImpl; typedef typename V::field_type field_type; ostr<()<>::is_numeric; for(size_t l=0;l(); ++l){ mm_print_vector_entry(vector,ostr, std::integral_constant(), l); } } // Versions for writing matrices template void writeMatrixMarket(const M& matrix, std::ostream& ostr, const std::integral_constant&) { ostr<::rows<<" " <::cols<<" " <begin(); col != row->end(); ++col) // Matrix Market indexing start with 1! mm_print_entry(*col, row.index()*MatrixMarketImpl::mm_multipliers::rows+1, col.index()*MatrixMarketImpl::mm_multipliers::cols+1, ostr); } /** * @brief writes a ISTL matrix or vector to a stream in matrix market format. */ template void writeMatrixMarket(const M& matrix, std::ostream& ostr) { using namespace MatrixMarketImpl; // Write header information mm_header_printer::print(ostr); mm_block_structure_header::print(ostr,matrix); // Choose the correct function for matrix and vector writeMatrixMarket(matrix,ostr,std::integral_constant::value>()); } static const int default_precision = -1; /** * @brief Stores a parallel matrix/vector in matrix market format in a file. * * More about the matrix market exchange format can be found * here. * * @param matrix The matrix/vector to store. * @param filename the name of the filename (without suffix and rank!) * rank i will write the file filename_i.mm * @param prec Set the decimal precision to be used */ template void storeMatrixMarket(const M& matrix, std::string filename, int prec=default_precision) { auto [pureFilename, extension] = MatrixMarketImpl::splitFilename(filename); std::string rfilename; std::ofstream file; if (extension != "") { rfilename = pureFilename + extension; file.open(rfilename.c_str()); if(!file) DUNE_THROW(IOError, "Could not open file for storage: " << rfilename.c_str()); } else { // only try .mm so we do not ignore potential errors rfilename = pureFilename + ".mm"; file.open(rfilename.c_str()); if(!file) DUNE_THROW(IOError, "Could not open file for storage: " << rfilename.c_str()); } file.setf(std::ios::scientific,std::ios::floatfield); if(prec>0) file.precision(prec); writeMatrixMarket(matrix, file); file.close(); } #if HAVE_MPI /** * @brief Stores a parallel matrix/vector in matrix market format in a file. * * More about the matrix market exchange format can be found * here. * * @param matrix The matrix/vector to store. * @param filename the name of the filename (without suffix and rank!) * rank i will write the file filename_i.mm * @param comm The information about the data distribution. * @param storeIndices Whether to store the parallel index information. * If true rank i writes the index information to file filename_i.idx. * @param prec Set the decimal precision to be used */ template void storeMatrixMarket(const M& matrix, std::string filename, const OwnerOverlapCopyCommunication& comm, bool storeIndices=true, int prec=default_precision) { // Get our rank int rank = comm.communicator().rank(); // Write the local matrix auto [pureFilename, extension] = MatrixMarketImpl::splitFilename(filename); std::string rfilename; std::ofstream file; if (extension != "") { rfilename = pureFilename + "_" + std::to_string(rank) + extension; file.open(rfilename.c_str()); dverb<< rfilename <0) file.precision(prec); writeMatrixMarket(matrix, file); file.close(); if(!storeIndices) return; // Write the global to local index mapping rfilename = pureFilename + "_" + std::to_string(rank) + ".idx"; file.open(rfilename.c_str()); if(!file) DUNE_THROW(IOError, "Could not open file for storage: " << rfilename.c_str()); file.setf(std::ios::scientific,std::ios::floatfield); typedef typename OwnerOverlapCopyCommunication::ParallelIndexSet IndexSet; typedef typename IndexSet::const_iterator Iterator; for(Iterator iter = comm.indexSet().begin(); iter != comm.indexSet().end(); ++iter) { file << iter->global()<<" "<<(std::size_t)iter->local()<<" " <<(int)iter->local().attribute()<<" "<<(int)iter->local().isPublic()<& neighbours=comm.remoteIndices().getNeighbours(); typedef std::set::const_iterator SIter; for(SIter neighbour=neighbours.begin(); neighbour != neighbours.end(); ++neighbour) { file<<" "<< *neighbour; } file.close(); } /** * @brief Load a parallel matrix/vector stored in matrix market format. * * More about the matrix market exchange format can be found * here. * * @param matrix Where to store the matrix/vector. * @param filename the name of the filename (without suffix and rank!) * rank i will read the file filename_i.mm * @param comm The information about the data distribution. * @param readIndices Whether to read the parallel index information. * If true rank i reads the index information form file filename_i.idx * And builds the remote indices information. */ template void loadMatrixMarket(M& matrix, const std::string& filename, OwnerOverlapCopyCommunication& comm, bool readIndices=true) { using namespace MatrixMarketImpl; using LocalIndexT = typename OwnerOverlapCopyCommunication::ParallelIndexSet::LocalIndex; typedef typename LocalIndexT::Attribute Attribute; // Get our rank int rank = comm.communicator().rank(); // load local matrix auto [pureFilename, extension] = MatrixMarketImpl::splitFilename(filename); std::string rfilename; std::ifstream file; if (extension != "") { rfilename = pureFilename + "_" + std::to_string(rank) + extension; file.open(rfilename.c_str(), std::ios::in); dverb<< rfilename <::ParallelIndexSet IndexSet; IndexSet& pis=comm.pis; rfilename = pureFilename + "_" + std::to_string(rank) + ".idx"; file.open(rfilename.c_str()); if(!file) DUNE_THROW(IOError, "Could not open file: " << rfilename.c_str()); if(pis.size()!=0) DUNE_THROW(InvalidIndexSetState, "Index set is not empty!"); pis.beginResize(); while(!file.eof() && file.peek()!='n') { G g; file >>g; std::size_t l; file >>l; int c; file >>c; bool b; file >> b; pis.add(g,LocalIndexT(l,Attribute(c),b)); lineFeed(file); } pis.endResize(); if(!file.eof()) { // read neighbours std::string s; file>>s; if(s!="neighbours:") DUNE_THROW(MatrixMarketFormatError, "was expecting the string: \"neighbours:\""); std::set nb; while(!file.eof()) { int i; file >> i; nb.insert(i); } file.close(); comm.ri.setNeighbours(nb); } comm.ri.template rebuild(); } #endif /** * @brief Load a matrix/vector stored in matrix market format. * * More about the matrix market exchange format can be found * here. * * @param matrix Where to store the matrix/vector. * @param filename the name of the filename (without suffix and rank!) * rank i will read the file filename_i.mm */ template void loadMatrixMarket(M& matrix, const std::string& filename) { auto [pureFilename, extension] = MatrixMarketImpl::splitFilename(filename); std::string rfilename; std::ifstream file; if (extension != "") { rfilename = pureFilename + extension; file.open(rfilename.c_str()); if(!file) DUNE_THROW(IOError, "Could not open file: " << rfilename.c_str()); } else { // try both .mm and .mtx rfilename = pureFilename + ".mm"; file.open(rfilename.c_str(), std::ios::in); if(!file) { rfilename = pureFilename + ".mtx"; file.open(rfilename.c_str(), std::ios::in); if(!file) DUNE_THROW(IOError, "Could not open file: " << rfilename.c_str()); } } readMatrixMarket(matrix,file); file.close(); } /** @} */ } #endif