All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 Read (std::istream &is, bool binary)
 I/O. More...
 
void Write (std::ostream &is, bool binary) const
 
void Swap (Vector< Real > *vec)
 
- 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 ApplySoftMax ()
 
void ApplyExp ()
 
void ApplyLog ()
 
MatrixIndexT ApplyFloor (Real floor_val)
 
MatrixIndexT ApplyCeiling (Real ceiling_val)
 
void ApplyPow (Real power)
 
Real Sum () const
 
void SetRandn ()
 
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)
 
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 72 of file matrix-common.h.

Constructor & Destructor Documentation

CuVector ( )
inline

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

231 { }
CuVector ( MatrixIndexT  dim,
MatrixResizeType  t = kSetZero 
)
inline

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

232 { Resize(dim, t); }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
CuVector ( const CuVectorBase< Real > &  v)

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

References CuVectorBase< Real >::Dim().

871  {
872  this->Resize(v.Dim());
873  this->CopyFromVec(v);
874 }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
CuVector ( const VectorBase< Real > &  v)

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

References VectorBase< Real >::dim_.

877  {
878  this->Resize(v.dim_);
879  this->CopyFromVec(v);
880 }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
CuVector ( const CuVector< Real > &  v)
inlineexplicit

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

237  : CuVectorBase<Real>() {
238  Resize(v.Dim(), kUndefined);
239  this->CopyFromVec(v);
240  }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
CuVector ( const CuVectorBase< OtherReal > &  v)
inlineexplicit

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

243  : CuVectorBase<Real>() {
244  Resize(v.Dim(), kUndefined);
245  this->CopyFromVec(v);
246  }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
CuVector ( const VectorBase< OtherReal > &  v)
inlineexplicit

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

249  : CuVectorBase<Real>() {
250  Resize(v.Dim(), kUndefined);
251  this->CopyFromVec(Vector<Real>(v));
252  }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
~CuVector ( )
inline

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

257 { Destroy(); }

Member Function Documentation

void Destroy ( )
private

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

References data_, and KALDI_MEMALIGN_FREE.

Referenced by CuVector< BaseFloat >::~CuVector().

945  {
946 #if HAVE_CUDA == 1
947  if (CuDevice::Instantiate().Enabled()) {
948  if (this->data_ != NULL)
949  CuDevice::Instantiate().Free(this->data_);
950  } else
951 #endif
952  {
953  if (this->data_ != NULL) KALDI_MEMALIGN_FREE(this->data_);
954  }
955  this->data_ = NULL;
956  this->dim_ = 0;
957 }
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:215
Real * data_
GPU data pointer (or regular data pointer if CUDA is not compiled in or we have no GPU)...
Definition: cu-vector.h:213
#define KALDI_MEMALIGN_FREE(x)
Definition: kaldi-utils.h:56
CuVector<Real>& operator= ( const CuVectorBase< Real > &  other)
inline

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

259  {
260  Resize(other.Dim(), kUndefined);
261  this->CopyFromVec(other);
262  return *this;
263  }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
CuVector<Real>& operator= ( const CuVector< Real > &  other)
inline

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

265  {
266  Resize(other.Dim(), kUndefined);
267  this->CopyFromVec(other);
268  return *this;
269  }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
CuVector<Real>& operator= ( const VectorBase< Real > &  other)
inline

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

270  {
271  Resize(other.Dim());
272  this->CopyFromVec(other);
273  return *this;
274  }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
void Resize ( MatrixIndexT  dim,
MatrixResizeType  t = kSetZero 
)

Allocate the memory.

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

References data_, Timer::Elapsed(), KALDI_ASSERT, kaldi::kSetZero, kaldi::kUndefined, and kaldi::nnet3::SetZero().

Referenced by NonlinearComponent::Add(), HiddenSoftmax::BackpropagateFnc(), SimpleSentenceAveragingComponent::BackpropagateFnc(), Convolutional2DComponent::BackpropagateFnc(), LstmProjected::BackpropagateFnc(), BlstmProjected::BackpropagateFnc(), NnetOnlineComputer::Compute(), DiscriminativeObjectiveInfo::Configure(), CuVector< BaseFloat >::CuVector(), CuMatrixBase< Real >::DiffXent(), DiscriminativeObjectiveInfo::DiscriminativeObjectiveInfo(), Xent::Eval(), RepeatedAffineComponent::Init(), NaturalGradientAffineComponent::Init(), AffineComponent::Init(), AffineComponentPreconditioned::Init(), AffineComponentPreconditionedOnline::Init(), BlockAffineComponent::Init(), PerElementScaleComponent::Init(), PerElementOffsetComponent::Init(), FixedAffineComponent::Init(), ConvolutionComponent::Init(), Convolutional1dComponent::Init(), LstmNonlinearityComponent::Init(), AffineTransform::InitData(), RecurrentComponent::InitData(), LstmProjected::InitData(), BlstmProjected::InitData(), ConvolutionalComponent::InitData(), Convolutional2DComponent::InitData(), Rbm::InitData(), AddShift::InitData(), Rescale::InitData(), AffineComponent::LimitRank(), SoftmaxComponent::MixUp(), CuVector< BaseFloat >::operator=(), SimpleSentenceAveragingComponent::PropagateFnc(), LengthNormComponent::PropagateFnc(), Rbm::RbmUpdate(), NaturalGradientAffineComponent::Resize(), AffineComponent::Resize(), AffineComponentPreconditionedOnline::Resize(), Convolutional1dComponent::Resize(), NonlinearComponent::SetDim(), NonlinearComponent::StoreStatsInternal(), RecurrentComponent::Update(), AddShift::Update(), ConvolutionalComponent::Update(), Convolutional2DComponent::Update(), Rescale::Update(), ConvolutionComponent::Update(), Convolutional1dComponent::Update(), NonlinearComponent::UpdateStats(), and AffineComponent::Widen().

883  {
884  KALDI_ASSERT(t == kSetZero || t == kUndefined); // Others not implemented
885  // yet.
886  if (this->dim_ == dim) {
887  this->SetZero();
888  return;
889  }
890  if (this->dim_ != 0)
891  this->Destroy();
892  if (dim == 0) return;
893 #if HAVE_CUDA == 1
894  if (CuDevice::Instantiate().Enabled()) {
895  Timer tim;
896  this->data_ = static_cast<Real*>(CuDevice::Instantiate().Malloc(dim * sizeof(Real)));
897  this->dim_ = dim;
898  if (t == kSetZero) this->SetZero();
899  CuDevice::Instantiate().AccuProfile("CuVector::Resize", tim.Elapsed());
900  } else
901 #endif
902  {
903  Vector<Real> vec(dim);
904  this->Swap(&vec);
905  }
906 }
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:215
Real * data_
GPU data pointer (or regular data pointer if CUDA is not compiled in or we have no GPU)...
Definition: cu-vector.h:213
void Swap(Vector< Real > *vec)
Definition: cu-vector.cc:909
void SetZero()
Math operations.
Definition: cu-vector.cc:979
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Swap ( Vector< Real > *  vec)

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

References VectorBase< Real >::data_, VectorBase< Real >::dim_, kaldi::kUndefined, Vector< Real >::Resize(), kaldi::swap(), and Vector< Real >::Swap().

909  {
910 #if HAVE_CUDA == 1
911  if (CuDevice::Instantiate().Enabled()) {
912  if (this->dim_ == 0) {
913  if (vec->dim_ != 0) {
914  // *this is empty, but vec is nonempty.
915  Resize(vec->dim_, kUndefined);
916  this->CopyFromVec(*vec);
917  vec->Resize(0);
918  }
919  // else both are empty.
920  } else { // *this is nonempty.
921  if (vec->dim_ != 0) {
922  // Both *this and *vec are nonempty. Recurse to simpler cases.
923  // this could be done more efficiently in the case where
924  // the size does not change.
925  Vector<Real> temp;
926  this->Swap(&temp); // now temp is full, *this is empty.
927  vec->Swap(&temp); // now vec has data from *this, temp has
928  // data from vec.
929  Swap(vec); // copy data in vec to *this, which is now empty.
930  } else { // *this is full but *vec is empty.
931  vec->Resize(this->dim_, kUndefined);
932  this->CopyToVec(vec);
933  this->Destroy();
934  }
935  }
936  } else
937 #endif
938  {
939  std::swap(vec->data_, this->data_);
940  std::swap(vec->dim_, this->dim_);
941  }
942 }
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:215
void Swap(Vector< Real > *vec)
Definition: cu-vector.cc:909
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:883
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:961
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:830
void Write ( std::ostream &  is,
bool  binary 
) const

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

References kaldi::kUndefined, and VectorBase< Real >::Write().

Referenced by kaldi::UnitTestCuVectorIO(), StatisticsExtractionComponentPrecomputedIndexes::Write(), NonlinearComponent::Write(), BackpropTruncationComponentPrecomputedIndexes::Write(), RepeatedAffineComponent::Write(), NaturalGradientAffineComponent::Write(), AffineComponent::Write(), AffineComponentPreconditioned::Write(), AffineComponentPreconditionedOnline::Write(), BlockAffineComponent::Write(), PerElementScaleComponent::Write(), BlockAffineComponentPreconditioned::Write(), PerElementOffsetComponent::Write(), ConstantFunctionComponent::Write(), NaturalGradientPerElementScaleComponent::Write(), FixedAffineComponent::Write(), FixedScaleComponent::Write(), ConvolutionComponent::Write(), FixedBiasComponent::Write(), LstmNonlinearityComponent::Write(), Convolutional1dComponent::Write(), Rbm::WriteAsNnet(), ParametricRelu::WriteData(), AffineTransform::WriteData(), RecurrentComponent::WriteData(), LstmProjected::WriteData(), Convolutional2DComponent::WriteData(), ConvolutionalComponent::WriteData(), BlstmProjected::WriteData(), Rbm::WriteData(), AddShift::WriteData(), and Rescale::WriteData().

863  {
864  Vector<BaseFloat> temp(this->dim_, kUndefined);
865  this->CopyToVec(&temp);
866  temp.Write(os, binary);
867 }
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:215
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:830

Friends And Related Function Documentation

friend class CuMatrixBase< Real >
friend

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

friend class CuPackedMatrix< Real >
friend

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

friend class CuSpMatrix< Real >
friend

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

friend class CuTpMatrix< Real >
friend

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

friend class CuVectorBase< double >
friend

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

friend class CuVectorBase< float >
friend

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


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