cu-array.h
Go to the documentation of this file.
1 // cudamatrix/cu-array.h
2 
3 // Copyright 2009-2012 Karel Vesely
4 // 2013 Johns Hopkins University (author: Daniel Povey)
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_ARRAY_H_
25 #define KALDI_CUDAMATRIX_CU_ARRAY_H_
26 
27 #include "matrix/kaldi-vector.h"
28 
29 
30 namespace kaldi {
31 
32 template <typename T> class CuArray;
33 template <typename T> class CuSubArray;
34 
35 
43 template<typename T>
44 class CuArrayBase {
45  friend class CuArray<T>;
46  friend class CuSubArray<T>;
47  public:
49  MatrixIndexT Dim() const { return dim_; }
50 
52  const T* Data() const { return data_; }
53 
54  T* Data() { return data_; }
55 
58  void SetZero();
59 
63  void CopyFromArray(const CuArrayBase<T> &src);
64 
68  void CopyFromVec(const std::vector<T> &src);
69 
75  void CopyToVec(std::vector<T> *dst) const;
76 
81  void CopyToHost(T *dst) const;
82 
83 
87  void Set(const T &value);
88 
91  void Sequence(const T base);
92 
95  void Add(const T &value);
96 
99  T Min() const;
100 
103  T Max() const;
104 
105  protected:
108  CuArrayBase<T>(): data_(NULL), dim_(0) { }
109 
110 
111  T *data_;
114 
115 };
116 
123 template<typename T>
124 class CuArray: public CuArrayBase<T> {
125  public:
126 
130 
133  explicit CuArray<T>(MatrixIndexT dim, MatrixResizeType resize_type = kSetZero)
134  { Resize(dim, resize_type); }
135 
137  explicit CuArray<T>(const std::vector<T> &src) { CopyFromVec(src); }
138 
141  CuArray<T>(const CuArray<T> &src) { CopyFromArray(src); }
142 
144  ~CuArray() { Destroy(); }
145 
148  void Resize(MatrixIndexT dim, MatrixResizeType resize_type = kSetZero);
149 
152  void Destroy();
153 
156  void CopyFromVec(const std::vector<T> &src);
157 
159  void CopyFromArray(const CuArrayBase<T> &src);
160 
161  CuArray<T> &operator= (const CuArray<T> &in) {
162  this->CopyFromArray(in); return *this;
163  }
164 
165  CuArray<T> &operator= (const std::vector<T> &in) {
166  this->CopyFromVec(in); return *this;
167  }
168 
170  void Swap(CuArray<T> *other);
171 
173  void Read(std::istream &is, bool binary);
174  void Write(std::ostream &is, bool binary) const;
175 
176 };
177 
178 
179 template<typename T>
180 class CuSubArray: public CuArrayBase<T> {
181  public:
185  explicit CuSubArray<T>(const CuArrayBase<T> &src,
186  MatrixIndexT offset, MatrixIndexT dim);
187 
189  CuSubArray(const T* data, MatrixIndexT length) {
190  // Yes, we're evading C's restrictions on const here, and yes, it can be used
191  // to do wrong stuff; unfortunately the workaround would be very difficult.
192  this->data_ = const_cast<T*>(data);
193  this->dim_ = length;
194  }
195 };
196 
197 
198 
200 template<typename T>
201 std::ostream &operator << (std::ostream &out, const CuArray<T> &vec);
202 
203 } // namespace
204 
205 #include "cudamatrix/cu-array-inl.h"
206 
207 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
MatrixResizeType
Definition: matrix-common.h:37
T Min() const
Get minimum value (for now implemented on CPU, reimplement if slow).
Definition: cu-array-inl.h:264
void CopyToVec(std::vector< T > *dst) const
This function resizes *dst if needed.
Definition: cu-array-inl.h:177
const T * Data() const
Get raw pointer.
Definition: cu-array.h:52
void CopyFromArray(const CuArrayBase< T > &src)
The caller is responsible to ensure dim is equal between *this and src.
Definition: cu-array-inl.h:157
void Add(const T &value)
Add a constant value.
Definition: cu-array-inl.h:254
int32 MatrixIndexT
Definition: matrix-common.h:98
T * data_
GPU data pointer (if GPU not available, will point to CPU memory).
Definition: cu-array.h:111
void Set(const T &value)
Set to a constant value.
Definition: cu-array-inl.h:234
void CopyToHost(T *dst) const
Version of the above function that copies contents to a host array (i.e.
Definition: cu-array-inl.h:198
~CuArray()
Destructor.
Definition: cu-array.h:144
MatrixIndexT dim_
dimension of the vector
Definition: cu-array.h:113
CuSubArray(const T *data, MatrixIndexT length)
Construct from raw pointers.
Definition: cu-array.h:189
Class CuArrayBase, CuSubArray and CuArray are analogues of classes CuVectorBase, CuSubVector and CuVe...
Definition: cu-array.h:44
Class CuArray represents a vector of an integer or struct of type T.
Definition: cu-array.h:32
void CopyFromVec(const std::vector< T > &src)
The caller is responsible to ensure dim is equal between *this and src.
Definition: cu-array-inl.h:100
void SetZero()
Sets the memory for the object to zero, via memset.
Definition: cu-array-inl.h:217
void Sequence(const T base)
Fill with the sequence [base ...
Definition: cu-array-inl.h:244
T Max() const
Get minimum value (for now implemented on CPU, reimplement if slow).
Definition: cu-array-inl.h:282
MatrixIndexT Dim() const
Return the vector dimension.
Definition: cu-array.h:49