Eigen  5.0.1
 
Loading...
Searching...
No Matches
SparseLU_kernel_bmod.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
5// Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef SPARSELU_KERNEL_BMOD_H
13#define SPARSELU_KERNEL_BMOD_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19namespace internal {
20
21template <int SegSizeAtCompileTime>
22struct LU_kernel_bmod {
36 template <typename BlockScalarVector, typename ScalarVector, typename IndexVector>
37 static EIGEN_DONT_INLINE void run(const Index segsize, BlockScalarVector& dense, ScalarVector& tempv,
38 ScalarVector& lusup, Index& luptr, const Index lda, const Index nrow,
39 IndexVector& lsub, const Index lptr, const Index no_zeros);
40};
41
42template <int SegSizeAtCompileTime>
43template <typename BlockScalarVector, typename ScalarVector, typename IndexVector>
44EIGEN_DONT_INLINE void LU_kernel_bmod<SegSizeAtCompileTime>::run(const Index segsize, BlockScalarVector& dense,
45 ScalarVector& tempv, ScalarVector& lusup, Index& luptr,
46 const Index lda, const Index nrow, IndexVector& lsub,
47 const Index lptr, const Index no_zeros) {
48 using Scalar = typename ScalarVector::Scalar;
49 // First, copy U[*,j] segment from dense(*) to tempv(*)
50 // The result of triangular solve is in tempv[*];
51 // The result of matrix-vector update is in dense[*]
52 Index isub = lptr + no_zeros;
53 Index i;
54 Index irow;
55 for (i = 0; i < ((SegSizeAtCompileTime == Dynamic) ? segsize : SegSizeAtCompileTime); i++) {
56 irow = lsub(isub);
57 tempv(i) = dense(irow);
58 ++isub;
59 }
60 // Dense triangular solve -- start effective triangle
61 luptr += lda * no_zeros + no_zeros;
62 // Form Eigen matrix and vector
63 Map<Matrix<Scalar, SegSizeAtCompileTime, SegSizeAtCompileTime, ColMajor>, 0, OuterStride<> > A(
64 &(lusup.data()[luptr]), segsize, segsize, OuterStride<>(lda));
65 Map<Matrix<Scalar, SegSizeAtCompileTime, 1> > u(tempv.data(), segsize);
66
67 u = A.template triangularView<UnitLower>().solve(u);
68
69 // Dense matrix-vector product y <-- B*x
70 luptr += segsize;
71 const Index PacketSize = internal::packet_traits<Scalar>::size;
72 Index ldl = internal::first_multiple(nrow, PacketSize);
73 Map<Matrix<Scalar, Dynamic, SegSizeAtCompileTime, ColMajor>, 0, OuterStride<> > B(&(lusup.data()[luptr]), nrow,
74 segsize, OuterStride<>(lda));
75 Index aligned_offset = internal::first_default_aligned(tempv.data() + segsize, PacketSize);
76 Index aligned_with_B_offset = (PacketSize - internal::first_default_aligned(B.data(), PacketSize)) % PacketSize;
77 Map<Matrix<Scalar, Dynamic, 1>, 0, OuterStride<> > l(tempv.data() + segsize + aligned_offset + aligned_with_B_offset,
78 nrow, OuterStride<>(ldl));
79
80 l.noalias() = B * u;
81
82 // Scatter tempv[] into SPA dense[] as a temporary storage
83 isub = lptr + no_zeros;
84 for (i = 0; i < ((SegSizeAtCompileTime == Dynamic) ? segsize : SegSizeAtCompileTime); i++) {
85 irow = lsub(isub++);
86 dense(irow) = tempv(i);
87 }
88
89 // Scatter l into SPA dense[]
90 for (i = sparse_scatter_sub_packets(dense.data(), lsub.data() + isub, l); i < nrow; i++) {
91 irow = lsub(isub + i);
92 dense(irow) -= l(i);
93 }
94}
95
96template <>
97struct LU_kernel_bmod<1> {
98 template <typename BlockScalarVector, typename ScalarVector, typename IndexVector>
99 static EIGEN_DONT_INLINE void run(const Index /*segsize*/, BlockScalarVector& dense, ScalarVector& /*tempv*/,
100 ScalarVector& lusup, Index& luptr, const Index lda, const Index nrow,
101 IndexVector& lsub, const Index lptr, const Index no_zeros);
102};
103
104template <typename BlockScalarVector, typename ScalarVector, typename IndexVector>
105EIGEN_DONT_INLINE void LU_kernel_bmod<1>::run(const Index /*segsize*/, BlockScalarVector& dense,
106 ScalarVector& /*tempv*/, ScalarVector& lusup, Index& luptr,
107 const Index lda, const Index nrow, IndexVector& lsub, const Index lptr,
108 const Index no_zeros) {
109 using Scalar = typename ScalarVector::Scalar;
110 using StorageIndex = typename IndexVector::Scalar;
111 Scalar f = dense(lsub(lptr + no_zeros));
112 luptr += lda * no_zeros + no_zeros + 1;
113 const Scalar* a(lusup.data() + luptr);
114 const StorageIndex* irow(lsub.data() + lptr + no_zeros + 1);
115 Index i = 0;
116 for (; i + 1 < nrow; i += 2) {
117 Index i0 = *(irow++);
118 Index i1 = *(irow++);
119 Scalar a0 = *(a++);
120 Scalar a1 = *(a++);
121 Scalar d0 = dense.coeff(i0);
122 Scalar d1 = dense.coeff(i1);
123 d0 -= f * a0;
124 d1 -= f * a1;
125 dense.coeffRef(i0) = d0;
126 dense.coeffRef(i1) = d1;
127 }
128 if (i < nrow) dense.coeffRef(*(irow++)) -= f * *(a++);
129}
130
131} // end namespace internal
132
133} // end namespace Eigen
134#endif // SPARSELU_KERNEL_BMOD_H