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 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if (size != size_) {
  28. MS_LOG(WARNING) << "SyncDeviceToHost ignored, host size: " << size << ", device size " << size_;
  29. return true;
  30. }
  31. return GPUDeviceManager::GetInstance().CopyDeviceMemToHost(host_ptr, ptr_, size_);
  32. }
  33. bool GPUDeviceAddress::SyncHostToDevice(const std::vector<int> &, size_t, TypeId, const void *host_ptr) const {
  34. MS_EXCEPTION_IF_NULL(host_ptr);
  35. return GPUDeviceManager::GetInstance().CopyHostMemToDevice(ptr_, host_ptr, size_);
  36. }
  37. GPUDeviceAddress::~GPUDeviceAddress() {
  38. if (ptr_ == nullptr) {
  39. return;
  40. }
  41. auto ms_context = MsContext::GetInstance();
  42. MS_EXCEPTION_IF_NULL(ms_context);
  43. if (from_mem_pool_) {
  44. GPUMemoryAllocator::GetInstance().FreeTensorMem(ptr_);
  45. ptr_ = nullptr;
  46. }
  47. }
  48. } // namespace gpu
  49. } // namespace device
  50. } // namespace mindspore