https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
14namespace Moose::Kokkos
15{
16namespace Utils
17{
18
24template <typename T>
25KOKKOS_INLINE_FUNCTION T
26sign(T x)
27{
28 return x >= 0.0 ? 1.0 : -1.0;
29}
30
38template <typename T>
39KOKKOS_INLINE_FUNCTION const T *
40find(const T & target, const T * const begin, const T * const end)
41{
42 if (begin == end)
43 return end;
44
45 auto left = begin;
46 auto right = end - 1;
47
48 while (left <= right)
49 {
50 auto mid = left + (right - left) / 2;
51
52 if (*mid == target)
53 return mid;
54 else if (*mid < target)
55 left = mid + 1;
56 else
57 right = mid - 1;
58 }
59
60 return end;
61}
62
71KOKKOS_INLINE_FUNCTION void
72choleskySolve(Real * const A, Real * const x, Real * const b, const unsigned int n)
73{
74 for (unsigned int i = 0; i < n; ++i)
75 {
76 for (unsigned int j = 0; j <= i; ++j)
77 {
78 Real sum = A[j + n * i];
79
80 for (unsigned int k = 0; k < j; ++k)
81 sum -= A[k + n * i] * A[k + n * j];
82
83 if (i == j)
84 A[j + n * i] = ::Kokkos::sqrt(sum);
85 else
86 A[j + n * i] = sum / A[j + n * j];
87 }
88 }
89
90 for (unsigned int i = 0; i < n; ++i)
91 {
92 Real sum = b[i];
93
94 for (unsigned int j = 0; j < i; ++j)
95 sum -= A[j + n * i] * b[j];
96
97 b[i] = sum / A[i + n * i];
98 }
99
100 for (int i = n - 1; i >= 0; --i)
101 {
102 Real sum = b[i];
103
104 for (unsigned int j = i + 1; j < n; ++j)
105 sum -= A[i + n * j] * x[j];
106
107 x[i] = sum / A[i + n * i];
108 }
109}
110
111} // namespace Utils
112} // namespace Moose::Kokkos
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:72
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:40
KOKKOS_INLINE_FUNCTION T sign(T x)
Returns the sign of a value.
Definition KokkosUtils.h:26