You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpu_device_address.cc 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "device/gpu/gpu_device_address.h"
  17. #include <vector>
  18. #include "device/gpu/gpu_device_manager.h"
  19. #include "utils/log_adapter.h"
  20. #include "device/gpu/gpu_memory_allocator.h"
  21. namespace mindspore {
  22. namespace device {
  23. namespace gpu {
  24. bool GPUDeviceAddress::SyncDeviceToHost(const std::vector<int> &, size_t size, TypeId, void *host_ptr) const {
  25. MS_EXCEPTION_IF_NULL(host_ptr);
  26. auto &stream = GPUDeviceManager::GetInstance().default_stream();
  27. MS_EXCEPTION_IF_NULL(stream);
  28. auto ret = GPUDeviceManager::GetInstance().SyncStream(stream);
  29. if (!ret) {
  30. MS_LOG(ERROR) << "SyncStream failed";
  31. return ret;
  32. }
  33. if (size != size_) {
  34. MS_LOG(WARNING) << "SyncDeviceToHost ignored, host size: " << size << ", device size " << size_;
  35. return true;
  36. }
  37. return GPUDeviceManager::GetInstance().CopyDeviceMemToHost(host_ptr, ptr_, size_);
  38. }
  39. bool GPUDeviceAddress::SyncHostToDevice(const std::vector<int> &, size_t, TypeId, const void *host_ptr) const {
  40. MS_EXCEPTION_IF_NULL(host_ptr);
  41. auto &stream = GPUDeviceManager::GetInstance().default_stream();
  42. MS_EXCEPTION_IF_NULL(stream);
  43. if (!GPUDeviceManager::GetInstance().CopyHostMemToDeviceAsync(ptr_, host_ptr, size_, stream)) {
  44. MS_LOG(ERROR) << "CopyHostMemToDeviceAsync failed";
  45. return false;
  46. }
  47. return GPUDeviceManager::GetInstance().SyncStream(stream);
  48. }
  49. GPUDeviceAddress::~GPUDeviceAddress() {
  50. if (ptr_ == nullptr) {
  51. return;
  52. }
  53. if (from_mem_pool_) {
  54. GPUMemoryAllocator::GetInstance().FreeTensorMem(ptr_);
  55. ptr_ = nullptr;
  56. }
  57. }
  58. } // namespace gpu
  59. } // namespace device
  60. } // namespace mindspore