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://mooseframework.inl.gov
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
69KOKKOS_INLINE_FUNCTION unsigned int
70packedLowerIndex(const unsigned int i, const unsigned int j)
71{
72 return i * (i + 1) / 2 + j;
73}
74
80KOKKOS_INLINE_FUNCTION void
81choleskyFactor(Real * const L, const unsigned int n)
82{
83 for (unsigned int i = 0; i < n; ++i)
84 {
85 for (unsigned int j = 0; j <= i; ++j)
86 {
87 Real sum = L[packedLowerIndex(i, j)];
88
89 for (unsigned int k = 0; k < j; ++k)
90 sum -= L[packedLowerIndex(i, k)] * L[packedLowerIndex(j, k)];
91
92 if (i == j)
93 L[packedLowerIndex(i, j)] = ::Kokkos::sqrt(sum);
94 else
95 L[packedLowerIndex(i, j)] = sum / L[packedLowerIndex(j, j)];
96 }
97 }
98}
99
109template <typename Vector>
110KOKKOS_INLINE_FUNCTION void
111choleskySubstitute(const Real * const L, Vector x, Vector b, const unsigned int n)
112{
113 for (unsigned int i = 0; i < n; ++i)
114 {
115 Real sum = b[i];
116
117 for (unsigned int j = 0; j < i; ++j)
118 sum -= L[packedLowerIndex(i, j)] * b[j];
119
120 b[i] = sum / L[packedLowerIndex(i, i)];
121 }
122
123 for (int i = n - 1; i >= 0; --i)
124 {
125 Real sum = b[i];
126
127 for (unsigned int j = i + 1; j < n; ++j)
128 sum -= L[packedLowerIndex(j, i)] * x[j];
129
130 x[i] = sum / L[packedLowerIndex(i, i)];
131 }
132}
133
134} // namespace Utils
135} // namespace Moose::Kokkos
The Kokkos wrapper class for PETSc vector.
KOKKOS_INLINE_FUNCTION void choleskyFactor(Real *const L, const unsigned int n)
Perform an in-place Cholesky decomposition of a symmetric positive definite matrix.
Definition KokkosUtils.h:81
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
KOKKOS_INLINE_FUNCTION unsigned int packedLowerIndex(const unsigned int i, const unsigned int j)
Get the index of entry (i, j), j <= i, of a lower-triangular matrix packed row by row.
Definition KokkosUtils.h:70
KOKKOS_INLINE_FUNCTION void choleskySubstitute(const Real *const L, Vector x, Vector b, const unsigned int n)
Solve a linear system with a Cholesky factor from choleskyFactor() The right-hand-side vector is modi...