Skip to content

Commit

Permalink
Refactor the CUDA error check (#49)
Browse files Browse the repository at this point in the history
* Add cuda_utils.h

* Factor out repeating code

* Formating
  • Loading branch information
Raimondas Galvelis authored Jan 25, 2022
1 parent 896335f commit b145a26
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
9 changes: 1 addition & 8 deletions src/pytorch/CFConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,10 @@
#include <torch/serialize/archive.h>
#include "CpuCFConv.h"
#include "CFConvNeighbors.h"

#include "cuda_utils.h"
#ifdef ENABLE_CUDA
#include <stdexcept>
#include <cuda_runtime.h>
// #include <c10/cuda/CUDAStream.h>
#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 {
Expand Down
12 changes: 1 addition & 11 deletions src/pytorch/CFConvNeighbors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,10 @@
*/
#include "CFConvNeighbors.h"
#include "CpuCFConv.h"

#include "cuda_utils.h"
#ifdef ENABLE_CUDA
#include <stdexcept>
#include <cuda_runtime.h>
// #include <c10/cuda/CUDAStream.h>
#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 {
Expand Down
7 changes: 1 addition & 6 deletions src/pytorch/SymmetryFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@
#include <torch/script.h>
#include <torch/serialize/archive.h>
#include "CpuANISymmetryFunctions.h"
#include "cuda_utils.h"
#ifdef ENABLE_CUDA
#include <stdexcept>
#include <c10/cuda/CUDAStream.h>
#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 {
Expand Down
39 changes: 39 additions & 0 deletions src/pytorch/cuda_utils.h
Original file line number Diff line number Diff line change
@@ -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 <stdexcept>
#include <cuda_runtime_api.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

#endif

0 comments on commit b145a26

Please sign in to comment.