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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "utils/context/ms_context.h"
  21. #include "device/gpu/gpu_memory_allocator.h"
  22. namespace mindspore {
  23. namespace device {
  24. namespace gpu {
  25. bool GPUDeviceAddress::SyncDeviceToHost(const std::vector<int> &, size_t size, TypeId, void *host_ptr) const {
  26. MS_EXCEPTION_IF_NULL(host_ptr);
  27. auto &stream = GPUDeviceManager::GetInstance().default_stream();
  28. MS_EXCEPTION_IF_NULL(stream);
  29. auto ret = GPUDeviceManager::GetInstance().SyncStream(stream);
  30. if (!ret) {
  31. MS_LOG(ERROR) << "SyncStream failed";
  32. return ret;
  33. }
  34. if (size != size_) {
  35. MS_LOG(WARNING) << "SyncDeviceToHost ignored, host size: " << size << ", device size " << size_;
  36. return true;
  37. }
  38. return GPUDeviceManager::GetInstance().CopyDeviceMemToHost(host_ptr, ptr_, size_);
  39. }
  40. bool GPUDeviceAddress::SyncHostToDevice(const std::vector<int> &, size_t, TypeId, const void *host_ptr) const {
  41. MS_EXCEPTION_IF_NULL(host_ptr);
  42. auto &stream = GPUDeviceManager::GetInstance().default_stream();
  43. MS_EXCEPTION_IF_NULL(stream);
  44. if (!GPUDeviceManager::GetInstance().CopyHostMemToDeviceAsync(ptr_, host_ptr, size_, stream)) {
  45. MS_LOG(ERROR) << "CopyHostMemToDeviceAsync failed";
  46. return false;
  47. }
  48. return GPUDeviceManager::GetInstance().SyncStream(stream);
  49. }
  50. GPUDeviceAddress::~GPUDeviceAddress() {
  51. if (ptr_ == nullptr) {
  52. return;
  53. }
  54. auto ms_context = MsContext::GetInstance();
  55. MS_EXCEPTION_IF_NULL(ms_context);
  56. if (from_mem_pool_) {
  57. GPUMemoryAllocator::GetInstance().FreeTensorMem(ptr_);
  58. ptr_ = nullptr;
  59. }
  60. }
  61. } // namespace gpu
  62. } // namespace device
  63. } // namespace mindspore