CuSpMatrix< Real > Class Template Reference

#include <matrix-common.h>

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

Public Member Functions

 CuSpMatrix ()
 
 CuSpMatrix (MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
 
 CuSpMatrix (const SpMatrix< Real > &orig)
 
 CuSpMatrix (const CuSpMatrix< Real > &orig)
 
 CuSpMatrix (const CuMatrixBase< Real > &orig, SpCopyType copy_type=kTakeLower)
 
CuSpMatrix< Real > & operator= (const CuSpMatrix< Real > &in)
 
 ~CuSpMatrix ()
 
void Resize (MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
 
Real FrobeniusNorm () const
 
bool IsUnit (Real tol=0.001) const
 
bool ApproxEqual (const CuSpMatrix< Real > &other, Real tol=0.001) const
 
void CopyFromSp (const CuSpMatrix< Real > &other)
 
void CopyFromSp (const SpMatrix< Real > &other)
 
void CopyFromMat (const CuMatrixBase< Real > &orig, SpCopyType copy_type=kTakeLower)
 
void CopyToSp (SpMatrix< Real > *dst) const
 
CuValue< Real > operator() (MatrixIndexT r, MatrixIndexT c)
 
Real operator() (MatrixIndexT r, MatrixIndexT c) const
 
void Invert ()
 Note: the CuMatrix version of the Invert() function will only work for positive definite matrices; it is based on Cholesky. More...
 
void AddVec2 (const Real alpha, const CuVectorBase< Real > &v)
 
void AddMat2 (const Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
 
void AddSp (const Real alpha, const CuSpMatrix< Real > &Ma)
 
- Public Member Functions inherited from CuPackedMatrix< Real >
 CuPackedMatrix ()
 
 CuPackedMatrix (MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
 
 CuPackedMatrix (const PackedMatrix< Real > &orig)
 
 CuPackedMatrix (const CuPackedMatrix< Real > &orig)
 
void SetZero ()
 
void SetUnit ()
 < Set to zero More...
 
void SetRandn ()
 < Set to unit matrix. More...
 
void SetDiag (Real alpha)
 < Set to random values of a normal distribution More...
 
void AddToDiag (Real r)
 < Set the diagonal value to alpha More...
 
void Scale (Real alpha)
 
void ScaleDiag (Real alpha)
 
Real Trace () const
 
 ~CuPackedMatrix ()
 
void Resize (MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
 Set packed matrix to a specified size (can be zero). More...
 
void CopyFromPacked (const CuPackedMatrix< Real > &src)
 
void CopyFromPacked (const PackedMatrix< Real > &src)
 
void CopyToPacked (PackedMatrix< Real > *dst) const
 
void Read (std::istream &in, bool binary)
 
void Write (std::ostream &out, bool binary) const
 
void Destroy ()
 
void Swap (CuPackedMatrix< Real > *other)
 Swaps the contents of *this and *other. Shallow swap. More...
 
void Swap (PackedMatrix< Real > *other)
 Swaps the contents of *this and *other. More...
 
Real * Data ()
 
const Real * Data () const
 
Real operator() (MatrixIndexT r, MatrixIndexT c) const
 
MatrixIndexT NumRows () const
 
MatrixIndexT NumCols () const
 
size_t SizeInBytes () const
 Returns size in bytes of the data held by the matrix. More...
 

Protected Member Functions

const SpMatrix< Real > & Mat () const
 
SpMatrix< Real > & Mat ()
 
- Protected Member Functions inherited from CuPackedMatrix< Real >
const PackedMatrix< Real > & Mat () const
 
PackedMatrix< Real > & Mat ()
 
void AddPacked (const Real alpha, const CuPackedMatrix< Real > &M)
 

Friends

class CuMatrixBase< Real >
 
class CuVectorBase< Real >
 
class CuTpMatrix< Real >
 
class CuSubMatrix< Real >
 
class CuRand< Real >
 
template<class R , class S >
TraceSpSp (const CuSpMatrix< R > &A, const CuSpMatrix< S > &B)
 

Additional Inherited Members

- Protected Attributes inherited from CuPackedMatrix< Real >
Real * data_
 
MatrixIndexT num_rows_
 

Detailed Description

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

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

Constructor & Destructor Documentation

◆ CuSpMatrix() [1/5]

CuSpMatrix ( )
inline

Definition at line 52 of file cu-sp-matrix.h.

52 : CuPackedMatrix<Real>() {}

◆ CuSpMatrix() [2/5]

CuSpMatrix ( MatrixIndexT  r,
MatrixResizeType  resize_type = kSetZero 
)
inlineexplicit

Definition at line 54 of file cu-sp-matrix.h.

55  : CuPackedMatrix<Real>(r, resize_type) {}

◆ CuSpMatrix() [3/5]

CuSpMatrix ( const SpMatrix< Real > &  orig)
inlineexplicit

Definition at line 57 of file cu-sp-matrix.h.

58  : CuPackedMatrix<Real>(orig) {}

◆ CuSpMatrix() [4/5]

CuSpMatrix ( const CuSpMatrix< Real > &  orig)
inline

Definition at line 62 of file cu-sp-matrix.h.

63  : CuPackedMatrix<Real>(orig) {}

◆ CuSpMatrix() [5/5]

CuSpMatrix ( const CuMatrixBase< Real > &  orig,
SpCopyType  copy_type = kTakeLower 
)
inlineexplicit

Definition at line 65 of file cu-sp-matrix.h.

References CuSpMatrix< Real >::CopyFromMat(), and CuSpMatrix< Real >::operator=().

67  : CuPackedMatrix<Real>(orig.NumRows(), kUndefined) {
68  CopyFromMat(orig, copy_type);
69  }
void CopyFromMat(const CuMatrixBase< Real > &orig, SpCopyType copy_type=kTakeLower)
Definition: cu-sp-matrix.cc:39

◆ ~CuSpMatrix()

~CuSpMatrix ( )
inline

Definition at line 73 of file cu-sp-matrix.h.

73 {}

Member Function Documentation

◆ AddMat2()

void AddMat2 ( const Real  alpha,
const CuMatrixBase< Real > &  M,
MatrixTransposeType  transM,
const Real  beta 
)

Definition at line 130 of file cu-sp-matrix.cc.

References CuMatrixBase< Real >::Data(), KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTakeLower, kaldi::kTrans, CuMatrixBase< Real >::Mat(), CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), and CuMatrixBase< Real >::Stride().

Referenced by CuSpMatrix< Real >::operator()(), OnlinePreconditioner::SelfTest(), OnlineNaturalGradient::SelfTest(), kaldi::UnitTestCuSpMatrixAddMat2(), kaldi::UnitTestCuSpMatrixInvert(), and kaldi::nnet2::UnitTestPreconditionDirections().

131  {
132  KALDI_ASSERT((transM == kNoTrans && this->NumRows() == M.NumRows())
133  || (transM == kTrans && this->NumRows() == M.NumCols()));
134 
135 #if HAVE_CUDA == 1
136  if (CuDevice::Instantiate().Enabled()) {
137  if (this->num_rows_ == 0) return;
138  CuTimer tim;
139  MatrixIndexT this_dim = this->NumRows(),
140  m_other_dim = (transM == kNoTrans ? M.NumCols() : M.NumRows());
141 
142  if (this_dim == 0) return;
143  if (alpha == 0.0) {
144  if (beta != 1.0) this->Scale(beta);
145  return;
146  }
147 
148  cublasOperation_t trans = (transM == kTrans ? CUBLAS_OP_N : CUBLAS_OP_T);
149 
150  CuMatrix<Real> tmp_mat(*this);
151  cublas_syrk(GetCublasHandle(), CUBLAS_FILL_MODE_UPPER, trans, this_dim, m_other_dim, alpha, M.Data(),
152  M.Stride(), beta, tmp_mat.Data(), tmp_mat.Stride());
153  this->CopyFromMat(tmp_mat, kTakeLower);
154 
155  CuDevice::Instantiate().AccuProfile("CuSpMatrix::AddMat2", tim);
156  } else
157 #endif
158  {
159  Mat().AddMat2(alpha, M.Mat(), transM, beta);
160  }
161 }
MatrixIndexT NumRows() const
const SpMatrix< Real > & Mat() const
Definition: cu-sp-matrix.h:132
int32 MatrixIndexT
Definition: matrix-common.h:98
void Scale(Real alpha)
void CopyFromMat(const CuMatrixBase< Real > &orig, SpCopyType copy_type=kTakeLower)
Definition: cu-sp-matrix.cc:39
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ AddSp()

void AddSp ( const Real  alpha,
const CuSpMatrix< Real > &  Ma 
)
inline

Definition at line 127 of file cu-sp-matrix.h.

References CuPackedMatrix< Real >::AddPacked().

Referenced by kaldi::UnitTestCuSpMatrixAddSp().

127  {
128  this->AddPacked(alpha, Ma);
129  }
void AddPacked(const Real alpha, const CuPackedMatrix< Real > &M)

◆ AddVec2()

void AddVec2 ( const Real  alpha,
const CuVectorBase< Real > &  v 
)

Definition at line 108 of file cu-sp-matrix.cc.

References CU2DBLOCK, CuVectorBase< Real >::Data(), CuVectorBase< Real >::Dim(), KALDI_ASSERT, and CuVectorBase< Real >::Vec().

Referenced by CuSpMatrix< Real >::operator()(), kaldi::UnitTestCuSpMatrixAddVec2(), and kaldi::nnet2::UnitTestPreconditionDirections().

108  {
109  KALDI_ASSERT(v.Dim() == this->NumRows());
110 #if HAVE_CUDA == 1
111  if (CuDevice::Instantiate().Enabled()) {
112  if (this->num_rows_ == 0) return;
113  CuTimer tim;
114  size_t nr = this->num_rows_;
115  dim3 dimBlock(CU2DBLOCK, CU2DBLOCK);
116  dim3 dimGrid(n_blocks(nr, CU2DBLOCK), n_blocks(nr, CU2DBLOCK));
117 
118  CUBLAS_SAFE_CALL(cublas_spr(GetCublasHandle(), CUBLAS_FILL_MODE_UPPER, this->num_rows_, alpha, v.Data(),
119  1, this->Data()));
120 
121  CuDevice::Instantiate().AccuProfile("CuSpMatrix::AddVec2", tim);
122  } else
123 #endif
124  {
125  Mat().AddVec2(alpha, v.Vec());
126  }
127 }
MatrixIndexT NumRows() const
const SpMatrix< Real > & Mat() const
Definition: cu-sp-matrix.h:132
#define CU2DBLOCK
Definition: cu-matrixdim.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ ApproxEqual()

bool ApproxEqual ( const CuSpMatrix< Real > &  other,
Real  tol = 0.001 
) const

Definition at line 200 of file cu-sp-matrix.cc.

References rnnlm::d, CuSpMatrix< Real >::FrobeniusNorm(), KALDI_ASSERT, and CuPackedMatrix< Real >::NumRows().

Referenced by kaldi::AssertEqual(), and CuSpMatrix< Real >::FrobeniusNorm().

200  {
201  KALDI_ASSERT(this->NumRows() == B.NumRows());
202  CuSpMatrix<Real> diff(*this);
203  diff.AddSp(-1.0, B);
204  Real a = this->FrobeniusNorm(), b = B.FrobeniusNorm(),
205  d = diff.FrobeniusNorm();
206  return (d <= tol * std::max(a, b));
207 }
MatrixIndexT NumRows() const
Real FrobeniusNorm() const
Definition: cu-sp-matrix.h:79
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromMat()

void CopyFromMat ( const CuMatrixBase< Real > &  orig,
SpCopyType  copy_type = kTakeLower 
)

Definition at line 39 of file cu-sp-matrix.cc.

References CU1DBLOCK, CU2DBLOCK, CuMatrixBase< Real >::Data(), data_, CuMatrixBase< Real >::Dim(), KALDI_ASSERT, KALDI_ERR, kaldi::kTakeLower, kaldi::kTakeMean, kaldi::kTakeMeanAndCheck, kaldi::kTakeUpper, CuMatrixBase< Real >::Mat(), CuMatrixBase< Real >::NumCols(), and CuMatrixBase< Real >::NumRows().

Referenced by CuMatrixBase< float >::Cholesky(), CuSpMatrix< Real >::CopyFromSp(), CuSpMatrix< Real >::CuSpMatrix(), kaldi::UnitTestCopyFromMat(), and kaldi::UnitTestCuSpMatrixCopyFromMat().

40  {
41  KALDI_ASSERT(this->num_rows_ == M.NumRows() &&
42  this->num_rows_ == M.NumCols());
43  if (this->num_rows_ == 0)
44  return;
45 #if HAVE_CUDA == 1
46  if (CuDevice::Instantiate().Enabled()) {
47  CuTimer tim;
48  MatrixIndexT D = this->NumRows();
49  if (D == 0)
50  return;
51  switch (copy_type) {
52  case kTakeMeanAndCheck:
53  KALDI_ERR << "kTakeMeanAndCheck not supported!";
54  // The grid/block dimensions have been very roughly tuned for the
55  // individual cases.
56  case kTakeMean:
57  {
58  dim3 dimBlock(CU2DBLOCK, CU2DBLOCK);
59  dim3 dimGrid(n_blocks(D, CU2DBLOCK), n_blocks(D, CU2DBLOCK));
60  cuda_take_mean(dimGrid, dimBlock, M.Data(), this->data_, M.Dim());
61  CU_SAFE_CALL(cudaGetLastError());
62  }
63  break;
64  case kTakeLower:
65  {
66  int32 block_size = std::min(CU1DBLOCK, this->num_rows_);
67  dim3 dimBlock(1, block_size);
68  dim3 dimGrid(D, n_blocks(D, block_size));
69  cuda_take_lower(dimGrid, dimBlock, M.Data(), this->data_, M.Dim());
70  CU_SAFE_CALL(cudaGetLastError());
71  }
72  break;
73  case kTakeUpper:
74  {
75  dim3 dimBlock(CU2DBLOCK, CU2DBLOCK);
76  dim3 dimGrid(n_blocks(D, CU2DBLOCK), n_blocks(D, CU2DBLOCK));
77  cuda_take_upper(dimGrid, dimBlock, M.Data(), this->data_, M.Dim());
78  CU_SAFE_CALL(cudaGetLastError());
79  }
80  break;
81  default:
82  KALDI_ASSERT("Invalid argument to CuSpMatrix::CopyFromMat");
83  }
84  CuDevice::Instantiate().AccuProfile("CuSpMatrix::CopyFromMat(from CuMatrixBase)", tim);
85  } else
86 #endif
87  {
88  Mat().CopyFromMat(M.Mat(), copy_type);
89  }
90 }
MatrixIndexT NumRows() const
kaldi::int32 int32
const SpMatrix< Real > & Mat() const
Definition: cu-sp-matrix.h:132
int32 MatrixIndexT
Definition: matrix-common.h:98
#define KALDI_ERR
Definition: kaldi-error.h:147
#define CU1DBLOCK
Definition: cu-matrixdim.h:57
#define CU2DBLOCK
Definition: cu-matrixdim.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CopyFromSp() [1/2]

void CopyFromSp ( const CuSpMatrix< Real > &  other)
inline

Definition at line 85 of file cu-sp-matrix.h.

References CuPackedMatrix< Real >::CopyFromPacked().

Referenced by kaldi::UnitTestCuSpMatrixOperator().

85  {
87  }
void CopyFromPacked(const CuPackedMatrix< Real > &src)

◆ CopyFromSp() [2/2]

void CopyFromSp ( const SpMatrix< Real > &  other)
inline

Definition at line 88 of file cu-sp-matrix.h.

References CuSpMatrix< Real >::CopyFromMat(), CuPackedMatrix< Real >::CopyFromPacked(), and kaldi::kTakeLower.

88  {
90  }
void CopyFromPacked(const CuPackedMatrix< Real > &src)

◆ CopyToSp()

◆ FrobeniusNorm()

Real FrobeniusNorm ( ) const
inline

Definition at line 79 of file cu-sp-matrix.h.

References CuSpMatrix< Real >::ApproxEqual(), CuSpMatrix< Real >::IsUnit(), and CuSpMatrix< Real >::TraceSpSp.

Referenced by CuSpMatrix< Real >::ApproxEqual().

79 { return sqrt(TraceSpSp(*this, *this)); }
friend R TraceSpSp(const CuSpMatrix< R > &A, const CuSpMatrix< S > &B)

◆ Invert()

void Invert ( )

Note: the CuMatrix version of the Invert() function will only work for positive definite matrices; it is based on Cholesky.

Definition at line 93 of file cu-sp-matrix.cc.

References CuMatrixBase< Real >::CopyFromSp(), and CuMatrixBase< Real >::SymInvertPosDef().

Referenced by CuSpMatrix< Real >::operator()(), kaldi::UnitTestCuSpMatrixInvert(), and kaldi::nnet2::UnitTestPreconditionDirections().

93  {
94 #if HAVE_CUDA == 1
95  if (CuDevice::Instantiate().Enabled()) {
96  CuMatrix<Real> mat(this->num_rows_, this->num_rows_);
97  mat.CopyFromSp(*this);
98  mat.SymInvertPosDef();
99  this->CopyFromMat(mat);
100  } else
101 #endif
102  { // Use inversion of CPU-based SpMatrix.
103  Mat().Invert();
104  }
105 }
const SpMatrix< Real > & Mat() const
Definition: cu-sp-matrix.h:132
void CopyFromMat(const CuMatrixBase< Real > &orig, SpCopyType copy_type=kTakeLower)
Definition: cu-sp-matrix.cc:39

◆ IsUnit()

bool IsUnit ( Real  tol = 0.001) const

Definition at line 210 of file cu-sp-matrix.cc.

References kaldi::TraceSpSp().

Referenced by CuSpMatrix< Real >::FrobeniusNorm().

210  {
211  // want to return:
212  //FrobeniusNorm(*this - I) <= tol * NumRows(), i.e.:
213  //sqrt (trace((*this - I)(*this-I)) <= tol * NumRows()
214  // trace((*this - I)(*this - I)) <= tol * NumRows()
215  // trace(*this * *this) + trace(I) - 2 * trace(*this) <= tol * NumRows()
216  // trace(*this * *this) + dim - 2*this.Trace() <= tol * NumRows()
217 
218  // Note: we could do this more efficiently still, by slightly changing the
219  // definition of IsUnit and getting rid of the extra stuff inside TraceSpSp
220  // that corrects for the diagonal being counted twice.
221  return (TraceSpSp(*this, *this) + this->NumRows() - 2.0 * this->Trace() <=
222  tol * this->NumRows());
223 }
MatrixIndexT NumRows() const
friend R TraceSpSp(const CuSpMatrix< R > &A, const CuSpMatrix< S > &B)

◆ Mat() [1/2]

const SpMatrix<Real>& Mat ( ) const
inlineprotected

Definition at line 132 of file cu-sp-matrix.h.

Referenced by CuVectorBase< float >::AddSpVec(), CuTpMatrix< Real >::Cholesky(), CuMatrixBase< float >::CopyFromSp(), and kaldi::TraceSpSp().

132  {
133  return *(reinterpret_cast<const SpMatrix<Real>* >(this));
134  }

◆ Mat() [2/2]

SpMatrix<Real>& Mat ( )
inlineprotected

Definition at line 135 of file cu-sp-matrix.h.

135  {
136  return *(reinterpret_cast<SpMatrix<Real>* >(this));
137  }

◆ operator()() [1/2]

CuValue<Real> operator() ( MatrixIndexT  r,
MatrixIndexT  c 
)
inline

Definition at line 99 of file cu-sp-matrix.h.

References CuPackedMatrix< Real >::data_, KALDI_ASSERT, CuPackedMatrix< Real >::num_rows_, and kaldi::swap().

99  {
100  if (static_cast<UnsignedMatrixIndexT>(c) >
101  static_cast<UnsignedMatrixIndexT>(r))
102  std::swap(c, r);
103  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
104  static_cast<UnsignedMatrixIndexT>(this->num_rows_));
105  return CuValue<Real>(this->data_ + (r * (r+1)) / 2 + c);
106  }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ operator()() [2/2]

Real operator() ( MatrixIndexT  r,
MatrixIndexT  c 
) const
inline

Definition at line 108 of file cu-sp-matrix.h.

References CuSpMatrix< Real >::AddMat2(), CuSpMatrix< Real >::AddVec2(), CuPackedMatrix< Real >::data_, CuSpMatrix< Real >::Invert(), KALDI_ASSERT, CuPackedMatrix< Real >::num_rows_, and kaldi::swap().

108  {
109  if (static_cast<UnsignedMatrixIndexT>(c) >
110  static_cast<UnsignedMatrixIndexT>(r))
111  std::swap(c, r);
112  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
113  static_cast<UnsignedMatrixIndexT>(this->num_rows_));
114  return CuValue<Real>(this->data_ + (r * (r+1)) / 2 + c); // will be
115  // casted to Real.
116  }
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ operator=()

CuSpMatrix< Real > & operator= ( const CuSpMatrix< Real > &  in)

Definition at line 226 of file cu-sp-matrix.cc.

References kaldi::kUndefined, and CuPackedMatrix< Real >::NumRows().

Referenced by CuSpMatrix< Real >::CuSpMatrix().

226  {
227  this->Resize(in.NumRows(), kUndefined);
228  this->CopyFromPacked(in);
229  return *this;
230 }
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Definition: cu-sp-matrix.h:75
void CopyFromPacked(const CuPackedMatrix< Real > &src)

◆ Resize()

void Resize ( MatrixIndexT  nRows,
MatrixResizeType  resize_type = kSetZero 
)
inline

Definition at line 75 of file cu-sp-matrix.h.

References CuPackedMatrix< Real >::Resize().

Referenced by SpMatrix< float >::SpMatrix().

75  {
76  CuPackedMatrix<Real>::Resize(nRows, resize_type);
77  }
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).

Friends And Related Function Documentation

◆ CuMatrixBase< Real >

friend class CuMatrixBase< Real >
friend

Definition at line 42 of file cu-sp-matrix.h.

◆ CuRand< Real >

friend class CuRand< Real >
friend

Definition at line 46 of file cu-sp-matrix.h.

◆ CuSubMatrix< Real >

friend class CuSubMatrix< Real >
friend

Definition at line 45 of file cu-sp-matrix.h.

◆ CuTpMatrix< Real >

friend class CuTpMatrix< Real >
friend

Definition at line 44 of file cu-sp-matrix.h.

◆ CuVectorBase< Real >

friend class CuVectorBase< Real >
friend

Definition at line 43 of file cu-sp-matrix.h.

◆ TraceSpSp

R TraceSpSp ( const CuSpMatrix< R > &  A,
const CuSpMatrix< S > &  B 
)
friend

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