ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
vector-io.hpp
Go to the documentation of this file.
1 #ifndef VECTOR_IO_HPP_
2 #define VECTOR_IO_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2015, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
21 #include <string>
22 #include <iostream>
23 #include <fstream>
24 
25 #include "viennacl/tools/tools.hpp"
27 #include "viennacl/traits/size.hpp"
28 
29 
30 template<typename MatrixType, typename ScalarType>
31 void insert(MatrixType & matrix, long row, long col, ScalarType value)
32 {
33  matrix(row, col) = value;
34 }
35 
36 #ifdef VIENNACL_HAVE_EIGEN
37 template<typename ScalarType, int option>
38 void insert(Eigen::SparseMatrix<ScalarType, option> & matrix, long row, long col, double value)
39 {
40  matrix.fill(row, col) = value;
41 }
42 #endif
43 
44 template<typename MatrixType>
46 {
47  public:
48  my_inserter(MatrixType & mat) : mat_(mat) {}
49 
50  void apply(long row, long col, double value)
51  {
52  insert(mat_, row, col, value);
53  }
54 
55  private:
56  MatrixType & mat_;
57 };
58 
59 #ifdef VIENNACL_HAVE_MTL4
60 #include <boost/numeric/mtl/matrix/inserter.hpp>
61 /*template<typename ScalarType>
62 void insert(mtl::compressed2D<ScalarType> & matrix, long row, long col, ScalarType value)
63 {
64  typedef mtl::compressed2D<ScalarType> MatrixType;
65  mtl::matrix::inserter<MatrixType> ins(matrix);
66 
67  typename mtl::Collection<MatrixType>::value_type val(value);
68  ins(row, col) << val;
69  //matrix.fill(row, col) = val;
70 }*/
71 
72 template<typename ScalarType>
73 void resize_vector(mtl::dense_vector<ScalarType> & vec, unsigned int size)
74 {
75  vec.change_dim(size);
76 }
77 
78 template<typename ScalarType>
79 class my_inserter<mtl::compressed2D<ScalarType> >
80 {
81  typedef mtl::compressed2D<ScalarType> MatrixType;
82  public:
83  my_inserter(MatrixType & mat) : mat_(mat), ins_(mat) {}
84 
85  void apply(long row, long col, ScalarType value)
86  {
87  typename mtl::Collection<MatrixType>::value_type val(value);
88  ins_(row, col) << val;
89  }
90 
91  private:
92  MatrixType & mat_;
93  mtl::matrix::inserter<MatrixType> ins_;
94 };
95 #endif
96 
97 template<typename VectorType>
98 void resize_vector(VectorType & vec, unsigned int size)
99 {
100  vec.resize(size);
101 }
102 
103 template<typename VectorType>
104 bool readVectorFromFile(const std::string & filename,
105  VectorType & vec)
106 {
108 
109  std::ifstream file(filename.c_str());
110 
111  if (!file) return false;
112 
113  unsigned int size;
114  file >> size;
115 
116  resize_vector(vec, size);
117 
118  for (unsigned int i = 0; i < size; ++i)
119  {
120  ScalarType element;
121  file >> element;
122  vec[i] = element;
123  }
124 
125  return true;
126 }
127 
128 
129 template<class MatrixType>
130 bool readMatrixFromFile(const std::string & filename, MatrixType & matrix)
131 {
133 
134  std::cout << "Reading matrix..." << std::endl;
135 
136  std::ifstream file(filename.c_str());
137 
138  if (!file) return false;
139 
140  std::string id;
141  file >> id;
142  if (id != "Matrix") return false;
143 
144 
145  unsigned int num_rows, num_columns;
146  file >> num_rows >> num_columns;
147  if (num_rows != num_columns) return false;
148 
149  viennacl::traits::resize(matrix, num_rows, num_rows);
150 
151  my_inserter<MatrixType> ins(matrix);
152  for (unsigned int row = 0; row < num_rows; ++row)
153  {
154  int num_entries;
155  file >> num_entries;
156  for (int j = 0; j < num_entries; ++j)
157  {
158  unsigned int column;
159  ScalarType element;
160  file >> column >> element;
161 
162  ins.apply(row, column, element);
163  //insert(matrix, row, column, element);
164  //note: the obvious 'matrix(row, column) = element;' does not work with Eigen, hence another level of indirection
165  }
166  //std::cout << "reading of row finished" << std::endl;
167  }
168 
169  return true;
170 }
171 
172 
173 #endif
Generic size and resize functionality for different vector and matrix types.
Various little tools used here and there in ViennaCL.
my_inserter(MatrixType &mat)
Definition: vector-io.hpp:48
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:235
void insert(MatrixType &matrix, long row, long col, ScalarType value)
Definition: vector-io.hpp:31
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.
Definition: size.hpp:63
bool readMatrixFromFile(const std::string &filename, MatrixType &matrix)
Definition: vector-io.hpp:130
bool readVectorFromFile(const std::string &filename, VectorType &vec)
Definition: vector-io.hpp:104
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_row > row(const matrix_base< NumericT, F > &A, unsigned int i)
Definition: matrix.hpp:900
void resize_vector(VectorType &vec, unsigned int size)
Definition: vector-io.hpp:98
float ScalarType
Definition: fft_1d.cpp:42
void apply(long row, long col, double value)
Definition: vector-io.hpp:50
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_column > column(const matrix_base< NumericT, F > &A, unsigned int j)
Definition: matrix.hpp:908
A collection of compile time type deductions.