cu-array.cc
Go to the documentation of this file.
1 // cudamatrix/cu-array.cc
2 
3 // Copyright 2016 Brno University of Technology (author: Karel Vesely)
4 // 2017 Shiyin Kang
5 
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 #include <vector>
23 
24 #if HAVE_CUDA == 1
25 #include <cuda_runtime_api.h>
26 #endif
27 
28 #include "base/timer.h"
29 #include "cudamatrix/cu-common.h"
30 #include "cudamatrix/cu-device.h"
32 #include "cudamatrix/cu-kernels.h"
33 
34 #include "cudamatrix/cu-array.h"
35 
36 namespace kaldi {
37 
38 template<>
40  if (dim_ == 0) return;
41 #if HAVE_CUDA == 1
42  if (CuDevice::Instantiate().Enabled()) {
43  CuTimer tim;
44 
45  dim3 dimBlock(CU1DBLOCK);
46  dim3 dimGrid(n_blocks(Dim(), CU1DBLOCK));
47 
48  cuda_sequence(dimGrid, dimBlock, Data(), Dim(), base);
49  CU_SAFE_CALL(cudaGetLastError());
50 
51  CuDevice::Instantiate().AccuProfile(__func__, tim);
52  } else
53 #endif
54  {
55  for (int32 i = 0; i < dim_; i++) {
56  data_[i] = base + i;
57  }
58  }
59 }
60 
61 
62 template<>
63 void CuArrayBase<int32>::Set(const int32 &value) {
64  if (dim_ == 0) return;
65 #if HAVE_CUDA == 1
66  if (CuDevice::Instantiate().Enabled()) {
67  CuTimer tim;
68 
69  dim3 dimBlock(CU2DBLOCK);
70  dim3 dimGrid(n_blocks(Dim(), CU2DBLOCK));
71  ::MatrixDim d = { 1, Dim(), Dim() };
72 
73  cuda_int32_set_const(dimGrid, dimBlock, data_, value, d);
74  CU_SAFE_CALL(cudaGetLastError());
75 
76  CuDevice::Instantiate().AccuProfile(__func__, tim);
77  } else
78 #endif
79  {
80  for (int32 i = 0; i < dim_; i++) {
81  data_[i] = value;
82  }
83  }
84 }
85 
86 
87 template<>
88 void CuArrayBase<int32>::Add(const int32 &value) {
89  if (dim_ == 0) return;
90 #if HAVE_CUDA == 1
91  if (CuDevice::Instantiate().Enabled()) {
92  CuTimer tim;
93 
94  dim3 dimBlock(CU2DBLOCK);
95  dim3 dimGrid(n_blocks(Dim(), CU2DBLOCK));
96  ::MatrixDim d = { 1, Dim(), Dim() };
97 
98  cuda_int32_add(dimGrid, dimBlock, data_, value, d);
99  CU_SAFE_CALL(cudaGetLastError());
100 
101  CuDevice::Instantiate().AccuProfile(__func__, tim);
102  } else
103 #endif
104  {
105  for (int32 i = 0; i < dim_; i++) {
106  data_[i] += value;
107  }
108  }
109 }
110 
111 } // namespace kaldi
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Structure containing size of the matrix plus stride.
Definition: cu-matrixdim.h:46
kaldi::int32 int32
uint64 data_
#define CU1DBLOCK
Definition: cu-matrixdim.h:57
#define CU2DBLOCK
Definition: cu-matrixdim.h:61
Class CuArrayBase, CuSubArray and CuArray are analogues of classes CuVectorBase, CuSubVector and CuVe...
Definition: cu-array.h:44
void Sequence(const T base)
Fill with the sequence [base ...
Definition: cu-array-inl.h:244