UnitTestCudaMath.cu 6.67 KB
Newer Older
1
//============================================================================
Kenneth Moreland's avatar
Kenneth Moreland committed
2
3
4
5
6
7
8
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
9
//============================================================================
Kenneth Moreland's avatar
Kenneth Moreland committed
10

11
#include <vtkm/cont/RuntimeDeviceTracker.h>
Kenneth Moreland's avatar
Kenneth Moreland committed
12
#include <vtkm/cont/cuda/DeviceAdapterCuda.h>
13
#include <vtkm/cont/testing/Testing.h>
14
#include <vtkm/testing/TestingMath.h>
Kenneth Moreland's avatar
Kenneth Moreland committed
15

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>

#include <cmath>
#include <iostream>
#include <limits>
#include <random>
#include <vector>

#include "curand_kernel.h"

namespace
{

struct TriggerICE : public vtkm::worklet::WorkletMapField
{
32
  using ControlSignature = void(FieldIn, FieldIn, FieldOut);
33
34
  using ExecutionSignature = _3(_1, _2, WorkIndex);

35
#ifdef VTKM_CUDA_DEVICE_PASS
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  template <class ValueType>
  __device__ ValueType operator()(const ValueType& bad,
                                  const ValueType& sane,
                                  const vtkm::Id sequenceId) const
  {

    curandState_t state;
    //Each thread uses same seed but different sequence numbers
    curand_init(42, sequenceId, 0, &state);

    int signBad = vtkm::SignBit(bad);
    int signGood = vtkm::SignBit(bad);

    vtkm::Vec<ValueType, 3> coord = { vtkm::Abs(bad * sane),
                                      bad * sane + (ValueType)signBad,
                                      bad * sane + (ValueType)signGood };

    for (int i = 0; i < 10; ++i)
    {
      for (int j = 0; j < 3; ++j)
      {
        if (vtkm::IsNan(coord[j]))
        {
          coord[j] = curand_normal(&state) * 5.0f;
          coord[j] = vtkm::Sqrt(vtkm::Dot(coord, coord));
          if (coord[j] <= 1.0f)
          {
            coord[j] += 1.0f;
          }
        }
        if (vtkm::IsInf(coord[j]))
        {
          coord[j] = curand_normal(&state) * 8.0f;
          coord[j] = vtkm::Tan(vtkm::Cos(vtkm::Dot(coord, coord)));
        }
      }
    }
    return coord[0] * 4.0f + coord[1] * 4.0f + coord[2] * 4.0f;
  }
#else
  template <class ValueType>
  ValueType operator()(const ValueType& bad, const ValueType& sane, const vtkm::Id sequenceId) const
  {
79
    return bad + sane * static_cast<ValueType>(sequenceId);
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  }
#endif
};

//-----------------------------------------------------------------------------
template <typename Device>
void RunEdgeCases()
{
  std::cout << "Testing complicated worklets that can cause NVCC to ICE." << std::endl;
  //When running CUDA on unsupported hardware we find that IsInf, IsNan, and
  //SignBit can cause the CUDA compiler to crash. This test is a consistent
  //way to detect this.
  //
  //The way it works is we generate all kinds of nasty floating point values
  //such as signaling Nan, quiet Nan, other Nans, +Inf, -Inf, -0, +0, a collection of
  //denormal numbers, and the min and max float values
  //and than a random collection of values from normal float space. We combine this
  //array which we will call 'bad' with another input array which we will call 'sane',
  //We than execute a worklet that takes values stored in 'bad' and 'sane' that does
  //some computation that takes into account the results of IsInf, IsNan, and
  //SignBit
  const vtkm::Id desired_size = 2048;
  std::vector<float> sanevalues;
  std::vector<float> badvalues = { std::numeric_limits<float>::signaling_NaN(),
                                   std::numeric_limits<float>::quiet_NaN(),
                                   std::nanf("1"),
                                   std::nanf("4200042"),
                                   std::numeric_limits<float>::infinity(),
                                   std::numeric_limits<float>::infinity() * -1,
                                   0.0f,
                                   -0.0f,
                                   std::numeric_limits<float>::denorm_min(),
                                   std::nextafter(std::numeric_limits<float>::min(), 0.0f),
                                   std::numeric_limits<float>::denorm_min() *
                                     (1 + std::numeric_limits<float>::epsilon()),
                                   std::nextafter(std::numeric_limits<float>::min(), 0.0f) *
                                     (1 + std::numeric_limits<float>::epsilon()),
                                   std::numeric_limits<float>::lowest(),
                                   std::numeric_limits<float>::min(),
                                   std::numeric_limits<float>::max() };
  const std::size_t bad_size = badvalues.size();
  const vtkm::Id bad_size_as_id = static_cast<vtkm::Id>(bad_size);

  badvalues.reserve(desired_size);
  sanevalues.reserve(desired_size);

  //construct a random number generator
  std::mt19937 rng;
  std::uniform_real_distribution<float> range(-1.0f, 1.0f);

  // now add in some random numbers to the bad values
  for (std::size_t i = 0; i < desired_size - bad_size; ++i)
  {
    badvalues.push_back(range(rng));
  }
  for (std::size_t i = 0; i < desired_size; ++i)
  {
    sanevalues.push_back(range(rng));
  }

  auto bad = vtkm::cont::make_ArrayHandle(badvalues);
  auto sane = vtkm::cont::make_ArrayHandle(sanevalues);
  decltype(sane) result;
143
144
  vtkm::worklet::DispatcherMapField<TriggerICE> dispatcher;
  dispatcher.SetDevice(Device());
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  dispatcher.Invoke(bad, sane, result);

  auto portal = result.GetPortalConstControl();

  //the first 6 values should be nan
  VTKM_TEST_ASSERT(vtkm::IsNan(portal.Get(0)), "Value should be NaN.");
  VTKM_TEST_ASSERT(vtkm::IsNan(portal.Get(1)), "Value should be NaN.");
  VTKM_TEST_ASSERT(vtkm::IsNan(portal.Get(2)), "Value should be NaN.");
  VTKM_TEST_ASSERT(vtkm::IsNan(portal.Get(3)), "Value should be NaN.");
  VTKM_TEST_ASSERT(vtkm::IsNan(portal.Get(4)), "Value should be NaN.");
  VTKM_TEST_ASSERT(vtkm::IsNan(portal.Get(5)), "Value should be NaN.");

  for (vtkm::Id i = bad_size_as_id; i < desired_size; ++i)
  { //The rest of the values shouldn't be Nan or Inf
    auto v = portal.Get(i);
    const bool valid = !vtkm::IsNan(v) && !vtkm::IsInf(v);
    VTKM_TEST_ASSERT(valid, "value shouldn't be NaN or INF");
  }
}

} //namespace

167
int UnitTestCudaMath(int argc, char* argv[])
Kenneth Moreland's avatar
Kenneth Moreland committed
168
{
169
  auto tracker = vtkm::cont::GetRuntimeDeviceTracker();
170
  tracker.ForceDevice(vtkm::cont::DeviceAdapterTagCuda{});
171
  int tests_valid = vtkm::cont::testing::Testing::Run(
172
    UnitTestMathNamespace::RunMathTests<vtkm::cont::DeviceAdapterTagCuda>, argc, argv);
173

174
175
  tests_valid +=
    vtkm::cont::testing::Testing::Run(RunEdgeCases<vtkm::cont::DeviceAdapterTagCuda>, argc, argv);
176
177

  return tests_valid;
Kenneth Moreland's avatar
Kenneth Moreland committed
178
}