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

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. namespace cpu {
  27. class CPUSimpleMemPlan;
  28. class CPUMemoryManager;
  29. class CPUKernelRuntime;
  30. } // namespace cpu
  31. namespace ascend {
  32. class AscendKernelRuntime;
  33. class AscendMemoryManager;
  34. class DataDumper;
  35. namespace tasksink {
  36. class TaskGenerator;
  37. } // namespace tasksink
  38. } // namespace ascend
  39. namespace gpu {
  40. class GPUKernelRuntime;
  41. class GPUMemoryManager;
  42. } // namespace gpu
  43. } // namespace device
  44. } // namespace mindspore
  45. namespace mindspore {
  46. namespace device {
  47. enum class DeviceAddressStatus { kInDevice, kInHost, kInDeviceToHost, kInHostToDevice };
  48. enum class DeviceAddressType { kUnknown, kAscend, kCPU, kGPU };
  49. class DeviceAddress : public mindspore::DeviceSync {
  50. public:
  51. explicit DeviceAddress(void *ptr, size_t size) : ptr_(ptr), size_(size) {}
  52. explicit DeviceAddress(void *ptr, size_t size, const string &format, TypeId type_id)
  53. : ptr_(ptr), size_(size), format_(format), type_id_(type_id) {}
  54. virtual ~DeviceAddress() { ptr_ = nullptr; }
  55. const void *GetPtr() const { return ptr_; }
  56. size_t GetSize() const { return size_; }
  57. std::string format() const { return format_; }
  58. TypeId type_id() const { return type_id_; }
  59. void set_host_shape(const ShapeVector &shape) { host_shape_ = shape; }
  60. virtual void set_status(DeviceAddressStatus status) {}
  61. virtual DeviceAddressStatus status() const { return DeviceAddressStatus::kInDevice; }
  62. virtual DeviceAddressType DeviceType() const { return DeviceAddressType::kUnknown; }
  63. void *GetMutablePtr() const override { return ptr_; }
  64. virtual bool DumpMemToFile(bool dump_mode, const std::string &filepath, const std::string &host_fmt,
  65. const ShapeVector &host_shape, TypeId host_type) const {
  66. return true;
  67. }
  68. #ifdef ENABLE_DEBUGGER
  69. virtual bool LoadMemToHost(const std::string &tensor_name, int execution_order, const std::string &host_fmt,
  70. const ShapeVector &host_shape, TypeId host_type, size_t slot, bool keep_prev) const {
  71. return true;
  72. }
  73. #endif
  74. protected:
  75. const void *ptr() const { return ptr_; }
  76. size_t size() const { return size_; }
  77. void set_ptr(void *ptr) { ptr_ = ptr; }
  78. void *ptr_{nullptr};
  79. size_t size_{0};
  80. size_t ref_count_{0};
  81. string format_{"DefaultFormat"};
  82. TypeId type_id_{kNumberTypeFloat16};
  83. bool from_mem_pool_{false};
  84. uint8_t *communication_ptr_{nullptr};
  85. ShapeVector host_shape_{};
  86. friend class KernelRuntime;
  87. friend class MemoryManager;
  88. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  89. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  90. friend class mindspore::device::cpu::CPUMemoryManager;
  91. friend class mindspore::device::cpu::CPUKernelRuntime;
  92. friend class mindspore::device::gpu::GPUKernelRuntime;
  93. friend class mindspore::device::gpu::GPUMemoryManager;
  94. friend class mindspore::device::ascend::AscendKernelRuntime;
  95. friend class mindspore::device::ascend::AscendMemoryManager;
  96. friend class mindspore::device::ascend::DataDumper;
  97. };
  98. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  99. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  100. } // namespace device
  101. } // namespace mindspore
  102. #endif // MINDSPORE_DEVICE_TENSOR_H