mirror of
https://github.com/OPM/opm-simulators.git
synced 2024-11-22 09:16:27 -06:00
Added safe conversion from size_t to int.
This commit is contained in:
parent
31e7ef04ba
commit
9418d5311d
@ -164,6 +164,7 @@ if(CUDA_FOUND)
|
||||
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/CuMatrixDescription.hpp)
|
||||
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/CuSparseResource.hpp)
|
||||
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/CuSparseResource_impl.hpp)
|
||||
list (APPEND PUBLIC_HEADER_FILES opm/simulators/linalg/cuistl/detail/safe_conversion.hpp)
|
||||
|
||||
|
||||
|
||||
@ -252,6 +253,8 @@ if(CUDA_FOUND)
|
||||
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_cusparse_handle.cpp)
|
||||
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_cuvector.cpp)
|
||||
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_cusparsematrix.cpp)
|
||||
list(APPEND TEST_SOURCE_FILES tests/cuistl/test_safe_conversion.cpp)
|
||||
|
||||
endif()
|
||||
if(OPENCL_FOUND)
|
||||
list(APPEND TEST_SOURCE_FILES tests/test_openclSolver.cpp)
|
||||
|
63
opm/simulators/linalg/cuistl/detail/safe_conversion.hpp
Normal file
63
opm/simulators/linalg/cuistl/detail/safe_conversion.hpp
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
Copyright 2023 SINTEF AS
|
||||
|
||||
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/>.
|
||||
*/
|
||||
#ifndef OPM_CUISTL_SAFE_CONVERSION_HPP
|
||||
#define OPM_CUISTL_SAFE_CONVERSION_HPP
|
||||
|
||||
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <limits>
|
||||
#include <opm/common/ErrorMacros.hpp>
|
||||
|
||||
|
||||
/**
|
||||
* Provides various utilities for doing signed to unsigned conversion, unsigned to signed, 32 bits to 64 bits and 64
|
||||
* bits to 32 bits.
|
||||
*
|
||||
* The main use case within cuistl is that the cusparse library requires signed int for all its size parameters,
|
||||
* while Dune::BlockVector (and relatives) use signed size_t.
|
||||
*/
|
||||
|
||||
namespace Opm::cuistl::detail
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief convert converts a (on most relevant platform) 64 bits unsigned size_t to a signed 32 bits signed int
|
||||
* @param s the unsigned integer
|
||||
* @throw std::invalid_argument exception if s is out of range for an int
|
||||
* @return converted s to int if s is within the range of int
|
||||
*
|
||||
* @todo This can be done for more generic types, but then it is probably wise to wait for C++20's cmp-functions
|
||||
*/
|
||||
int
|
||||
convert(size_t s)
|
||||
{
|
||||
if (s > size_t(std::numeric_limits<int>::max())) {
|
||||
OPM_THROW(std::invalid_argument,
|
||||
fmt::format("Trying to convert {} to int, but it is out of range. Maximum possible int: {}. ",
|
||||
s,
|
||||
std::numeric_limits<int>::max()));
|
||||
}
|
||||
|
||||
// We know it will be in range here:
|
||||
return int(s);
|
||||
}
|
||||
} // namespace Opm::cuistl::detail
|
||||
|
||||
#endif
|
41
tests/cuistl/test_safe_conversion.cpp
Normal file
41
tests/cuistl/test_safe_conversion.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
Copyright 2023 SINTEF AS
|
||||
|
||||
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>
|
||||
|
||||
#define BOOST_TEST_MODULE TestSafeConversion
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <opm/simulators/linalg/cuistl/detail/safe_conversion.hpp>
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TestThrowsOutofRange)
|
||||
{
|
||||
BOOST_CHECK_THROW(Opm::cuistl::detail::convert(size_t(std::numeric_limits<int>::max()) + size_t(1));
|
||||
, std::invalid_argument);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TestConvertInRange)
|
||||
{
|
||||
// This might seem slow, but it is really fast:
|
||||
for (size_t i = 0; i <= size_t(1024 * 1024); ++i) {
|
||||
BOOST_CHECK_EQUAL(int(i), Opm::cuistl::detail::convert(i));
|
||||
}
|
||||
|
||||
BOOST_CHECK_EQUAL(std::numeric_limits<int>::max(),
|
||||
Opm::cuistl::detail::convert(size_t(std::numeric_limits<int>::max())));
|
||||
}
|
Loading…
Reference in New Issue
Block a user