cu-block-matrix.h
Go to the documentation of this file.
1 // cudamatrix/cu-block-matrix.h
2 
3 // Copyright 2013 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 
22 #ifndef KALDI_CUDAMATRIX_CU_BLOCK_MATRIX_H_
23 #define KALDI_CUDAMATRIX_CU_BLOCK_MATRIX_H_
24 
25 #include <sstream>
26 
27 #include <vector>
28 #include "cudamatrix/cu-common.h"
29 
30 
31 namespace kaldi {
32 
33 
50 template<typename Real>
52  public:
53  friend class CuMatrixBase<Real>;
54 
55  CuBlockMatrix();
56 
57  CuBlockMatrix(const std::vector<CuMatrix<Real> > &data);
58 
60 
62  CuBlockMatrix(const CuBlockMatrix &other);
63 
65  CuBlockMatrix &operator= (const CuBlockMatrix &other);
66 
67  void Write(std::ostream &os, bool binary) const;
68 
69  void Read(std::istream &is, bool binary);
70 
71  MatrixIndexT NumRows() const { return num_rows_; }
72 
73  MatrixIndexT NumCols() const { return data_.num_cols_; }
74 
75  MatrixIndexT NumBlocks() const { return block_data_.size(); }
76 
77  // Returns max num-columns of any block
78  MatrixIndexT MaxBlockCols() const ;
79 
80  // Returns max num-rows of any block
81  MatrixIndexT MaxBlockRows() const;
82 
83  const CuSubMatrix<Real> Block(MatrixIndexT b) const;
84 
85  CuSubMatrix<Real> Block(MatrixIndexT b); // return CuMatrixBase to disallow resizes.
86 
87 
91  void AddMatMat(BaseFloat alpha,
92  const CuMatrix<Real> &A, MatrixTransposeType transA,
93  const CuMatrix<Real> &B, MatrixTransposeType transB,
94  BaseFloat beta);
95 
96 
100  void CopyFromMat(const CuMatrix<Real> &M);
101 
105  void NormalizeColumns();
106 
107  void Swap(CuBlockMatrix *other);
108 
109  protected:
110  CuMatrix<Real> data_; // This is a single matrix into which
111  // we pack all the blocks (possibly with spaces left over)
112 
118  };
119 
120 
121 #if HAVE_CUDA == 1
122  const CuBlockMatrixData* CuData() const { return cu_data_; }
123 #endif
124  private:
125 
127  void FreeCudaData();
129  void SetCudaData();
130 
131 
133  void Destroy();
134 
135  std::vector<BlockMatrixData> block_data_;
136 
137  MatrixIndexT num_rows_; // sum of num_rows of elements of block_data_.
138 #if HAVE_CUDA == 1
139  CuBlockMatrixData *cu_data_; // We store the pointers and some additional info
140  // on the GPU card in a form more suited to
141  // use by CUDA kernels.
142 #endif
143 }; // class CuBlockMatrix
144 
145 template<typename Real>
146 std::ostream &operator << (std::ostream &out, const CuBlockMatrix<Real> &mat);
147 
148 
149 } // namespace Kaldi
150 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
MatrixIndexT MaxBlockRows() const
CuBlockMatrix & operator=(const CuBlockMatrix &other)
Assignment operator.
std::vector< BlockMatrixData > block_data_
void FreeCudaData()
If using GPU and cu_data_ != NULL, free cu_data_ and set it to NULL.
void CopyFromMat(const CuMatrix< Real > &M)
Copies elements within the block structure from matrix M, discarding others.
void NormalizeColumns()
Normalizes the columns of *this so that each one sums to one.
The class CuBlockMatrix holds a vector of objects of type CuMatrix, say, M_1, M_2, .
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
This structure is used in cu-block-matrix.h to store information about a block-diagonal matrix...
Definition: cu-matrixdim.h:68
MatrixIndexT NumRows() const
CuMatrix< Real > data_
int32 MatrixIndexT
Definition: matrix-common.h:98
void AddMatMat(BaseFloat alpha, const CuMatrix< Real > &A, MatrixTransposeType transA, const CuMatrix< Real > &B, MatrixTransposeType transB, BaseFloat beta)
Does *this = alpha A B + beta * *this, discarding elements of the product outside the block structure...
void SetCudaData()
If using GPU, allocate and set cu_data_ on the GPU to reflect "data_".
This class is used for a piece of a CuMatrix.
Definition: matrix-common.h:70
Matrix for CUDA computing.
Definition: matrix-common.h:69
MatrixIndexT NumBlocks() const
void Swap(CuBlockMatrix *other)
MatrixTransposeType
Definition: matrix-common.h:32
void Read(std::istream &is, bool binary)
MatrixIndexT NumCols() const
MatrixIndexT MaxBlockCols() const
void Destroy()
Frees and deinitializes everything.
const CuSubMatrix< Real > Block(MatrixIndexT b) const
void Write(std::ostream &os, bool binary) const