FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
batched_vector_apply.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
9#include <kernel/util/memory_arbiter.hpp>
10#include <kernel/util/omp_util.hpp>
11#include <kernel/backend.hpp>
12
13
14
15namespace FEAT::LAFEM::Arch
16{
18 {
19 // free scratch space of batched kernel
20 static void free();
21 };
22
23
24 // The local dot-product results are passed as a Memory::Arbiter (rather than a raw host pointer), so
25 // the batched kernels acquire the view they need themselves and the arbiter transparently mediates
26 // the host<->device transfers and the on-device accumulation across meta-vector components. The
27 // arbiter must already hold the (zeroed or partially accumulated) values that are added upon.
29 {
30 template<typename DT_, typename DTT_>
31 static void exec_generic(Memory::Arbiter& local_dots, const Memory::Arbiter& target, Index dot_size, Memory::Arbiter const* const* others, Index num_dots)
32 {
33 // the generic kernel operates on main memory, so it acquires main-memory views itself
35 DT_* dots_ptr = dots_view.get_w();
37 const DTT_* target_ptr = target_view.get_r();
38
39 for(Index l = 0; l < num_dots; ++l)
40 {
42 const DTT_* other_ptr = other_view.get_r();
43 DTT_ loc_v = DTT_(0);
44 FEAT_PRAGMA_OMP(parallel for reduction(+:loc_v))
45 for(Index k = 0; k < dot_size; ++k)
46 {
47 loc_v += DT_(target_ptr[k] * other_ptr[k]);
48 }
49 dots_ptr[l] += loc_v;
50 }
51 }
52
53 template<typename DT_, typename DTT_>
54 static void exec_cuda(Memory::Arbiter& local_dots, const Memory::Arbiter& target, Index dot_size, Memory::Arbiter const * const * others, Index num_dots);
55
56 template<typename DT_, typename DTT_>
57 static void exec(Memory::Arbiter& local_dots, const Memory::Arbiter& target, Index dot_size, Memory::Arbiter const * const * others, Index num_dots)
58 {
60 {
62 #ifdef FEAT_HAVE_CUDA
63 exec_cuda<DT_, DTT_>(local_dots, target, dot_size, others, num_dots);
64 #else
65 XABORTM("LAFEM::ARCH::BatchedDotProduct::exec: CUDA backend not available");
66 #endif
67 break;
68 default:
69 exec_generic<DT_, DTT_>(local_dots, target, dot_size, others, num_dots);
70 break;
71 }
72 }
73
74 };
75
76
78 {
79
80 template<typename DT_, typename DTT_>
81 static inline void exec_generic(Memory::Arbiter& local_dots, const Memory::Arbiter& target, Index dot_size, Memory::Arbiter const * const * others, const Memory::Arbiter& freqs, Index num_dots)
82 {
83 // the generic kernel operates on main memory, so it acquires main-memory views itself
85 DT_* dots_ptr = dots_view.get_w();
87 const DTT_* target_ptr = target_view.get_r();
89 const DTT_* freqs_ptr = freqs_view.get_r();
90
91 for(Index l = 0; l < num_dots; ++l)
92 {
94 const DTT_* other_ptr = other_view.get_r();
95 DTT_ loc_v = DTT_(0);
96 FEAT_PRAGMA_OMP(parallel for reduction(+:loc_v))
97 for(Index k = 0; k < dot_size; ++k)
98 {
99 loc_v += DT_(target_ptr[k] * freqs_ptr[k] * other_ptr[k]);
100 }
101 dots_ptr[l] += loc_v;
102 }
103 }
104
105 template<typename DT_, typename DTT_>
106 static void exec_cuda(Memory::Arbiter& local_dots, const Memory::Arbiter& target, Index dot_size, Memory::Arbiter const * const * others, const Memory::Arbiter& freqs, Index num_dots);
107
108 template<typename DT_, typename DTT_>
109 static void exec(Memory::Arbiter& local_dots, const Memory::Arbiter& target, Index dot_size, Memory::Arbiter const * const * others, const Memory::Arbiter& freqs, Index num_dots)
110 {
112 {
114 #ifdef FEAT_HAVE_CUDA
115 exec_cuda<DT_, DTT_>(local_dots, target, dot_size, others, freqs, num_dots);
116 #else
117 XABORTM("LAFEM::ARCH::BatchedTripleDotProduct::exec: CUDA backend not available");
118 #endif
119 break;
120 default:
121 exec_generic<DT_, DTT_>(local_dots, target, dot_size, others, freqs, num_dots);
122 break;
123 }
124 }
125 };
126
127
128
130 {
131 template<typename DT_>
132 static void exec_generic(Memory::Arbiter& target, Index vec_size, Memory::Arbiter const * const * others, DT_ const * const local_scales, Index num_axpy, DT_ alpha)
133 {
134 // the generic kernel operates on main memory, so it acquires its main-memory views itself
136 DT_* target_ptr = target_view.get_w();
137
138 for(Index l = 0; l < num_axpy; ++l)
139 {
140 const Memory::TypedView<DT_> other_view(others[l]->view(Memory::Location::main, Memory::Access::read));
141 const DT_* other_ptr = other_view.get_r();
142 FEAT_PRAGMA_OMP(parallel for)
143 for(Index k = 0; k < vec_size; ++k)
144 {
145 target_ptr[k] += DT_(alpha * local_scales[l]) * other_ptr[k];
146 }
147 }
148 }
149
150 template<typename DT_>
151 static void exec_cuda(Memory::Arbiter& target, Index vec_size, Memory::Arbiter const * const * others, DT_ const * const local_scales, Index num_axpy, DT_ alpha);
152
153 template<typename DT_>
154 static void exec(Memory::Arbiter& target, Index vec_size, Memory::Arbiter const * const * others, DT_ const * const local_scales, Index num_axpy, DT_ alpha)
155 {
157 {
159 #ifdef FEAT_HAVE_CUDA
160 exec_cuda<DT_>(target, vec_size, others, local_scales, num_axpy, alpha);
161 #else
162 XABORTM("LAFEM::ARCH::CumulativeAxpy::exec: CUDA backend not available");
163 #endif
164 break;
165 default:
166 exec_generic<DT_>(target, vec_size, others, local_scales, num_axpy, alpha);
167 break;
168 }
169 }
170 };
171
172}
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
FEAT Kernel base header.
static PreferredBackend get_preferred_backend()
get current preferred backend
Definition: backend.cpp:32
Memory arbiter class.
View view(Location mem_loc, Access access)
Maps a raw view to a memory block.
Typed memory view class.
const T_ * get_r() const
Returns a const typed pointer to the array that grants read access.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
@ read_write
read-write access
@ read
read-only access
std::uint64_t Index
Index data type.