cu-sparse-matrix.h
Go to the documentation of this file.
1 // cudamatrix/cu-sparse-matrix.h
2 
3 // Copyright 2015 Johns Hopkins University (author: Daniel Povey)
4 // 2015 Guoguo Chen
5 // 2017 Shiyin Kang
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 
23 
24 #ifndef KALDI_CUDAMATRIX_CU_SPARSE_MATRIX_H_
25 #define KALDI_CUDAMATRIX_CU_SPARSE_MATRIX_H_
26 
27 #include <sstream>
28 #include <vector>
29 
31 #include "cudamatrix/cu-common.h"
32 #include "cudamatrix/cu-value.h"
33 #include "matrix/matrix-common.h"
34 #include "matrix/kaldi-matrix.h"
35 #include "matrix/sparse-matrix.h"
36 #include "cudamatrix/cu-array.h"
37 #include "cudamatrix/cu-math.h"
38 #include "cudamatrix/cu-rand.h"
39 
40 namespace kaldi {
41 
42 template <typename Real>
43 Real TraceMatSmat(const CuMatrixBase<Real> &A,
44  const CuSparseMatrix<Real> &B,
46 
47 template<class Real>
48 class CuSparseMatrix {
49 public:
50  friend class CuMatrixBase<float> ;
51  friend class CuMatrixBase<double> ;
52  friend class CuMatrixBase<Real> ;
53  friend class CuVectorBase<float> ;
54  friend class CuVectorBase<double> ;
55  friend class CuVectorBase<Real> ;
56 
57  friend Real TraceMatSmat<Real>(const CuMatrixBase<Real> &A,
58  const CuSparseMatrix<Real> &B,
59  MatrixTransposeType trans);
60 
61  MatrixIndexT NumRows() const;
62 
63  MatrixIndexT NumCols() const;
64 
65  MatrixIndexT NumElements() const;
66 
67  template<typename OtherReal>
69  kNoTrans) const;
70 
71  Real Sum() const;
72 
73  Real FrobeniusNorm() const;
74 
77 
80 
83  template<typename OtherReal>
84  void CopyFromSmat(const SparseMatrix<OtherReal> &smat);
85 
88  void CopyFromSmat(const CuSparseMatrix<Real> &smat,
90 
95  void SelectRows(const CuArray<int32> &row_indexes,
96  const CuSparseMatrix<Real> &smat_other);
97 
100  template<typename OtherReal>
101  void CopyToSmat(SparseMatrix<OtherReal> *smat) const;
102 
105  void CopyElementsToVec(CuVectorBase<Real> *vec) const;
106 
108  void Swap(SparseMatrix<Real> *smat);
109 
111  void Swap(CuSparseMatrix<Real> *smat);
112 
116  void SetRandn(BaseFloat zero_prob);
117 
118  void Write(std::ostream &os, bool binary) const;
119 
120  void Read(std::istream &is, bool binary);
121 
125  NULL) {
126  }
127 
129  explicit CuSparseMatrix(const SparseMatrix<Real> &smat) :
131  NULL) {
132  this->CopyFromSmat(smat);
133  }
134 
137  kNoTrans) :
139  NULL) {
140  this->CopyFromSmat(smat, trans);
141  }
142 
153  CuSparseMatrix(const CuArray<int32> &indexes, int32 dim,
154  MatrixTransposeType trans = kNoTrans);
155 
166  CuSparseMatrix(const CuArray<int32> &indexes,
167  const CuVectorBase<Real> &weights, int32 dim,
168  MatrixTransposeType trans = kNoTrans);
169 
171  Destroy();
172  }
173 
174 protected:
175  // The following two functions should only be called if we did not compile
176  // with CUDA or could not get a CUDA card; in that case the contents are
177  // interpreted the same as a regular sparse matrix.
178  inline const SparseMatrix<Real> &Smat() const {
179  return *(reinterpret_cast<const SparseMatrix<Real>*>(this));
180  }
182  return *(reinterpret_cast<SparseMatrix<Real>*>(this));
183  }
184 
187  void Resize(const MatrixIndexT num_rows, const MatrixIndexT num_cols,
188  const MatrixIndexT nnz, MatrixResizeType resize_type = kSetZero);
189 
192  const Real* CsrVal() const {
193  return csr_val_;
194  }
195  Real* CsrVal() {
196  return csr_val_;
197  }
198 
202  const int* CsrRowPtr() const {
203  return csr_row_ptr_col_idx_;
204  }
205  int* CsrRowPtr() {
206  return csr_row_ptr_col_idx_;
207  }
208 
211  const int* CsrColIdx() const {
212  return csr_row_ptr_col_idx_ + num_rows_ + 1;
213  }
214  int* CsrColIdx() {
215  return csr_row_ptr_col_idx_ + num_rows_ + 1;
216  }
217 
218 private:
219  void Destroy();
220 
221 private:
222  // This member is only used if we did not compile for the GPU, or if the GPU
223  // is not enabled. It needs to be first because we reinterpret_cast this
224  std::vector<SparseVector<Real> > cpu_rows_;
225 
226  // This is where the data lives if we are using a GPU.
227  // The sparse matrix is stored in CSR format, as documented here.
228  // http://docs.nvidia.com/cuda/cusparse/index.html#compressed-sparse-row-format-csr
229  // The 3 arrays are stored in 2 allocated blocks of memory.
230  // Row ptr and col idx are both int arrays, thus stored in one block pointed
231  // 'by csr_row_ptr_col_idx_'
232  // Val are Real array, pointed by `csr_val_`
233 
234  // matrix size num_rows_ x num_cols_
237 
238  // number of non-zeros
240 
241  // csr row ptrs and col indices in a single int array
242  // of the length (num_rows_ + 1 + nnz_)
244 
245  // csr value array of the length nnz_
246  Real* csr_val_;
247 };
248 
249 
250 } // namespace
251 
252 #endif // KALDI_CUDAMATRIX_CU_SPARSE_MATRIX_H_
CuSparseMatrix(const SparseMatrix< Real > &smat)
Constructor from CPU-based sparse matrix.
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
MatrixIndexT NumRows() const
const Real * CsrVal() const
Returns pointer to the data array of length nnz_ that holds all nonzero values in zero-based CSR form...
MatrixResizeType
Definition: matrix-common.h:37
void CopyToMat(CuMatrixBase< OtherReal > *dest, MatrixTransposeType trans=kNoTrans) const
void Resize(const MatrixIndexT num_rows, const MatrixIndexT num_cols, const MatrixIndexT nnz, MatrixResizeType resize_type=kSetZero)
Users of this class won&#39;t normally have to use Resize.
void Swap(SparseMatrix< Real > *smat)
Swap with CPU-based matrix.
void CopyFromSmat(const SparseMatrix< OtherReal > &smat)
Copy from CPU-based matrix.
void SelectRows(const CuArray< int32 > &row_indexes, const CuSparseMatrix< Real > &smat_other)
Select a subset of the rows of a CuSparseMatrix.
void Read(std::istream &is, bool binary)
kaldi::int32 int32
std::vector< SparseVector< Real > > cpu_rows_
int32 MatrixIndexT
Definition: matrix-common.h:98
const int * CsrColIdx() const
Returns pointer to the integer array of length nnz_ that contains the column indices of the correspon...
const SparseMatrix< Real > & Smat() const
void CopyElementsToVec(CuVectorBase< Real > *vec) const
Copy elements to CuVector.
SparseMatrix< Real > & Smat()
MatrixIndexT NumElements() const
CuSparseMatrix< Real > & operator=(const SparseMatrix< Real > &smat)
Copy from CPU-based matrix.
CuSparseMatrix(const CuSparseMatrix< Real > &smat, MatrixTransposeType trans=kNoTrans)
Constructor from GPU-based sparse matrix (supports transposition).
void CopyToSmat(SparseMatrix< OtherReal > *smat) const
Copy to CPU-based matrix.
CuSparseMatrix()
Default constructor.
const int * CsrRowPtr() const
Returns pointer to the integer array of length NumRows()+1 that holds indices of the first nonzero el...
Matrix for CUDA computing.
Definition: matrix-common.h:69
MatrixTransposeType
Definition: matrix-common.h:32
Real TraceMatSmat(const MatrixBase< Real > &A, const SparseMatrix< Real > &B, MatrixTransposeType trans)
void Write(std::ostream &os, bool binary) const
MatrixIndexT NumCols() const
Vector for CUDA computing.
Definition: matrix-common.h:72
void SetRandn(BaseFloat zero_prob)
Sets up to a pseudo-randomly initialized matrix, with each element zero with probability zero_prob an...