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_context.h 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Copyright 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_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_H_
  18. #include <string>
  19. #include <vector>
  20. #include <memory>
  21. #include "runtime/device/device_address.h"
  22. #include "backend/session/kernel_graph.h"
  23. #include "backend/session/anf_runtime_algorithm.h"
  24. namespace mindspore {
  25. namespace device {
  26. using mindspore::kernel::AddressPtr;
  27. using mindspore::kernel::KernelMod;
  28. struct DeviceContextKey {
  29. // device type name, such as 'GPU' 'Ascend' 'CPU'.
  30. std::string device_name_;
  31. uint32_t device_id_{0};
  32. // Use the result of ToString() as key to look up DeviceContext
  33. // in cache map which maintains created DeviceContext objects.
  34. std::string ToString() const { return device_name_ + "_" + std::to_string(device_id_); }
  35. };
  36. // DeviceContext is unified interface of interaction with device.
  37. class DeviceContext {
  38. public:
  39. explicit DeviceContext(const DeviceContextKey &device_context_key) : device_context_key_(device_context_key) {}
  40. virtual ~DeviceContext() = default;
  41. // Initialize the device context and return success or not.
  42. virtual bool Initialize() = 0;
  43. // Destroy device context and release device resource.
  44. virtual void Destroy() {}
  45. // Relevant function to allocate and free device memory.
  46. virtual bool AllocateMemory(DeviceAddress *const &address, size_t size) const = 0;
  47. virtual void FreeMemory(DeviceAddress *const &address) const = 0;
  48. // Allocate continuous device memory end to end into 'addr_list'.
  49. // Communication operators may need continuous memory for input and output
  50. // to optimize the communication performance.
  51. virtual bool AllocateContinuousMemory(const std::vector<DeviceAddressPtr> &addr_list, size_t total_size,
  52. const std::vector<size_t> &size_list) const {
  53. return true;
  54. }
  55. // Create concrete device address according different device type.
  56. virtual DeviceAddressPtr CreateDeviceAddress(void *device_ptr, size_t device_size, const string &format,
  57. TypeId type_id) const = 0;
  58. // Get device address type according different device type, such GPU, Ascend.
  59. virtual DeviceAddressType GetDeviceAddressType() const = 0;
  60. // Optimize the kernel graph for graph mode.
  61. virtual void OptimizeGraph(const KernelGraphPtr &graph) const {}
  62. // Optimize the single operator graph for PyNative mode.
  63. virtual void OptimizeSingleOpGraph(const KernelGraphPtr &graph) const {}
  64. // Select the matching backend kernels according to the data type and format of input and output for all
  65. // execution operators, and set final device data type and format information for backend kernels, device
  66. // data type and format which replace original data type and format will use for executing kernels.
  67. virtual void SetOperatorInfo(const std::vector<CNodePtr> &nodes) const = 0;
  68. // Generate 'KernelMod' for all kernels and set 'KernelMod' into kernel,
  69. // 'KernelMod' is real executive object of kernel.
  70. virtual void CreateKernel(const std::vector<CNodePtr> &nodes) const = 0;
  71. // Launch a kernel via 'KernelMod' of the kernel.
  72. virtual bool LaunchKernel(KernelMod *kernel_mod, const std::vector<AddressPtr> &inputs,
  73. const std::vector<AddressPtr> &workspace, const std::vector<AddressPtr> &outputs) const = 0;
  74. // Synchronize stream, device such as GPU and Ascend need stream to launch kernel asynchronously,
  75. // using 'SyncStream' to block thread and wait for completing all tasks in stream.
  76. // Devices that do not need stream could ignore the implementation of this function.
  77. virtual bool SyncStream(size_t stream_id = 0) const { return true; }
  78. // Get device_context_key_ to obtain device name and device id.
  79. const DeviceContextKey &device_context_key() const { return device_context_key_; }
  80. protected:
  81. DeviceContextKey device_context_key_;
  82. };
  83. using DeviceContextPtr = std::shared_ptr<DeviceContext>;
  84. } // namespace device
  85. } // namespace mindspore
  86. #endif // MINDSPORE_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_H_