Blame view
src/cudamatrix/cu-array-inl.h
9.25 KB
8dcb6dfcb first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
// cudamatrix/cu-array-inl.h // Copyright 2009-2016 Karel Vesely // 2013 Johns Hopkins University (author: Daniel Povey) // 2017 Shiyin Kang // See ../../COPYING for clarification regarding multiple authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. #ifndef KALDI_CUDAMATRIX_CU_ARRAY_INL_H_ #define KALDI_CUDAMATRIX_CU_ARRAY_INL_H_ #include <algorithm> #if HAVE_CUDA == 1 #include <cuda_runtime_api.h> #include "cudamatrix/cu-common.h" #include "cudamatrix/cu-device.h" #include "cudamatrix/cu-kernels.h" #endif #include "base/timer.h" namespace kaldi { template<typename T> void CuArray<T>::Resize(MatrixIndexT dim, MatrixResizeType resize_type) { KALDI_ASSERT((resize_type == kSetZero || resize_type == kUndefined) && dim >= 0); if (this->dim_ == dim) { if (resize_type == kSetZero) this->SetZero(); return; } Destroy(); if (dim == 0) return; #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuTimer tim; this->data_ = static_cast<T*>(CuDevice::Instantiate().Malloc(dim * sizeof(T))); this->dim_ = dim; if (resize_type == kSetZero) this->SetZero(); CuDevice::Instantiate().AccuProfile("CuArray::Resize", tim); } else #endif { this->data_ = static_cast<T*>(malloc(dim * sizeof(T))); // We allocate with malloc because we don't want constructors being called. // We basically ignore memory alignment issues here-- we assume the malloc // implementation is forgiving enough that it will automatically align on // sensible boundaries. if (this->data_ == 0) KALDI_ERR << "Memory allocation failed when initializing CuVector " << "with dimension " << dim << " object size in bytes: " << sizeof(T); } this->dim_ = dim; if (resize_type == kSetZero) this->SetZero(); } template<typename T> void CuArray<T>::Destroy() { #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { if (this->data_ != NULL) { CuDevice::Instantiate().Free(this->data_); } } else #endif { if (this->data_ != NULL) free(this->data_); } this->dim_ = 0; this->data_ = NULL; } template<typename T> void CuArrayBase<T>::CopyFromVec(const std::vector<T> &src) { KALDI_ASSERT(dim_ == src.size()); if (src.empty()) return; #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuTimer tim; CU_SAFE_CALL( cudaMemcpyAsync(data_, &src.front(), src.size() * sizeof(T), cudaMemcpyHostToDevice, cudaStreamPerThread)); CU_SAFE_CALL(cudaStreamSynchronize(cudaStreamPerThread)); CuDevice::Instantiate().AccuProfile(__func__, tim); } else #endif { memcpy(data_, &src.front(), src.size() * sizeof(T)); } } template<typename T> void CuArray<T>::CopyFromVec(const std::vector<T> &src) { Resize(src.size(), kUndefined); if (src.empty()) return; #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuTimer tim; CU_SAFE_CALL(cudaMemcpyAsync(this->data_, &src.front(), src.size()*sizeof(T), cudaMemcpyHostToDevice, cudaStreamPerThread)); CU_SAFE_CALL(cudaStreamSynchronize(cudaStreamPerThread)); CuDevice::Instantiate().AccuProfile(__func__, tim); } else #endif { memcpy(this->data_, &src.front(), src.size()*sizeof(T)); } } template<typename T> void CuArray<T>::CopyFromArray(const CuArrayBase<T> &src) { this->Resize(src.Dim(), kUndefined); if (this->dim_ == 0) return; #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuTimer tim; CU_SAFE_CALL(cudaMemcpyAsync(this->data_, src.data_, this->dim_ * sizeof(T), cudaMemcpyDeviceToDevice, cudaStreamPerThread)); CuDevice::Instantiate().AccuProfile(__func__, tim); } else #endif { memcpy(this->data_, src.data_, this->dim_ * sizeof(T)); } } template<typename T> void CuArrayBase<T>::CopyFromArray(const CuArrayBase<T> &src) { KALDI_ASSERT(src.Dim() == Dim()); if (dim_ == 0) return; #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuTimer tim; CU_SAFE_CALL( cudaMemcpyAsync(this->data_, src.data_, dim_ * sizeof(T), cudaMemcpyDeviceToDevice, cudaStreamPerThread)); CuDevice::Instantiate().AccuProfile(__func__, tim); } else #endif { memcpy(this->data_, src.data_, dim_ * sizeof(T)); } } template<typename T> void CuArrayBase<T>::CopyToVec(std::vector<T> *dst) const { if (static_cast<MatrixIndexT>(dst->size()) != this->dim_) { dst->resize(this->dim_); } if (this->dim_ == 0) return; #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuTimer tim; CU_SAFE_CALL(cudaMemcpyAsync(&dst->front(), Data(), this->dim_ * sizeof(T), cudaMemcpyDeviceToHost, cudaStreamPerThread)); CU_SAFE_CALL(cudaStreamSynchronize(cudaStreamPerThread)); CuDevice::Instantiate().AccuProfile("CuArray::CopyToVecD2H", tim); } else #endif { memcpy(&dst->front(), this->data_, this->dim_ * sizeof(T)); } } template<typename T> void CuArrayBase<T>::CopyToHost(T *dst) const { if (this->dim_ == 0) return; KALDI_ASSERT(dst != NULL); #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuTimer tim; CU_SAFE_CALL(cudaMemcpyAsync(dst, Data(), this->dim_ * sizeof(T), cudaMemcpyDeviceToHost, cudaStreamPerThread)); CU_SAFE_CALL(cudaStreamSynchronize(cudaStreamPerThread)); CuDevice::Instantiate().AccuProfile("CuArray::CopyToVecD2H", tim); } else #endif { memcpy(dst, this->data_, this->dim_ * sizeof(T)); } } template<typename T> void CuArrayBase<T>::SetZero() { if (this->dim_ == 0) return; #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuTimer tim; CU_SAFE_CALL(cudaMemsetAsync(this->data_, 0, this->dim_ * sizeof(T), cudaStreamPerThread)); CuDevice::Instantiate().AccuProfile("CuArray::SetZero", tim); } else #endif { memset(static_cast<void*>(this->data_), 0, this->dim_ * sizeof(T)); } } template<class T> void CuArrayBase<T>::Set(const T &value) { // This is not implemented yet, we'll do so if it's needed. KALDI_ERR << "CuArray<T>::Set not implemented yet for this type."; } // int32 specialization implemented in 'cudamatrix/cu-array.cc', template<> void CuArrayBase<int32>::Set(const int32 &value); template<class T> void CuArrayBase<T>::Sequence(const T base) { // This is not implemented yet, we'll do so if it's needed. KALDI_ERR << "CuArray<T>::Sequence not implemented yet for this type."; } // int32 specialization implemented in 'cudamatrix/cu-array.cc', template<> void CuArrayBase<int32>::Sequence(const int32 base); template<class T> void CuArrayBase<T>::Add(const T &value) { // This is not implemented yet, we'll do so if it's needed. KALDI_ERR << "CuArray<T>::Add not implemented yet for this type."; } // int32 specialization implemented in 'cudamatrix/cu-array.cc', template<> void CuArrayBase<int32>::Add(const int32 &value); template<class T> inline T CuArrayBase<T>::Min() const { KALDI_ASSERT(this->Dim() > 0); #if HAVE_CUDA == 1 CuTimer tim; #endif std::vector<T> tmp(Dim()); CopyToVec(&tmp); T ans = *std::min_element(tmp.begin(), tmp.end()); #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuDevice::Instantiate().AccuProfile(__func__, tim); } #endif return ans; } template<class T> inline T CuArrayBase<T>::Max() const { KALDI_ASSERT(this->Dim() > 0); #if HAVE_CUDA == 1 CuTimer tim; #endif std::vector<T> tmp(Dim()); CopyToVec(&tmp); T ans = *std::max_element(tmp.begin(), tmp.end()); #if HAVE_CUDA == 1 if (CuDevice::Instantiate().Enabled()) { CuDevice::Instantiate().AccuProfile(__func__, tim); } #endif return ans; } template<typename T> void CuArray<T>::Read(std::istream& in, bool binary) { std::vector<T> tmp; ReadIntegerVector(in, binary, &tmp); (*this) = tmp; } template<typename T> void CuArray<T>::Write(std::ostream& out, bool binary) const { std::vector<T> tmp(this->Dim()); this->CopyToVec(&tmp); WriteIntegerVector(out, binary, tmp); } template<typename T> CuSubArray<T>::CuSubArray(const CuArrayBase<T> &src, MatrixIndexT offset, MatrixIndexT dim) { KALDI_ASSERT(offset >= 0 && dim >= 0 && offset + dim <= src.Dim()); this->data_ = src.data_ + offset; this->dim_ = dim; } /** * Print the vector to stream */ template<typename T> std::ostream &operator << (std::ostream &out, const CuArray<T> &vec) { std::vector<T> tmp; vec.CopyToVec(&tmp); out << "["; for(int32 i=0; i<tmp.size(); i++) { out << " " << tmp[i]; } out << " ] "; return out; } template <typename T> void CuArray<T>::Swap(CuArray<T> *other) { std::swap(this->dim_, other->dim_); std::swap(this->data_, other->data_); } } // namespace kaldi #endif |