• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_CUDA_DRIVER_H_
18 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_CUDA_DRIVER_H_
19 
20 #include <cuda_runtime_api.h>
21 
22 namespace mindspore {
23 namespace device {
24 namespace gpu {
25 typedef void *CudaDeviceStream;
26 typedef void *CudaDeviceEvent;
27 typedef void *HostMemPtr;
28 typedef void *DeviceMemPtr;
29 
30 class CudaDriver {
31  public:
32   // Encapsulate the cuda APIs associated with memory operations
33   // such as malloc/free and memory copy from host to device and reverse.
34   static size_t AllocDeviceMem(size_t size, DeviceMemPtr *addr);
35   static bool FreeDeviceMem(const DeviceMemPtr &addr);
36   static size_t AllocHostPinnedMem(size_t size, void **addr);
37   static void FreeHostPinnedMem(void *addr);
38 
39   static void CudaHostRegister(void *addr, size_t alloc_size);
40 
41   static void CudaHostUnregister(void *addr);
42 
43   static bool CopyHostMemToDevice(const DeviceMemPtr &dst, const void *src, size_t size);
44   static bool CopyDeviceMemToHost(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size);
45   static bool CopyHostMemToHost(const DeviceMemPtr &dst, const void *src, size_t size);
46 
47   static bool CopyHostMemToDeviceAsync(const DeviceMemPtr &dst, const void *src, size_t size,
48                                        CudaDeviceStream stream = 0);
49   static bool CopyDeviceMemToHostAsync(const HostMemPtr &dst, const void *src, size_t size,
50                                        CudaDeviceStream stream = 0);
51   static bool CopyDeviceMemToDeviceAsync(const DeviceMemPtr &dst, const void *src, size_t size,
52                                          CudaDeviceStream stream = 0);
53 
54   static size_t total_mem_size();
55   static size_t free_mem_size();
56 
57   // Encapsulate the cuda APIs associated with device resource
58   // such as Stream and Event.
59   static bool CreateStream(CudaDeviceStream *stream);
60   static bool CreateStreamWithPriority(CudaDeviceStream *stream, int priority);
61   static bool DestroyStream(const CudaDeviceStream &stream);
62   static bool SyncStream(const CudaDeviceStream &stream);
63   static bool QueryStream(const CudaDeviceStream &stream);
64 
65   static bool ConstructEvent(CudaDeviceEvent *event, unsigned int flag = cudaEventDefault);
66   static bool DestroyEvent(const CudaDeviceEvent &event);
67   static bool RecordEvent(CudaDeviceEvent event, CudaDeviceStream stream = 0);
68   static bool SyncEvent(const CudaDeviceEvent &event);
69   static bool QueryEvent(const CudaDeviceEvent &event);
70   static bool ElapsedTime(float *cost_time, const CudaDeviceEvent &start, const CudaDeviceEvent &end);
71 
72   // Encapsulate the cuda APIs associated with device management.
73   static int device_count();
74   static bool SetDevice(int index);
75 
76  private:
77   CudaDriver() = delete;
78   ~CudaDriver() = delete;
79   CudaDriver(const CudaDriver &) = delete;
80   CudaDriver &operator=(const CudaDriver &) = delete;
81 
82   static constexpr float mem_malloc_retry_rate_{0.99};
83   static constexpr size_t mem_malloc_retry_conut_max_{10};
84   static constexpr size_t mem_malloc_align_size_{4};
85 };
86 }  // namespace gpu
87 }  // namespace device
88 }  // namespace mindspore
89 
90 #endif  // MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_CUDA_DRIVER_H_
91