cu-common.h
Go to the documentation of this file.
1 // cudamatrix/cu-common.h
2 
3 // Copyright 2009-2011 Karel Vesely
4 // 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 
22 
23 #ifndef KALDI_CUDAMATRIX_CU_COMMON_H_
24 #define KALDI_CUDAMATRIX_CU_COMMON_H_
25 #include "cudamatrix/cu-matrixdim.h" // for CU1DBLOCK and CU2DBLOCK
26 
27 #include <iostream>
28 #include <sstream>
29 #include "base/kaldi-error.h"
30 #include "matrix/matrix-common.h"
31 
32 #if HAVE_CUDA == 1
33 #include <cublas_v2.h>
34 #include <cusparse.h>
35 #include <curand.h>
36 #include <cuda_runtime_api.h>
37 #include "nvToolsExt.h"
38 
39 #define CU_SAFE_CALL(fun) \
40 { \
41  int32 ret; \
42  if ((ret = (fun)) != 0) { \
43  KALDI_ERR << "cudaError_t " << ret << " : \"" << cudaGetErrorString((cudaError_t)ret) << "\" returned from '" << #fun << "'"; \
44  } \
45 }
46 
47 #define CUFFT_SAFE_CALL(fun) \
48 { \
49  int32 ret; \
50  if ((ret = (fun)) != CUFFT_SUCCESS) { \
51  KALDI_ERR << "cublasResult " << ret << " returned from '" << #fun << "'"; \
52  } \
53 }
54 
55 #define CUBLAS_SAFE_CALL(fun) \
56 { \
57  int32 ret; \
58  if ((ret = (fun)) != 0) { \
59  KALDI_ERR << "cublasStatus_t " << ret << " : \"" << cublasGetStatusString((cublasStatus_t)ret) << "\" returned from '" << #fun << "'"; \
60  } \
61 }
62 
63 #define CUSOLVER_SAFE_CALL(fun) \
64 { \
65  int32 ret; \
66  if ((ret = (fun)) != 0) { \
67  KALDI_ERR << "cusolverStatus_t " << ret << " : \"" << ret << "\" returned from '" << #fun << "'"; \
68  } \
69 }
70 
71 
72 #define CUSPARSE_SAFE_CALL(fun) \
73 { \
74  int32 ret; \
75  if ((ret = (fun)) != 0) { \
76  KALDI_ERR << "cusparseStatus_t " << ret << " : \"" << cusparseGetStatusString((cusparseStatus_t)ret) << "\" returned from '" << #fun << "'"; \
77  } \
78 }
79 
80 #define CURAND_SAFE_CALL(fun) \
81 { \
82  int32 ret; \
83  if ((ret = (fun)) != 0) { \
84  KALDI_ERR << "curandStatus_t " << ret << " : \"" << curandGetStatusString((curandStatus_t)ret) << "\" returned from '" << #fun << "'"; \
85  } \
86 }
87 
88 #define KALDI_CUDA_ERR(ret, msg) \
89 { \
90  if (ret != 0) { \
91  KALDI_ERR << msg << ", diagnostics: cudaError_t " << ret << " : \"" << cudaGetErrorString((cudaError_t)ret) << "\", in " << __FILE__ << ":" << __LINE__; \
92  } \
93 }
94 
95 
96 namespace kaldi {
97 
98 #ifdef USE_NVTX
99 class NvtxTracer {
100 public:
101  NvtxTracer(const char* name);
102  ~NvtxTracer();
103 };
104 #define NVTX_RANGE(name) NvtxTracer uniq_name_using_macros(name);
105 #else
106 #define NVTX_RANGE(name)
107 #endif
108 
110 inline int32 n_blocks(int32 size, int32 block_size) {
111  return size / block_size + ((size % block_size == 0)? 0 : 1);
112 }
113 
114 cublasOperation_t KaldiTransToCuTrans(MatrixTransposeType kaldi_trans);
115 
116 
117 /*
118  This function gives you suitable dimBlock and dimGrid sizes for a simple
119  matrix operation (one that applies to each element of the matrix. The x
120  indexes will be interpreted as column indexes, and the y indexes will be
121  interpreted as row indexes; this is based on our interpretation of a matrix as
122  being row-major, i.e. having column-stride = 1, not based on CuBLAS's
123  opposite interpretation. There is a good reason for associating the column
124  index with x and not y; this helps memory locality in adjacent kernels.
125  */
126 void GetBlockSizesForSimpleMatrixOperation(int32 num_rows,
127  int32 num_cols,
128  dim3 *dimGrid,
129  dim3 *dimBlock);
130 
132 const char* cublasGetStatusString(cublasStatus_t status);
133 
135 const char* cusparseGetStatusString(cusparseStatus_t status);
136 
138 const char* curandGetStatusString(curandStatus_t status);
139 }
140 
141 #else
142 namespace kaldi {
143 #define NVTX_RANGE(name)
144 };
145 #endif // HAVE_CUDA
146 
147 namespace kaldi {
148 // Some forward declarations, needed for friend declarations.
149 template<typename Real> class CuVectorBase;
150 template<typename Real> class CuVector;
151 template<typename Real> class CuSubVector;
152 template<typename Real> class CuRand;
153 template<typename Real> class CuMatrixBase;
154 template<typename Real> class CuMatrix;
155 template<typename Real> class CuSubMatrix;
156 template<typename Real> class CuPackedMatrix;
157 template<typename Real> class CuSpMatrix;
158 template<typename Real> class CuTpMatrix;
159 template<typename Real> class CuSparseMatrix;
160 
161 template<typename Real> class CuBlockMatrix; // this has no non-CU counterpart.
162 
163 
164 }
165 
166 
167 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
The class CuBlockMatrix holds a vector of objects of type CuMatrix, say, M_1, M_2, .
kaldi::int32 int32
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 class is used for a piece of a CuMatrix.
Definition: matrix-common.h:70
Matrix for CUDA computing.
Definition: matrix-common.h:69
Matrix for CUDA computing.
Definition: matrix-common.h:75
MatrixTransposeType
Definition: matrix-common.h:32