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

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "ir/device_sync.h"
  23. #include "utils/shape_utils.h"
  24. namespace mindspore {
  25. namespace device {
  26. class Bucket;
  27. namespace cpu {
  28. class CPUSimpleMemPlan;
  29. class CPUMemoryManager;
  30. class CPUKernelRuntime;
  31. class CPUDeviceContext;
  32. } // namespace cpu
  33. namespace ascend {
  34. class AscendKernelRuntime;
  35. class AscendMemoryManager;
  36. class DataDumper;
  37. namespace tasksink {
  38. class TaskGenerator;
  39. } // namespace tasksink
  40. } // namespace ascend
  41. namespace gpu {
  42. class GPUKernelRuntime;
  43. class GPUMemoryManager;
  44. class GPUDeviceContext;
  45. } // namespace gpu
  46. } // namespace device
  47. } // namespace mindspore
  48. namespace mindspore {
  49. namespace device {
  50. enum class DeviceAddressStatus { kInDevice, kInHost, kInDeviceToHost, kInHostToDevice };
  51. enum class DeviceAddressType { kUnknown, kAscend, kCPU, kGPU };
  52. class DeviceAddress : public mindspore::DeviceSync {
  53. public:
  54. explicit DeviceAddress(void *ptr, size_t size) : ptr_(ptr), size_(size) {}
  55. explicit DeviceAddress(void *ptr, size_t size, const string &format, TypeId type_id)
  56. : ptr_(ptr), size_(size), format_(format), type_id_(type_id) {}
  57. virtual ~DeviceAddress() { ptr_ = nullptr; }
  58. const void *GetPtr() const { return ptr_; }
  59. size_t GetSize() const { return size_; }
  60. std::string format() const { return format_; }
  61. TypeId type_id() const { return type_id_; }
  62. void set_host_shape(const ShapeVector &shape) { host_shape_ = shape; }
  63. virtual void set_status(DeviceAddressStatus status) {}
  64. virtual DeviceAddressStatus status() const { return DeviceAddressStatus::kInDevice; }
  65. virtual DeviceAddressType DeviceType() const { return DeviceAddressType::kUnknown; }
  66. void *GetMutablePtr() const override { return ptr_; }
  67. void set_ref_count(size_t ref_count) { ref_count_ = ref_count; }
  68. void IncreaseRefCount() { ref_count_++; }
  69. void DecreaseRefCountUsed() { ref_count_dynamic_used_--; }
  70. void ResetRefCountUsed() { ref_count_dynamic_used_ = ref_count_; }
  71. size_t ref_count_dynamic_used() const { return ref_count_dynamic_used_; }
  72. virtual bool DumpMemToFile(bool dump_mode, const std::string &filepath, const std::string &host_fmt,
  73. const ShapeVector &host_shape, TypeId host_type) const {
  74. return true;
  75. }
  76. #ifdef ENABLE_DEBUGGER
  77. virtual bool LoadMemToHost(const std::string &tensor_name, int execution_order, const std::string &host_fmt,
  78. const ShapeVector &host_shape, TypeId host_type, size_t slot, bool keep_prev) const {
  79. return true;
  80. }
  81. #endif
  82. protected:
  83. const void *ptr() const { return ptr_; }
  84. size_t size() const { return size_; }
  85. void set_ptr(void *ptr) { ptr_ = ptr; }
  86. void *ptr_{nullptr};
  87. size_t size_{0};
  88. size_t ref_count_{1};
  89. // It will be decreased in the running, and reset by ref_count_ when it is zero.
  90. size_t ref_count_dynamic_used_{1};
  91. string format_{"DefaultFormat"};
  92. TypeId type_id_{kNumberTypeFloat16};
  93. bool from_mem_pool_{false};
  94. uint8_t *communication_ptr_{nullptr};
  95. ShapeVector host_shape_{};
  96. friend class KernelRuntime;
  97. friend class MemoryManager;
  98. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  99. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  100. friend class mindspore::device::cpu::CPUMemoryManager;
  101. friend class mindspore::device::cpu::CPUKernelRuntime;
  102. friend class mindspore::device::cpu::CPUDeviceContext;
  103. friend class mindspore::device::gpu::GPUKernelRuntime;
  104. friend class mindspore::device::gpu::GPUMemoryManager;
  105. friend class mindspore::device::gpu::GPUDeviceContext;
  106. friend class mindspore::device::ascend::AscendKernelRuntime;
  107. friend class mindspore::device::ascend::AscendMemoryManager;
  108. friend class mindspore::device::ascend::DataDumper;
  109. friend class mindspore::device::Bucket;
  110. };
  111. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  112. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  113. } // namespace device
  114. } // namespace mindspore
  115. #endif // MINDSPORE_DEVICE_TENSOR_H