SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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>
20 
21 #include <memory>
22 #include <atomic>
23 
24 namespace Eigen
25 {
26  template <class, int, int, int, int, int> class Matrix;
27  template<int, int> class Stride;
28  template <class, int, class> class Map;
29 }
30 
31 namespace shogun
32 {
33  template<class T> class SGVector;
34  template<typename T> struct GPUMemoryBase;
35  class CFile;
36 
38 template<class T> class SGMatrix : public SGReferencedData
39 {
40  friend class LinalgBackendEigen;
41 
42  public:
43  typedef Eigen::Matrix<T,-1,-1,0,-1,-1> EigenMatrixXt;
45 
47  typedef T Scalar;
48 
50  SGMatrix();
51 
54  SGMatrix(bool ref_counting);
55 
57  SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);
58 
60  SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset);
61 
63  SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
64 
74 
79  bool on_gpu() const
80  {
81  return gpu_ptr != NULL;
82  }
83 
84 #ifndef SWIG // SWIG should skip this part
85 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11)
86 
88  template <typename ST> using container_type = SGMatrix<ST>;
89 
90 #endif // define (HAVE_CXX0X) || defined(HAVE_CXX11)
91 
102  SGMatrix(SGVector<T> vec);
103 
117  SGMatrix(SGVector<T> vec, index_t nrows, index_t ncols);
118 
120  SGMatrix(EigenMatrixXt& mat);
121 
123  operator EigenMatrixXtMap() const;
124 
127 #endif // SWIG
128 
130  SGMatrix(const SGMatrix &orig);
131 
133  virtual ~SGMatrix();
134 
135 #ifndef SWIG // SWIG should skip this part
136 
141  {
142  assert_on_cpu();
143  const int64_t c = col;
144  return &matrix[c*num_rows];
145  }
146 
153 
159 
164  inline const T& operator()(index_t i_row, index_t i_col) const
165  {
166  assert_on_cpu();
167  const int64_t c = i_col;
168  return matrix[c*num_rows + i_row];
169  }
170 
174  inline const T& operator[](index_t index) const
175  {
176  assert_on_cpu();
177  return matrix[index];
178  }
179 
184  inline T& operator()(index_t i_row, index_t i_col)
185  {
186  assert_on_cpu();
187  const int64_t c = i_col;
188  return matrix[c*num_rows + i_row];
189  }
190 
194  inline T& operator[](index_t index)
195  {
196  assert_on_cpu();
197  return matrix[index];
198  }
199 
200 #endif // SWIG should skip this part
201 
208  const T& get_element(index_t row, index_t col)
209  {
210  return (*this)(row, col);
211  }
212 
219  void set_element(const T& el, index_t row, index_t col)
220  {
221  (*this)(row, col)=el;
222  }
223 
224 #ifndef SWIG // SWIG should skip this part
225 
231  inline SGMatrix<T> get()
232  {
233  return *this;
234  }
235 
237  inline T* data() const
238  {
239  return matrix;
240  }
241 
243  inline uint64_t size() const
244  {
245  const uint64_t c=num_cols;
246  return num_rows*c;
247  }
248 
250  SG_FORCED_INLINE bool operator==(const SGMatrix<T>& other) const
251  {
252  if (num_rows!=other.num_rows || num_cols!=other.num_cols)
253  return false;
254 
255  if (on_gpu())
256  {
257  if (!other.on_gpu())
258  return false;
259  if (gpu_ptr!=other.gpu_ptr)
260  return false;
261  }
262 
263  if (matrix != other.matrix)
264  return false;
265 
266  return true;
267  }
268 
276  bool equals(const SGMatrix<T>& other) const;
277 
279  void set_const(T const_elem);
280 
282  void zero();
283 
293  bool is_symmetric() const;
294 
296  T max_single() const;
297 
299  SGMatrix<T> clone() const;
300 
302  static T* clone_matrix(const T* matrix, int32_t nrows, int32_t ncols);
303 
305  static void transpose_matrix(
306  T*& matrix, int32_t& num_feat, int32_t& num_vec);
307 
309  static void create_diagonal_matrix(T* matrix, T* v,int32_t size);
310 
317 
318 #ifdef HAVE_LAPACK
319 
329 
337  static double* compute_eigenvectors(double* matrix, int n, int m);
338 
349  void compute_few_eigenvectors(double* matrix_, double*& eigenvalues, double*& eigenvectors,
350  int n, int il, int iu);
351 #endif
352 
362  bool transpose_A=false, bool transpose_B=false,
363  float64_t scale=1.0);
364 #ifdef HAVE_LAPACK
365 
366  static void inverse(SGMatrix<float64_t> matrix);
367 
371  static float64_t* pinv(
372  float64_t* matrix, int32_t rows, int32_t cols,
373  float64_t* target=NULL);
374 
375 #endif
376 
378  static float64_t trace(
379  float64_t* mat, int32_t cols, int32_t rows);
380 
382  static T* get_row_sum(T* matrix, int32_t m, int32_t n);
383 
385  static T* get_column_sum(T* matrix, int32_t m, int32_t n);
386 
388  void center();
389 
391  static void center_matrix(T* matrix, int32_t m, int32_t n);
392 
394  void remove_column_mean();
395 
397  void display_matrix(const char* name="matrix") const;
398 
400  static void display_matrix(
401  const T* matrix, int32_t rows, int32_t cols,
402  const char* name="matrix", const char* prefix="");
403 
405  static void display_matrix(
406  const SGMatrix<T> matrix, const char* name="matrix",
407  const char* prefix="");
408 
421  index_t num_cols, SGMatrix<T> pre_allocated=SGMatrix<T>());
422 
427  void load(CFile* loader);
428 
433  void save(CFile* saver);
434 #endif // #ifndef SWIG // SWIG should skip this part
435 
436  protected:
438  virtual void copy_data(const SGReferencedData &orig);
439 
441  virtual void init_data();
442 
444  virtual void free_data();
445 
446  private:
448  std::atomic<bool> m_on_gpu;
449 
453  void assert_on_cpu() const
454  {
455  if (on_gpu())
456  SG_SERROR("Direct memory access not possible when data is in GPU memory.\n");
457  }
458 
459  public:
461  T* matrix;
467  std::shared_ptr<GPUMemoryBase<T>> gpu_ptr;
468 };
469 }
470 #endif // __SGMATRIX_H__
void display_matrix(const char *name="matrix") const
Definition: SGMatrix.cpp:480
Eigen::Map< EigenMatrixXt, 0, Eigen::Stride< 0, 0 > > EigenMatrixXtMap
Definition: SGMatrix.h:44
static void center_matrix(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:436
SGMatrix< T > clone() const
Definition: SGMatrix.cpp:330
int32_t index_t
Definition: common.h:72
T * data() const
Definition: SGMatrix.h:237
T & operator()(index_t i_row, index_t i_col)
Definition: SGMatrix.h:184
void save(CFile *saver)
Definition: SGMatrix.cpp:1172
static T * get_column_sum(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:416
virtual void free_data()
Definition: SGMatrix.cpp:1141
void scale(SGVector< T > &a, SGVector< T > &result, T alpha=1)
Definition: SGMatrix.h:24
static void create_diagonal_matrix(T *matrix, T *v, int32_t size)
Definition: SGMatrix.cpp:375
Eigen::Matrix< T,-1,-1, 0,-1,-1 > EigenMatrixXt
Definition: SGMatrix.h:43
index_t num_cols
Definition: SGMatrix.h:465
virtual ~SGMatrix()
Definition: SGMatrix.cpp:138
const T & operator[](index_t index) const
Definition: SGMatrix.h:174
static SGMatrix< T > get_allocated_matrix(index_t num_rows, index_t num_cols, SGMatrix< T > pre_allocated=SGMatrix< T >())
Definition: SGMatrix.cpp:1090
index_t num_rows
Definition: SGMatrix.h:463
shogun matrix
#define SG_FORCED_INLINE
Definition: common.h:91
static void transpose_matrix(T *&matrix, int32_t &num_feat, int32_t &num_vec)
Definition: SGMatrix.cpp:358
static float64_t * pinv(float64_t *matrix, int32_t rows, int32_t cols, float64_t *target=NULL)
Definition: SGMatrix.cpp:933
static T * clone_matrix(const T *matrix, int32_t nrows, int32_t ncols)
Definition: SGMatrix.cpp:345
static SGMatrix< float64_t > matrix_multiply(SGMatrix< float64_t > A, SGMatrix< float64_t > B, bool transpose_A=false, bool transpose_B=false, float64_t scale=1.0)
Definition: SGMatrix.cpp:1037
SGVector< T > get_diagonal_vector() const
Definition: SGMatrix.cpp:1200
bool on_gpu() const
Definition: SGMatrix.h:79
shogun vector
void remove_column_mean()
Definition: SGMatrix.cpp:461
shogun reference count managed data
double float64_t
Definition: common.h:60
void compute_few_eigenvectors(double *matrix_, double *&eigenvalues, double *&eigenvectors, int n, int il, int iu)
Definition: SGMatrix.cpp:1023
A File access base class.
Definition: File.h:34
T max_single() const
Definition: SGMatrix.cpp:311
bool equals(const SGMatrix< T > &other) const
Definition: SGMatrix.cpp:144
T & operator[](index_t index)
Definition: SGMatrix.h:194
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:140
static float64_t trace(float64_t *mat, int32_t cols, int32_t rows)
Definition: SGMatrix.cpp:391
virtual void copy_data(const SGReferencedData &orig)
Definition: SGMatrix.cpp:1120
SGMatrix< T > & operator=(const SGMatrix< T > &)
Definition: SGMatrix.cpp:125
SG_FORCED_INLINE bool operator==(const SGMatrix< T > &other) const
Definition: SGMatrix.h:250
void set_element(const T &el, index_t row, index_t col)
Definition: SGMatrix.h:219
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Interface for GPU memory libraries.
Definition: SGMatrix.h:34
uint64_t size() const
Definition: SGMatrix.h:243
std::shared_ptr< GPUMemoryBase< T > > gpu_ptr
Definition: SGMatrix.h:467
#define SG_SERROR(...)
Definition: SGIO.h:178
static T * get_row_sum(T *matrix, int32_t m, int32_t n)
Definition: SGMatrix.cpp:402
bool is_symmetric() const
Definition: SGMatrix.cpp:227
static void inverse(SGMatrix< float64_t > matrix)
inverses square matrix in-place
Definition: SGMatrix.cpp:972
Linalg methods with Eigen3 backend.
SGVector< T > get_row_vector(index_t row) const
Definition: SGMatrix.cpp:1188
void set_const(T const_elem)
Definition: SGMatrix.cpp:209
static SGMatrix< T > create_identity_matrix(index_t size, T scale)
static SGVector< float64_t > compute_eigenvectors(SGMatrix< float64_t > matrix)
Definition: SGMatrix.cpp:983
virtual void init_data()
Definition: SGMatrix.cpp:1131
const T & operator()(index_t i_row, index_t i_col) const
Definition: SGMatrix.h:164
void load(CFile *loader)
Definition: SGMatrix.cpp:1150
const T & get_element(index_t row, index_t col)
Definition: SGMatrix.h:208

SHOGUN Machine Learning Toolbox - Documentation