|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file GPUCommonHelpers.h |
| 13 | +/// \author David Rohr |
| 14 | + |
| 15 | +// GPUChkErr and GPUChkErrI will both check x for an error, using the loaded backend of GPUReconstruction (requiring GPUReconstruction.h to be included by the user). |
| 16 | +// In case of an error, it will print out the corresponding CUDA / HIP / OpenCL error code |
| 17 | +// GPUChkErr will download GPUReconstruction error values from GPU, print them, and terminate the application with an exception if an error occured. |
| 18 | +// GPUChkErrI will return 0 or 1, depending on whether an error has occurred. |
| 19 | +// These Macros must be called ona GPUReconstruction instance. |
| 20 | +// The GPUChkErrS and GPUChkErrSI are similar but static, without required GPUReconstruction instance. |
| 21 | +// Examples: |
| 22 | +// if (mRec->GPUChkErrI(cudaMalloc(...))) { exit(1); } |
| 23 | +// gpuRecObj.GPUChkErr(cudaMalloc(...)); |
| 24 | +// if (GPUChkErrSI(cudaMalloc(..))) { exit(1); } |
| 25 | + |
| 26 | +#ifndef GPUCOMMONHELPERS_H |
| 27 | +#define GPUCOMMONHELPERS_H |
| 28 | + |
| 29 | +// Please #include "GPUReconstruction.h" in your code, if you use these 2! |
| 30 | +#define GPUChkErr(x) GPUChkErrA(x, __FILE__, __LINE__, true) |
| 31 | +#define GPUChkErrI(x) GPUChkErrA(x, __FILE__, __LINE__, false) |
| 32 | +#define GPUChkErrS(x) o2::gpu::internal::GPUReconstructionChkErr(x, __FILE__, __LINE__, true) |
| 33 | +#define GPUChkErrSI(x) o2::gpu::internal::GPUReconstructionChkErr(x, __FILE__, __LINE__, false) |
| 34 | + |
| 35 | +#include "GPUCommonDef.h" |
| 36 | +#include <cstdint> |
| 37 | + |
| 38 | +namespace o2::gpu::internal |
| 39 | +{ |
| 40 | +#define GPUCOMMON_INTERNAL_CAT_A(a, b, c) a##b##c |
| 41 | +#define GPUCOMMON_INTERNAL_CAT(...) GPUCOMMON_INTERNAL_CAT_A(__VA_ARGS__) |
| 42 | +extern int32_t GPUCOMMON_INTERNAL_CAT(GPUReconstruction, GPUCA_GPUTYPE, ChkErr)(const int64_t error, const char* file, int32_t line); |
| 43 | +inline int32_t GPUReconstructionCPUChkErr(const int64_t error, const char* file, int32_t line) |
| 44 | +{ |
| 45 | + if (error) { |
| 46 | + GPUError("GPUCommon Error Code %d (%s:%d)", error, file, line); |
| 47 | + } |
| 48 | + return error != 0; |
| 49 | +} |
| 50 | +static inline int32_t GPUReconstructionChkErr(const int64_t error, const char* file, int32_t line, bool failOnError) |
| 51 | +{ |
| 52 | + int32_t retVal = error && GPUCOMMON_INTERNAL_CAT(GPUReconstruction, GPUCA_GPUTYPE, ChkErr)(error, file, line); |
| 53 | + if (retVal && failOnError) { |
| 54 | + throw std::runtime_error("GPU API Call Failure"); |
| 55 | + } |
| 56 | + return error; |
| 57 | +} |
| 58 | +#undef GPUCOMMON_INTERNAL_CAT_A |
| 59 | +#undef GPUCOMMON_INTERNAL_CAT |
| 60 | +} // namespace o2::gpu::internal |
| 61 | + |
| 62 | +#endif |
0 commit comments