Skip to content

Commit 315cfa4

Browse files
committed
GPU: Provide static versions of GPUChkErr() macros
test2 GPU: Provide static versions of GPUChkErr() macros
1 parent 20f1352 commit 315cfa4

14 files changed

+127
-56
lines changed

GPU/Common/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ set(HDRS_INSTALL
1515
GPUCommonAlgorithm.h
1616
GPUCommonDef.h
1717
GPUCommonDefAPI.h
18-
GPUCommonChkErr.h
18+
GPUCommonHelpers.h
1919
GPUCommonDefSettings.h
2020
GPUCommonConstants.h
2121
GPUCommonLogger.h

GPU/Common/GPUCommonChkErr.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

GPU/Common/GPUCommonDef.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@
3535
#define GPUCA_GPUCODE // Compiled by GPU compiler
3636
#endif
3737

38-
#if defined(__CUDA_ARCH__) || defined(__OPENCL__) || defined(__HIP_DEVICE_COMPILE__)
39-
#define GPUCA_GPUCODE_DEVICE // Executed on device
38+
#if defined(GPUCA_GPUCODE)
39+
#if defined(__CUDA_ARCH__) || defined(__OPENCL__) || defined(__HIP_DEVICE_COMPILE__)
40+
#define GPUCA_GPUCODE_DEVICE // Executed on device
41+
#endif
42+
#if defined(__CUDACC__)
43+
#define GPUCA_GPUTYPE CUDA
44+
#elif defined(__HIPCC__)
45+
#define GPUCA_GPUTYPE HIP
46+
#elif defined(__OPENCL__) || defined(__OPENCL_HOST__)
47+
#define GPUCA_GPUTYPE OCL
48+
#endif
4049
#endif
4150
#endif
51+
#ifndef GPUCA_GPUTYPE
52+
#define GPUCA_GPUTYPE CPU
53+
#endif
4254

4355
#if defined(GPUCA_STANDALONE) || (defined(GPUCA_O2_LIB) && !defined(GPUCA_O2_INTERFACE)) || defined (GPUCA_GPUCODE)
44-
#define GPUCA_ALIGPUCODE
56+
#define GPUCA_ALIGPUCODE // Part of GPUTracking library but not of interface
4557
#endif
4658

4759
#if (defined(__CUDACC__) && defined(GPUCA_CUDA_NO_CONSTANT_MEMORY)) || (defined(__HIPCC__) && defined(GPUCA_HIP_NO_CONSTANT_MEMORY)) || (defined(__OPENCL__) && defined(GPUCA_OPENCL_NO_CONSTANT_MEMORY))
@@ -51,13 +63,13 @@
5163
#endif
5264

5365
#if !defined(GPUCA_GPUCODE) && !defined(GPUCA_STANDALONE) && defined(DEBUG_STREAMER)
54-
#define GPUCA_DEBUG_STREAMER_CHECK(...) __VA_ARGS__
66+
#define GPUCA_DEBUG_STREAMER_CHECK(...) __VA_ARGS__
5567
#else
56-
#define GPUCA_DEBUG_STREAMER_CHECK(...)
68+
#define GPUCA_DEBUG_STREAMER_CHECK(...)
5769
#endif
5870

5971
#ifndef GPUCA_RTC_SPECIAL_CODE
60-
#define GPUCA_RTC_SPECIAL_CODE(...)
72+
#define GPUCA_RTC_SPECIAL_CODE(...)
6173
#endif
6274

6375
// API Definitions for GPU Compilation

GPU/Common/GPUCommonHelpers.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

GPU/GPUTracking/Base/cuda/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ endif()
1818
message(STATUS "Building GPUTracking with CUDA support ${TMP_TARGET}")
1919

2020
set(SRCS GPUReconstructionCUDA.cu GPUReconstructionCUDAGenRTC.cxx GPUReconstructionCUDAKernels.cu)
21-
set(HDRS GPUReconstructionCUDA.h GPUReconstructionCUDAInternals.h GPUReconstructionCUDADef.h GPUReconstructionCUDAIncludesHost.h CUDAThrustHelpers.h)
21+
set(HDRS GPUReconstructionCUDA.h GPUReconstructionCUDAInternals.h GPUReconstructionCUDAHelpers.inc GPUReconstructionCUDADef.h GPUReconstructionCUDAIncludesHost.h CUDAThrustHelpers.h)
2222
# -------------------------------- Prepare RTC -------------------------------------------------------
2323
enable_language(ASM)
2424
if(ALIGPU_BUILD_TYPE STREQUAL "O2")

GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.cu

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "CUDAThrustHelpers.h"
2323
#include "GPUReconstructionIncludes.h"
2424
#include "GPUParamRTC.h"
25+
#include "GPUReconstructionCUDAHelpers.inc"
2526

2627
#if defined(GPUCA_KERNEL_COMPILE_MODE) && GPUCA_KERNEL_COMPILE_MODE == 1
2728
#include "utils/qGetLdBinarySymbols.h"
@@ -62,13 +63,9 @@ GPUReconstructionCUDABackend::~GPUReconstructionCUDABackend()
6263
}
6364

6465
static_assert(sizeof(cudaError_t) <= sizeof(int64_t) && cudaSuccess == 0);
65-
int32_t GPUReconstructionCUDABackend::GPUChkErrStatic(const int64_t error, const char* file, int32_t line)
66+
int32_t GPUReconstructionCUDABackend::GPUChkErrInternal(const int64_t error, const char* file, int32_t line) const
6667
{
67-
if (error == cudaSuccess) {
68-
return (0);
69-
}
70-
GPUError("CUDA Error: %ld / %s (%s:%d)", error, cudaGetErrorString((cudaError_t)error), file, line);
71-
return 1;
68+
return internal::GPUReconstructionCUDAChkErr(error, file, line);
7269
}
7370

7471
GPUReconstructionCUDA::GPUReconstructionCUDA(const GPUSettingsDeviceBackend& cfg) : GPUReconstructionKernels(cfg)

GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ class GPUReconstructionCUDABackend : public GPUReconstructionDeviceBase
3333
{
3434
public:
3535
~GPUReconstructionCUDABackend() override;
36-
static int32_t GPUChkErrStatic(const int64_t error, const char* file, int32_t line);
3736

3837
protected:
3938
GPUReconstructionCUDABackend(const GPUSettingsDeviceBackend& cfg);
4039

4140
void PrintKernelOccupancies() override;
42-
virtual int32_t GPUChkErrInternal(const int64_t error, const char* file, int32_t line) const override { return GPUChkErrStatic(error, file, line); }
41+
virtual int32_t GPUChkErrInternal(const int64_t error, const char* file, int32_t line) const override;
4342

4443
template <class T, int32_t I = 0, typename... Args>
4544
void runKernelBackend(const krnlSetupArgs<T, I, Args...>& args);

GPU/GPUTracking/Base/cuda/GPUReconstructionCUDAExternalProvider.cu

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ using namespace o2::gpu;
3232
#include "TrackParametrizationWithError.cxx"
3333
#include "Propagator.cxx"
3434
#include "TrackLTIntegral.cxx"
35+
#include "GPUReconstructionCUDAHelpers.inc"
3536

3637
#ifndef GPUCA_NO_CONSTANT_MEMORY
3738
static GPUReconstructionDeviceBase::deviceConstantMemRegistration registerConstSymbol([]() {
3839
void* retVal = nullptr;
39-
if (GPUReconstructionCUDA::GPUChkErrStatic(cudaGetSymbolAddress(&retVal, gGPUConstantMemBuffer), __FILE__, __LINE__)) {
40+
if (GPUChkErrS(cudaGetSymbolAddress(&retVal, gGPUConstantMemBuffer))) {
4041
throw std::runtime_error("Could not obtain GPU constant memory symbol");
4142
}
4243
return retVal;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 GPUReconstructionCUDAHelpers.inc
13+
/// \author David Rohr
14+
15+
#ifndef GPURECONSTRUCTIONCUDAHELPERS_INC_H
16+
#define GPURECONSTRUCTIONCUDAHELPERS_INC_H
17+
18+
#include "GPUCommonHelpers.h"
19+
20+
namespace o2::gpu::internal
21+
{
22+
int32_t __attribute__((weak)) GPUReconstructionCUDAChkErr(const int64_t error, const char* file, int32_t line)
23+
{
24+
if (error != cudaSuccess) {
25+
GPUError("CUDA Error: %ld / %s (%s:%d)", error, cudaGetErrorString((cudaError_t)error), file, line);
26+
}
27+
return error != cudaSuccess;
28+
}
29+
} // namespace o2::gpu::internal
30+
31+
#endif

GPU/GPUTracking/Base/cuda/GPUReconstructionCUDAInternals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <vector>
2323
#include <memory>
2424
#include <string>
25-
#include "GPUCommonChkErr.h"
25+
#include "GPUCommonHelpers.h"
2626

2727
namespace o2::gpu
2828
{

0 commit comments

Comments
 (0)