|
| 1 | +// Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and |
| 2 | +// other Axom Project Developers. See the top-level LICENSE file for details. |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: (BSD-3-Clause) |
| 5 | + |
| 6 | +#include "axom/config.hpp" // for compile time definitions |
| 7 | + |
| 8 | +#include "axom/core/NumericLimits.hpp" |
| 9 | + |
| 10 | +// for gtest macros |
| 11 | +#include "gtest/gtest.h" |
| 12 | + |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | +// UNIT TESTS |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | +TEST(core_NumericLimits, check_CPU) |
| 19 | +{ |
| 20 | + // |
| 21 | + // Tests to compare axom::numeric_limits to std::numeric_limits |
| 22 | + // to ensure that Axom type aliasing is correct. |
| 23 | + // |
| 24 | + EXPECT_TRUE(axom::numeric_limits<int>::lowest() == |
| 25 | + std::numeric_limits<int>::lowest()); |
| 26 | + EXPECT_TRUE(axom::numeric_limits<int>::min() == std::numeric_limits<int>::min()); |
| 27 | + EXPECT_TRUE(axom::numeric_limits<int>::max() == std::numeric_limits<int>::max()); |
| 28 | + EXPECT_TRUE(axom::numeric_limits<int>::is_signed == |
| 29 | + std::numeric_limits<int>::is_signed); |
| 30 | + |
| 31 | + EXPECT_TRUE(axom::numeric_limits<float>::lowest() == |
| 32 | + std::numeric_limits<float>::lowest()); |
| 33 | + EXPECT_TRUE(axom::numeric_limits<float>::min() == |
| 34 | + std::numeric_limits<float>::min()); |
| 35 | + EXPECT_TRUE(axom::numeric_limits<float>::max() == |
| 36 | + std::numeric_limits<float>::max()); |
| 37 | + |
| 38 | + EXPECT_TRUE(axom::numeric_limits<double>::lowest() == |
| 39 | + std::numeric_limits<double>::lowest()); |
| 40 | + EXPECT_TRUE(axom::numeric_limits<double>::min() == |
| 41 | + std::numeric_limits<double>::min()); |
| 42 | + EXPECT_TRUE(axom::numeric_limits<double>::max() == |
| 43 | + std::numeric_limits<double>::max()); |
| 44 | +} |
| 45 | + |
| 46 | +//------------------------------------------------------------------------------ |
| 47 | +#if defined(AXOM_USE_CUDA) |
| 48 | +// |
| 49 | +// Tests to ensure axom::numeric_limits type alias does the correct thing |
| 50 | +// in host and CUDA device code. |
| 51 | +// |
| 52 | + |
| 53 | +// |
| 54 | +// Simple device kernel |
| 55 | +// |
| 56 | +__global__ void cuda_kernel(int* a, size_t* b, float* c, double* d) |
| 57 | +{ |
| 58 | + a[0] = axom::numeric_limits<int>::min(); |
| 59 | + b[0] = axom::numeric_limits<size_t>::max(); |
| 60 | + c[0] = axom::numeric_limits<float>::lowest(); |
| 61 | + d[0] = axom::numeric_limits<double>::max(); |
| 62 | +} |
| 63 | + |
| 64 | +TEST(core_NumericLimits, check_CUDA) |
| 65 | +{ |
| 66 | + // |
| 67 | + // Device memory allocation and initialiation for a few different types. |
| 68 | + // |
| 69 | + int* a; |
| 70 | + (void)cudaMalloc(&a, sizeof(int)); |
| 71 | + (void)cudaMemset(a, 0, sizeof(int)); |
| 72 | + |
| 73 | + size_t* b; |
| 74 | + (void)cudaMalloc(&b, sizeof(size_t)); |
| 75 | + (void)cudaMemset(b, 0, sizeof(size_t)); |
| 76 | + |
| 77 | + float* c; |
| 78 | + (void)cudaMalloc(&c, sizeof(float)); |
| 79 | + (void)cudaMemset(c, 0, sizeof(float)); |
| 80 | + |
| 81 | + double* d; |
| 82 | + (void)cudaMalloc(&d, sizeof(double)); |
| 83 | + (void)cudaMemset(d, 0, sizeof(double)); |
| 84 | + |
| 85 | + // |
| 86 | + // Set values in device code. |
| 87 | + // |
| 88 | + cuda_kernel<<<1, 1>>>(a, b, c, d); |
| 89 | + |
| 90 | + // |
| 91 | + // Copy device values back to host and compare with expectations.... |
| 92 | + // |
| 93 | + int ha; |
| 94 | + size_t hb; |
| 95 | + float hc; |
| 96 | + double hd; |
| 97 | + (void)cudaMemcpy(&ha, a, sizeof(int), cudaMemcpyDeviceToHost); |
| 98 | + (void)cudaMemcpy(&hb, b, sizeof(size_t), cudaMemcpyDeviceToHost); |
| 99 | + (void)cudaMemcpy(&hc, c, sizeof(float), cudaMemcpyDeviceToHost); |
| 100 | + (void)cudaMemcpy(&hd, d, sizeof(double), cudaMemcpyDeviceToHost); |
| 101 | + |
| 102 | + EXPECT_TRUE(ha == axom::numeric_limits<int>::min()); |
| 103 | + EXPECT_TRUE(hb == axom::numeric_limits<size_t>::max()); |
| 104 | + EXPECT_TRUE(hc == axom::numeric_limits<float>::lowest()); |
| 105 | + EXPECT_TRUE(hd == axom::numeric_limits<double>::max()); |
| 106 | +} |
| 107 | +#endif |
| 108 | + |
| 109 | +//------------------------------------------------------------------------------ |
| 110 | +#if defined(AXOM_USE_HIP) |
| 111 | +// |
| 112 | +// Tests to ensure axom::numeric_limits type alias does the correct thing |
| 113 | +// in host and CUDA device code. |
| 114 | +// |
| 115 | + |
| 116 | +// |
| 117 | +// Simple device kernel |
| 118 | +// |
| 119 | +__global__ void hip_kernel(int* a, size_t* b, float* c, double* d) |
| 120 | +{ |
| 121 | + a[0] = axom::numeric_limits<int>::min(); |
| 122 | + b[0] = axom::numeric_limits<size_t>::max(); |
| 123 | + c[0] = axom::numeric_limits<float>::lowest(); |
| 124 | + d[0] = axom::numeric_limits<double>::max(); |
| 125 | +} |
| 126 | + |
| 127 | +TEST(core_NumericLimits, check_HIP) |
| 128 | +{ |
| 129 | + // |
| 130 | + // Device memory allocation and initialiation for a few different types. |
| 131 | + // |
| 132 | + int* a; |
| 133 | + (void)hipMalloc(&a, sizeof(int)); |
| 134 | + (void)hipMemset(a, 0, sizeof(int)); |
| 135 | + |
| 136 | + size_t* b; |
| 137 | + (void)hipMalloc(&b, sizeof(size_t)); |
| 138 | + (void)hipMemset(b, 0, sizeof(size_t)); |
| 139 | + |
| 140 | + float* c; |
| 141 | + (void)hipMalloc(&c, sizeof(float)); |
| 142 | + (void)hipMemset(c, 0, sizeof(float)); |
| 143 | + |
| 144 | + double* d; |
| 145 | + (void)hipMalloc(&d, sizeof(double)); |
| 146 | + (void)hipMemset(d, 0, sizeof(double)); |
| 147 | + |
| 148 | + // |
| 149 | + // Set values in device code. |
| 150 | + // |
| 151 | + hip_kernel<<<1, 1>>>(a, b, c, d); |
| 152 | + |
| 153 | + // |
| 154 | + // Copy device values back to host and compare with expectations.... |
| 155 | + // |
| 156 | + int ha; |
| 157 | + size_t hb; |
| 158 | + float hc; |
| 159 | + double hd; |
| 160 | + (void)hipMemcpy(&ha, a, sizeof(int), hipMemcpyDeviceToHost); |
| 161 | + (void)hipMemcpy(&hb, b, sizeof(size_t), hipMemcpyDeviceToHost); |
| 162 | + (void)hipMemcpy(&hc, c, sizeof(float), hipMemcpyDeviceToHost); |
| 163 | + (void)hipMemcpy(&hd, d, sizeof(double), hipMemcpyDeviceToHost); |
| 164 | + |
| 165 | + EXPECT_TRUE(ha == axom::numeric_limits<int>::min()); |
| 166 | + EXPECT_TRUE(hb == axom::numeric_limits<size_t>::max()); |
| 167 | + EXPECT_TRUE(hc == axom::numeric_limits<float>::lowest()); |
| 168 | + EXPECT_TRUE(hd == axom::numeric_limits<double>::max()); |
| 169 | +} |
| 170 | +#endif |
0 commit comments