https://mooseframework.inl.gov
KokkosUtils.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "KokkosHeader.h"
13 
14 namespace Moose
15 {
16 namespace Kokkos
17 {
18 namespace Utils
19 {
20 
26 template <typename T>
27 KOKKOS_INLINE_FUNCTION T
28 sign(T x)
29 {
30  return x >= 0.0 ? 1.0 : -1.0;
31 }
32 
40 template <typename T>
41 KOKKOS_INLINE_FUNCTION const T *
42 find(const T & target, const T * const begin, const T * const end)
43 {
44  if (begin == end)
45  return end;
46 
47  auto left = begin;
48  auto right = end - 1;
49 
50  while (left <= right)
51  {
52  auto mid = left + (right - left) / 2;
53 
54  if (*mid == target)
55  return mid;
56  else if (*mid < target)
57  left = mid + 1;
58  else
59  right = mid - 1;
60  }
61 
62  return end;
63 }
64 
73 KOKKOS_INLINE_FUNCTION void
74 choleskySolve(Real * const A, Real * const x, Real * const b, const unsigned int n)
75 {
76  for (unsigned int i = 0; i < n; ++i)
77  {
78  for (unsigned int j = 0; j <= i; ++j)
79  {
80  Real sum = A[j + n * i];
81 
82  for (unsigned int k = 0; k < j; ++k)
83  sum -= A[k + n * i] * A[k + n * j];
84 
85  if (i == j)
86  A[j + n * i] = ::Kokkos::sqrt(sum);
87  else
88  A[j + n * i] = sum / A[j + n * j];
89  }
90  }
91 
92  for (unsigned int i = 0; i < n; ++i)
93  {
94  Real sum = b[i];
95 
96  for (unsigned int j = 0; j < i; ++j)
97  sum -= A[j + n * i] * b[j];
98 
99  b[i] = sum / A[i + n * i];
100  }
101 
102  for (int i = n - 1; i >= 0; --i)
103  {
104  Real sum = b[i];
105 
106  for (unsigned int j = i + 1; j < n; ++j)
107  sum -= A[i + n * j] * b[j];
108 
109  x[i] = sum / A[i + n * i];
110  }
111 }
112 
113 } // namespace Utils
114 } // namespace Kokkos
115 } // namespace Moose
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition: KokkosUtils.h:42
KOKKOS_INLINE_FUNCTION void choleskySolve(Real *const A, Real *const x, Real *const b, const unsigned int n)
Perform an in-place linear solve using Cholesky decomposition Matrix and right-hand-side vector are m...
Definition: KokkosUtils.h:74
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
KOKKOS_INLINE_FUNCTION T sign(T x)
Returns the sign of a value.
Definition: KokkosUtils.h:28
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...