SparseLU_kernel_bmod.h
Go to the documentation of this file.
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 
11 #ifndef SPARSELU_KERNEL_BMOD_H
12 #define SPARSELU_KERNEL_BMOD_H
13 
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 namespace internal {
18 
19 template <int SegSizeAtCompileTime> struct LU_kernel_bmod
20 {
34  template <typename BlockScalarVector, typename ScalarVector, typename IndexVector>
35  static EIGEN_DONT_INLINE void run(const Index segsize, BlockScalarVector& dense, ScalarVector& tempv, ScalarVector& lusup, Index& luptr, const Index lda,
36  const Index nrow, IndexVector& lsub, const Index lptr, const Index no_zeros);
37 };
38 
39 template <int SegSizeAtCompileTime>
40 template <typename BlockScalarVector, typename ScalarVector, typename IndexVector>
41 EIGEN_DONT_INLINE void LU_kernel_bmod<SegSizeAtCompileTime>::run(const Index segsize, BlockScalarVector& dense, ScalarVector& tempv, ScalarVector& lusup, Index& luptr, const Index lda,
42  const Index nrow, IndexVector& lsub, const Index lptr, const Index no_zeros)
43 {
44  typedef typename ScalarVector::Scalar Scalar;
45  // First, copy U[*,j] segment from dense(*) to tempv(*)
46  // The result of triangular solve is in tempv[*];
47  // The result of matric-vector update is in dense[*]
48  Index isub = lptr + no_zeros;
49  Index i;
50  Index irow;
51  for (i = 0; i < ((SegSizeAtCompileTime==Dynamic)?segsize:SegSizeAtCompileTime); i++)
52  {
53  irow = lsub(isub);
54  tempv(i) = dense(irow);
55  ++isub;
56  }
57  // Dense triangular solve -- start effective triangle
58  luptr += lda * no_zeros + no_zeros;
59  // Form Eigen matrix and vector
60  Map<Matrix<Scalar,SegSizeAtCompileTime,SegSizeAtCompileTime, ColMajor>, 0, OuterStride<> > A( &(lusup.data()[luptr]), segsize, segsize, OuterStride<>(lda) );
61  Map<Matrix<Scalar,SegSizeAtCompileTime,1> > u(tempv.data(), segsize);
62 
63  u = A.template triangularView<UnitLower>().solve(u);
64 
65  // Dense matrix-vector product y <-- B*x
66  luptr += segsize;
68  Index ldl = internal::first_multiple(nrow, PacketSize);
69  Map<Matrix<Scalar,Dynamic,SegSizeAtCompileTime, ColMajor>, 0, OuterStride<> > B( &(lusup.data()[luptr]), nrow, segsize, OuterStride<>(lda) );
70  Index aligned_offset = internal::first_default_aligned(tempv.data()+segsize, PacketSize);
71  Index aligned_with_B_offset = (PacketSize-internal::first_default_aligned(B.data(), PacketSize))%PacketSize;
72  Map<Matrix<Scalar,Dynamic,1>, 0, OuterStride<> > l(tempv.data()+segsize+aligned_offset+aligned_with_B_offset, nrow, OuterStride<>(ldl) );
73 
74  l.noalias() = B * u;
75 
76  // Scatter tempv[] into SPA dense[] as a temporary storage
77  isub = lptr + no_zeros;
78  for (i = 0; i < ((SegSizeAtCompileTime==Dynamic)?segsize:SegSizeAtCompileTime); i++)
79  {
80  irow = lsub(isub++);
81  dense(irow) = tempv(i);
82  }
83 
84  // Scatter l into SPA dense[]
85  for (i = 0; i < nrow; i++)
86  {
87  irow = lsub(isub++);
88  dense(irow) -= l(i);
89  }
90 }
91 
92 template <> struct LU_kernel_bmod<1>
93 {
94  template <typename BlockScalarVector, typename ScalarVector, typename IndexVector>
95  static EIGEN_DONT_INLINE void run(const Index /*segsize*/, BlockScalarVector& dense, ScalarVector& /*tempv*/, ScalarVector& lusup, Index& luptr,
96  const Index lda, const Index nrow, IndexVector& lsub, const Index lptr, const Index no_zeros);
97 };
98 
99 
100 template <typename BlockScalarVector, typename ScalarVector, typename IndexVector>
101 EIGEN_DONT_INLINE void LU_kernel_bmod<1>::run(const Index /*segsize*/, BlockScalarVector& dense, ScalarVector& /*tempv*/, ScalarVector& lusup, Index& luptr,
102  const Index lda, const Index nrow, IndexVector& lsub, const Index lptr, const Index no_zeros)
103 {
104  typedef typename ScalarVector::Scalar Scalar;
105  typedef typename IndexVector::Scalar StorageIndex;
106  Scalar f = dense(lsub(lptr + no_zeros));
107  luptr += lda * no_zeros + no_zeros + 1;
108  const Scalar* a(lusup.data() + luptr);
109  const StorageIndex* irow(lsub.data()+lptr + no_zeros + 1);
110  Index i = 0;
111  for (; i+1 < nrow; i+=2)
112  {
113  Index i0 = *(irow++);
114  Index i1 = *(irow++);
115  Scalar a0 = *(a++);
116  Scalar a1 = *(a++);
117  Scalar d0 = dense.coeff(i0);
118  Scalar d1 = dense.coeff(i1);
119  d0 -= f*a0;
120  d1 -= f*a1;
121  dense.coeffRef(i0) = d0;
122  dense.coeffRef(i1) = d1;
123  }
124  if(i<nrow)
125  dense.coeffRef(*(irow++)) -= f * *(a++);
126 }
127 
128 } // end namespace internal
129 
130 } // end namespace Eigen
131 #endif // SPARSELU_KERNEL_BMOD_H
MatrixXcf A
MatrixXf B
#define EIGEN_DONT_INLINE
Definition: Macros.h:844
Index first_multiple(Index size, Index base)
Definition: Memory.h:592
static Index first_default_aligned(const DenseBase< Derived > &m)
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24