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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. using std::string;
  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. namespace tasksink {
  34. class TaskGenerator;
  35. } // namespace tasksink
  36. } // namespace ascend
  37. namespace gpu {
  38. class GPUKernelRuntime;
  39. class GPUMemoryManager;
  40. } // namespace gpu
  41. } // namespace device
  42. } // namespace mindspore
  43. namespace mindspore {
  44. namespace device {
  45. class DeviceAddress {
  46. public:
  47. explicit DeviceAddress(void *ptr, size_t size) : ptr_(ptr), size_(size) {}
  48. explicit DeviceAddress(void *ptr, size_t size, const string &format, TypeId type_id)
  49. : ptr_(ptr), size_(size), format_(format), type_id_(type_id) {}
  50. virtual ~DeviceAddress() { ptr_ = nullptr; }
  51. virtual bool SyncDeviceToHost(const std::vector<int> &shape, size_t size, TypeId type, void *host_ptr) const = 0;
  52. virtual bool SyncHostToDevice(const std::vector<int> &shape, size_t size, TypeId type,
  53. const void *host_ptr) const = 0;
  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. protected:
  59. const void *ptr() const { return ptr_; }
  60. size_t size() const { return size_; }
  61. void set_ptr(void *ptr) { ptr_ = ptr; }
  62. void *ptr_{nullptr};
  63. size_t size_{0};
  64. size_t ref_count_{0};
  65. string format_{"DefaultFormat"};
  66. TypeId type_id_{kNumberTypeFloat16};
  67. bool from_mem_pool_{false};
  68. friend class KernelRuntime;
  69. friend class MemoryManager;
  70. friend class mindspore::device::ascend::tasksink::TaskGenerator;
  71. friend class mindspore::device::cpu::CPUSimpleMemPlan;
  72. friend class mindspore::device::cpu::CPUResourceManager;
  73. friend class mindspore::device::cpu::CPUKernelRuntime;
  74. friend class mindspore::device::gpu::GPUKernelRuntime;
  75. friend class mindspore::device::gpu::GPUMemoryManager;
  76. friend class mindspore::device::ascend::AscendKernelRuntime;
  77. friend class mindspore::device::ascend::AscendMemoryManager;
  78. };
  79. using DeviceAddressPtr = std::shared_ptr<DeviceAddress>;
  80. using DeviceAddressPtrList = std::vector<DeviceAddressPtr>;
  81. } // namespace device
  82. } // namespace mindspore
  83. #endif // MINDSPORE_DEVICE_TENSOR_H