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 17 #ifndef MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_CUDA_IMPL_CUDA_HELPER_H_ 18 #define MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_CUDA_IMPL_CUDA_HELPER_H_ 19 20 #include <cuda_runtime.h> 21 #include <algorithm> 22 23 class CudaHelper { 24 public: 25 int GetThreadNum() const; 26 int GetThreadNum(const int block_size) const; 27 int GetBlocksNum(const int total_threads) const; 28 int GetBlocksNum(const int total_threads, const int block_size) const; 29 static CudaHelper &GetInstance(); 30 31 private: 32 CudaHelper(); 33 ~CudaHelper() = default; 34 CudaHelper(const CudaHelper &) = delete; 35 CudaHelper &operator=(const CudaHelper &) = delete; 36 37 int max_blocks_; 38 int threads_per_block_; 39 }; 40 41 #ifndef GET_BLOCKS 42 #define GET_BLOCKS(total_threads) CudaHelper::GetInstance().GetBlocksNum(total_threads) 43 #endif 44 #define GET_BLOCKS_CAL(total_threads, block_size) CudaHelper::GetInstance().GetBlocksNum(total_threads, block_size) 45 #ifndef GET_THREADS 46 #define GET_THREADS CudaHelper::GetInstance().GetThreadNum() 47 #endif 48 #define GET_THREADS_CAL(block_size) CudaHelper::GetInstance().GetThreadNum(block_size) 49 50 #define CUDA_CHECK(ret) \ 51 do { \ 52 cudaError_t cuda_ret = (ret); \ 53 if ((cuda_ret) != cudaSuccess) { \ 54 return -1; \ 55 } \ 56 } while (0) 57 58 #define CUDA_CHECK_VOID(ret) \ 59 do { \ 60 cudaError_t cuda_ret = (ret); \ 61 if ((cuda_ret) != cudaSuccess) { \ 62 return; \ 63 } \ 64 } while (0) 65 66 #endif // MINDSPORE_LITE_SRC_EXTENDRT_DELEGATE_TENSORRT_CUDA_IMPL_CUDA_HELPER_H_ 67