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 ()
 
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)
 
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 233 of file cu-vector.h.

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

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

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

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

References CuVectorBase< Real >::Dim().

879  {
880  this->Resize(v.Dim());
881  this->CopyFromVec(v);
882 }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:891
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:969
CuVector ( const VectorBase< Real > &  v)

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

References VectorBase< Real >::dim_.

885  {
886  this->Resize(v.dim_);
887  this->CopyFromVec(v);
888 }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:891
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:969
CuVector ( const CuVector< Real > &  v)
inlineexplicit

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

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

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

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

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

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

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

259 { Destroy(); }

Member Function Documentation

void Destroy ( )
private

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

References data_, and KALDI_MEMALIGN_FREE.

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

953  {
954 #if HAVE_CUDA == 1
955  if (CuDevice::Instantiate().Enabled()) {
956  if (this->data_ != NULL)
957  CuDevice::Instantiate().Free(this->data_);
958  } else
959 #endif
960  {
961  if (this->data_ != NULL) KALDI_MEMALIGN_FREE(this->data_);
962  }
963  this->data_ = NULL;
964  this->dim_ = 0;
965 }
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:217
Real * data_
GPU data pointer (or regular data pointer if CUDA is not compiled in or we have no GPU)...
Definition: cu-vector.h:215
#define KALDI_MEMALIGN_FREE(x)
Definition: kaldi-utils.h:56
CuVector<Real>& operator= ( const CuVectorBase< Real > &  other)
inline

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

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

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

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

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

272  {
273  Resize(other.Dim());
274  this->CopyFromVec(other);
275  return *this;
276  }
void Resize(MatrixIndexT dim, MatrixResizeType t=kSetZero)
Allocate the memory.
Definition: cu-vector.cc:891
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:969
void Read ( std::istream &  is,
bool  binary 
)
void Resize ( MatrixIndexT  dim,
MatrixResizeType  t = kSetZero 
)

Allocate the memory.

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

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

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().

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

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

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

917  {
918 #if HAVE_CUDA == 1
919  if (CuDevice::Instantiate().Enabled()) {
920  if (this->dim_ == 0) {
921  if (vec->dim_ != 0) {
922  // *this is empty, but vec is nonempty.
923  Resize(vec->dim_, kUndefined);
924  this->CopyFromVec(*vec);
925  vec->Resize(0);
926  }
927  // else both are empty.
928  } else { // *this is nonempty.
929  if (vec->dim_ != 0) {
930  // Both *this and *vec are nonempty. Recurse to simpler cases.
931  // this could be done more efficiently in the case where
932  // the size does not change.
933  Vector<Real> temp;
934  this->Swap(&temp); // now temp is full, *this is empty.
935  vec->Swap(&temp); // now vec has data from *this, temp has
936  // data from vec.
937  Swap(vec); // copy data in vec to *this, which is now empty.
938  } else { // *this is full but *vec is empty.
939  vec->Resize(this->dim_, kUndefined);
940  this->CopyToVec(vec);
941  this->Destroy();
942  }
943  }
944  } else
945 #endif
946  {
947  std::swap(vec->data_, this->data_);
948  std::swap(vec->dim_, this->dim_);
949  }
950 }
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:217
void Swap(Vector< Real > *vec)
Definition: cu-vector.cc:917
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:891
void CopyFromVec(const CuVectorBase< Real > &src)
Copy functions; these will crash if the dimension do not match.
Definition: cu-vector.cc:969
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:838
void Write ( std::ostream &  is,
bool  binary 
) const

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

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

Referenced by kaldi::UnitTestCuVectorIO(), StatisticsExtractionComponentPrecomputedIndexes::Write(), NonlinearComponent::Write(), BackpropTruncationComponentPrecomputedIndexes::Write(), RepeatedAffineComponent::Write(), ConstantComponent::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().

871  {
872  Vector<BaseFloat> temp(this->dim_, kUndefined);
873  this->CopyToVec(&temp);
874  temp.Write(os, binary);
875 }
MatrixIndexT dim_
dimension of the vector
Definition: cu-vector.h:217
void CopyToVec(VectorBase< OtherReal > *dst) const
Definition: cu-vector.cc:838

Friends And Related Function Documentation

friend class CuMatrixBase< Real >
friend

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

friend class CuPackedMatrix< Real >
friend

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

friend class CuSpMatrix< Real >
friend

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

friend class CuTpMatrix< Real >
friend

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

friend class CuVectorBase< double >
friend

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

friend class CuVectorBase< float >
friend

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


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