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

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright 2019-2021 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 <map>
  22. #include <utility>
  23. #include "ir/dtype.h"
  24. #include "ir/device_sync.h"
  25. #include "utils/shape_utils.h"
  26. namespace mindspore {
  27. namespace device {
  28. class Bucket;
  29. namespace cpu {
  30. class CPUSimpleMemPlan;
  31. class CPUMemoryManager;
  32. class CPUKernelRuntime;
  33. class CPUDeviceContext;
  34. } // namespace cpu
  35. namespace ascend {
  36. class AscendKernelRuntime;
  37. class AscendMemoryManager;
  38. #ifndef ENABLE_SECURITY
  39. class DataDumper;
  40. #endif
  41. namespace tasksink {
  42. class TaskGenerator;
  43. } // namespace tasksink
  44. } // namespace ascend
  45. namespace gpu {
  46. class GPUKernelRuntime;
  47. class GPUMemoryManager;
  48. class GPUDeviceContext;
  49. } // namespace gpu
  50. } // namespace device
  51. } // namespace mindspore
  52. namespace mindspore {
  53. namespace device {
  54. using KernelWithIndex = std::pair<AnfNodePtr, size_t>;
  55. enum class DeviceAddressStatus { kInDevice, kInHost, kInDeviceToHost, kInHostToDevice };
  56. enum class DeviceAddressType { kUnknown, kAscend, kCPU, kGPU };
  57. static const std::map<DeviceAddressType, std::string> kDeviceTypeToName = {{DeviceAddressType::kUnknown, "Unknown"},
  58. {DeviceAddressType::kAscend, "Ascend"},
  59. {DeviceAddressType::kCPU, "CPU"},
  60. {DeviceAddressType::kGPU, "GPU"}};
  61. class DeviceAddress : public mindspore::DeviceSync {
  62. public:
  63. explicit DeviceAddress(void *ptr, size_t size) : ptr_(ptr), size_(size) {}
  64. explicit DeviceAddress(void *ptr, size_t size, const string &format, TypeId type_id)
  65. : ptr_(ptr), size_(size), format_(format), type_id_(type_id) {}
  66. explicit DeviceAddress(void *ptr, size_t size, const std::string &format, TypeId type_id,
  67. const KernelWithIndex &node_index)
  68. : ptr_(ptr), size_(size), format_(format), type_id_(type_id), node_index_(node_index) {}
  69. virtual ~DeviceAddress() { ptr_ = nullptr; }
  70. const void *GetPtr() const { return ptr_; }
  71. size_t GetSize() const { return size_; }
  72. void SetSize(size_t size) { size_ = size; }
  73. std::string format() const { return format_; }
  74. TypeId type_id() const { return type_id_; }
  75. bool from_mem_pool() const { return from_mem_pool_; }
  76. void set_host_shape(const ShapeVector &shape) { host_shape_ = shape; }
  77. virtual void set_status(DeviceAddressStatus status) {}
  78. virtual DeviceAddressStatus status() const { return DeviceAddressStatus::kInDevice; }
  79. virtual DeviceAddressType DeviceType() const { return DeviceAddressType::kUnknown; }
  80. void *GetMutablePtr() const override { return ptr_; }
  81. virtual void SetNodeIndex(const AnfNodePtr &node, size_t out_index) { node_index_ = {node, out_index}; }
  82. virtual bool DumpMemToFile(const std::string &filepath, const std::string &host_fmt, const ShapeVector &host_shape,
  83. TypeId host_type, bool trans_flag) const {
  84. return true;
  85. }
  86. #ifdef ENABLE_DEBUGGER
  87. virtual bool LoadMemToHost(const std::string &tensor_name, int execution_order, const std::string &host_fmt,
  88. const ShapeVector &host_shape, TypeId host_type, size_t slot, bool keep_prev) const {
  89. return true;
  90. }
  91. #endif
  92. protected:
  93. const void *ptr() const { return ptr_; }
  94. size_t size() const { return size_; }
  95. void set_ptr(void *ptr) { ptr_ = ptr; }
  96. KernelWithIndex GetNodeIndex() const {
  97. return node_index_.first.expired() ? KernelWithIndex{nullptr, node_index_.second}
  98. : KernelWithIndex{node_index_.first.lock(), node_index_.second};
  99. }
  100. mutable void *ptr_{nullptr};
  101. size_t size_{0};
  102. string format_{"DefaultFormat"};
  103. TypeId type_id_{kNumberTypeFloat16};
  104. mutable bool from_mem_pool_{false};
  105. uint8_t *communication_ptr_{nullptr};
  106. ShapeVector host_shape_{};
  107. // {node, out_index}
  108. std::pair<AnfNodeWeakPtr, size_t> node_index_{AnfNodePtr(nullptr), 0};
  109. friend class KernelRuntime;
  110. friend class MemoryManager;
  111. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  112. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  113. friend class mindspore::device::cpu::CPUMemoryManager;
  114. friend class mindspore::device::cpu::CPUKernelRuntime;
  115. friend class mindspore::device::cpu::CPUDeviceContext;
  116. friend class mindspore::device::gpu::GPUKernelRuntime;
  117. friend class mindspore::device::gpu::GPUMemoryManager;
  118. friend class mindspore::device::gpu::GPUDeviceContext;
  119. friend class mindspore::device::ascend::AscendKernelRuntime;
  120. friend class mindspore::device::ascend::AscendMemoryManager;
  121. #ifndef ENABLE_SECURITY
  122. friend class mindspore::device::ascend::DataDumper;
  123. #endif
  124. friend class mindspore::device::Bucket;
  125. };
  126. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  127. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  128. } // namespace device
  129. } // namespace mindspore
  130. #endif // MINDSPORE_DEVICE_TENSOR_H