cu-sp-matrix.h
Go to the documentation of this file.
1 // cudamatrix/cu-sp-matrix.h
2 
3 // Copyright 2009-2013 Karel Vesely
4 // 2014 Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #ifndef KALDI_CUDAMATRIX_CU_SP_MATRIX_H_
22 #define KALDI_CUDAMATRIX_CU_SP_MATRIX_H_
23 
24 #include <sstream>
25 
26 #include "cudamatrix/cu-common.h"
27 #include "matrix/matrix-common.h"
28 #include "matrix/sp-matrix.h"
29 #include "cudamatrix/cu-array.h"
30 #include "cudamatrix/cu-math.h"
32 #include "cudamatrix/cu-matrix.h"
33 
34 namespace kaldi {
35 
37 template<typename Real, typename OtherReal>
38 Real TraceSpSp(const CuSpMatrix<Real> &A, const CuSpMatrix<OtherReal> &B);
39 
40 template<typename Real>
41 class CuSpMatrix : public CuPackedMatrix<Real> {
42  friend class CuMatrixBase<Real>;
43  friend class CuVectorBase<Real>;
44  friend class CuTpMatrix<Real>;
45  friend class CuSubMatrix<Real>;
46  friend class CuRand<Real>;
47 
48  template<class R, class S>
49  friend R TraceSpSp(const CuSpMatrix<R> &A, const CuSpMatrix<S> &B);
50  public:
51 
52  CuSpMatrix(): CuPackedMatrix<Real>() {}
53 
54  explicit CuSpMatrix(MatrixIndexT r, MatrixResizeType resize_type = kSetZero)
55  : CuPackedMatrix<Real>(r, resize_type) {}
56 
57  explicit CuSpMatrix(const SpMatrix<Real> &orig)
58  : CuPackedMatrix<Real>(orig) {}
59 
60  // This constructor lacks the "explicit" keyword so that
61  // we can include it in std::vector.
63  : CuPackedMatrix<Real>(orig) {}
64 
65  explicit CuSpMatrix(const CuMatrixBase<Real> &orig,
66  SpCopyType copy_type = kTakeLower)
67  : CuPackedMatrix<Real>(orig.NumRows(), kUndefined) {
68  CopyFromMat(orig, copy_type);
69  }
70 
72 
74 
75  inline void Resize(MatrixIndexT nRows, MatrixResizeType resize_type = kSetZero) {
76  CuPackedMatrix<Real>::Resize(nRows, resize_type);
77  }
78 
79  Real FrobeniusNorm() const { return sqrt(TraceSpSp(*this, *this)); }
80 
81  bool IsUnit(Real tol = 0.001) const;
82 
83  bool ApproxEqual(const CuSpMatrix<Real> &other, Real tol = 0.001) const;
84 
85  void CopyFromSp(const CuSpMatrix<Real> &other) {
87  }
88  void CopyFromSp(const SpMatrix<Real> &other) {
90  }
91 
92  void CopyFromMat(const CuMatrixBase<Real> &orig,
93  SpCopyType copy_type = kTakeLower);
94 
95  void CopyToSp(SpMatrix<Real> *dst) const {
97  }
98 
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  }
107 
108  inline Real operator() (MatrixIndexT r, MatrixIndexT c) const {
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  }
117 
120  void Invert();
121 
122  void AddVec2(const Real alpha, const CuVectorBase<Real> &v);
123 
124  void AddMat2(const Real alpha, const CuMatrixBase<Real> &M,
125  MatrixTransposeType transM, const Real beta);
126 
127  void AddSp(const Real alpha, const CuSpMatrix<Real> &Ma) {
128  this->AddPacked(alpha, Ma);
129  }
130 
131  protected:
132  inline const SpMatrix<Real> &Mat() const {
133  return *(reinterpret_cast<const SpMatrix<Real>* >(this));
134  }
135  inline SpMatrix<Real> &Mat() {
136  return *(reinterpret_cast<SpMatrix<Real>* >(this));
137  }
138 };
139 
140 template<typename Real>
141 inline bool ApproxEqual(const CuSpMatrix<Real> &A,
142  const CuSpMatrix<Real> &B, Real tol = 0.001) {
143  return A.ApproxEqual(B, tol);
144 }
145 
146 template<typename Real>
147 inline void AssertEqual(const CuSpMatrix<Real> &A,
148  const CuSpMatrix<Real> &B, Real tol = 0.001) {
149  KALDI_ASSERT(ApproxEqual(A, B, tol));
150 }
151 
152 
153 template<typename Real>
155  Resize(cu.NumRows());
156  cu.CopyToSp(this);
157 }
158 
159 
160 
161 } // namespace
162 
163 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Packed symetric matrix class.
Definition: matrix-common.h:62
CuSpMatrix< Real > & operator=(const CuSpMatrix< Real > &in)
MatrixResizeType
Definition: matrix-common.h:37
void AddPacked(const Real alpha, const CuPackedMatrix< Real > &M)
MatrixIndexT NumRows() const
CuValue< Real > operator()(MatrixIndexT r, MatrixIndexT c)
Definition: cu-sp-matrix.h:99
void CopyToPacked(PackedMatrix< Real > *dst) const
CuSpMatrix(MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
Definition: cu-sp-matrix.h:54
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
void Invert()
Note: the CuMatrix version of the Invert() function will only work for positive definite matrices; it...
Definition: cu-sp-matrix.cc:93
SpMatrix< Real > & Mat()
Definition: cu-sp-matrix.h:135
The following class is used to simulate non-const references to Real, e.g.
Definition: cu-value.h:34
const SpMatrix< Real > & Mat() const
Definition: cu-sp-matrix.h:132
int32 MatrixIndexT
Definition: matrix-common.h:98
void CopyFromSp(const SpMatrix< Real > &other)
Definition: cu-sp-matrix.h:88
double TraceSpSp(const SpMatrix< double > &A, const SpMatrix< double > &B)
Definition: sp-matrix.cc:326
This class is used for a piece of a CuMatrix.
Definition: matrix-common.h:70
bool IsUnit(Real tol=0.001) const
Real FrobeniusNorm() const
Definition: cu-sp-matrix.h:79
void CopyFromMat(const CuMatrixBase< Real > &orig, SpCopyType copy_type=kTakeLower)
Definition: cu-sp-matrix.cc:39
void CopyToSp(SpMatrix< Real > *dst) const
Definition: cu-sp-matrix.h:95
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Definition: cu-sp-matrix.h:75
void CopyFromSp(const CuSpMatrix< Real > &other)
Definition: cu-sp-matrix.h:85
Matrix for CUDA computing.
Definition: matrix-common.h:69
void AddSp(const Real alpha, const CuSpMatrix< Real > &Ma)
Definition: cu-sp-matrix.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Matrix for CUDA computing.
Definition: matrix-common.h:75
MatrixTransposeType
Definition: matrix-common.h:32
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
friend R TraceSpSp(const CuSpMatrix< R > &A, const CuSpMatrix< S > &B)
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).
CuSpMatrix(const CuSpMatrix< Real > &orig)
Definition: cu-sp-matrix.h:62
CuSpMatrix(const CuMatrixBase< Real > &orig, SpCopyType copy_type=kTakeLower)
Definition: cu-sp-matrix.h:65
void AddMat2(const Real alpha, const CuMatrixBase< Real > &M, MatrixTransposeType transM, const Real beta)
void CopyFromPacked(const CuPackedMatrix< Real > &src)
Vector for CUDA computing.
Definition: matrix-common.h:72
CuSpMatrix(const SpMatrix< Real > &orig)
Definition: cu-sp-matrix.h:57
void AddVec2(const Real alpha, const CuVectorBase< Real > &v)
bool ApproxEqual(const CuSpMatrix< Real > &other, Real tol=0.001) const