-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Task/rhornung67/device numeric limits #1196
Changes from 7 commits
87c4d77
9ad3a06
b255612
f401e6e
90aed51
ec4e42f
7aadf4b
29f555f
ef7af10
088e566
ab3106a
7d4ca50
2fea1ad
1ee4b00
07d7016
fc43024
effc432
e2602cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and | ||
// other Axom Project Developers. See the top-level LICENSE file for details. | ||
// | ||
// SPDX-License-Identifier: (BSD-3-Clause) | ||
|
||
/*! | ||
* | ||
* \file NumericLimits.hpp | ||
* | ||
* \brief Header file containing portability layer for std::numeric_limits | ||
* capabilities | ||
* | ||
*/ | ||
|
||
#ifndef AXOM_NUMERICLIMITS_HPP_ | ||
#define AXOM_NUMERICLIMITS_HPP_ | ||
|
||
#include "axom/config.hpp" // for compile-time definitions | ||
|
||
#include <limits> | ||
|
||
#if defined(AXOM_USE_CUDA) | ||
#include <cuda/std/limits> | ||
#endif | ||
|
||
namespace axom | ||
{ | ||
#if defined(AXOM_USE_CUDA) && defined(AXOM_DEVICE_CODE) | ||
template <typename T> | ||
using numeric_limits = cuda::std::numeric_limits<T>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the point of using CUDA-specific numeric limits to get host device on them? Or, does that not matter since they are probably all constexpr? Does HIP have a header like this? Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main point of this is to prevent compiler warnings (calling host function from host-device function) that a few users have reported. It is not needed for HIP since the amdclang compiler is a unified host-device compiler and know how to sort everything out. |
||
#else | ||
template <typename T> | ||
using numeric_limits = std::numeric_limits<T>; | ||
#endif | ||
|
||
} // namespace axom | ||
|
||
#endif // AXOM_NUMERICLIMITS_HPP_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and | ||
// other Axom Project Developers. See the top-level LICENSE file for details. | ||
// | ||
// SPDX-License-Identifier: (BSD-3-Clause) | ||
|
||
#include "axom/config.hpp" // for compile time definitions | ||
|
||
#include "axom/core/NumericLimits.hpp" | ||
|
||
// for gtest macros | ||
#include "gtest/gtest.h" | ||
|
||
//------------------------------------------------------------------------------ | ||
// UNIT TESTS | ||
//------------------------------------------------------------------------------ | ||
|
||
//------------------------------------------------------------------------------ | ||
TEST(core_NumericLimits, check_CPU) | ||
{ | ||
// | ||
// Tests to compare axom::numeric_limits to std::numeric_limits | ||
// to ensure that Axom type aliasing is correct. | ||
// | ||
EXPECT_TRUE(axom::numeric_limits<int>::lowest() == | ||
std::numeric_limits<int>::lowest()); | ||
EXPECT_TRUE(axom::numeric_limits<int>::min() == std::numeric_limits<int>::min()); | ||
EXPECT_TRUE(axom::numeric_limits<int>::max() == std::numeric_limits<int>::max()); | ||
EXPECT_TRUE(axom::numeric_limits<int>::is_signed == | ||
std::numeric_limits<int>::is_signed); | ||
|
||
EXPECT_TRUE(axom::numeric_limits<float>::lowest() == | ||
std::numeric_limits<float>::lowest()); | ||
EXPECT_TRUE(axom::numeric_limits<float>::min() == | ||
std::numeric_limits<float>::min()); | ||
EXPECT_TRUE(axom::numeric_limits<float>::max() == | ||
std::numeric_limits<float>::max()); | ||
|
||
EXPECT_TRUE(axom::numeric_limits<double>::lowest() == | ||
std::numeric_limits<double>::lowest()); | ||
EXPECT_TRUE(axom::numeric_limits<double>::min() == | ||
std::numeric_limits<double>::min()); | ||
EXPECT_TRUE(axom::numeric_limits<double>::max() == | ||
std::numeric_limits<double>::max()); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
#if defined(AXOM_USE_CUDA) | ||
// | ||
// Tests to ensure axom::numeric_limits type alias does the correct thing | ||
// in host and CUDA device code. | ||
// | ||
|
||
// | ||
// Simple device kernel | ||
// | ||
__global__ void cuda_kernel(int* a, size_t* b, float* c, double* d) | ||
{ | ||
a[0] = axom::numeric_limits<int>::min(); | ||
b[0] = axom::numeric_limits<size_t>::max(); | ||
c[0] = axom::numeric_limits<float>::lowest(); | ||
d[0] = axom::numeric_limits<double>::max(); | ||
} | ||
|
||
TEST(core_NumericLimits, check_CUDA) | ||
{ | ||
// | ||
// Device memory allocation and initialiation for a few different types. | ||
// | ||
int* a; | ||
(void)cudaMalloc(&a, sizeof(int)); | ||
(void)cudaMemset(a, 0, sizeof(int)); | ||
|
||
size_t* b; | ||
(void)cudaMalloc(&b, sizeof(size_t)); | ||
(void)cudaMemset(b, 0, sizeof(size_t)); | ||
|
||
float* c; | ||
(void)cudaMalloc(&c, sizeof(float)); | ||
(void)cudaMemset(c, 0, sizeof(float)); | ||
|
||
double* d; | ||
(void)cudaMalloc(&d, sizeof(double)); | ||
(void)cudaMemset(d, 0, sizeof(double)); | ||
|
||
// | ||
// Set values in device code. | ||
// | ||
cuda_kernel<<<1, 1>>>(a, b, c, d); | ||
|
||
// | ||
// Copy device values back to host and compare with expectations.... | ||
// | ||
int ha; | ||
size_t hb; | ||
float hc; | ||
double hd; | ||
(void)cudaMemcpy(&ha, a, sizeof(int), cudaMemcpyDeviceToHost); | ||
(void)cudaMemcpy(&hb, b, sizeof(size_t), cudaMemcpyDeviceToHost); | ||
(void)cudaMemcpy(&hc, c, sizeof(float), cudaMemcpyDeviceToHost); | ||
(void)cudaMemcpy(&hd, d, sizeof(double), cudaMemcpyDeviceToHost); | ||
|
||
EXPECT_TRUE(ha == axom::numeric_limits<int>::min()); | ||
EXPECT_TRUE(hb == axom::numeric_limits<size_t>::max()); | ||
EXPECT_TRUE(hc == axom::numeric_limits<float>::lowest()); | ||
EXPECT_TRUE(hd == axom::numeric_limits<double>::max()); | ||
} | ||
#endif | ||
|
||
//------------------------------------------------------------------------------ | ||
#if defined(AXOM_USE_HIP) | ||
// | ||
// Tests to ensure axom::numeric_limits type alias does the correct thing | ||
// in host and CUDA device code. | ||
// | ||
|
||
// | ||
// Simple device kernel | ||
// | ||
__global__ void hip_kernel(int* a, size_t* b, float* c, double* d) | ||
{ | ||
a[0] = axom::numeric_limits<int>::min(); | ||
b[0] = axom::numeric_limits<size_t>::max(); | ||
c[0] = axom::numeric_limits<float>::lowest(); | ||
d[0] = axom::numeric_limits<double>::max(); | ||
} | ||
|
||
TEST(core_NumericLimits, check_HIP) | ||
{ | ||
// | ||
// Device memory allocation and initialiation for a few different types. | ||
// | ||
int* a; | ||
(void)hipMalloc(&a, sizeof(int)); | ||
(void)hipMemset(a, 0, sizeof(int)); | ||
|
||
size_t* b; | ||
(void)hipMalloc(&b, sizeof(size_t)); | ||
(void)hipMemset(b, 0, sizeof(size_t)); | ||
|
||
float* c; | ||
(void)hipMalloc(&c, sizeof(float)); | ||
(void)hipMemset(c, 0, sizeof(float)); | ||
|
||
double* d; | ||
(void)hipMalloc(&d, sizeof(double)); | ||
(void)hipMemset(d, 0, sizeof(double)); | ||
|
||
// | ||
// Set values in device code. | ||
// | ||
hip_kernel<<<1, 1>>>(a, b, c, d); | ||
|
||
// | ||
// Copy device values back to host and compare with expectations.... | ||
// | ||
int ha; | ||
size_t hb; | ||
float hc; | ||
double hd; | ||
(void)hipMemcpy(&ha, a, sizeof(int), hipMemcpyDeviceToHost); | ||
(void)hipMemcpy(&hb, b, sizeof(size_t), hipMemcpyDeviceToHost); | ||
(void)hipMemcpy(&hc, c, sizeof(float), hipMemcpyDeviceToHost); | ||
(void)hipMemcpy(&hd, d, sizeof(double), hipMemcpyDeviceToHost); | ||
|
||
EXPECT_TRUE(ha == axom::numeric_limits<int>::min()); | ||
EXPECT_TRUE(hb == axom::numeric_limits<size_t>::max()); | ||
EXPECT_TRUE(hc == axom::numeric_limits<float>::lowest()); | ||
EXPECT_TRUE(hd == axom::numeric_limits<double>::max()); | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to remove
if defined(AXOM_DEVICE_CODE)
and just have theAXOM_USE_CUDA
guard? It's my impression thatcuda::std
should work on both the host and device.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it does, that would be preferable. I need to look into that. @kennyweiss discussed this PR yesterday. That resulted in some concerns about several things in the code that we should discuss as a team. Unfortunately, that will have to wait for a couple of weeks as next Monday is a LLNL holiday and NECDC is the week after that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@publixsubfan
cuda::std
does work in device and host code. I ran into some issues with some Axom tests wherecuda::std::numeric_limits
does not supportlong double
. The intent of my change was to usestd::numeric_limits
in host code for all builds. However, it's not clear to me that we need to supportlong double
. long double is automatically converted to double in device code and attempting to pass long double data between host and device code is problematic.