Skip to content

Commit

Permalink
unify CUDA/HIP function calls/types
Browse files Browse the repository at this point in the history
- add macro `ALPAKA_API_PREFIX()` to refix functions and types
- add helper `ALPAKA_PP_CONCAT()` to construct `make_cudaPitched()`
- reduce duplicated runtime error checks for CUDA/HIP
  • Loading branch information
psychocoderHPC authored and BenjaminW3 committed Mar 6, 2020
1 parent d56d352 commit d651b5c
Show file tree
Hide file tree
Showing 16 changed files with 521 additions and 1,334 deletions.
18 changes: 9 additions & 9 deletions include/alpaka/acc/AccGpuUniformCudaHipRt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,41 +153,41 @@ namespace alpaka
// Reading only the necessary attributes with cudaDeviceGetAttribute is faster than reading all with cuda
// https://devblogs.nvidia.com/cuda-pro-tip-the-fast-way-to-query-device-properties/
int multiProcessorCount = {};
ALPAKA_CUDA_RT_CHECK(cudaDeviceGetAttribute(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cudaDeviceGetAttribute(
&multiProcessorCount,
cudaDevAttrMultiProcessorCount,
dev.m_iDevice));

int maxGridSize[3] = {};
ALPAKA_CUDA_RT_CHECK(cudaDeviceGetAttribute(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cudaDeviceGetAttribute(
&maxGridSize[0],
cudaDevAttrMaxGridDimX,
dev.m_iDevice));
ALPAKA_CUDA_RT_CHECK(cudaDeviceGetAttribute(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cudaDeviceGetAttribute(
&maxGridSize[1],
cudaDevAttrMaxGridDimY,
dev.m_iDevice));
ALPAKA_CUDA_RT_CHECK(cudaDeviceGetAttribute(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cudaDeviceGetAttribute(
&maxGridSize[2],
cudaDevAttrMaxGridDimZ,
dev.m_iDevice));

int maxBlockDim[3] = {};
ALPAKA_CUDA_RT_CHECK(cudaDeviceGetAttribute(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cudaDeviceGetAttribute(
&maxBlockDim[0],
cudaDevAttrMaxBlockDimX,
dev.m_iDevice));
ALPAKA_CUDA_RT_CHECK(cudaDeviceGetAttribute(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cudaDeviceGetAttribute(
&maxBlockDim[1],
cudaDevAttrMaxBlockDimY,
dev.m_iDevice));
ALPAKA_CUDA_RT_CHECK(cudaDeviceGetAttribute(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cudaDeviceGetAttribute(
&maxBlockDim[2],
cudaDevAttrMaxBlockDimZ,
dev.m_iDevice));

int maxThreadsPerBlock = {};
ALPAKA_CUDA_RT_CHECK(cudaDeviceGetAttribute(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(cudaDeviceGetAttribute(
&maxThreadsPerBlock,
cudaDevAttrMaxThreadsPerBlock,
dev.m_iDevice));
Expand Down Expand Up @@ -219,7 +219,7 @@ namespace alpaka

#else
hipDeviceProp_t hipDevProp;
ALPAKA_HIP_RT_CHECK(hipGetDeviceProperties(
ALPAKA_UNIFORM_CUDA_HIP_RT_CHECK(hipGetDeviceProperties(
&hipDevProp,
dev.m_iDevice));

Expand Down
99 changes: 6 additions & 93 deletions include/alpaka/core/Cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
#include <cuda_runtime.h>
#include <cuda.h>

#include <array>
#include <type_traits>
#include <utility>
#include <iostream>
#include <string>
#include <stdexcept>
#include <cstddef>
Expand All @@ -50,97 +48,10 @@
#error "CUDA version 9.0 or greater required!"
#endif

namespace alpaka
{
namespace cuda
{
namespace detail
{
//-----------------------------------------------------------------------------
//! CUDA runtime API error checking with log and exception, ignoring specific error values
ALPAKA_FN_HOST inline auto cudaRtCheck(
cudaError_t const & error,
char const * desc,
char const * file,
int const & line)
-> void
{
if(error != cudaSuccess)
{
std::string const sError(std::string(file) + "(" + std::to_string(line) + ") " + std::string(desc) + " : '" + cudaGetErrorName(error) + "': '" + std::string(cudaGetErrorString(error)) + "'!");
#if ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL
std::cerr << sError << std::endl;
#endif
ALPAKA_DEBUG_BREAK;
// reset the last error to allow user side error handling
cudaGetLastError();
throw std::runtime_error(sError);
}
}
//-----------------------------------------------------------------------------
//! CUDA runtime API error checking with log and exception, ignoring specific error values
// NOTE: All ignored errors have to be convertible to cudaError_t.
template<
typename... TErrors>
ALPAKA_FN_HOST auto cudaRtCheckIgnore(
cudaError_t const & error,
char const * cmd,
char const * file,
int const & line,
TErrors && ... ignoredErrorCodes)
-> void
{
if(error != cudaSuccess)
{
std::array<cudaError_t, sizeof...(ignoredErrorCodes)> const aIgnoredErrorCodes{ignoredErrorCodes...};

// If the error code is not one of the ignored ones.
if(std::find(aIgnoredErrorCodes.cbegin(), aIgnoredErrorCodes.cend(), error) == aIgnoredErrorCodes.cend())
{
cudaRtCheck(error, ("'" + std::string(cmd) + "' returned error ").c_str(), file, line);
}
}
}
//-----------------------------------------------------------------------------
//! CUDA runtime API last error checking with log and exception.
ALPAKA_FN_HOST inline auto cudaRtCheckLastError(
char const * desc,
char const * file,
int const & line)
-> void
{
cudaError_t const error(cudaGetLastError());
cudaRtCheck(error, desc, file, line);
}
}
}
}

#if BOOST_COMP_MSVC
//-----------------------------------------------------------------------------
//! CUDA runtime error checking with log and exception, ignoring specific error values
#define ALPAKA_CUDA_RT_CHECK_IGNORE(cmd, ...)\
::alpaka::cuda::detail::cudaRtCheckLastError("'" #cmd "' A previous CUDA call (not this one) set the error ", __FILE__, __LINE__);\
::alpaka::cuda::detail::cudaRtCheckIgnore(cmd, #cmd, __FILE__, __LINE__, __VA_ARGS__)
#else
#if BOOST_COMP_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
//-----------------------------------------------------------------------------
//! CUDA runtime error checking with log and exception, ignoring specific error values
#define ALPAKA_CUDA_RT_CHECK_IGNORE(cmd, ...)\
::alpaka::cuda::detail::cudaRtCheckLastError("'" #cmd "' A previous CUDA call (not this one) set the error ", __FILE__, __LINE__);\
::alpaka::cuda::detail::cudaRtCheckIgnore(cmd, #cmd, __FILE__, __LINE__, ##__VA_ARGS__)
#if BOOST_COMP_CLANG
#pragma clang diagnostic pop
#endif
#endif

//-----------------------------------------------------------------------------
//! CUDA runtime error checking with log and exception.
#define ALPAKA_CUDA_RT_CHECK(cmd)\
ALPAKA_CUDA_RT_CHECK_IGNORE(cmd)
#define ALPAKA_PP_CONCAT_DO(X,Y) X##Y
#define ALPAKA_PP_CONCAT(X,Y) ALPAKA_PP_CONCAT_DO(X,Y)
//! prefix a name with `cuda`
#define ALPAKA_API_PREFIX(name) ALPAKA_PP_CONCAT_DO(cuda,name)

namespace alpaka
{
Expand Down Expand Up @@ -747,4 +658,6 @@ namespace alpaka
}
}

#include <alpaka/core/UniformCudaHip.hpp>

#endif
99 changes: 6 additions & 93 deletions include/alpaka/core/Hip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,107 +26,18 @@

#include <hip/hip_runtime.h>

#include <array>
#include <type_traits>
#include <utility>
#include <iostream>
#include <string>
#include <stdexcept>
#include <cstddef>

#ifdef __HIP_PLATFORM_HCC__
#define HIPRT_CB
#endif


namespace alpaka
{
namespace hip
{
namespace detail
{
//-----------------------------------------------------------------------------
//! HIP runtime API error checking with log and exception, ignoring specific error values
ALPAKA_FN_HOST inline auto hipRtCheck(
hipError_t const & error,
char const * desc,
char const * file,
int const & line)
-> void
{
if(error != hipSuccess)
{
std::string const sError(std::string(file) + "(" + std::to_string(line) + ") " + std::string(desc) + " : '" + hipGetErrorName(error) + "': '" + std::string(hipGetErrorString(error)) + "'!");
#if ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL
std::cerr << sError << std::endl;
#endif
ALPAKA_DEBUG_BREAK;
throw std::runtime_error(sError);
}
}
//-----------------------------------------------------------------------------
//! HIP runtime API error checking with log and exception, ignoring specific error values
// NOTE: All ignored errors have to be convertible to hipError_t.
template<
typename... TErrors>
ALPAKA_FN_HOST auto hipRtCheckIgnore(
hipError_t const & error,
char const * cmd,
char const * file,
int const & line,
TErrors && ... ignoredErrorCodes)
-> void
{
if(error != hipSuccess)
{
std::array<hipError_t, sizeof...(ignoredErrorCodes)> const aIgnoredErrorCodes{ignoredErrorCodes...};
// If the error code is not one of the ignored ones.
if(std::find(aIgnoredErrorCodes.cbegin(), aIgnoredErrorCodes.cend(), error) == aIgnoredErrorCodes.cend())
{
hipRtCheck(error, ("'" + std::string(cmd) + "' returned error ").c_str(), file, line);
}
}
}
//-----------------------------------------------------------------------------
//! HIP runtime API last error checking with log and exception.
ALPAKA_FN_HOST inline auto hipRtCheckLastError(
char const * desc,
char const * file,
int const & line)
-> void
{
hipError_t const error(hipGetLastError());
hipRtCheck(error, desc, file, line);
}
}
}
}

#if BOOST_COMP_MSVC
//-----------------------------------------------------------------------------
//! HIP runtime error checking with log and exception, ignoring specific error values
#define ALPAKA_HIP_RT_CHECK_IGNORE(cmd, ...)\
::alpaka::hip::detail::hipRtCheckLastError("'" #cmd "' A previous HIP call (not this one) set the error ", __FILE__, __LINE__);\
::alpaka::hip::detail::hipRtCheckIgnore(cmd, #cmd, __FILE__, __LINE__, __VA_ARGS__)
#else
#if BOOST_COMP_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
//-----------------------------------------------------------------------------
//! HIP runtime error checking with log and exception, ignoring specific error values
#define ALPAKA_HIP_RT_CHECK_IGNORE(cmd, ...)\
::alpaka::hip::detail::hipRtCheckLastError("'" #cmd "' A previous HIP call (not this one) set the error ", __FILE__, __LINE__);\
::alpaka::hip::detail::hipRtCheckIgnore(cmd, #cmd, __FILE__, __LINE__, ##__VA_ARGS__)
#if BOOST_COMP_CLANG
#pragma clang diagnostic pop
#endif
#endif

//-----------------------------------------------------------------------------
//! HIP runtime error checking with log and exception.
#define ALPAKA_HIP_RT_CHECK(cmd)\
ALPAKA_HIP_RT_CHECK_IGNORE(cmd)
#define ALPAKA_PP_CONCAT_DO(X,Y) X##Y
#define ALPAKA_PP_CONCAT(X,Y) ALPAKA_PP_CONCAT_DO(X,Y)
//! prefix a name with `hip`
#define ALPAKA_API_PREFIX(name) ALPAKA_PP_CONCAT_DO(hip,name)

//-----------------------------------------------------------------------------
// HIP vector_types.h trait specializations.
Expand Down Expand Up @@ -704,4 +615,6 @@ namespace alpaka
}
}

#include <alpaka/core/UniformCudaHip.hpp>

#endif
Loading

0 comments on commit d651b5c

Please sign in to comment.