CuVector< Real > Class Template Reference

#include <matrix-common.h>

Inheritance diagram for CuVector< Real >:
Collaboration diagram for CuVector< Real >:

Public Member Functions

 CuVector ()
 
 CuVector (MatrixIndexT dim, MatrixResizeType t=kSetZero)
 
 CuVector (const CuVectorBase< Real > &v)
 
 CuVector (const VectorBase< Real > &v)
 
 CuVector (const CuVector< Real > &v)
 
template<typename OtherReal >
 CuVector (const CuVectorBase< OtherReal > &v)
 
template<typename OtherReal >
 CuVector (const VectorBase< OtherReal > &v)
 
void Resize (MatrixIndexT dim, MatrixResizeType t=kSetZero)
 Allocate the memory. More...
 
 ~CuVector ()
 
CuVector< Real > & operator= (const CuVectorBase< Real > &other)
 
CuVector< Real > & operator= (const CuVector< Real > &other)
 
CuVector< Real > & operator= (const VectorBase< Real > &other)
 
void Swap (CuVector< Real > *vec)
 
void Swap (Vector< Real > *vec)
 
void Read (std::istream &is, bool binary)
 I/O. More...
 
void Write (std::ostream &is, bool binary) const
 
- Public Member Functions inherited from CuVectorBase< Real >
MatrixIndexT Dim () const
 Dimensions. More...
 
Real * Data ()
 Returns a pointer to the start of the vector's data. More...
 
const Real * Data () const
 Returns a pointer to the start of the vector's data (const). More...
 
void CopyFromVec (const CuVectorBase< Real > &src)
 Copy functions; these will crash if the dimension do not match. More...
 
template<typename OtherReal >
void CopyFromVec (const CuVectorBase< OtherReal > &M)
 
template<typename OtherReal >
void CopyFromVec (const VectorBase< OtherReal > &src)
 
template<typename OtherReal >
void CopyToVec (VectorBase< OtherReal > *dst) const
 
void CopyRowsFromMat (const CuMatrixBase< Real > &M)
 
void CopyRowsFromMat (const MatrixBase< Real > &M)
 
void SetZero ()
 Math operations. More...
 
void Set (Real value)
 
void Add (Real value)
 
void Scale (Real value)
 
void AddVec (Real alpha, const CuVectorBase< Real > &vec, Real beta=1.0)
 
template<typename OtherReal >
void AddVec (Real alpha, const CuVectorBase< OtherReal > &vec, Real beta=1.0)
 
void AddRowSumMat (Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
 Sum the rows of the matrix, add to vector. More...
 
void AddColSumMat (Real alpha, const CuMatrixBase< Real > &mat, Real beta=1.0)
 Sum the columns of the matrix, add to vector. More...
 
void AddTpVec (const Real alpha, const CuTpMatrix< Real > &M, const MatrixTransposeType trans, const CuVectorBase< Real > &v, const Real beta)
 Add triangular matrix times vector: this <– beta*this + alpha*M*v. More...
 
void MulTp (const CuTpMatrix< Real > &M, const MatrixTransposeType trans)
 Multiplies this vector by lower-triangular marix: *this <– *this *M. More...
 
bool ApproxEqual (const CuVectorBase< Real > &other, float tol=0.01) const
 
void InvertElements ()
 
void CopyElements (const CuMatrixBase< Real > &mat, const MatrixTransposeType trans, const CuArrayBase< int32 > &elements)
 Copies selected elements from 'mat' to *this. More...
 
void Floor (const CuVectorBase< Real > &src, Real floor_val, MatrixIndexT *floored_count=NULL)
 
void Ceiling (const CuVectorBase< Real > &src, Real ceiling_val, MatrixIndexT *ceiled_count=NULL)
 
void Pow (const CuVectorBase< Real > &src, Real power)
 
void ApplyFloor (Real floor_val, MatrixIndexT *floored_count=NULL)
 
void ApplyCeiling (Real ceiling_val, MatrixIndexT *ceiled_count=NULL)
 
void ApplyPow (Real power)
 
void ApplySoftMax ()
 
void ApplyLogSoftMax ()
 
void ApplyExp ()
 
void ApplyLog ()
 
Real Sum () const
 
void SetRandn ()
 
void SetRandUniform ()
 
CuSubVector< Real > Range (const MatrixIndexT o, const MatrixIndexT l)
 
const CuSubVector< Real > Range (const MatrixIndexT o, const MatrixIndexT l) const
 
void CopyColFromMat (const CuMatrixBase< Real > &mat, MatrixIndexT col)
 
template<typename OtherReal >
void CopyColFromMat (const CuMatrixBase< OtherReal > &mat, MatrixIndexT col)
 
void AddMatVec (const Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType trans, const CuVectorBase< Real > &v, const Real beta)
 
void AddVecVec (Real alpha, const CuVectorBase< Real > &v, const CuVectorBase< Real > &r, Real beta)
 
void AddSpVec (const Real alpha, const CuSpMatrix< Real > &S, const CuVectorBase< Real > &v, const Real beta)
 
void AddDiagMat2 (Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType trans, Real beta)
 Add the diagonal of a matrix times itself: *this = diag(M M^T) + beta * *this (if trans == kNoTrans), or *this = diag(M^T M) + beta * *this (if trans == kTrans). More...
 
void AddDiagMatMat (Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType transM, const CuMatrixBase< Real > &N, MatrixTransposeType transN, Real beta=1.0)
 Add the diagonal of a matrix product: *this = diag(M N), assuming the "trans" arguments are both kNoTrans; for transpose arguments, it behaves as you would expect. More...
 
CuValue< Real > operator() (MatrixIndexT i)
 
Real Norm (Real p)
 
Real operator() (MatrixIndexT i) const
 
void CopyDiagFromPacked (const CuPackedMatrix< Real > &M)
 Extracts the diagonal of a packed matrix M; works for Sp or Tp. More...
 
void CopyDiagFromMat (const CuMatrix< Real > &M)
 Extracts the diagonal of a matrix. More...
 
Real Max () const
 Returns the maximum value of any element, or -infinity for the empty vector. More...
 
Real Min () const
 Returns the minimum value of any element, or +infinity for the empty vector. More...
 
void ReplaceValue (Real orig, Real changed)
 
void MulElements (const CuVectorBase< Real > &v)
 
void DivElements (const CuVectorBase< Real > &v)
 
const VectorBase< Real > & Vec () const
 
VectorBase< Real > & Vec ()
 
template<>
void CopyColFromMat (const CuMatrixBase< float > &mat, MatrixIndexT col)
 
template<>
void CopyColFromMat (const CuMatrixBase< double > &mat, MatrixIndexT col)
 
template<>
void CopyFromVec (const CuVectorBase< float > &src)
 
template<>
void CopyFromVec (const CuVectorBase< double > &src)
 
template<>
void CopyFromVec (const CuVectorBase< float > &src)
 
template<>
void CopyFromVec (const CuVectorBase< double > &src)
 

Private Member Functions

void Destroy ()
 

Friends

class CuVectorBase< float >
 
class CuVectorBase< double >
 
class CuMatrixBase< Real >
 
class CuPackedMatrix< Real >
 
class CuSpMatrix< Real >
 
class CuTpMatrix< Real >
 

Additional Inherited Members

- Protected Member Functions inherited from CuVectorBase< Real >
 CuVectorBase ()
 Default constructor: make it protected so the user cannot instantiate this class. More...
 
- Protected Attributes inherited from CuVectorBase< Real >
Real * data_
 GPU data pointer (or regular data pointer if CUDA is not compiled in or we have no GPU). More...
 
MatrixIndexT dim_
 dimension of the vector More...
 

Detailed Description

template<typename Real>
class kaldi::CuVector< Real >

Definition at line 74 of file matrix-common.h.

Constructor & Destructor Documentation

◆ CuVector() [1/7]

CuVector ( )
inline

Definition at line 266 of file cu-vector.h.

266 { }

◆ CuVector() [2/7]

CuVector ( MatrixIndexT  dim,
MatrixResizeType  t = kSetZero 
)
inline

Definition at line 267 of file cu-vector.h.

267 { Resize(dim, t); }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ CuVector() [3/7]

CuVector ( const CuVectorBase< Real > &  v)

Definition at line 981 of file cu-vector.cc.

981  {
982  this->Resize(v.Dim());
983  this->CopyFromVec(v);
984 }
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ CuVector() [4/7]

CuVector ( const VectorBase< Real > &  v)

Definition at line 987 of file cu-vector.cc.

987  {
988  this->Resize(v.dim_);
989  this->CopyFromVec(v);
990 }
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ CuVector() [5/7]

CuVector ( const CuVector< Real > &  v)
inlineexplicit

Definition at line 272 of file cu-vector.h.

272  : CuVectorBase<Real>() {
273  Resize(v.Dim(), kUndefined);
274  this->CopyFromVec(v);
275  }
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ CuVector() [6/7]

CuVector ( const CuVectorBase< OtherReal > &  v)
inlineexplicit

Definition at line 278 of file cu-vector.h.

278  : CuVectorBase<Real>() {
279  Resize(v.Dim(), kUndefined);
280  this->CopyFromVec(v);
281  }
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ CuVector() [7/7]

CuVector ( const VectorBase< OtherReal > &  v)
inlineexplicit

Definition at line 284 of file cu-vector.h.

284  : CuVectorBase<Real>() {
285  Resize(v.Dim(), kUndefined);
286  this->CopyFromVec(Vector<Real>(v));
287  }
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ ~CuVector()

~CuVector ( )
inline

Definition at line 292 of file cu-vector.h.

292 { Destroy(); }

Member Function Documentation

◆ Destroy()

void Destroy ( )
private

Definition at line 1062 of file cu-vector.cc.

1062  {
1063 #if HAVE_CUDA == 1
1064  if (CuDevice::Instantiate().Enabled()) {
1065  if (this->data_ != NULL)
1066  CuDevice::Instantiate().Free(this->data_);
1067  } else
1068 #endif
1069  {
1070  if (this->data_ != NULL) KALDI_MEMALIGN_FREE(this->data_);
1071  }
1072  this->data_ = NULL;
1073  this->dim_ = 0;
1074 }
Real * data_
GPU data pointer (or regular data pointer if CUDA is not compiled in or we have no GPU)...
Definition: cu-vector.h:248
#define KALDI_MEMALIGN_FREE(x)
Definition: kaldi-utils.h:60
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:250

◆ operator=() [1/3]

CuVector<Real>& operator= ( const CuVectorBase< Real > &  other)
inline

Definition at line 294 of file cu-vector.h.

294  {
295  Resize(other.Dim(), kUndefined);
296  this->CopyFromVec(other);
297  return *this;
298  }
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ operator=() [2/3]

CuVector<Real>& operator= ( const CuVector< Real > &  other)
inline

Definition at line 300 of file cu-vector.h.

300  {
301  Resize(other.Dim(), kUndefined);
302  this->CopyFromVec(other);
303  return *this;
304  }
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ operator=() [3/3]

CuVector<Real>& operator= ( const VectorBase< Real > &  other)
inline

Definition at line 305 of file cu-vector.h.

305  {
306  Resize(other.Dim());
307  this->CopyFromVec(other);
308  return *this;
309  }
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993

◆ Read()

void Read ( std::istream &  is,
bool  binary 
)

I/O.

Definition at line 963 of file cu-vector.cc.

Referenced by NormalizeComponent::Read(), BatchNormComponent::Read(), AffineComponent::Read(), NoOpComponent::Read(), and kaldi::UnitTestCuVectorIO().

963  {
964  Vector<Real> temp;
965  temp.Read(is, binary);
966  Destroy();
967  Swap(&temp);
968 }
void Swap(CuVector< Real > *vec)
Definition: cu-vector.cc:1019

◆ Resize()

void Resize ( MatrixIndexT  dim,
MatrixResizeType  t = kSetZero 
)

Allocate the memory.

Definition at line 993 of file cu-vector.cc.

Referenced by DiscriminativeObjectiveInfo::Configure(), CuMatrixBase< float >::DiffXent(), DiscriminativeObjectiveInfo::DiscriminativeObjectiveInfo(), Xent::Eval(), LstmNonlinearityComponent::Init(), BatchNormComponent::InitFromConfig(), SoftmaxComponent::MixUp(), NnetBatchComputer::SplitUtteranceIntoTasks(), BatchNormComponent::StoreStats(), ConvolutionComponent::Update(), and Convolutional1dComponent::Update().

993  {
994  KALDI_ASSERT(t == kSetZero || t == kUndefined); // Others not implemented
995  // yet.
996  if (this->dim_ == dim) {
997  this->SetZero();
998  return;
999  }
1000  if (this->dim_ != 0)
1001  this->Destroy();
1002  if (dim == 0) return;
1003 #if HAVE_CUDA == 1
1004  if (CuDevice::Instantiate().Enabled()) {
1005  CuTimer tim;
1006  this->data_ = static_cast<Real*>(CuDevice::Instantiate().Malloc(dim * sizeof(Real)));
1007  this->dim_ = dim;
1008  if (t == kSetZero) this->SetZero();
1009  CuDevice::Instantiate().AccuProfile("CuVector::Resize", tim);
1010  } else
1011 #endif
1012  {
1013  Vector<Real> vec(dim);
1014  this->Swap(&vec);
1015  }
1016 }
void SetZero()
Math operations.
Definition: cu-vector.cc:1098
Real * data_
GPU data pointer (or regular data pointer if CUDA is not compiled in or we have no GPU)...
Definition: cu-vector.h:248
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:250
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Swap(CuVector< Real > *vec)
Definition: cu-vector.cc:1019

◆ Swap() [1/2]

void Swap ( CuVector< Real > *  vec)

Definition at line 1019 of file cu-vector.cc.

Referenced by NonlinearComponent::ConsolidateMemory(), kaldi::nnet3::ReduceRankOfComponents(), and kaldi::nnet3::time_height_convolution::ZeroBlankRows().

1019  {
1020  std::swap(this->data_, vec->data_);
1021  std::swap(this->dim_, vec->dim_);
1022 }
Real * data_
GPU data pointer (or regular data pointer if CUDA is not compiled in or we have no GPU)...
Definition: cu-vector.h:248
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:250

◆ Swap() [2/2]

void Swap ( Vector< Real > *  vec)

Definition at line 1026 of file cu-vector.cc.

1026  {
1027 #if HAVE_CUDA == 1
1028  if (CuDevice::Instantiate().Enabled()) {
1029  if (this->dim_ == 0) {
1030  if (vec->dim_ != 0) {
1031  // *this is empty, but vec is nonempty.
1032  Resize(vec->dim_, kUndefined);
1033  this->CopyFromVec(*vec);
1034  vec->Resize(0);
1035  }
1036  // else both are empty.
1037  } else { // *this is nonempty.
1038  if (vec->dim_ != 0) {
1039  // Both *this and *vec are nonempty. Recurse to simpler cases.
1040  // this could be done more efficiently in the case where
1041  // the size does not change.
1042  Vector<Real> temp;
1043  this->Swap(&temp); // now temp is full, *this is empty.
1044  vec->Swap(&temp); // now vec has data from *this, temp has
1045  // data from vec.
1046  Swap(&temp); // copy data in vec to *this, which is now empty.
1047  } else { // *this is full but *vec is empty.
1048  vec->Resize(this->dim_, kUndefined);
1049  this->CopyToVec(vec);
1050  this->Destroy();
1051  }
1052  }
1053  } else
1054 #endif
1055  {
1056  std::swap(vec->data_, this->data_);
1057  std::swap(vec->dim_, this->dim_);
1058  }
1059 }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:1078
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:993
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:250
void Swap(CuVector< Real > *vec)
Definition: cu-vector.cc:1019
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:938

◆ Write()

void Write ( std::ostream &  is,
bool  binary 
) const

Definition at line 973 of file cu-vector.cc.

Referenced by kaldi::UnitTestCuVectorIO(), and BatchNormComponent::Write().

973  {
974  Vector<BaseFloat> temp(this->dim_, kUndefined);
975  this->CopyToVec(&temp);
976  temp.Write(os, binary);
977 }
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:250
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:938

Friends And Related Function Documentation

◆ CuMatrixBase< Real >

friend class CuMatrixBase< Real >
friend

Definition at line 260 of file cu-vector.h.

◆ CuPackedMatrix< Real >

friend class CuPackedMatrix< Real >
friend

Definition at line 261 of file cu-vector.h.

◆ CuSpMatrix< Real >

friend class CuSpMatrix< Real >
friend

Definition at line 262 of file cu-vector.h.

◆ CuTpMatrix< Real >

friend class CuTpMatrix< Real >
friend

Definition at line 263 of file cu-vector.h.

◆ CuVectorBase< double >

friend class CuVectorBase< double >
friend

Definition at line 259 of file cu-vector.h.

◆ CuVectorBase< float >

friend class CuVectorBase< float >
friend

Definition at line 258 of file cu-vector.h.


The documentation for this class was generated from the following files: