Skip to content

Commit

Permalink
Add runtime macro alpaka_throw_acc
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetyusufoglu authored and psychocoderHPC committed Sep 17, 2024
1 parent fe262f6 commit 3575cfd
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/alpaka/alpaka.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include "alpaka/core/OmpSchedule.hpp"
#include "alpaka/core/Positioning.hpp"
#include "alpaka/core/RemoveRestrict.hpp"
#include "alpaka/core/RuntimeMacros.hpp"
#include "alpaka/core/Sycl.hpp"
#include "alpaka/core/ThreadPool.hpp"
#include "alpaka/core/Unreachable.hpp"
Expand Down
52 changes: 52 additions & 0 deletions include/alpaka/core/RuntimeMacros.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright 2022 Andrea Bocci, Mehmet Yusufoglu, René Widera, Aurora Perego
* SPDX-License-Identifier: MPL-2.0
*/

#pragma once

// Implementation details
#include "alpaka/core/Sycl.hpp"

//! ALPAKA_THROW_ACC either aborts(terminating the program and creating a core dump) or throws std::runtime_error
//! depending on the Acc. The std::runtime_error exception can be catched in the catch block.
//!
//! For CUDA __trap function is used which triggers std::runtime_error but can be catched during wait not exec.
//! For HIP abort() function is used and calls __builtin_trap()
//! For Sycl assert(false) is not used since it can be disabled -DNDEBUG compile option. abort() is used although it
//! generates a runtime error instead of aborting in GPUs: "Caught synchronous SYCL exception: Unresolved Symbol
//! <abort> -999 (Unknown PI error)."
//!
//! The OpenMP specification mandates that exceptions thrown by some thread must be handled by the same thread.
//! Therefore std::runtime_error thrown by ALPAKA_THROW_ACC aborts the the program for OpenMP backends. If needed
//! the SIGABRT signal can be catched by signal handler.
#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) && defined(__CUDA_ARCH__)
# define ALPAKA_THROW_ACC(MSG) \
{ \
printf( \
"alpaka encountered a user-defined error condition while running on the CUDA back-end:\n%s", \
(MSG)); \
__trap(); \
}
#elif defined(ALPAKA_ACC_GPU_HIP_ENABLED) && defined(__HIP_DEVICE_COMPILE__)
# define ALPAKA_THROW_ACC(MSG) \
{ \
printf( \
"alpaka encountered a user-defined error condition while running on the HIP back-end:\n%s", \
(MSG)); \
abort(); \
}
#elif defined(ALPAKA_ACC_SYCL_ENABLED) && defined(__SYCL_DEVICE_ONLY__)
# define ALPAKA_THROW_ACC(MSG) \
{ \
printf( \
"alpaka encountered a user-defined error condition while running on the SYCL back-end:\n%s", \
(MSG)); \
abort(); \
}
#else
# define ALPAKA_THROW_ACC(MSG) \
{ \
printf("alpaka encountered a user-defined error condition:\n%s", (MSG)); \
throw std::runtime_error(MSG); \
}
#endif
1 change: 1 addition & 0 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_subdirectory("mem/view/")
add_subdirectory("meta/")
add_subdirectory("queue/")
add_subdirectory("rand/")
add_subdirectory("runtime/")
add_subdirectory("traits/")
add_subdirectory("vec/")
add_subdirectory("warp/")
Expand Down
20 changes: 20 additions & 0 deletions test/unit/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright 2014-2020 Benjamin Worpitz, Axel Huebl, Jan Stephan
# SPDX-License-Identifier: MPL-2.0
#

set(_TARGET_NAME "runtimeTest")

append_recursive_files_add_to_src_group("src/" "src/" "cpp" _FILES_SOURCE)

alpaka_add_executable(
${_TARGET_NAME}
${_FILES_SOURCE})
target_link_libraries(
${_TARGET_NAME}
PRIVATE common)

set_target_properties(${_TARGET_NAME} PROPERTIES FOLDER "test/unit")
target_compile_definitions(${_TARGET_NAME} PRIVATE "-DTEST_UNIT_RUNTIME")

add_test(NAME ${_TARGET_NAME} COMMAND ${_TARGET_NAME} ${_alpaka_TEST_OPTIONS})
84 changes: 84 additions & 0 deletions test/unit/runtime/src/KernelThrow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* Copyright 2022 René Widera, Mehmet Yusufoglu, Andrea Bocci
* SPDX-License-Identifier: MPL-2.0
*/

#include <alpaka/core/RuntimeMacros.hpp>
#include <alpaka/test/acc/TestAccs.hpp>
#include <alpaka/test/queue/Queue.hpp>

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>

class KernelWithThrow
{
public:
template<typename TAcc>
ALPAKA_FN_ACC auto operator()(TAcc const& acc) const -> void
{
using Idx = alpaka::Idx<TAcc>;
using Dim = alpaka::Dim<TAcc>;
using Vec = alpaka::Vec<Dim, Idx>;
Vec const globalThreadIdx = alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc);
if(globalThreadIdx == Vec::zeros())
{
// Throw abort or std::runtime_error depending on acc type
ALPAKA_THROW_ACC("Exception thrown by the kernel.");
}
alpaka::syncBlockThreads(acc);
}
};

template<typename T, typename Acc>
void checkThrow(std::string const& expectedErrStr)
{
if constexpr(alpaka::accMatchesTags<Acc, T>)
{
using Idx = alpaka::Idx<Acc>;
using Dim = alpaka::Dim<Acc>;
using Vec = alpaka::Vec<Dim, Idx>;
using Queue = alpaka::Queue<Acc, alpaka::Blocking>;

auto const platformAcc = alpaka::Platform<Acc>{};
auto const devAcc = alpaka::getDevByIdx(platformAcc, 0);

Queue queue(devAcc);
auto const workDiv = alpaka::WorkDivMembers<Dim, Idx>{Vec{8}, Vec{1}, Vec{1}};

try
{
alpaka::exec<Acc>(queue, workDiv, KernelWithThrow{});
// Cuda can catch exceptions which were thrown at kernel during the wait(); therefore wait is added.
alpaka::wait(queue);
}
catch(std::runtime_error& e)
{
std::string const errorStr{e.what()};
printf("The error str catched: %s \n", errorStr.c_str());
printf("The expected str in error str: %s \n", expectedErrStr.c_str());

auto const found = errorStr.find(expectedErrStr);
CHECK(found != std::string::npos);
}
catch(std::exception& e)
{
FAIL(std::string("Wrong exception type thrown in kernel:") + e.what());
}
}
}

using TestAccs = alpaka::test::EnabledAccs<alpaka::DimInt<1u>, std::uint32_t>;

TEMPLATE_LIST_TEST_CASE("ThrowForCpuThreadAndSerial", "[runtime]", TestAccs)
{
using Acc = TestType;
// Test runtime-error exceptions.
checkThrow<alpaka::TagCpuThreads, Acc>("Exception thrown by the kernel");
checkThrow<alpaka::TagCpuSerial, Acc>("Exception thrown by the kernel");
}

TEMPLATE_LIST_TEST_CASE("ThrowForGpuBackend", "[runtime]", TestAccs)
{
using Acc = TestType;
// Test runtime-error exceptions.
checkThrow<alpaka::TagGpuCudaRt, Acc>("cudaErrorLaunchFailure");
}

0 comments on commit 3575cfd

Please sign in to comment.