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 bool CopyHostMemToDevice(const DeviceMemPtr &dst, const void *src, size_t size); 40 static bool CopyDeviceMemToHost(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size); 41 42 static bool CopyHostMemToDeviceAsync(const DeviceMemPtr &dst, const void *src, size_t size, 43 CudaDeviceStream stream = 0); 44 static bool CopyDeviceMemToHostAsync(const HostMemPtr &dst, const DeviceMemPtr &src, size_t size, 45 CudaDeviceStream stream = 0); 46 static bool CopyDeviceMemToDeviceAsync(const DeviceMemPtr &dst, const DeviceMemPtr &src, size_t size, 47 CudaDeviceStream stream = 0); 48 49 static size_t total_mem_size(); 50 static size_t free_mem_size(); 51 52 // Encapsulate the cuda APIs associated with device resource 53 // such as Stream and Event. 54 static bool CreateStream(CudaDeviceStream *stream); 55 static bool DestroyStream(const CudaDeviceStream &stream); 56 static bool SyncStream(const CudaDeviceStream &stream); 57 58 static bool CreateEvent(CudaDeviceEvent *event, unsigned int flag = cudaEventDefault); 59 static bool DestroyEvent(const CudaDeviceEvent &event); 60 static bool RecordEvent(CudaDeviceEvent event, CudaDeviceStream stream = 0); 61 static bool SyncEvent(const CudaDeviceEvent &event); 62 static bool QueryEvent(const CudaDeviceEvent &event); 63 static bool ElapsedTime(float *cost_time, const CudaDeviceEvent &start, const CudaDeviceEvent &end); 64 65 // Encapsulate the cuda APIs associated with device management. 66 static int device_count(); 67 static bool SetDevice(int index); 68 69 private: 70 CudaDriver() = delete; 71 ~CudaDriver() = delete; 72 CudaDriver(const CudaDriver &) = delete; 73 CudaDriver &operator=(const CudaDriver &) = delete; 74 75 static constexpr float mem_malloc_retry_rate_{0.99}; 76 static constexpr size_t mem_malloc_retry_conut_max_{10}; 77 static constexpr size_t mem_malloc_align_size_{4}; 78 }; 79 } // namespace gpu 80 } // namespace device 81 } // namespace mindspore 82 83 #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_GPU_CUDA_DRIVER_H_ 84