cu-value.h
Go to the documentation of this file.
1 // cudamatrix/cu-value.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_VALUE_H_
23 #define KALDI_CUDAMATRIX_CU_VALUE_H_
24 
25 #include "cudamatrix/cu-device.h"
26 
27 namespace kaldi {
28 
33 template<typename Real>
34 class CuValue {
35  public:
36  CuValue(Real *data): data_(data) { }
37  CuValue(const CuValue &other): data_(other.data_) { }
38 
39  inline CuValue operator = (const CuValue<Real> &other) {
40 #if HAVE_CUDA == 1
41  if (CuDevice::Instantiate().Enabled()) {
42  CU_SAFE_CALL(
43  cudaMemcpyAsync(data_, other.data_, sizeof(Real),
44  cudaMemcpyDeviceToDevice, cudaStreamPerThread));
45  return *this;
46  } else
47 #endif
48  {
49  *data_ = *other.data_;
50  return *this;
51  }
52  }
53 
54  inline Real operator = (Real r) { // assignment from Real
55 #if HAVE_CUDA == 1
56  if (CuDevice::Instantiate().Enabled()) {
57  CU_SAFE_CALL(cudaMemcpyAsync(data_, &r, sizeof(Real),
58  cudaMemcpyHostToDevice, cudaStreamPerThread));
59  CU_SAFE_CALL(cudaStreamSynchronize(cudaStreamPerThread));
60  return r;
61  } else
62 #endif
63  {
64  *data_ = r;
65  return r;
66  }
67  }
68 
69  inline Real operator += (Real r) { return (*this = r + Real(*this)); }
70  inline Real operator -= (Real r) { return (*this = Real(*this) - r); }
71 
72 
73  inline operator Real () const { // assignment to Real
74 #if HAVE_CUDA == 1
75  if (CuDevice::Instantiate().Enabled()) {
76  Real value;
77  CU_SAFE_CALL(cudaMemcpyAsync(&value, data_, sizeof(Real),
78  cudaMemcpyDeviceToHost, cudaStreamPerThread));
79  CU_SAFE_CALL(cudaStreamSynchronize(cudaStreamPerThread));
80  return value;
81  } else
82 #endif
83  return *data_;
84  }
85  private:
86  Real *data_;
87 }; // class CuValue<Real>
88 
89 
90 } // namespace
91 
92 
93 
94 #endif // KALDI_CUDAMATRIX_CU_VALUE_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
CuValue(Real *data)
Definition: cu-value.h:36
The following class is used to simulate non-const references to Real, e.g.
Definition: cu-value.h:34
Real * data_
Definition: cu-value.h:86
Real operator+=(Real r)
Definition: cu-value.h:69
CuValue operator=(const CuValue< Real > &other)
Definition: cu-value.h:39
Real operator-=(Real r)
Definition: cu-value.h:70
CuValue(const CuValue &other)
Definition: cu-value.h:37