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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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(const DeviceAddressPtr &address, size_t size) const = 0;
  47. virtual void FreeMemory(const DeviceAddressPtr &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 DeviceAddressPtrList &addr_list, size_t total_size,
  52. const std::vector<size_t> &size_list) const {
  53. return true;
  54. }
  55. // Optimize the kernel graph according to different devices.
  56. virtual void OptimizeGraph(const KernelGraphPtr &graph) const {}
  57. // Select the matching backend kernels according to the data type and format of input and output for all
  58. // execution operators, and set final device data type and format information for backend kernels, device
  59. // data type and format which replace original data type and format will use for executing kernels.
  60. virtual void SetOperatorInfo(const std::vector<CNodePtr> &nodes) const {}
  61. // Generate 'KernelMod' for all kernels and set 'KernelMod' into kernel,
  62. // 'KernelMod' is real executive object of kernel.
  63. virtual void CreateKernel(const std::vector<CNodePtr> &nodes) const {}
  64. // Launch a kernel via 'KernelMod' of the kernel.
  65. virtual bool LaunchKernel(KernelMod *kernel_mod, const std::vector<AddressPtr> &inputs,
  66. const std::vector<AddressPtr> &workspace, const std::vector<AddressPtr> &outputs) const = 0;
  67. // Synchronize stream, device such as GPU and Ascend need stream to launch kernel asynchronously,
  68. // using 'SyncStream' to block thread and wait for completing all tasks in stream.
  69. // Devices that do not need stream could ignore the implementation of this function.
  70. virtual bool SyncStream(size_t stream_id = 0) { return true; }
  71. protected:
  72. DeviceContextKey device_context_key_;
  73. };
  74. using DeviceContextPtr = std::shared_ptr<DeviceContext>;
  75. } // namespace device
  76. } // namespace mindspore
  77. #endif // MINDSPORE_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_H_