Skip to content

Commit

Permalink
Switching to C++17 (if constexpr support), full support for invalid n…
Browse files Browse the repository at this point in the history
…umeric_types
  • Loading branch information
neoblizz committed Oct 28, 2020
1 parent 08918f2 commit 2aa9447
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 40 deletions.
4 changes: 2 additions & 2 deletions examples/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ DEFINES = -DGIT_SHA1="\"$(shell git rev-parse HEAD)\""
# ARCH = -m64
# endif

NVCCFLAGS += -std=c++14
NVCCFLAGS += -std=c++17
NVCCFLAGS += $(SM_TARGETS)
NVCCFLAGS += --expt-extended-lambda --expt-relaxed-constexpr --use_fast_math --ptxas-options -v --relocatable-device-code true

CXXFLAGS += -std=c++14
CXXFLAGS += -std=c++17
CXXFLAGS += -Wall
CXXFLAGS += -Wno-unused-local-typedefs -Wno-strict-aliasing -Wno-unused-function -Wno-format-security

Expand Down
2 changes: 1 addition & 1 deletion gunrock/graph/graph.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class graph_t : public graph_view_t... {
}

__host__ __device__ __forceinline__ edge_type
get_starting_edge(vertex_type const& v) const {
get_starting_edge(vertex_type const& v) const override {
return first_view_t::get_starting_edge(v);
}

Expand Down
67 changes: 35 additions & 32 deletions gunrock/util/type_limits.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,40 @@

namespace gunrock {

template <typename type_t>
template <typename type_t, typename enable_t = void>
struct numeric_limits : std::numeric_limits<type_t> {};

// Numeric Limits (additional support) for invalid() values.
template <>
struct numeric_limits<int> : std::numeric_limits<int> {
// XXX: This doesn't seem like the right thing to do for a signed integral
// type. It implies, -1 is an invalid value. Integers do not have a notion of
// invalid values.
constexpr static int invalid() { return ~((int)0); }
};

template <>
struct numeric_limits<float> : std::numeric_limits<float> {
constexpr static float invalid() {
return std::numeric_limits<float>::quiet_NaN();
template <typename type_t>
struct numeric_limits<
type_t,
typename std::enable_if_t<std::is_integral<type_t>::value &&
std::is_signed<type_t>::value>>
: std::numeric_limits<type_t> {
constexpr static type_t invalid() {
return std::integral_constant<type_t, -1>::value;
}
};

template <>
struct numeric_limits<double> : std::numeric_limits<double> {
constexpr static double invalid() {
return std::numeric_limits<double>::quiet_NaN();
template <typename type_t>
struct numeric_limits<
type_t,
typename std::enable_if_t<std::is_integral<type_t>::value &&
std::is_unsigned<type_t>::value>>
: std::numeric_limits<type_t> {
constexpr static type_t invalid() {
return std::integral_constant<type_t,
std::numeric_limits<type_t>::max()>::value;
}
};

template <>
struct numeric_limits<long double> : std::numeric_limits<long double> {
constexpr static long double invalid() {
return std::numeric_limits<long double>::quiet_NaN();
template <typename type_t>
struct numeric_limits<
type_t,
typename std::enable_if_t<std::is_floating_point<type_t>::value>>
: std::numeric_limits<type_t> {
constexpr static type_t invalid() {
return std::numeric_limits<type_t>::quiet_NaN();
}
};

Expand All @@ -52,17 +56,16 @@ namespace gunrock {
namespace util {
namespace limits {

// XXX: Revisit later...
template <typename numeric_t>
__host__ __device__ __forceinline__ bool is_valid(numeric_t const& value) {
static_assert(std::is_arithmetic_v<numeric_t>);
return !isnan(value);
}

template <typename numeric_t>
__host__ __device__ __forceinline__ bool is_invalid(numeric_t const& value) {
static_assert(std::is_arithmetic_v<numeric_t>);
return isnan(value);
template <typename type_t>
__host__ __device__ __forceinline__ bool is_valid(type_t value) {
static_assert((std::is_integral<type_t>::value ||
std::is_floating_point<type_t>::value),
"type_t must be an arithmetic type.");
if constexpr (std::is_integral<type_t>::value)
return (value != gunrock::numeric_limits<type_t>::invalid());
else
// XXX: test this on device
return (bool)!isnan(value);
}

} // namespace limits
Expand Down
2 changes: 2 additions & 0 deletions gunrock/util/type_traits.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace std {

#if __cplusplus == 201402L
// Supported in C++ 17 (https://en.cppreference.com/w/cpp/types/disjunction)
template <class...>
struct disjunction : std::false_type {};
Expand All @@ -25,4 +26,5 @@ constexpr bool is_arithmetic_v = is_arithmetic<T>::value;
// (https://en.cppreference.com/w/cpp/types/is_floating_point)
template <class T>
constexpr bool is_floating_point_v = is_floating_point<T>::value;
#endif
} // namespace std
4 changes: 2 additions & 2 deletions unittests/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ DEFINES = -DGIT_SHA1="\"$(shell git rev-parse HEAD)\""
# ARCH = -m64
# endif

NVCCFLAGS += -std=c++14
NVCCFLAGS += -std=c++17
NVCCFLAGS += $(SM_TARGETS)
NVCCFLAGS += --expt-extended-lambda --expt-relaxed-constexpr --use_fast_math --ptxas-options -v --relocatable-device-code true

CXXFLAGS += -std=c++14
CXXFLAGS += -std=c++17
CXXFLAGS += -Wall
CXXFLAGS += -Wno-unused-local-typedefs -Wno-strict-aliasing -Wno-unused-function -Wno-format-security

Expand Down
7 changes: 4 additions & 3 deletions unittests/type_limits/test_type_limits.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include <gunrock/util/type_limits.hxx>

void test_type_limits() {
float i = gunrock::numeric_limits<float>::invalid();
std::cout << i << " is valid? " << std::boolalpha
<< gunrock::util::limits::is_valid(i) << std::endl;
using type_t = short unsigned int;
type_t i = gunrock::numeric_limits<type_t>::invalid();
std::cout << "i = " << i << " (is valid? " << std::boolalpha
<< gunrock::util::limits::is_valid(i) << ")" << std::endl;
}

int main(int argc, char** argv) {
Expand Down

0 comments on commit 2aa9447

Please sign in to comment.