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
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. 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. class DataDumper;
  34. namespace tasksink {
  35. class TaskGenerator;
  36. } // namespace tasksink
  37. } // namespace ascend
  38. namespace gpu {
  39. class GPUKernelRuntime;
  40. class GPUMemoryManager;
  41. } // namespace gpu
  42. } // namespace device
  43. } // namespace mindspore
  44. namespace mindspore {
  45. namespace device {
  46. enum class DeviceAddressStatus { kInDevice, kInHost, kInDeviceToHost, kInHostToDevice };
  47. enum class DeviceAddressType { kUnknown, kAscend, kCPU, kGPU };
  48. class DeviceAddress : public mindspore::DeviceSync {
  49. public:
  50. explicit DeviceAddress(void *ptr, size_t size) : ptr_(ptr), size_(size) {}
  51. explicit DeviceAddress(void *ptr, size_t size, const string &format, TypeId type_id)
  52. : ptr_(ptr), size_(size), format_(format), type_id_(type_id) {}
  53. virtual ~DeviceAddress() { ptr_ = nullptr; }
  54. const void *GetPtr() const { return ptr_; }
  55. size_t GetSize() const { return size_; }
  56. std::string format() const { return format_; }
  57. TypeId type_id() const { return type_id_; }
  58. void set_host_shape(const std::vector<int> &shape) { host_shape_ = shape; }
  59. virtual void set_status(DeviceAddressStatus status) {}
  60. virtual DeviceAddressStatus status() const { return DeviceAddressStatus::kInDevice; }
  61. virtual DeviceAddressType DeviceType() const { return DeviceAddressType::kUnknown; }
  62. void *GetMutablePtr() const override { return ptr_; }
  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. uint8_t *communication_ptr_{nullptr};
  74. std::vector<int> host_shape_{};
  75. friend class KernelRuntime;
  76. friend class MemoryManager;
  77. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  78. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  79. friend class mindspore::device::cpu::CPUResourceManager;
  80. friend class mindspore::device::cpu::CPUKernelRuntime;
  81. friend class mindspore::device::gpu::GPUKernelRuntime;
  82. friend class mindspore::device::gpu::GPUMemoryManager;
  83. friend class mindspore::device::ascend::AscendKernelRuntime;
  84. friend class mindspore::device::ascend::AscendMemoryManager;
  85. friend class mindspore::device::ascend::DataDumper;
  86. };
  87. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  88. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  89. } // namespace device
  90. } // namespace mindspore
  91. #endif // MINDSPORE_DEVICE_TENSOR_H