cu-packed-matrix.h
Go to the documentation of this file.
1 // cudamatrix/cu-packed-matrix.h
2 
3 // Copyright 2009-2013 Johns Hopkins University (author: Daniel Povey)
4 // Karel Vesely
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 
22 
23 #ifndef KALDI_CUDAMATRIX_CU_PACKED_MATRIX_H_
24 #define KALDI_CUDAMATRIX_CU_PACKED_MATRIX_H_
25 
26 #include <sstream>
27 
28 #include "cudamatrix/cu-common.h"
29 #include "cudamatrix/cu-value.h"
30 #include "matrix/matrix-common.h"
31 #include "matrix/kaldi-matrix.h"
32 #include "matrix/packed-matrix.h"
33 #include "matrix/sp-matrix.h"
34 #include "cudamatrix/cu-array.h"
35 #include "cudamatrix/cu-math.h"
36 #include "cudamatrix/cu-matrix.h"
37 
38 namespace kaldi {
39 
40 
50 template<typename Real>
53 class CuPackedMatrix {
54  public:
55  friend class CuMatrixBase<Real>;
56  friend class CuVectorBase<Real>;
57  friend class CuSubMatrix<Real>;
58  friend class CuRand<Real>;
59 
60  CuPackedMatrix() : data_(NULL), num_rows_(0) {}
61 
63  MatrixResizeType resize_type = kSetZero):
64  data_(NULL), num_rows_(0) { Resize(r, resize_type); }
65 
66  explicit CuPackedMatrix(const PackedMatrix<Real> &orig) : data_(NULL), num_rows_(0) {
68  CopyFromPacked(orig);
69  }
70 
71  explicit CuPackedMatrix(const CuPackedMatrix<Real> &orig) : data_(NULL), num_rows_(0) {
72  Resize(orig.NumRows(), kUndefined);
73  CopyFromPacked(orig);
74  }
75 
76  void SetZero();
77  void SetUnit();
78  void SetRandn();
79  void SetDiag(Real alpha);
80  void AddToDiag(Real r);
81 
82  void Scale(Real alpha);
83  void ScaleDiag(Real alpha);
84  Real Trace() const;
85 
87 
95  void Resize(MatrixIndexT nRows, MatrixResizeType resize_type = kSetZero);
96 
97  // Copy functions (do not resize).
98  void CopyFromPacked(const CuPackedMatrix<Real> &src);
99  void CopyFromPacked(const PackedMatrix<Real> &src);
100  void CopyToPacked(PackedMatrix<Real> *dst) const;
101 
102  void Read(std::istream &in, bool binary);
103 
104  void Write(std::ostream &out, bool binary) const;
105 
106  void Destroy();
107 
109  void Swap(CuPackedMatrix<Real> *other);
110 
112  void Swap(PackedMatrix<Real> *other);
113  Real* Data() { return data_; }
114  const Real* Data() const { return data_; }
115 
116  inline Real operator() (MatrixIndexT r, MatrixIndexT c) const {
117  if (static_cast<UnsignedMatrixIndexT>(c) >
118  static_cast<UnsignedMatrixIndexT>(r))
119  std::swap(c, r);
120  KALDI_ASSERT(static_cast<UnsignedMatrixIndexT>(r) <
121  static_cast<UnsignedMatrixIndexT>(this->num_rows_));
122 #if HAVE_CUDA == 1
123  if (CuDevice::Instantiate().Enabled()) {
124  Real value;
125  CU_SAFE_CALL(cudaMemcpyAsync(&value, this->data_ + (r * (r+1)) / 2 + c,
126  sizeof(Real), cudaMemcpyDeviceToHost,
127  cudaStreamPerThread));
128  CU_SAFE_CALL(cudaStreamSynchronize(cudaStreamPerThread));
129  return value;
130  } else
131 #endif
132  return this->data_[(r * (r+1)) / 2 + c];
133  }
134 
135  inline MatrixIndexT NumRows() const { return num_rows_; }
136  inline MatrixIndexT NumCols() const { return num_rows_; }
137 
139  size_t SizeInBytes() const {
140  size_t nr = static_cast<size_t>(num_rows_),
141  num_bytes = ((nr * (nr+1)) / 2) * sizeof(Real);
142  return num_bytes;
143  }
144 
145 
146  protected:
147  // The following two functions should only be called if we did not compile with CUDA
148  // or could not get a CUDA card; in that case the contents are interpreted the
149  // same as a regular matrix.
150  inline const PackedMatrix<Real> &Mat() const {
151  return *(reinterpret_cast<const PackedMatrix<Real>* >(this));
152  }
154  return *(reinterpret_cast<PackedMatrix<Real>* >(this));
155  }
156 
157 
158  // Will only be called from this class or derived classes.
159 
160  Real *data_;
162  void AddPacked(const Real alpha, const CuPackedMatrix<Real> &M);
163 
164  private:
165  // Disallow assignment.
167 }; // class CuPackedMatrix
168 
169 
171 template<typename Real>
172 std::ostream &operator << (std::ostream &out, const CuPackedMatrix<Real> &mat);
173 
174 } // namespace
175 
176 
177 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
CuPackedMatrix(MatrixIndexT r, MatrixResizeType resize_type=kSetZero)
MatrixResizeType
Definition: matrix-common.h:37
const PackedMatrix< Real > & Mat() const
void Swap(CuPackedMatrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
void AddPacked(const Real alpha, const CuPackedMatrix< Real > &M)
void SetDiag(Real alpha)
< Set to random values of a normal distribution
MatrixIndexT NumRows() const
CuPackedMatrix(const CuPackedMatrix< Real > &orig)
void CopyToPacked(PackedMatrix< Real > *dst) const
void AddToDiag(Real r)
< Set the diagonal value to alpha
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
const Real * Data() const
void SetRandn()
< Set to unit matrix.
MatrixIndexT num_rows_
int32 MatrixIndexT
Definition: matrix-common.h:98
Packed matrix: base class for triangular and symmetric matrices.
Definition: matrix-common.h:64
void Read(std::istream &in, bool binary)
void Scale(Real alpha)
PackedMatrix< Real > & operator=(const PackedMatrix< Real > &other)
void SetUnit()
< Set to zero
This class is used for a piece of a CuMatrix.
Definition: matrix-common.h:70
size_t SizeInBytes() const
Returns size in bytes of the data held by the matrix.
CuPackedMatrix(const PackedMatrix< Real > &orig)
void ScaleDiag(Real alpha)
Matrix for CUDA computing.
Definition: matrix-common.h:69
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Matrix for CUDA computing.
Definition: matrix-common.h:75
PackedMatrix< Real > & Mat()
void Resize(MatrixIndexT nRows, MatrixResizeType resize_type=kSetZero)
Set packed matrix to a specified size (can be zero).
void Write(std::ostream &out, bool binary) const
MatrixIndexT NumCols() const
Real operator()(MatrixIndexT r, MatrixIndexT c) const
void CopyFromPacked(const CuPackedMatrix< Real > &src)
Vector for CUDA computing.
Definition: matrix-common.h:72