Skip to content

Commit b145a26

Browse files
author
Raimondas Galvelis
authored
Refactor the CUDA error check (#49)
* Add cuda_utils.h * Factor out repeating code * Formating
1 parent 896335f commit b145a26

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

src/pytorch/CFConv.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,10 @@
2424
#include <torch/serialize/archive.h>
2525
#include "CpuCFConv.h"
2626
#include "CFConvNeighbors.h"
27-
27+
#include "cuda_utils.h"
2828
#ifdef ENABLE_CUDA
29-
#include <stdexcept>
30-
#include <cuda_runtime.h>
3129
// #include <c10/cuda/CUDAStream.h>
3230
#include "CudaCFConv.h"
33-
34-
#define CHECK_CUDA_RESULT(result) \
35-
if (result != cudaSuccess) { \
36-
throw std::runtime_error(std::string("Encountered error ")+cudaGetErrorName(result)+" at "+__FILE__+":"+std::to_string(__LINE__));\
37-
}
3831
#endif
3932

4033
namespace NNPOps {

src/pytorch/CFConvNeighbors.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,10 @@
2222
*/
2323
#include "CFConvNeighbors.h"
2424
#include "CpuCFConv.h"
25-
25+
#include "cuda_utils.h"
2626
#ifdef ENABLE_CUDA
27-
#include <stdexcept>
28-
#include <cuda_runtime.h>
2927
// #include <c10/cuda/CUDAStream.h>
3028
#include "CudaCFConv.h"
31-
32-
#define int2 int[2]
33-
#define float3 float[3]
34-
35-
#define CHECK_CUDA_RESULT(result) \
36-
if (result != cudaSuccess) { \
37-
throw std::runtime_error(std::string("Encountered error ")+cudaGetErrorName(result)+" at "+__FILE__+":"+std::to_string(__LINE__));\
38-
}
3929
#endif
4030

4131
namespace NNPOps {

src/pytorch/SymmetryFunctions.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,10 @@
2424
#include <torch/script.h>
2525
#include <torch/serialize/archive.h>
2626
#include "CpuANISymmetryFunctions.h"
27+
#include "cuda_utils.h"
2728
#ifdef ENABLE_CUDA
28-
#include <stdexcept>
2929
#include <c10/cuda/CUDAStream.h>
3030
#include "CudaANISymmetryFunctions.h"
31-
32-
#define CHECK_CUDA_RESULT(result) \
33-
if (result != cudaSuccess) { \
34-
throw std::runtime_error(std::string("Encountered error ")+cudaGetErrorName(result)+" at "+__FILE__+":"+std::to_string(__LINE__));\
35-
}
3631
#endif
3732

3833
namespace NNPOps {

src/pytorch/cuda_utils.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2020-2022 Acellera
3+
* Authors: Raimondas Galvelis
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
#ifndef NNPOPS_CUDA_UTILS
24+
#define NNPOPS_CUDA_UTILS
25+
26+
#ifdef ENABLE_CUDA
27+
28+
#include <stdexcept>
29+
#include <cuda_runtime_api.h>
30+
31+
#define CHECK_CUDA_RESULT(result) \
32+
if (result != cudaSuccess) { \
33+
throw std::runtime_error(std::string("Encountered error ") + \
34+
cudaGetErrorName(result) + " at " + __FILE__ + ":" + std::to_string(__LINE__)); \
35+
}
36+
37+
#endif
38+
39+
#endif

0 commit comments

Comments
 (0)