libMesh
Loading...
Searching...
No Matches
kokkos_numerics_oracle_test_utils.h
Go to the documentation of this file.
1#ifndef KOKKOS_NUMERICS_ORACLE_TEST_UTILS_H
2#define KOKKOS_NUMERICS_ORACLE_TEST_UTILS_H
3
4// Shared plumbing for the Kokkos numerics oracle tests: uploading test
5// data to the device and comparing device-computed results against
6// host-computed references. The comparison helpers mirror device data
7// back to the host and return a count of components outside tolerance
8// (rather than asserting), so the runners can aggregate failures across
9// suites and report them all in one run.
10
11#include "libmesh/libmesh.h"
12
13// Avoid conflicting complex operators between CUDA and PETSc
14#define PETSC_SKIP_CXX_COMPLEX_FIX 1
15#include "libmesh/ignore_warnings.h"
16#include <Kokkos_Core.hpp>
17#include "libmesh/restore_warnings.h"
18#undef __CUDACC_VER__
19
20#include <cmath>
21#include <string>
22#include <vector>
23
24namespace libMeshTest
25{
26namespace KokkosOracle
27{
28
29using libMesh::Real;
30
31// Copies a host vector of plain objects into a new device view, for
32// test inputs that don't go through the gpu/ storage-policy views
33// (e.g. the per-case leading dims in the tensor suite).
34template <typename T>
35inline ::Kokkos::View<T *>
36upload_objects(const std::vector<T> & values, const char * label)
37{
38 ::Kokkos::View<T *> d(std::string(label), values.size());
39 auto h = ::Kokkos::create_mirror_view(d);
40 for (std::size_t i = 0; i < values.size(); ++i)
41 h(i) = values[i];
42 ::Kokkos::deep_copy(d, h);
43 return d;
44}
45
46// The compare_device_* helpers below deep-copy the device results to a
47// host mirror and count individual components that differ from the
48// reference by more than tol; boolean results are encoded as 1.0/0.0
49// by the kernels so a single tolerance comparison covers
50// everything.
51
52inline int
53compare_device_scalars(const ::Kokkos::View<double *> & d_values,
54 const std::vector<Real> & ref_values,
55 const double tol)
56{
57 auto h_values = ::Kokkos::create_mirror_view(d_values);
58 ::Kokkos::deep_copy(h_values, d_values);
59
60 int fail = 0;
61 for (std::size_t i = 0; i < ref_values.size(); ++i)
62 if (std::fabs(h_values(i) - ref_values[i]) > tol)
63 ++fail;
64
65 return fail;
66}
67
68template <typename ViewType, typename VectorType>
69inline int
70compare_device_vectors(const ViewType & d_values,
71 const std::vector<VectorType> & ref_values,
72 const double tol)
73{
74 auto h_values = ::Kokkos::create_mirror_view(d_values);
75 ::Kokkos::deep_copy(h_values, d_values);
76
77 int fail = 0;
78 for (std::size_t i = 0; i < ref_values.size(); ++i)
79 for (unsigned int d = 0; d < LIBMESH_DIM; ++d)
80 if (std::fabs(h_values(i, d) - ref_values[i](d)) > tol)
81 ++fail;
82
83 return fail;
84}
85
86template <typename ViewType, typename TensorType>
87inline int
88compare_device_tensors(const ViewType & d_values,
89 const std::vector<TensorType> & ref_values,
90 const double tol)
91{
92 auto h_values = ::Kokkos::create_mirror_view(d_values);
93 ::Kokkos::deep_copy(h_values, d_values);
94
95 int fail = 0;
96 for (std::size_t i = 0; i < ref_values.size(); ++i)
97 for (unsigned int row = 0; row < LIBMESH_DIM; ++row)
98 for (unsigned int col = 0; col < LIBMESH_DIM; ++col)
99 if (std::fabs(h_values(i, row, col) - ref_values[i](row, col)) > tol)
100 ++fail;
101
102 return fail;
103}
104
105} // namespace KokkosOracle
106} // namespace libMeshTest
107
108#endif
inline ::Kokkos::View< T * > upload_objects(const std::vector< T > &values, const char *label)
int compare_device_scalars(const ::Kokkos::View< double * > &d_values, const std::vector< Real > &ref_values, const double tol)
int compare_device_vectors(const ViewType &d_values, const std::vector< VectorType > &ref_values, const double tol)
int compare_device_tensors(const ViewType &d_values, const std::vector< TensorType > &ref_values, const double tol)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real