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.

device_address.h 3.5 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef MINDSPORE_DEVICE_TENSOR_H
  17. #define MINDSPORE_DEVICE_TENSOR_H
  18. #include <string>
  19. #include <vector>
  20. #include <memory>
  21. #include "ir/dtype.h"
  22. using std::string;
  23. namespace mindspore {
  24. namespace device {
  25. namespace cpu {
  26. class CPUSimpleMemPlan;
  27. class CPUResourceManager;
  28. class CPUKernelRuntime;
  29. } // namespace cpu
  30. namespace ascend {
  31. class AscendKernelRuntime;
  32. class AscendMemoryManager;
  33. namespace tasksink {
  34. class TaskGenerator;
  35. } // namespace tasksink
  36. } // namespace ascend
  37. namespace gpu {
  38. class GPUKernelRuntime;
  39. class GPUMemoryManager;
  40. } // namespace gpu
  41. } // namespace device
  42. } // namespace mindspore
  43. namespace mindspore {
  44. namespace device {
  45. enum class DeviceAddressStatus { kInDevice, kInHost, kInDeviceToHost, kInHostToDevice };
  46. enum class DeviceAddressType { kUnknown, kAscend, kCPU, kGPU };
  47. class DeviceAddress {
  48. public:
  49. explicit DeviceAddress(void *ptr, size_t size) : ptr_(ptr), size_(size) {}
  50. explicit DeviceAddress(void *ptr, size_t size, const string &format, TypeId type_id)
  51. : ptr_(ptr), size_(size), format_(format), type_id_(type_id) {}
  52. virtual ~DeviceAddress() { ptr_ = nullptr; }
  53. virtual bool SyncDeviceToHost(const std::vector<int> &shape, size_t size, TypeId type, void *host_ptr) const = 0;
  54. virtual bool SyncHostToDevice(const std::vector<int> &shape, size_t size, TypeId type,
  55. const void *host_ptr) const = 0;
  56. const void *GetPtr() const { return ptr_; }
  57. size_t GetSize() const { return size_; }
  58. std::string format() const { return format_; }
  59. TypeId type_id() const { return type_id_; }
  60. virtual void set_status(DeviceAddressStatus status) {}
  61. virtual DeviceAddressStatus status() const { return DeviceAddressStatus::kInDevice; }
  62. virtual DeviceAddressType DeviceType() const { return DeviceAddressType::kUnknown; }
  63. protected:
  64. const void *ptr() const { return ptr_; }
  65. size_t size() const { return size_; }
  66. void set_ptr(void *ptr) { ptr_ = ptr; }
  67. void *ptr_{nullptr};
  68. size_t size_{0};
  69. size_t ref_count_{0};
  70. string format_{"DefaultFormat"};
  71. TypeId type_id_{kNumberTypeFloat16};
  72. bool from_mem_pool_{false};
  73. friend class KernelRuntime;
  74. friend class MemoryManager;
  75. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  76. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  77. friend class mindspore::device::cpu::CPUResourceManager;
  78. friend class mindspore::device::cpu::CPUKernelRuntime;
  79. friend class mindspore::device::gpu::GPUKernelRuntime;
  80. friend class mindspore::device::gpu::GPUMemoryManager;
  81. friend class mindspore::device::ascend::AscendKernelRuntime;
  82. friend class mindspore::device::ascend::AscendMemoryManager;
  83. };
  84. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  85. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  86. } // namespace device
  87. } // namespace mindspore
  88. #endif // MINDSPORE_DEVICE_TENSOR_H