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 some central GPU macros #5984

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
gpu_macros
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 @@ -483,6 +483,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_gpu_macros.cu)


# for loop providing the flag --expt-relaxed-constexpr to fix some cuda issues with constexpr
if(NOT CONVERT_CUDA_TO_HIP)
Expand Down
78 changes: 78 additions & 0 deletions tests/gpuistl/test_gpu_macros.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
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 <config.h>
#include <opm/common/utility/gpuDecorators.hpp>

#define BOOST_TEST_MODULE TestGPUMacros


#include <boost/test/unit_test.hpp>
#include <opm/simulators/linalg/gpuistl/gpu_smart_pointer.hpp>
namespace
{

__global__ void testKernelSetIfGPU(int* ptr) {
#if OPM_IS_INSIDE_DEVICE_FUNCTION
*ptr = 42;
#else
*ptr = 123;
#endif
}

__global__ void testKernelSetIfNotCPU(int* ptr) {
#if OPM_IS_INSIDE_HOST_FUNCTION
*ptr = 42;
#else
*ptr = 123;
#endif
}


} // namespace


BOOST_AUTO_TEST_CASE(TestInsideDevice)
{
auto sharedPtr = Opm::gpuistl::make_gpu_shared_ptr<int>(1);

testKernelSetIfGPU<<<1, 1>>>(sharedPtr.get());
auto valueFromDevice = Opm::gpuistl::copyFromGPU(sharedPtr);
BOOST_CHECK_EQUAL(valueFromDevice, 42);
}

BOOST_AUTO_TEST_CASE(TestOutsideHost)
{
auto sharedPtr = Opm::gpuistl::make_gpu_shared_ptr<int>(1);

testKernelSetIfNotCPU<<<1, 1>>>(sharedPtr.get());
auto valueFromDevice = Opm::gpuistl::copyFromGPU(sharedPtr);
BOOST_CHECK_EQUAL(valueFromDevice, 123);
}

BOOST_AUTO_TEST_CASE(TestMacrosOnHost)
{
#if OPM_IS_INSIDE_HOST_FUNCTION
BOOST_CHECK(true);
#else
BOOST_CHECK(false);
#endif

#if OPM_IS_INSIDE_DEVICE_FUNCTION
BOOST_CHECK(false);
#else
BOOST_CHECK(true);
#endif
}