SHOGUN  6.1.3
SGMatrix.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011-2013 Heiko Strathmann
8  * Written (W) 2012 Fernando Jose Iglesias Garcia
9  * Written (W) 2010,2012 Soeren Sonnenburg
10  * Copyright (C) 2010 Berlin Institute of Technology
11  * Copyright (C) 2012 Soeren Sonnenburg
12  */
13 #ifndef __SGMATRIX_H__
14 #define __SGMATRIX_H__
15 
16 #include <shogun/io/SGIO.h>
17 #include <shogun/lib/config.h>
18 #include <shogun/lib/common.h>
19 #include <shogun/util/iterators.h>
21 
22 #include <memory>
23 #include <atomic>
24 
25 namespace Eigen
26 {
27  template <class, int, int, int, int, int> class Matrix;
28  template<int, int> class Stride;
29  template <class, int, class> class Map;
30 }
31 
32 namespace shogun
33 {
34  template<class T> class SGVector;
35  template<typename T> struct GPUMemoryBase;
36  class CFile;
37 
39 template<class T> class SGMatrix : public SGReferencedData
40 {
41  friend class LinalgBackendEigen;
42 
43  public:
45 
46  public:
47  typedef Eigen::Matrix<T,-1,-1,0,-1,-1> EigenMatrixXt;
49 
51  typedef T Scalar;
52 
54  SGMatrix();
55 
58  SGMatrix(bool ref_counting);
59 
61  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
62 
64  SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset);
65 
67  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
68 
77  SGMatrix(GPUMemoryBase<T>* matrix, index_t nrows, index_t ncols);
78 
83  bool on_gpu() const
84  {
85  return gpu_ptr != NULL;
86  }
87 
88 #ifndef SWIG // SWIG should skip this part
89 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11)
90 
92  template <typename ST> using container_type = SGMatrix<ST>;
93 
94 #endif // define (HAVE_CXX0X) || defined(HAVE_CXX11)
95 
106  SGMatrix(SGVector<T> vec);
107 
121  SGMatrix(SGVector<T> vec, index_t nrows, index_t ncols);
122 
124  SGMatrix(EigenMatrixXt& mat);
125 
127  operator EigenMatrixXtMap() const;
128 
130  SGMatrix<T>& operator=(const SGMatrix<T>&);
131 #endif // SWIG
132 
134  SGMatrix(const SGMatrix &orig);
135 
137  virtual ~SGMatrix();
138 
139 #ifndef SWIG // SWIG should skip this parts
140 
145  {
146  assert_on_cpu();
147  const int64_t c = col;
148  return &matrix[c*num_rows];
149  }
150 
158  SGMatrix<T> submatrix(index_t col_start, index_t col_end) const;
159 
165  SGVector<T> get_column(index_t col) const;
166 
171  void set_column(index_t col, const SGVector<T> vec);
172 
178  SGVector<T> get_row_vector(index_t row) const;
179 
184  SGVector<T> get_diagonal_vector() const;
185 
190  inline const T& operator()(index_t i_row, index_t i_col) const
191  {
192  assert_on_cpu();
193  const int64_t c = i_col;
194  return matrix[c*num_rows + i_row];
195  }
196 
200  inline const T& operator[](index_t index) const
201  {
202  assert_on_cpu();
203  return matrix[index];
204  }
205 
210  inline T& operator()(index_t i_row, index_t i_col)
211  {
212  assert_on_cpu();
213  const int64_t c = i_col;
214  return matrix[c*num_rows + i_row];
215  }
216 
220  inline T& operator[](index_t index)
221  {
222  assert_on_cpu();
223  return matrix[index];
224  }
225 
227  iterator begin() noexcept { return iterator(matrix); }
228 
230  iterator end() noexcept { return iterator(matrix + (num_rows * num_cols)); }
231 
232 #endif // SWIG should skip this part
233 
240  const T& get_element(index_t row, index_t col)
241  {
242  return (*this)(row, col);
243  }
244 
251  void set_element(const T& el, index_t row, index_t col)
252  {
253  (*this)(row, col)=el;
254  }
255 
256 #ifndef SWIG // SWIG should skip this part
257 
263  inline SGMatrix<T> get()
264  {
265  return *this;
266  }
267 
269  inline T* data() const
270  {
271  return matrix;
272  }
273 
275  inline int64_t size() const
276  {
277  const int64_t c=num_cols;
278  return num_rows*c;
279  }
280 
282  SG_FORCED_INLINE bool operator==(const SGMatrix<T>& other) const
283  {
284  if (num_rows!=other.num_rows || num_cols!=other.num_cols)
285  return false;
286 
287  if (on_gpu())
288  {
289  if (!other.on_gpu())
290  return false;
291  if (gpu_ptr!=other.gpu_ptr)
292  return false;
293  }
294 
295  if (matrix != other.matrix)
296  return false;
297 
298  return true;
299  }
300 
308  bool equals(const SGMatrix<T>& other) const;
309 
311  void set_const(T const_elem);
312 
314  void zero();
315 
325  bool is_symmetric() const;
326 
328  T max_single() const;
329 
331  SGMatrix<T> clone() const;
332 
334  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
335 
337  static void transpose_matrix(
338  T*& matrix, int32_t& num_feat, int32_t& num_vec);
339 
341  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
342 
348  static SGMatrix<T> create_identity_matrix(index_t size, T scale);
349 
350 #ifdef HAVE_LAPACK
351 
359  static SGVector<float64_t> compute_eigenvectors(
360  SGMatrix<float64_t> matrix);
361 
369  static double* compute_eigenvectors(double* matrix, int n, int m);
370 
381  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
382  int n, int il, int iu);
383 #endif
384 
392  static SGMatrix<float64_t> matrix_multiply(
394  bool transpose_A=false, bool transpose_B=false,
395  float64_t scale=1.0);
396 #ifdef HAVE_LAPACK
397 
398  static void inverse(SGMatrix<float64_t> matrix);
399 
403  static float64_t* pinv(
404  float64_t* matrix, int32_t rows, int32_t cols,
405  float64_t* target=NULL);
406 
407 #endif
408 
410  static float64_t trace(
411  float64_t* mat, int32_t cols, int32_t rows);
412 
414  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
415 
417  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
418 
420  void center();
421 
423  static void center_matrix(T* matrix, int32_t m, int32_t n);
424 
426  void remove_column_mean();
427 
429  void display_matrix(const char* name="matrix") const;
430 
432  static void display_matrix(
433  const T* matrix, int32_t rows, int32_t cols,
434  const char* name="matrix", const char* prefix="");
435 
437  static void display_matrix(
438  const SGMatrix<T> matrix, const char* name="matrix",
439  const char* prefix="");
440 
452  static SGMatrix<T> get_allocated_matrix(index_t num_rows,
453  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
454 
459  void load(CFile* loader);
460 
465  void save(CFile* saver);
466 #endif // #ifndef SWIG // SWIG should skip this part
467 
468  protected:
470  virtual void copy_data(const SGReferencedData &orig);
471 
473  virtual void init_data();
474 
476  virtual void free_data();
477 
478  private:
480  std::atomic<bool> m_on_gpu;
481 
485  void assert_on_cpu() const
486  {
487  if (on_gpu())
488  SG_SERROR("Direct memory access not possible when data is in GPU memory.\n");
489  }
490 
491  public:
493  T* matrix;
499  std::shared_ptr<GPUMemoryBase<T>> gpu_ptr;
500 };
501 }
502 #endif // __SGMATRIX_H__
T & operator[](index_t index)
Definition: SGMatrix.h:220
const T & get_element(index_t row, index_t col)
Definition: SGMatrix.h:240
Eigen::Map< EigenMatrixXt, 0, Eigen::Stride< 0, 0 > > EigenMatrixXtMap
Definition: SGMatrix.h:48
SGMatrix< T > transpose_matrix(const SGMatrix< T > &A)
int32_t index_t
Definition: common.h:72
bool on_gpu() const
Definition: SGMatrix.h:83
void scale(SGVector< T > &a, SGVector< T > &result, T alpha=1)
void set_element(const T &el, index_t row, index_t col)
Definition: SGMatrix.h:251
Eigen::Matrix< T,-1,-1, 0,-1,-1 > EigenMatrixXt
Definition: SGMatrix.h:47
Definition: SGMatrix.h:25
void set_const(Container< T > &a, T value)
iterator end() noexcept
Definition: SGMatrix.h:230
RandomIterator< T > iterator
Definition: SGMatrix.h:44
T trace(const SGMatrix< T > &A)
#define SG_FORCED_INLINE
Definition: common.h:91
void zero(Container< T > &a)
std::shared_ptr< GPUMemoryBase< T > > gpu_ptr
Definition: SGMatrix.h:499
shogun reference count managed data
double float64_t
Definition: common.h:60
A File access base class.
Definition: File.h:34
const T & operator[](index_t index) const
Definition: SGMatrix.h:200
iterator begin() noexcept
Definition: SGMatrix.h:227
index_t num_rows
Definition: SGMatrix.h:495
shogun vector
const T & operator()(index_t i_row, index_t i_col) const
Definition: SGMatrix.h:190
index_t num_cols
Definition: SGMatrix.h:497
SG_FORCED_INLINE bool operator==(const SGMatrix< T > &other) const
Definition: SGMatrix.h:282
shogun matrix
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void center_matrix(SGMatrix< T > &A)
Interface for GPU memory libraries.
Definition: SGMatrix.h:35
#define SG_SERROR(...)
Definition: SGIO.h:164
T * data() const
Definition: SGMatrix.h:269
Linalg methods with Eigen3 backend.
T & operator()(index_t i_row, index_t i_col)
Definition: SGMatrix.h:210
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:144
int64_t size() const
Definition: SGMatrix.h:275

SHOGUN Machine Learning Toolbox - Documentation