1 #ifndef VIENNACL_IO_MATRIX_MARKET_HPP
2 #define VIENNACL_IO_MATRIX_MARKET_HPP
45 inline void trim(
char * buffer,
long max_size)
49 for (
long i=0; i<max_size; ++i)
59 for (
long i=stop; i<max_size; ++i)
68 for (
long i=0; i<=stop -
start; ++i)
70 buffer[i] = buffer[start + i];
74 buffer[stop - start + 1] = 0;
79 inline std::string
tolower(std::string & s)
81 std::transform(s.begin(), s.end(), s.begin(), static_cast < int(*)(int) > (
std::tolower));
101 template<
typename MatrixT>
110 std::ifstream reader(file);
113 bool symmetric =
false;
114 bool dense_format =
false;
115 bool is_header =
true;
116 bool pattern_matrix =
false;
120 long valid_entries = 0;
125 std::cerr <<
"ViennaCL: Matrix Market Reader: Cannot open file " << file << std::endl;
129 while (reader.good())
134 reader.getline(buffer, 1024);
138 while (reader.good() && buffer[0] == 0);
140 if (buffer[0] ==
'%')
142 if (buffer[1] ==
'%')
145 std::stringstream line(std::string(buffer + 2));
149 std::cerr <<
"Error in file " << file <<
" at line " << linenum <<
" in file " << file <<
": Expected 'MatrixMarket', got '" << token <<
"'" << std::endl;
156 std::cerr <<
"Error in file " << file <<
" at line " << linenum <<
" in file " << file <<
": Expected 'matrix', got '" << token <<
"'" << std::endl;
166 std::cerr <<
"Error in file " << file <<
" at line " << linenum <<
" in file " << file <<
": 'array' type is not supported yet!" << std::endl;
171 std::cerr <<
"Error in file " << file <<
" at line " << linenum <<
" in file " << file <<
": Expected 'array' or 'coordinate', got '" << token <<
"'" << std::endl;
179 pattern_matrix =
true;
187 std::cerr <<
"Error in file " << file <<
": The MatrixMarket reader provided with ViennaCL supports only real valued floating point arithmetic or pattern type matrices." << std::endl;
196 std::cerr <<
"Error in file " << file <<
": The MatrixMarket reader provided with ViennaCL supports only general or symmetric matrices." << std::endl;
204 std::stringstream line(std::stringstream::in | std::stringstream::out);
205 line << std::string(buffer);
217 std::cerr <<
"Error in file " << file <<
": Could not get matrix dimensions (rows) in line " << linenum << std::endl;
225 std::cerr <<
"Error in file " << file <<
": Could not get matrix dimensions (columns) in line " << linenum << std::endl;
234 std::cerr <<
"Error in file " << file <<
": Could not get matrix dimensions (columns) in line " << linenum << std::endl;
239 if (rows > 0 && cols > 0)
264 ScalarT value = ScalarT(1);
271 std::cerr <<
"Error in file " << file <<
": Parse error for matrix row entry in line " << linenum << std::endl;
279 std::cerr <<
"Error in file " << file <<
": Parse error for matrix col entry in line " << linenum << std::endl;
295 std::cerr <<
"Error in file " << file <<
": Parse error for matrix entry in line " << linenum << std::endl;
316 if (++valid_entries == nnz)
338 template<
typename MatrixT>
346 template<
typename MatrixT>
348 const std::string & file,
354 template<
typename ScalarT>
363 template<
typename ScalarT>
365 const std::string & file,
374 template<
typename MatrixT>
377 std::ofstream writer(file);
379 long num_entries = 0;
380 for (
typename MatrixT::const_iterator1 row_it = mat.begin1();
381 row_it != mat.end1();
383 for (
typename MatrixT::const_iterator2 col_it = row_it.begin();
384 col_it != row_it.end();
388 writer <<
"%%MatrixMarket matrix coordinate real general" << std::endl;
389 writer << mat.size1() <<
" " << mat.size2() <<
" " << num_entries << std::endl;
391 for (
typename MatrixT::const_iterator1 row_it = mat.begin1();
392 row_it != mat.end1();
394 for (
typename MatrixT::const_iterator2 col_it = row_it.begin();
395 col_it != row_it.end();
397 writer << col_it.index1() + index_base <<
" " << col_it.index2() + index_base <<
" " << *col_it << std::endl;
402 template<
typename ScalarT>
411 template<
typename ScalarT>
413 const std::string & file,
428 template<
typename MatrixT>
430 const std::string & file,
void fill(MatrixType &matrix, vcl_size_t row_index, vcl_size_t col_index, NumericT value)
Generic filler routine for setting an entry of a matrix to a particular value.
void trim(char *buffer, long max_size)
Adapter classes for sparse matrices made of the STL type std::vector >
Generic size and resize functionality for different vector and matrix types.
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
void write_matrix_market_file_impl(MatrixT const &mat, const char *file, long index_base)
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
void write_matrix_market_file(std::vector< std::map< unsigned int, ScalarT > > const &mat, const char *file, long index_base=1)
std::string tolower(std::string &s)
void resize(MatrixType &matrix, vcl_size_t rows, vcl_size_t cols)
Generic resize routine for resizing a matrix (ViennaCL, uBLAS, etc.) to a new size/dimension.
result_of::size_type< T >::type start(T const &obj)
Generic fill functionality for different matrix types.
long read_matrix_market_file_impl(MatrixT &mat, const char *file, long index_base)
Reads a sparse or dense matrix from a file (MatrixMarket format)
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_row > row(const matrix_base< NumericT, F > &A, unsigned int i)
Helper meta function for retrieving the main RAM-based value type. Particularly important to obtain T...
long read_matrix_market_file(MatrixT &mat, const char *file, long index_base=1)
Reads a sparse matrix from a file (MatrixMarket format)