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

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // The related interface of reference count operation.
  68. void set_original_ref_count(size_t original_ref_count) { original_ref_count_ = original_ref_count; }
  69. size_t original_ref_count() const { return original_ref_count_; }
  70. void set_ref_count(size_t ref_count) { ref_count_ = ref_count; }
  71. size_t ref_count() const { return ref_count_; }
  72. void IncreaseOriginalRefCount() { original_ref_count_++; }
  73. void DecreaseRefCount() { ref_count_--; }
  74. void ResetRefCount() { ref_count_ = original_ref_count_; }
  75. virtual bool DumpMemToFile(const std::string &filepath, const std::string &host_fmt, const ShapeVector &host_shape,
  76. TypeId host_type, bool trans_flag) const {
  77. return true;
  78. }
  79. #ifdef ENABLE_DEBUGGER
  80. virtual bool LoadMemToHost(const std::string &tensor_name, int execution_order, const std::string &host_fmt,
  81. const ShapeVector &host_shape, TypeId host_type, size_t slot, bool keep_prev) const {
  82. return true;
  83. }
  84. #endif
  85. protected:
  86. const void *ptr() const { return ptr_; }
  87. size_t size() const { return size_; }
  88. void set_ptr(void *ptr) { ptr_ = ptr; }
  89. void *ptr_{nullptr};
  90. size_t size_{0};
  91. size_t original_ref_count_{1};
  92. // It will be decreased in the running, and reset by original_ref_count_ when it is zero.
  93. size_t ref_count_{1};
  94. string format_{"DefaultFormat"};
  95. TypeId type_id_{kNumberTypeFloat16};
  96. bool from_mem_pool_{false};
  97. uint8_t *communication_ptr_{nullptr};
  98. ShapeVector host_shape_{};
  99. friend class KernelRuntime;
  100. friend class MemoryManager;
  101. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  102. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  103. friend class mindspore::device::cpu::CPUMemoryManager;
  104. friend class mindspore::device::cpu::CPUKernelRuntime;
  105. friend class mindspore::device::cpu::CPUDeviceContext;
  106. friend class mindspore::device::gpu::GPUKernelRuntime;
  107. friend class mindspore::device::gpu::GPUMemoryManager;
  108. friend class mindspore::device::gpu::GPUDeviceContext;
  109. friend class mindspore::device::ascend::AscendKernelRuntime;
  110. friend class mindspore::device::ascend::AscendMemoryManager;
  111. friend class mindspore::device::ascend::DataDumper;
  112. friend class mindspore::device::Bucket;
  113. };
  114. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  115. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  116. } // namespace device
  117. } // namespace mindspore
  118. #endif // MINDSPORE_DEVICE_TENSOR_H