Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for error macros on GPU. #5962

Merged
merged 3 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ if(CUDA_FOUND)
gpu_resources
gpu_smart_pointers
is_gpu_pointer
throw_macros_on_gpu
PROPERTIES LABELS ${gpu_label})
endif()

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ if (HAVE_CUDA)
ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_gpu_smart_pointers.cu)
ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_gpu_resources.cu)
ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_is_gpu_pointer.cpp)
ADD_CUDA_OR_HIP_FILE(TEST_SOURCE_FILES tests test_throw_macros_on_gpu.cu)


# for loop providing the flag --expt-relaxed-constexpr to fix some cuda issues with constexpr
if(NOT CONVERT_CUDA_TO_HIP)
Expand Down
62 changes: 62 additions & 0 deletions tests/gpuistl/test_throw_macros_on_gpu.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2025 Equinor ASA

This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/test/tools/old/interface.hpp>
#include <config.h>
#include <stdexcept>

#define BOOST_TEST_MODULE TestThrowMacrosOnGPU

#include <cuda.h>
#include <cuda_runtime.h>
#include <boost/test/unit_test.hpp>
#include <opm/common/ErrorMacros.hpp>
#include <opm/simulators/linalg/gpuistl/detail/gpu_safe_call.hpp>

namespace {
// NOTE: We have to split this into a separate function due
// to some weirdness of hipcc. Note however that this is
// the realistic use case of the macro.
__device__ __host__ void functionThatContainsMacros(bool call) {
if (call) {
OPM_THROW(std::logic_error, "Something went wrong");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something has gone wrong with the macro that assess whether we are inside a GPU kernel or not, as I get an error on an AMD gpu as it expands the wrong macro from ErrorMacros.h. It should have expanded the macro on line 110 in ErrorMacros.hpp

opm-simulators/tests/gpuistl_hip/test_throw_macros_on_gpu.hip:32:9: error: no matching constructor for initialization of 'std::string' (aka 'basic_string')
32 | OPM_THROW(std::logic_error, "Something went wrong");
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
opm-common/opm/common/ErrorMacros.hpp:56:28: note: expanded from macro 'OPM_THROW'
56 | std::string oss_ = std::string{"["} + FILE + ":" + \

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minimal example that can not be compiled on HIP

#include <hip/hip_runtime.h>
#include

global void test_kernel() {
#if defined(CUDA_ARCH) || (defined(HIP_DEVICE_COMPILE) && HIP_DEVICE_COMPILE > 0)
printf("Hello from HIP kernel!\n");
#else
static_assert(false, "This should not be compiled for host");
#endif
}

int main(int, char**) {
test_kernel<<<1, 1>>>();
hipDeviceSynchronize();
return 0;
}

OPM_THROW_NOLOG(std::logic_error, "Something went wrong");
OPM_THROW_PROBLEM(std::logic_error, "Something went wrong");
}
OPM_ERROR_IF(!call, "Something went horribly wrong");
}
__global__ void codeThatContainsMacros(bool call) {
functionThatContainsMacros(call);
}
}

BOOST_AUTO_TEST_CASE(TestKernel)
{
OPM_GPU_SAFE_CALL(cudaDeviceSynchronize());
OPM_GPU_SAFE_CALL(cudaGetLastError());
codeThatContainsMacros<<<1, 1>>>(false);
OPM_GPU_SAFE_CALL(cudaDeviceSynchronize());
OPM_GPU_SAFE_CALL(cudaGetLastError());
}

BOOST_AUTO_TEST_CASE(TestOutsideKernel)
{
// This is to make sure that the macros work outside of kernels but inside a .cu file
// ie. inside a file compiled by nvcc/hipcc.
BOOST_CHECK_THROW(OPM_THROW(std::runtime_error, "THROW"), std::runtime_error);
BOOST_CHECK_THROW(OPM_THROW_NOLOG(std::runtime_error, "THROW_NOLOG"), std::runtime_error);
BOOST_CHECK_THROW(OPM_THROW_PROBLEM(std::runtime_error, "THROW_PROBLEM"), std::runtime_error);
BOOST_CHECK_THROW(OPM_ERROR_IF(true, "ERROR_IF"), std::logic_error);
}