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.

cuda_driver.h 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_CCSRC_DEVICE_GPU_CUDA_DRIVER_H_
  17. #define MINDSPORE_CCSRC_DEVICE_GPU_CUDA_DRIVER_H_
  18. #include <cuda_runtime_api.h>
  19. namespace mindspore {
  20. namespace device {
  21. namespace gpu {
  22. typedef void *DeviceStream;
  23. typedef void *DeviceEvent;
  24. typedef void *HostMemPtr;
  25. typedef void *DeviceMemPtr;
  26. class CudaDriver {
  27. public:
  28. // Encapsulate the cuda APIs associated with memory operations
  29. // such as malloc/free and memory copy from host to device and reverse.
  30. static size_t AllocDeviceMem(size_t size, DeviceMemPtr *addr);
  31. static bool FreeDeviceMem(const DeviceMemPtr &addr);
  32. static size_t AllocHostPinnedMem(size_t size, void **addr);
  33. static void FreeHostPinnedMem(void *addr);
  34. static bool CopyHostMemToDevice(const DeviceMemPtr &dst, const void *src, size_t size);
  35. static bool CopyDeviceMemToHost(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size);
  36. static bool CopyHostMemToDeviceAsync(const DeviceMemPtr &dst, const void *src, size_t size, DeviceStream stream = 0);
  37. static bool CopyDeviceMemToHostAsync(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size,
  38. DeviceStream stream = 0);
  39. static size_t total_mem_size();
  40. static size_t free_mem_size();
  41. // Encapsulate the cuda APIs associated with device resource
  42. // such as Stream and Event.
  43. static bool CreateStream(DeviceStream *stream);
  44. static bool DestroyStream(const DeviceStream &stream);
  45. static bool SyncStream(const DeviceStream &stream);
  46. static bool CreateEvent(DeviceEvent *event, unsigned int flag = cudaEventDefault);
  47. static bool DestroyEvent(const DeviceEvent &event);
  48. static bool RecordEvent(DeviceEvent event, DeviceStream stream = 0);
  49. static bool SyncEvent(const DeviceEvent &event);
  50. static bool QueryEvent(const DeviceEvent &event);
  51. // Encapsulate the cuda APIs associated with device management.
  52. static int device_count();
  53. static bool set_current_device(int index);
  54. private:
  55. CudaDriver() = delete;
  56. ~CudaDriver() = delete;
  57. CudaDriver(const CudaDriver &) = delete;
  58. CudaDriver &operator=(const CudaDriver &) = delete;
  59. static constexpr float mem_malloc_retry_rate_{0.99};
  60. static constexpr size_t mem_malloc_retry_conut_max_{10};
  61. static constexpr size_t mem_malloc_align_size_{4};
  62. };
  63. } // namespace gpu
  64. } // namespace device
  65. } // namespace mindspore
  66. #endif // MINDSPORE_CCSRC_DEVICE_GPU_CUDA_DRIVER_H_