1 #ifndef VIENNACL_IO_MATRIX_MARKET_HPP
2 #define VIENNACL_IO_MATRIX_MARKET_HPP
44 void trim(
char * buffer,
long max_size)
48 for (
long i=0; i<max_size; ++i)
58 for (
long i=stop; i<max_size; ++i)
67 for (
long i=0; i<=stop -
start; ++i)
69 buffer[i] = buffer[start + i];
73 buffer[stop - start + 1] = 0;
78 std::string tolower(std::string & s)
80 std::transform(s.begin(), s.end(), s.begin(), static_cast < int(*)(int) > (std::tolower));
98 template <
typename MatrixType>
103 std::cout <<
"Reading matrix market file" << std::endl;
105 std::ifstream reader(file);
108 bool symmetric =
false;
109 bool dense_format =
false;
110 bool is_header =
true;
113 long valid_entries = 0;
118 std::cerr <<
"ViennaCL: Matrix Market Reader: Cannot open file " << file << std::endl;
122 while (reader.good())
127 reader.getline(buffer, 1024);
131 while (reader.good() && buffer[0] == 0);
133 if (buffer[0] ==
'%')
135 if (buffer[1] ==
'%')
138 std::stringstream line(std::string(buffer + 2));
140 if (tolower(token) !=
"matrixmarket")
142 std::cerr <<
"Error in file " << file <<
" at line " << linenum <<
" in file " << file <<
": Expected 'MatrixMarket', got '" << token <<
"'" << std::endl;
147 if (tolower(token) !=
"matrix")
149 std::cerr <<
"Error in file " << file <<
" at line " << linenum <<
" in file " << file <<
": Expected 'matrix', got '" << token <<
"'" << std::endl;
154 if (tolower(token) !=
"coordinate")
156 if (tolower(token) ==
"array")
159 std::cerr <<
"Error in file " << file <<
" at line " << linenum <<
" in file " << file <<
": 'array' type is not supported yet!" << std::endl;
164 std::cerr <<
"Error in file " << file <<
" at line " << linenum <<
" in file " << file <<
": Expected 'array' or 'coordinate', got '" << token <<
"'" << std::endl;
170 if (tolower(token) !=
"real")
172 std::cerr <<
"Error in file " << file <<
": The MatrixMarket reader provided with ViennaCL supports only real valued floating point arithmetic." << std::endl;
177 if (tolower(token) ==
"general"){ }
178 else if (tolower(token) ==
"symmetric"){ symmetric =
true; }
181 std::cerr <<
"Error in file " << file <<
": The MatrixMarket reader provided with ViennaCL supports only general or symmetric matrices." << std::endl;
189 std::stringstream line(std::stringstream::in | std::stringstream::out);
190 line << std::string(buffer);
202 std::cerr <<
"Error in file " << file <<
": Could not get matrix dimensions (rows) in line " << linenum << std::endl;
210 std::cerr <<
"Error in file " << file <<
": Could not get matrix dimensions (columns) in line " << linenum << std::endl;
219 std::cerr <<
"Error in file " << file <<
": Could not get matrix dimensions (columns) in line " << linenum << std::endl;
224 if (rows > 0 && cols > 0)
256 std::cerr <<
"Error in file " << file <<
": Parse error for matrix entry in line " << linenum << std::endl;
264 std::cerr <<
"Error in file " << file <<
": Parse error for matrix entry in line " << linenum << std::endl;
276 std::cerr <<
"Error in file " << file <<
": Parse error for matrix entry in line " << linenum << std::endl;
296 if (++valid_entries == nnz)
304 std::cout << linenum <<
" lines read." << std::endl;
318 template <
typename MatrixType>
326 template <
typename MatrixType>
328 const std::string & file,
334 template <
typename ScalarType>
343 template <
typename ScalarType>
345 const std::string & file,
354 template <
typename MatrixType>
357 std::ofstream writer(file);
359 long num_entries = 0;
360 for (
typename MatrixType::const_iterator1 row_it = mat.begin1();
361 row_it != mat.end1();
363 for (
typename MatrixType::const_iterator2 col_it = row_it.begin();
364 col_it != row_it.end();
368 writer <<
"%%MatrixMarket matrix coordinate real general" << std::endl;
369 writer << mat.size1() <<
" " << mat.size2() <<
" " << num_entries << std::endl;
371 for (
typename MatrixType::const_iterator1 row_it = mat.begin1();
372 row_it != mat.end1();
374 for (
typename MatrixType::const_iterator2 col_it = row_it.begin();
375 col_it != row_it.end();
377 writer << col_it.index1() + index_base <<
" " << col_it.index2() + index_base <<
" " << *col_it << std::endl;
382 template <
typename ScalarType>
391 template <
typename ScalarType>
393 const std::string & file,
408 template <
typename MatrixType>
410 const std::string & file,