forked from altera-fpga/hls-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_utils.hpp
234 lines (206 loc) · 8.24 KB
/
memory_utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef __MEMORY_UTILS_HPP__
#define __MEMORY_UTILS_HPP__
#include <type_traits>
#include "metaprogramming_utils.hpp"
//
// The utilities in this file are used for converting streaming data to/from
// memory from/to a pipe.
//
namespace fpga_tools {
namespace detail {
//
// Helper to check if a SYCL pipe and pointer have the same base type
//
template <typename PipeT, typename PtrT>
struct pipe_and_pointer_have_same_base {
using PipeBaseT =
std::conditional_t<fpga_tools::has_subscript_v<PipeT>,
std::decay_t<decltype(std::declval<PipeT>()[0])>,
PipeT>;
using PtrBaseT = std::decay_t<decltype(std::declval<PtrT>()[0])>;
static constexpr bool value = std::is_same_v<PipeBaseT, PtrBaseT>;
};
template <typename PipeT, typename PtrT>
inline constexpr bool pipe_and_pointer_have_same_base_v =
pipe_and_pointer_have_same_base<PipeT, PtrT>::value;
//
// Streams data from 'in_ptr' into 'Pipe', 'elements_per_cycle' elements at a
// time
//
template <typename Pipe, int elements_per_cycle, typename PtrT>
void MemoryToPipeRemainder(PtrT in_ptr, size_t full_count,
size_t remainder_count) {
static_assert(fpga_tools::is_sycl_pipe_v<Pipe>);
using PipeT = decltype(Pipe::read());
static_assert(fpga_tools::has_subscript_v<PipeT>);
static_assert(fpga_tools::has_subscript_v<PtrT>);
static_assert(PipeT::size == elements_per_cycle);
static_assert(pipe_and_pointer_have_same_base_v<PipeT, PtrT>);
for (size_t i = 0; i < full_count; i++) {
PipeT pipe_data;
#pragma unroll
for (int j = 0; j < elements_per_cycle; j++) {
pipe_data[j] = in_ptr[i * elements_per_cycle + j];
}
Pipe::write(pipe_data);
}
PipeT pipe_data;
for (size_t i = 0; i < remainder_count; i++) {
pipe_data[i] = in_ptr[full_count * elements_per_cycle + i];
}
Pipe::write(pipe_data);
}
//
// Streams data from 'in_ptr' into 'Pipe', 'elements_per_cycle' elements at a
// time with the guarantee that 'elements_per_cycle' is a multiple of 'count'
//
template <typename Pipe, int elements_per_cycle, typename PtrT>
void MemoryToPipeNoRemainder(PtrT in_ptr, size_t count) {
static_assert(fpga_tools::is_sycl_pipe_v<Pipe>);
using PipeT = decltype(Pipe::read());
static_assert(fpga_tools::has_subscript_v<PipeT>);
static_assert(fpga_tools::has_subscript_v<PtrT>);
static_assert(PipeT::size == elements_per_cycle);
static_assert(pipe_and_pointer_have_same_base_v<PipeT, PtrT>);
for (size_t i = 0; i < count; i++) {
PipeT pipe_data;
#pragma unroll
for (int j = 0; j < elements_per_cycle; j++) {
pipe_data[j] = in_ptr[i * elements_per_cycle + j];
}
Pipe::write(pipe_data);
}
}
//
// Streams data from 'Pipe' to 'out_ptr', 'elements_per_cycle' elements at a
// time
//
template <typename Pipe, int elements_per_cycle, typename PtrT>
void PipeToMemoryRemainder(PtrT out_ptr, size_t full_count,
size_t remainder_count) {
static_assert(fpga_tools::is_sycl_pipe_v<Pipe>);
using PipeT = decltype(Pipe::read());
static_assert(fpga_tools::has_subscript_v<PipeT>);
static_assert(fpga_tools::has_subscript_v<PtrT>);
static_assert(PipeT::size == elements_per_cycle);
static_assert(pipe_and_pointer_have_same_base_v<PipeT, PtrT>);
for (size_t i = 0; i < full_count; i++) {
auto pipe_data = Pipe::read();
#pragma unroll
for (int j = 0; j < elements_per_cycle; j++) {
out_ptr[i * elements_per_cycle + j] = pipe_data[j];
}
}
auto pipe_data = Pipe::read();
for (size_t i = 0; i < remainder_count; i++) {
out_ptr[full_count * elements_per_cycle + i] = pipe_data[i];
}
}
//
// Streams data from 'Pipe' to 'out_ptr', 'elements_per_cycle' elements at a
// time with the guarantee that 'elements_per_cycle' is a multiple of 'count'
//
template <typename Pipe, int elements_per_cycle, typename PtrT>
void PipeToMemoryNoRemainder(PtrT out_ptr, size_t count) {
static_assert(fpga_tools::is_sycl_pipe_v<Pipe>);
using PipeT = decltype(Pipe::read());
static_assert(fpga_tools::has_subscript_v<PipeT>);
static_assert(fpga_tools::has_subscript_v<PtrT>);
static_assert(PipeT::size == elements_per_cycle);
static_assert(pipe_and_pointer_have_same_base_v<PipeT, PtrT>);
for (size_t i = 0; i < count; i++) {
auto pipe_data = Pipe::read();
#pragma unroll
for (int j = 0; j < elements_per_cycle; j++) {
out_ptr[i * elements_per_cycle + j] = pipe_data[j];
}
}
}
} // namespace detail
//
// Streams data from memory to a SYCL pipe 1 element a time
//
template <typename Pipe, typename PtrT>
void MemoryToPipe(PtrT in_ptr, size_t count) {
static_assert(fpga_tools::is_sycl_pipe_v<Pipe>);
using PipeT = decltype(Pipe::read());
static_assert(fpga_tools::has_subscript_v<PtrT>);
static_assert(detail::pipe_and_pointer_have_same_base_v<PipeT, PtrT>);
for (size_t i = 0; i < count; i++) {
Pipe::write(in_ptr[i]);
}
}
//
// Streams data from memory to a SYCL pipe 'elements_per_cycle' elements a time
//
template <typename Pipe, int elements_per_cycle, bool remainder, typename PtrT>
void MemoryToPipe(PtrT in_ptr, size_t count) {
if constexpr (!remainder) {
// user promises there is not remainder
detail::MemoryToPipeNoRemainder<Pipe, elements_per_cycle>(in_ptr, count);
} else {
// might have a remainder and it was not specified, so calculate it
auto full_count = (count / elements_per_cycle) * elements_per_cycle;
auto remainder_count = count % elements_per_cycle;
detail::MemoryToPipeRemainder<Pipe, elements_per_cycle>(in_ptr, full_count,
remainder_count);
}
}
//
// Streams data from memory to a SYCL pipe 'elements_per_cycle' elements a time
// In this version, the user has specified a the amount of remainder
//
template <typename Pipe, int elements_per_cycle, bool remainder, typename PtrT>
void MemoryToPipe(PtrT in_ptr, size_t full_count, size_t remainder_count) {
if constexpr (!remainder) {
// user promises there is not remainder
detail::MemoryToPipeNoRemainder<Pipe, elements_per_cycle>(in_ptr,
full_count);
} else {
// might have a remainder that was specified by the user
detail::MemoryToPipeRemainder<Pipe, elements_per_cycle>(in_ptr, full_count,
remainder_count);
}
}
//
// Streams data from a SYCL pipe to memory 1 element a time
//
template <typename Pipe, typename PtrT>
void PipeToMemory(PtrT out_ptr, size_t count) {
using PipeT = decltype(Pipe::read());
static_assert(fpga_tools::has_subscript_v<PtrT>);
static_assert(detail::pipe_and_pointer_have_same_base_v<PipeT, PtrT>);
for (size_t i = 0; i < count; i++) {
out_ptr[i] = Pipe::read();
}
}
//
// Streams data from a SYCL pipe to memory 'elements_per_cycle' elements a time
//
template <typename Pipe, int elements_per_cycle, bool remainder, typename PtrT>
void PipeToMemory(PtrT out_ptr, size_t count) {
if constexpr (!remainder) {
detail::PipeToMemoryNoRemainder<Pipe, elements_per_cycle>(out_ptr, count);
} else {
auto full_count = (count / elements_per_cycle) * elements_per_cycle;
auto remainder_count = count % elements_per_cycle;
detail::PipeToMemoryRemainder<Pipe, elements_per_cycle>(out_ptr, full_count,
remainder_count);
}
}
//
// Streams data from a SYCL pipe to memory 'elements_per_cycle' elements a time
// In this version, the user has specified a the amount of remainder
//
template <typename Pipe, int elements_per_cycle, bool remainder, typename PtrT>
void PipeToMemory(PtrT out_ptr, size_t full_count, size_t remainder_count) {
if constexpr (!remainder) {
detail::PipeToMemoryNoRemainder<Pipe, elements_per_cycle>(out_ptr,
full_count);
} else {
detail::PipeToMemoryRemainder<Pipe, elements_per_cycle>(out_ptr, full_count,
remainder_count);
}
}
} // namespace fpga_tools
#endif /* __MEMORY_UTILS_HPP__ */