FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
matvecmult_banded_dense_generic.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8#ifndef KERNEL_LAFEM_ARCH_MATVECMULT_BANDED_DENSE_HPP
9#error "Do not include this implementation-only header file directly!"
10#endif
11
12#include <kernel/util/math.hpp>
13
14namespace FEAT::LAFEM::Arch
15{
16 namespace Intern
17 {
18 template <Index Start, Index End, Index Step = 1>
20 {
21 template <typename ... Params>
22 static FORCE_INLINE void step(void f(Index, Params ...), Params... parameters)
23 {
24 f(Start, parameters ...);
26 }
27 };
28
29 template <Index Start, Index Step>
30 struct LoopUnroller<Start, Start, Step>
31 {
32 template <typename ... Params>
33 static FORCE_INLINE void step(void (Index, Params ...), Params ...)
34 {
35 }
36 };
37
38 namespace ApplyBanded
39 {
40 template <typename IT_>
41 inline Index start_offset(const Index i, const IT_ * const offsets,
42 const Index rows, const Index cols, const Index noo)
43 {
44 if (i == Index(-1))
45 {
46 return rows;
47 }
48 else if (i == noo)
49 {
50 return Index(0);
51 }
52 else
53 {
54 return Math::max(cols + Index(1), rows + cols - Index(offsets[i])) - cols - Index(1);
55 }
56 }
57
58 template <typename IT_>
59 inline Index end_offset(const Index i, const IT_ * const offsets,
60 const Index rows, const Index cols, const Index noo)
61 {
62 if (i == Index (-1))
63 {
64 return rows - 1;
65 }
66 else if (i == noo)
67 {
68 return Index(-1);
69 }
70 else
71 {
72 return Math::min(rows, cols + rows - Index(offsets[i]) - Index(1)) - Index(1);
73 }
74 }
75
76 template <typename DT_, typename IT_>
77 FORCE_INLINE void single_matrix_entry(Index k, DT_ * const res, const DT_ * const val,
78 const IT_ * const offsets, const DT_ * const x, Index rows)
79 {
80 *res += val[k * rows] * x[offsets[k]];
81 }
82
83 template <typename DT_, typename IT_, Index noo, Index i, Index j>
85 {
86 void f(DT_ * const r, const DT_ alpha,
87 const DT_ * const val, const IT_ * const offsets,
88 const DT_ * const x, const Index rows, const Index cols)
89 {
90 Index start(Math::max(Intern::ApplyBanded::start_offset(j-1, offsets, rows, cols, noo),
91 Intern::ApplyBanded::end_offset(i-1, offsets, rows, cols, noo) + 1));
92 Index end (Math::min(Intern::ApplyBanded::start_offset(j-2, offsets, rows, cols, noo),
93 Intern::ApplyBanded::end_offset(i-2, offsets, rows, cols, noo) + 1));
94
95 FEAT_PRAGMA_IVDEP
96 for (Index l = start; l < end; ++l)
97 {
98 DT_ tmp(0);
99 Intern::LoopUnroller<0, i-j>::step(Intern::ApplyBanded::single_matrix_entry, &tmp, val + (j-1) * rows + l,
100 offsets + (j-1), x + l + 1 - rows, rows);
101 r[l] += alpha * tmp;
102 }
103
104 Iteration_Left<DT_, IT_, noo, i, j-1>::f(r, alpha, val, offsets, x, rows, cols);
105 }
106 };
107
108 template <typename DT_, typename IT_, Index noo, Index i>
109 struct Iteration_Left<DT_, IT_, noo, i, 0>
110 {
111 void f(DT_ * const /*r*/, const DT_ /*alpha*/,
112 const DT_ * const /*val*/, const IT_ * const /*offsets*/,
113 const DT_ * const /*x*/, const Index /*rows*/, const Index /*cols*/)
114 {
115 }
116 };
117
118 /********************************************************************/
119
120 template <typename DT_, typename IT_, Index noo, Index i>
122 {
123 void f(DT_ * const r, const DT_ alpha,
124 const DT_ * const val, const IT_ * const offsets,
125 const DT_ * const x, const Index rows, const Index cols)
126 {
127 Iteration_Left<DT_, IT_, noo, i, i-1>::f(r, alpha, val, offsets, x, rows, cols);
128 Iteration_Right<DT_, IT_, noo, i-1>::f(r, alpha, val, offsets, x, rows, cols);
129 }
130 };
131
132 template <typename DT_, typename IT_, Index noo>
133 struct Iteration_Right<DT_, IT_, noo, 0>
134 {
135 void f(DT_ * const /*r*/, const DT_ /*alpha*/,
136 const DT_ * const /*val*/, const IT_ * const /*offsets*/,
137 const DT_ * const /*x*/, const Index /*rows*/, const Index /*cols*/)
138 {
139 }
140 };
141
142 /********************************************************************/
143
144 template <typename DT_, typename IT_>
145 void apply_banded_generic(DT_ * r, const DT_ alpha, const DT_ * const x,
146 const DT_ * const val, const IT_ * const offsets,
147 const Index bands, const Index rows, const Index cols)
148 {
149 // Search first offset of the upper triangular matrix
150 Index k(0);
151 while (k < bands && offsets[k] + 1 < rows)
152 {
153 ++k;
154 }
155
156 // iteration over all offsets of the lower triangular matrix
157 for (Index i(k + 1); i > 0;)
158 {
159 --i;
160
161 // iteration over all offsets of the upper triangular matrix
162 for (Index j(bands + 1); j > 0;)
163 {
164 --j;
165
166 // iteration over all rows which contain the offsets between offset i and offset j
167 const Index start(Math::max(Intern::ApplyBanded::start_offset( i, offsets, rows, cols, bands),
168 Intern::ApplyBanded::end_offset ( j, offsets, rows, cols, bands) + 1));
169 const Index stop (Math::min(Intern::ApplyBanded::start_offset(i-1, offsets, rows, cols, bands),
170 Intern::ApplyBanded::end_offset (j-1, offsets, rows, cols, bands) + 1));
171 for (Index l(start); l < stop; ++l)
172 {
173 DT_ s(0);
174 for (Index a(i); a < j; ++a)
175 {
176 s += val[a * rows + l] * x[l + offsets[a] + 1 - rows];
177 }
178 r[l] += alpha * s;
179 }
180 }
181 }
182 }
183 } // namespace ApplyBanded
184 } // namespace Intern
185
186 template <typename DT_, typename IT_>
187 void MatVecMultBandedDense::exec_generic_impl(DT_ * r, const DT_ alpha, const DT_ * const x, const DT_ * const y,
188 const DT_ * const val, const IT_ * const offsets, const Index bands, const Index rows, const Index cols)
189 {
190 if(y == nullptr)
191 Memory::memset_main(r, 0, sizeof(DT_) * rows);
192 else if(r != y)
193 Memory::memcopy_main(r, y, sizeof(DT_) * rows);
194
195#ifdef FEAT_UNROLL_BANDED
196 switch (bands)
197 {
198 case 3:
199 Intern::ApplyBanded::Iteration_Right<DT_, IT_, 3, 4>::f(r, alpha, val, offsets, x, rows, cols);
200 break;
201 case 5:
202 Intern::ApplyBanded::Iteration_Right<DT_, IT_, 5, 6>::f(r, alpha, val, offsets, x, rows, cols);
203 break;
204 case 9:
205 Intern::ApplyBanded::Iteration_Right<DT_, IT_, 9, 10>::f(r, alpha, val, offsets, x, rows, cols);
206 break;
207 case 25:
208 Intern::ApplyBanded::Iteration_Right<DT_, IT_, 25, 26>::f(r, alpha, val, offsets, x, rows, cols);
209 break;
210 default:
211#ifdef DEBUG
213 std::cout << "Warning: Apply not optimized for " << bands << " offsets!\n";
214#endif
215 Intern::ApplyBanded::apply_banded_generic(r, alpha, x, val, offsets, bands, rows, cols);
216 }
217#else
218 Intern::ApplyBanded::apply_banded_generic(r, alpha, x, val, offsets, bands, rows, cols);
219#endif //FEAT_UNROLL_BANDED
220 }
221} // namespace FEAT::LAFEM::Arch
T_ min(T_ a, T_ b)
Returns the minimum of two values.
Definition: math.hpp:123
T_ max(T_ a, T_ b)
Returns the maximum of two values.
Definition: math.hpp:137
void memcopy_main(void *dst, const void *src, std::size_t bytes)
Copies a region of a main memory block into another main memory block.
void memset_main(void *ptr, int value, std::size_t bytes)
Formats all bytes of a memory block in main memory to a given value.
std::uint64_t Index
Index data type.