opm-simulators
Toggle main menu visibility
Loading...
Searching...
No Matches
safe_conversion.hpp
1
/*
2
Copyright 2023 SINTEF AS
3
4
This file is part of the Open Porous Media project (OPM).
5
6
OPM is free software: you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
10
11
OPM is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
#ifndef OPM_CUISTL_SAFE_CONVERSION_HPP
20
#define OPM_CUISTL_SAFE_CONVERSION_HPP
21
22
23
24
#include <cstddef>
25
#include <fmt/format.h>
26
#include <limits>
27
#include <opm/common/ErrorMacros.hpp>
28
#include <type_traits>
29
#include <cuda_runtime.h>
30
31
39
40
namespace
Opm::gpuistl::detail
41
{
42
51
inline
int
52
to_int
(std::size_t s)
53
{
54
static_assert
(
55
std::is_signed_v<int>,
56
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function."
);
57
static_assert
(
58
!std::is_signed_v<std::size_t>,
59
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function."
);
60
61
static_assert
(
62
sizeof
(int) <=
sizeof
(std::size_t),
63
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function."
);
64
65
66
if
(s > std::size_t(std::numeric_limits<int>::max())) {
67
OPM_THROW(std::invalid_argument,
68
fmt::format(
"Trying to convert {} to int, but it is out of range. Maximum possible int: {}. "
,
69
s,
70
std::numeric_limits<int>::max()));
71
}
72
73
// We know it will be in range here:
74
return
int(s);
75
}
76
85
__host__ __device__
inline
std::size_t
86
to_size_t
(
int
i)
87
{
88
static_assert
(
89
std::is_signed_v<int>,
90
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function."
);
91
static_assert
(
92
!std::is_signed_v<std::size_t>,
93
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function."
);
94
95
static_assert
(
96
sizeof
(int) <=
sizeof
(std::size_t),
97
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function."
);
98
99
#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
100
assert(i >=
int
(0));
101
#else
102
if
(i <
int
(0)) {
103
OPM_THROW(std::invalid_argument, fmt::format(
"Trying to convert the negative number {} to size_t."
, i));
104
}
105
#endif
106
107
return
std::size_t(i);
108
}
109
}
// namespace Opm::gpuistl::detail
110
111
#endif
Opm::gpuistl::detail
Contains wrappers to make the CuBLAS library behave as a modern C++ library with function overlading.
Definition
autotuner.hpp:30
Opm::gpuistl::detail::to_int
int to_int(std::size_t s)
to_int converts a (on most relevant platforms) 64 bits unsigned size_t to a signed 32 bits signed int
Definition
safe_conversion.hpp:52
Opm::gpuistl::detail::to_size_t
__host__ __device__ std::size_t to_size_t(int i)
to_size_t converts a (on most relevant platforms) a 32 bit signed int to a 64 bits unsigned int
Definition
safe_conversion.hpp:86
opm
simulators
linalg
gpuistl
detail
safe_conversion.hpp
Generated by
1.17.0