SHOGUN  6.1.3
SGVector.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) 2013 Soumyajit De
9  * Written (W) 2012 Fernando Jose Iglesias Garcia
10  * Written (W) 2010,2012 Soeren Sonnenburg
11  * Copyright (C) 2010 Berlin Institute of Technology
12  * Copyright (C) 2012 Soeren Sonnenburg
13  */
14 #ifndef __SGVECTOR_H__
15 #define __SGVECTOR_H__
16 
17 #include <shogun/lib/config.h>
18 
19 #include <shogun/io/SGIO.h>
20 #include <shogun/lib/common.h>
22 #include <shogun/util/iterators.h>
24 
25 #include <memory>
26 #include <atomic>
27 #include <initializer_list>
28 
29 namespace Eigen
30 {
31  template <class, int, int, int, int, int> class Matrix;
32  template<int, int> class Stride;
33  template <class, int, class> class Map;
34 }
35 
36 namespace shogun
37 {
38  template <class T> class SGSparseVector;
39  template <class T> class SGMatrix;
40  class CFile;
41  class CRandom;
42 
44 template<class T> class SGVector : public SGReferencedData
45 {
46  friend class LinalgBackendEigen;
47 
48  public:
50 
51  public:
52  typedef Eigen::Matrix<T,-1,1,0,-1,1> EigenVectorXt;
53  typedef Eigen::Matrix<T,1,-1,0x1,1,-1> EigenRowVectorXt;
54 
57 
59  typedef T Scalar;
60 
62  SGVector();
63 
65  SGVector(T* v, index_t len, bool ref_counting=true);
66 
68  SGVector(T* m, index_t len, index_t offset);
69 
71  SGVector(index_t len, bool ref_counting=true);
72 
74  SGVector(SGMatrix<T> matrix);
75 
82  SGVector(GPUMemoryBase<T>* vector, index_t len);
83 
85  SGVector(const SGVector &orig);
86 
91  bool on_gpu() const
92  {
93  return gpu_ptr != NULL;
94  }
95 
96 #ifndef SWIG // SWIG should skip this part
97 
99  template <typename ST> using container_type = SGVector<ST>;
100 
102  template<typename InputIt>
103  SGVector(InputIt begin, InputIt end):
104  SGReferencedData(true),
105  vlen(std::distance(begin, end)),
106  gpu_ptr(nullptr)
107  {
108  vector = SG_MALLOC(T, vlen);
109  std::copy(begin, end, vector);
110  m_on_gpu.store(false, std::memory_order_release);
111  }
112 
114  SGVector(std::initializer_list<T> il);
115 
117  SGVector(EigenVectorXt& vec);
118 
120  SGVector(EigenRowVectorXt& vec);
121 
123  operator EigenVectorXtMap() const;
124 
126  operator EigenRowVectorXtMap() const;
127 #endif // SWIG
128 
133  void set_const(T const_elem);
134 
141  {
142  return *this;
143  }
144 
145 #ifndef SWIG // SWIG should skip this part
146 
150  void set(SGVector<T> orig);
151 
153  virtual ~SGVector();
154 
156  inline int32_t size() const { return vlen; }
157 
159  inline T* data() const
160  {
161  assert_on_cpu();
162  return vector;
163  }
164 
166  iterator begin() noexcept { return iterator(vector); }
167 
169  iterator end() noexcept { return iterator(vector + vlen); }
170 
171  SGVector<T>& operator=(const SGVector<T>&);
172 
174  operator T*() { return vector; }
175 
177  void zero();
178 
183  void range_fill(T start=0);
184 
190  void random(T min_value, T max_value);
191 
198  index_t find_position_to_insert(T element);
199 
201  SGVector<T> clone() const;
202 
204  static T* clone_vector(const T* vec, int32_t len);
205 
207  static void fill_vector(T* vec, int32_t len, T value);
208 
210  static void range_fill_vector(T* vec, int32_t len, T start=0);
211 
213  static void random_vector(T* vec, int32_t len, T min_value, T max_value);
214 #endif // SWIG // SWIG should skip this part
215 
221  const T& get_element(index_t index)
222  {
223  return (*this)[index];
224  }
225 
231  void set_element(const T& el, index_t index)
232  {
233  (*this)[index]=el;
234  }
235 
236 #ifndef SWIG // SWIG should skip this part
237 
241  void resize_vector(int32_t n);
242 
248  inline const T& operator[](uint64_t index) const
249  {
250  assert_on_cpu();
251  return vector[index];
252  }
253 
259  inline const T& operator[](int64_t index) const
260  {
261  assert_on_cpu();
262  return vector[index];
263  }
264 
270  inline const T& operator[](uint32_t index) const
271  {
272  assert_on_cpu();
273  return vector[index];
274  }
275 
281  inline const T& operator[](int32_t index) const
282  {
283  assert_on_cpu();
284  return vector[index];
285  }
286 
292  inline T& operator[](uint64_t index)
293  {
294  assert_on_cpu();
295  return vector[index];
296  }
297 
303  inline T& operator[](int64_t index)
304  {
305  assert_on_cpu();
306  return vector[index];
307  }
308 
314  inline T& operator[](uint32_t index)
315  {
316  assert_on_cpu();
317  return vector[index];
318  }
319 
325  inline T& operator[](int32_t index)
326  {
327  assert_on_cpu();
328  return vector[index];
329  }
330 
335  void add(const SGVector<T> x);
336 
341  void add(const SGSparseVector<T>& x);
342 
347  void add(const T x);
348 
350  SGVector<T> operator+ (SGVector<T> x);
351 
353  SGVector<T> operator+= (SGVector<T> x)
354  {
355  add(x);
356  return *this;
357  }
358 
361  {
362  add(x);
363  return *this;
364  }
365 
371  bool equals(SGVector<T>& other);
372 
374  static T twonorm(const T* x, int32_t len);
375 
377  static float64_t onenorm(T* x, int32_t len);
378 
380  static T qsq(T* x, int32_t len, float64_t q);
381 
383  static T qnorm(T* x, int32_t len, float64_t q);
384 
386  static void vec1_plus_scalar_times_vec2(T* vec1,
387  const T scalar, const T* vec2, int32_t n);
388 
390  static inline void vector_multiply(
391  T* target, const T* v1, const T* v2,int32_t len)
392  {
393  for (int32_t i=0; i<len; i++)
394  target[i]=v1[i]*v2[i];
395  }
396 
397 
399  static inline void add(
400  T* target, T alpha, const T* v1, T beta, const T* v2,
401  int32_t len)
402  {
403  for (int32_t i=0; i<len; i++)
404  target[i]=alpha*v1[i]+beta*v2[i];
405  }
406 
408  static inline void add_scalar(T alpha, T* vec, int32_t len)
409  {
410  for (int32_t i=0; i<len; i++)
411  vec[i]+=alpha;
412  }
413 
415  static void scale_vector(T alpha, T* vec, int32_t len);
416 
418  static inline T sum(T* vec, int32_t len)
419  {
420  T result=0;
421  for (int32_t i=0; i<len; i++)
422  result+=vec[i];
423 
424  return result;
425  }
426 
428  static inline T sum(SGVector<T> vec)
429  {
430  return sum(vec.vector, vec.vlen);
431  }
432 
434  static inline T product(T* vec, int32_t len)
435  {
436  T result=1;
437  for (int32_t i=0; i<len; i++)
438  result*=vec[i];
439 
440  return result;
441  }
442 
444  inline T product()
445  {
446  return product(vector, vlen);
447  }
448 
450  static T sum_abs(T* vec, int32_t len);
451 
455  static int32_t unique(T* output, int32_t size);
456 
458  void display_size() const;
459 
461  void display_vector(const char* name="vector",
462  const char* prefix="") const;
463 
465  static void display_vector(
466  const T* vector, int32_t n, const char* name="vector",
467  const char* prefix="");
468 
470  static void display_vector(
471  const SGVector<T>, const char* name="vector",
472  const char* prefix="");
473 
477  SGVector<index_t> find(T elem);
478 
482  template <typename Predicate>
484  {
485  SGVector<index_t> idx(vlen);
486  index_t k=0;
487 
488  for (index_t i=0; i < vlen; ++i)
489  if (p(vector[i]))
490  idx[k++] = i;
491 
492  idx.vlen = k;
493  return idx;
494  }
495 
497  void scale(T alpha);
498 
503  void load(CFile* loader);
504 
509  void save(CFile* saver);
510 
512  SGVector<float64_t> get_real();
513 
515  SGVector<float64_t> get_imag();
516 
527  static SGMatrix<T> convert_to_matrix(SGVector<T> vector, index_t nrows, index_t ncols, bool fortran_order);
528 
529 
542  static void convert_to_matrix(T*& matrix, index_t nrows, index_t ncols, const T* vector, int32_t vlen, bool fortran_order);
543 #endif // #ifndef SWIG // SWIG should skip this part
544  protected:
546  virtual void copy_data(const SGReferencedData &orig);
547 
549  virtual void init_data();
550 
552  virtual void free_data();
553 
554  private:
556  std::atomic<bool> m_on_gpu;
557 
561  void assert_on_cpu() const
562  {
563  if (on_gpu())
564  SG_SERROR("Direct memory access not possible when data is in GPU memory.\n");
565  }
566 
567  public:
569  T* vector;
573  std::shared_ptr<GPUMemoryBase<T>> gpu_ptr;
574 };
575 
576 #ifndef DOXYGEN_SHOULD_SKIP_THIS
578  const float64_t scalar, const float64_t* vec2, int32_t n);
579 
581  const float32_t scalar, const float32_t* vec2, int32_t n);
582 #endif // DOXYGEN_SHOULD_SKIP_THIS
583 }
584 #endif // __SGVECTOR_H__
std::shared_ptr< GPUMemoryBase< T > > gpu_ptr
Definition: SGVector.h:573
float distance(CJLCoverTreePoint p1, CJLCoverTreePoint p2, float64_t upper_bound)
T & operator[](uint32_t index)
Definition: SGVector.h:314
T & operator[](int64_t index)
Definition: SGVector.h:303
SGVector(InputIt begin, InputIt end)
Definition: SGVector.h:103
int32_t index_t
Definition: common.h:72
const T & operator[](uint64_t index) const
Definition: SGVector.h:248
static T sum(T *vec, int32_t len)
Return sum(vec)
Definition: SGVector.h:418
void scale(SGVector< T > &a, SGVector< T > &result, T alpha=1)
Definition: SGMatrix.h:25
Definition: basetag.h:132
void add(SGVector< T > &a, SGVector< T > &b, SGVector< T > &result, T alpha=1, T beta=1)
Eigen::Map< EigenVectorXt, 0, Eigen::Stride< 0, 0 > > EigenVectorXtMap
Definition: SGVector.h:55
void set_const(Container< T > &a, T value)
static void add(T *target, T alpha, const T *v1, T beta, const T *v2, int32_t len)
target=alpha*vec1 + beta*vec2
Definition: SGVector.h:399
static void vector_multiply(T *target, const T *v1, const T *v2, int32_t len)
Compute vector multiplication.
Definition: SGVector.h:390
iterator begin() noexcept
Definition: SGVector.h:166
static T sum(SGVector< T > vec)
Return sum(vec)
Definition: SGVector.h:428
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry<T>* vector is orde...
void zero(Container< T > &a)
int32_t size() const
Definition: SGVector.h:156
Eigen::Matrix< T, 1,-1, 0x1, 1,-1 > EigenRowVectorXt
Definition: SGVector.h:53
iterator end() noexcept
Definition: SGVector.h:169
shogun reference count managed data
double float64_t
Definition: common.h:60
static T product(T *vec, int32_t len)
Return the product of the vectors elements.
Definition: SGVector.h:434
void range_fill(Container< T > &a, const T start=0)
A File access base class.
Definition: File.h:34
shogun vector
const T & operator[](int32_t index) const
Definition: SGVector.h:281
const T & get_element(index_t index)
Definition: SGVector.h:221
float float32_t
Definition: common.h:59
shogun matrix
const T & operator[](int64_t index) const
Definition: SGVector.h:259
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
T sum(const Container< T > &a, bool no_diag=false)
Interface for GPU memory libraries.
Definition: SGMatrix.h:35
const T & operator[](uint32_t index) const
Definition: SGVector.h:270
#define SG_SERROR(...)
Definition: SGIO.h:164
void set_element(const T &el, index_t index)
Definition: SGVector.h:231
Eigen::Map< EigenRowVectorXt, 0, Eigen::Stride< 0, 0 > > EigenRowVectorXtMap
Definition: SGVector.h:56
Linalg methods with Eigen3 backend.
T product()
Return product(vec)
Definition: SGVector.h:444
T * data() const
Definition: SGVector.h:159
static void add_scalar(T alpha, T *vec, int32_t len)
Add scalar to vector inplace.
Definition: SGVector.h:408
bool on_gpu() const
Definition: SGVector.h:91
T & operator[](int32_t index)
Definition: SGVector.h:325
Eigen::Matrix< T,-1, 1, 0,-1, 1 > EigenVectorXt
Definition: SGVector.h:52
RandomIterator< T > iterator
Definition: SGVector.h:49
T & operator[](uint64_t index)
Definition: SGVector.h:292
SGVector< index_t > find_if(Predicate p)
Definition: SGVector.h:483
index_t vlen
Definition: SGVector.h:571

SHOGUN Machine Learning Toolbox - Documentation