From b145a268b38d72aaf114abc31a7d517865cf85da Mon Sep 17 00:00:00 2001 From: Raimondas Galvelis Date: Tue, 25 Jan 2022 17:09:20 +0100 Subject: [PATCH] Refactor the CUDA error check (#49) * Add cuda_utils.h * Factor out repeating code * Formating --- src/pytorch/CFConv.cpp | 9 +------ src/pytorch/CFConvNeighbors.cpp | 12 +--------- src/pytorch/SymmetryFunctions.cpp | 7 +----- src/pytorch/cuda_utils.h | 39 +++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 src/pytorch/cuda_utils.h diff --git a/src/pytorch/CFConv.cpp b/src/pytorch/CFConv.cpp index 68dd6bf..a43b2c8 100644 --- a/src/pytorch/CFConv.cpp +++ b/src/pytorch/CFConv.cpp @@ -24,17 +24,10 @@ #include #include "CpuCFConv.h" #include "CFConvNeighbors.h" - +#include "cuda_utils.h" #ifdef ENABLE_CUDA -#include -#include // #include #include "CudaCFConv.h" - -#define CHECK_CUDA_RESULT(result) \ - if (result != cudaSuccess) { \ - throw std::runtime_error(std::string("Encountered error ")+cudaGetErrorName(result)+" at "+__FILE__+":"+std::to_string(__LINE__));\ - } #endif namespace NNPOps { diff --git a/src/pytorch/CFConvNeighbors.cpp b/src/pytorch/CFConvNeighbors.cpp index a786532..738cd60 100644 --- a/src/pytorch/CFConvNeighbors.cpp +++ b/src/pytorch/CFConvNeighbors.cpp @@ -22,20 +22,10 @@ */ #include "CFConvNeighbors.h" #include "CpuCFConv.h" - +#include "cuda_utils.h" #ifdef ENABLE_CUDA -#include -#include // #include #include "CudaCFConv.h" - -#define int2 int[2] -#define float3 float[3] - -#define CHECK_CUDA_RESULT(result) \ - if (result != cudaSuccess) { \ - throw std::runtime_error(std::string("Encountered error ")+cudaGetErrorName(result)+" at "+__FILE__+":"+std::to_string(__LINE__));\ - } #endif namespace NNPOps { diff --git a/src/pytorch/SymmetryFunctions.cpp b/src/pytorch/SymmetryFunctions.cpp index 495ffc9..3961e59 100644 --- a/src/pytorch/SymmetryFunctions.cpp +++ b/src/pytorch/SymmetryFunctions.cpp @@ -24,15 +24,10 @@ #include #include #include "CpuANISymmetryFunctions.h" +#include "cuda_utils.h" #ifdef ENABLE_CUDA -#include #include #include "CudaANISymmetryFunctions.h" - -#define CHECK_CUDA_RESULT(result) \ - if (result != cudaSuccess) { \ - throw std::runtime_error(std::string("Encountered error ")+cudaGetErrorName(result)+" at "+__FILE__+":"+std::to_string(__LINE__));\ - } #endif namespace NNPOps { diff --git a/src/pytorch/cuda_utils.h b/src/pytorch/cuda_utils.h new file mode 100644 index 0000000..472885a --- /dev/null +++ b/src/pytorch/cuda_utils.h @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2020-2022 Acellera + * Authors: Raimondas Galvelis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef NNPOPS_CUDA_UTILS +#define NNPOPS_CUDA_UTILS + +#ifdef ENABLE_CUDA + +#include +#include + +#define CHECK_CUDA_RESULT(result) \ + if (result != cudaSuccess) { \ + throw std::runtime_error(std::string("Encountered error ") + \ + cudaGetErrorName(result) + " at " + __FILE__ + ":" + std::to_string(__LINE__)); \ + } + +#endif + +#endif \ No newline at end of file