-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Grützmacher
committed
Sep 12, 2019
1 parent
3b92144
commit be85815
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
ginkgo_create_cuda_test(cuda_executor) | ||
ginkgo_create_cuda_test(exception_helpers) | ||
ginkgo_create_cuda_test(math) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/*******************************<GINKGO LICENSE>****************************** | ||
Copyright (c) 2017-2019, the Ginkgo authors | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
******************************<GINKGO LICENSE>*******************************/ | ||
|
||
#include <ginkgo/core/base/math.hpp> | ||
|
||
|
||
#include <cmath> | ||
#include <complex> | ||
#include <limits> | ||
#include <memory> | ||
|
||
|
||
#include <gtest/gtest.h> | ||
|
||
|
||
#include <ginkgo/core/base/array.hpp> | ||
#include <ginkgo/core/base/executor.hpp> | ||
|
||
|
||
#include "cuda/base/math.hpp" | ||
|
||
|
||
namespace { | ||
|
||
|
||
template <typename T> | ||
__global__ void test_real_isfinite(bool *result) | ||
{ | ||
using limits = std::numeric_limits<T>; | ||
constexpr auto inf = INFINITY; | ||
bool test_true{}; | ||
bool test_false{}; | ||
|
||
test_true = | ||
gko::isfinite(T{0}) && gko::isfinite(-T{0}) && gko::isfinite(T{1}); | ||
test_false = | ||
gko::isfinite(inf) || gko::isfinite(-inf) || gko::isfinite(NAN); | ||
*result = test_true && !test_false; | ||
} | ||
|
||
|
||
template <typename T> | ||
__global__ void test_complex_isfinite(bool *result) | ||
{ | ||
using limits = std::numeric_limits<T>; | ||
using c_type = thrust::complex<T>; // std::complex<T>; | ||
constexpr T inf = INFINITY; | ||
constexpr T quiet_nan = NAN; | ||
bool test_true{}; | ||
bool test_false{}; | ||
|
||
test_true = gko::isfinite(c_type{T{0}, T{0}}) && | ||
gko::isfinite(c_type{-T{0}, -T{0}}) && | ||
gko::isfinite(c_type{T{1}, T{0}}) && | ||
gko::isfinite(c_type{T{0}, T{1}}); | ||
test_false = | ||
gko::isfinite(c_type{inf, T{0}}) || gko::isfinite(c_type{-inf, T{0}}) || | ||
gko::isfinite(c_type{quiet_nan, T{0}}) || | ||
gko::isfinite(c_type{T{0}, inf}) || gko::isfinite(c_type{T{0}, -inf}) || | ||
gko::isfinite(c_type{T{0}, quiet_nan}); | ||
*result = test_true && !test_false; | ||
} | ||
|
||
|
||
class IsFinite : public ::testing::Test { | ||
protected: | ||
IsFinite() | ||
: ref(gko::ReferenceExecutor::create()), | ||
cuda(gko::CudaExecutor::create(0, ref)) | ||
{} | ||
|
||
template <typename T> | ||
bool test_real_isfinite_kernel() | ||
{ | ||
gko::Array<bool> result(cuda, 1); | ||
test_real_isfinite<T><<<1, 1>>>(result.get_data()); | ||
result.set_executor(ref); | ||
return *result.get_data(); | ||
} | ||
|
||
template <typename T> | ||
bool test_complex_isfinite_kernel() | ||
{ | ||
gko::Array<bool> result(cuda, 1); | ||
test_real_isfinite<T><<<1, 1>>>(result.get_data()); | ||
result.set_executor(ref); | ||
return *result.get_data(); | ||
} | ||
|
||
std::shared_ptr<gko::ReferenceExecutor> ref; | ||
std::shared_ptr<gko::CudaExecutor> cuda; | ||
}; | ||
|
||
|
||
TEST_F(IsFinite, Float) { ASSERT_TRUE(test_real_isfinite_kernel<float>()); } | ||
|
||
|
||
TEST_F(IsFinite, Double) { ASSERT_TRUE(test_real_isfinite_kernel<double>()); } | ||
|
||
|
||
TEST_F(IsFinite, FloatComplex) | ||
{ | ||
ASSERT_TRUE(test_complex_isfinite_kernel<float>()); | ||
} | ||
|
||
|
||
TEST_F(IsFinite, DoubleComplex) | ||
{ | ||
ASSERT_TRUE(test_complex_isfinite_kernel<double>()); | ||
} | ||
|
||
|
||
} // namespace |