Skip to content

Commit d40d21f

Browse files
committed
Implement review suggestions
1 parent a9334c7 commit d40d21f

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

example/complex/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2014-2022 Erik Zenker, Benjamin Worpitz, Jan Stephan, Sergei Bastrakov
2+
# Copyright 2022 Erik Zenker, Benjamin Worpitz, Jan Stephan, Sergei Bastrakov
33
#
44
# This file exemplifies usage of alpaka.
55
#

include/alpaka/math/Complex.hpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,36 @@
1515
#include <cmath>
1616
#include <complex>
1717
#include <iostream>
18+
#include <type_traits>
1819

1920
namespace alpaka
2021
{
2122
//! Implementation of a complex number useable on host and device.
2223
//!
2324
//! It follows the layout of std::complex and so array-oriented access.
24-
//! Inside the class template implements all methods and operators as std::complex<T>.
25-
//! Additionally, provides an implicit conversion to and from std::complex<T>.
25+
//! The class template implements all methods and operators as std::complex<T>.
26+
//! Additionally, it provides an implicit conversion to and from std::complex<T>.
2627
//! All methods besides operators << and >> are host-device.
27-
//! Note that it does not provide non-member functions of std::complex besides the operators.
28+
//! It does not provide non-member functions of std::complex besides the operators.
2829
//! Those are provided the same way as alpaka math functions for real numbers.
2930
//!
3031
//! Note that unlike most of alpaka, this is a concrete type template, and not merely a concept.
3132
//!
32-
//! Naming and order of the methods match https://en.cppreference.com/w/cpp/numeric/complex.
33+
//! Naming and order of the methods match https://en.cppreference.com/w/cpp/numeric/complex in C++17.
34+
//! Implementation chose to not extend it e.g. by adding constexpr to some places that would get it in C++20.
35+
//! The motivation is that with internal conversion to std::complex<T> for CPU backends, it would define the common
36+
//! interface for genetic code anyways.
37+
//! So it is more clear to have alpaka's interface exactly matching when possible, and not "improving".
38+
//! \TODO: when switching to C++20, update the interface to match C++20 std::complex<T>.
3339
//!
3440
//! @tparam T type of the real and imaginary part: float, double, or long double.
3541
template<typename T>
3642
class Complex
3743
{
3844
public:
45+
// Make sure the input type is floating-point
46+
static_assert(std::is_floating_point_v<T>);
47+
3948
//! Type of the real and imaginary parts
4049
using value_type = T;
4150

@@ -307,8 +316,7 @@ namespace alpaka
307316
template<typename T>
308317
constexpr ALPAKA_FN_HOST_ACC bool operator!=(Complex<T> const& lhs, Complex<T> const& rhs)
309318
{
310-
return !math::floatEqualExactNoWarning(lhs.real(), rhs.real())
311-
|| !math::floatEqualExactNoWarning(lhs.imag(), rhs.imag());
319+
return !(lhs == rhs);
312320
}
313321

314322
//! Inequality of a complex and a real number
@@ -381,7 +389,7 @@ namespace alpaka
381389
return std::acosh(std::complex<T>(x));
382390
}
383391

384-
//! Host-side arg falling back to std:: implementation
392+
//! Argument
385393
template<typename T>
386394
constexpr ALPAKA_FN_HOST T arg(Complex<T> const& x)
387395
{

include/alpaka/math/Traits.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ namespace alpaka::math
166166
template<typename T, typename TArgument, typename TSfinae = void>
167167
struct Arg
168168
{
169-
//!@todo why is this needed here, but not in other functions?
169+
// It is unclear why this is needed here and not in other math trait structs. But removing it causes
170+
// warnings with calling a __host__ function from a __host__ __device__ function when building for CUDA.
170171
ALPAKA_NO_HOST_ACC_WARNING
171172
ALPAKA_FN_HOST_ACC auto operator()(T const& /* ctx */, TArgument const& argument)
172173
{

test/unit/math/src/Functor.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace alpaka
6161
template< \
6262
typename TAcc, \
6363
typename... TArgs, /* SFINAE: Enables if called from device. */ \
64-
typename std::enable_if<!std::is_same<TAcc, std::nullptr_t>::value, int>::type = 0> \
64+
typename std::enable_if_t<!std::is_same_v<TAcc, std::nullptr_t>, int> = 0> \
6565
ALPAKA_FN_ACC auto execute(TAcc const& acc, TArgs const&... args) const \
6666
{ \
6767
return ALPAKA_OP(acc, args...); \
@@ -71,7 +71,7 @@ namespace alpaka
7171
template< \
7272
typename TAcc = std::nullptr_t, \
7373
typename TArg1, /* SFINAE: Enables if called from host. */ \
74-
typename std::enable_if<std::is_same<TAcc, std::nullptr_t>::value, int>::type = 0> \
74+
typename std::enable_if_t<std::is_same_v<TAcc, std::nullptr_t>, int> = 0> \
7575
ALPAKA_FN_HOST auto execute(TAcc const& /* acc */, TArg1 const& arg1) const \
7676
{ \
7777
return STD_OP(typename StdLibType<TArg1>::type(arg1)); \
@@ -82,7 +82,7 @@ namespace alpaka
8282
typename TAcc = std::nullptr_t, \
8383
typename TArg1, \
8484
typename TArg2, /* SFINAE: Enables if called from host. */ \
85-
typename std::enable_if<std::is_same<TAcc, std::nullptr_t>::value, int>::type = 0> \
85+
typename std::enable_if_t<std::is_same_v<TAcc, std::nullptr_t>, int> = 0> \
8686
ALPAKA_FN_HOST auto execute(TAcc const& /* acc */, TArg1 const& arg1, TArg2 const& arg2) const \
8787
{ \
8888
return STD_OP(typename StdLibType<TArg1>::type(arg1), typename StdLibType<TArg2>::type(arg2)); \

test/unit/math/src/mathComplexFloat.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <catch2/catch.hpp>
1818

19+
#include <complex>
1920
#include <tuple>
2021

2122
using TestAccs = alpaka::test::EnabledAccs<alpaka::DimInt<1u>, std::size_t>;
@@ -34,3 +35,17 @@ TEMPLATE_LIST_TEST_CASE("mathOpsComplexFloat", "[math] [operator]", TestAccFunct
3435
auto testTemplate = TestTemplate<Acc, Functor>{};
3536
testTemplate.template operator()<alpaka::Complex<float>>();
3637
}
38+
39+
#ifdef __has_include
40+
# if __has_include(<version>)
41+
# include <version>
42+
# endif
43+
#endif
44+
#ifdef __cpp_lib_is_layout_compatible
45+
TEMPLATE_LIST_TEST_CASE("mathOpsComplex", "[layout]", TestAccs)
46+
{
47+
// Check both float and double here to avoid duplicating the wrapper code
48+
REQUIRE(std::is_layout_compatible_v<alpaka::Complex<float>, std::complex<float>>);
49+
REQUIRE(std::is_layout_compatible_v<alpaka::Complex<double>, std::complex<double>>);
50+
}
51+
#endif

0 commit comments

Comments
 (0)