61 const std::string &command =
"") {
62 parseParams(paramsPath);
63 selectCommand(command, paramsPath);
64 scratchpadBo = run.get_ctrl_scratchpad_bo();
65 boMap = scratchpadBo.map<uint32_t *>();
66 if (scratchpadBo.size() < scratchpadSizeBytes)
67 throw std::runtime_error(
"ParameterScratchpad: BO size (" +
68 std::to_string(scratchpadBo.size()) +
69 ") < required scratchpad size (" +
70 std::to_string(scratchpadSizeBytes) +
")");
80 const std::string ¶msPath,
81 const std::string &command =
"")
83 parseParams(paramsPath);
84 selectCommand(command, paramsPath);
85 if (bufferSizeBytes < scratchpadSizeBytes)
86 throw std::runtime_error(
"ParameterScratchpad: buffer size (" +
87 std::to_string(bufferSizeBytes) +
88 ") < required scratchpad size (" +
89 std::to_string(scratchpadSizeBytes) +
")");
98 void writeBytes(
const std::string &name,
const void *data,
size_t len) {
100 std::memcpy(&bits, data, std::min(len,
sizeof(bits)));
113 void writeBits(
const std::string &name, uint32_t bits) {
114 unsigned index = memberSlot(name);
115 if (
auto multiple = offsetMultiples.find(name);
116 multiple != offsetMultiples.end() &&
117 static_cast<int32_t
>(bits) % multiple->second)
118 throw std::invalid_argument(
119 "ParameterScratchpad: value " +
120 std::to_string(
static_cast<int32_t
>(bits)) +
" of '" + name +
121 "' is not a multiple of " + std::to_string(multiple->second) +
122 " elements, which the DMA address it offsets needs to stay "
124 if (
auto range = ranges.find(name); range != ranges.end()) {
125 int32_t value =
static_cast<int32_t
>(bits);
126 if (value < range->second.first || value > range->second.second)
127 throw std::invalid_argument(
128 "ParameterScratchpad: value " + std::to_string(value) +
" of '" +
129 name +
"' is outside [" + std::to_string(range->second.first) +
130 ", " + std::to_string(range->second.second) +
"], " +
131 (kinds.at(name) ==
"repeat"
132 ?
"its task's largest repeat count"
133 :
"which keeps its DMA transfers within their buffers"));
134 values[name] = value;
136 boMap[index] = bits << kindShift(kinds.at(name));
147 template <
typename T>
148 void write(
const std::string &name, T value) {
149 static_assert(
sizeof(T) <= 4,
"Parameter values must be at most 32 bits");
151 std::memcpy(&bits, &value,
sizeof(T));
159 for (
const JointBound &bound : jointBounds) {
160 int64_t offset = values.at(bound.offset);
161 int64_t length = values.at(bound.length);
162 int64_t end = offset + bound.lengthStep * length;
164 throw std::invalid_argument(
165 "ParameterScratchpad: '" + bound.offset +
166 "' = " + std::to_string(offset) +
" plus " +
167 std::to_string(bound.lengthStep) +
" * '" + bound.length +
168 "' = " + std::to_string(length) +
" is " + std::to_string(end) +
169 ", above " + std::to_string(bound.max) +
170 ", so a DMA transfer using both runs past its buffer");
174#if TEST_UTILS_USE_XRT
179 scratchpadBo.sync(XCL_BO_SYNC_BO_TO_DEVICE);
184 std::vector<std::string>
names()
const {
185 return std::vector<std::string>(members.begin(), members.end());
199 unsigned index = memberSlot(name);
200 const std::string &kind = kinds.at(name);
201 return {index, kindShift(kind), kind, types.at(name)};
206 static unsigned slotCount(
const std::string ¶msPath) {
207 std::ifstream file(paramsPath);
208 unsigned numParams = 0, numSlots = 0;
209 if (!(file >> numParams >> numSlots))
210 throw std::runtime_error(
211 "ParameterScratchpad: missing parameter and slot counts in '" +
217 uint32_t
read(
const std::string &name)
const {
218 return boMap[memberSlot(name)];
222#if TEST_UTILS_USE_XRT
223 xrt::bo scratchpadBo;
225 uint32_t *boMap =
nullptr;
226 size_t scratchpadSizeBytes = 0;
227 std::unordered_map<std::string, uint8_t> paramMap;
228 std::unordered_set<std::string> unslotted;
230 std::unordered_map<std::string, std::string> kinds, types;
231 std::unordered_map<std::string, std::pair<int32_t, int32_t>> ranges;
232 std::unordered_map<std::string, int32_t> values;
235 std::unordered_map<std::string, int32_t> offsetMultiples;
237 std::string length, offset;
238 int64_t lengthStep, max;
240 std::vector<JointBound> jointBounds;
242 std::unordered_map<std::string, std::unordered_set<std::string>> commands;
243 std::vector<std::string> commandNames;
245 std::unordered_set<std::string> members;
250 static unsigned kindShift(
const std::string &kind) {
251 return kind ==
"core" ? 2 : kind ==
"repeat" ? 16 : 0;
256 unsigned memberSlot(
const std::string &name)
const {
257 if (unslotted.count(name))
258 throw std::runtime_error(
"ParameterScratchpad: no command reads "
260 name +
"', so it has no slot");
261 auto it = paramMap.find(name);
262 if (it == paramMap.end())
263 throw std::runtime_error(
"ParameterScratchpad: unknown parameter '" +
265 if (!members.count(name))
266 throw std::runtime_error(
"ParameterScratchpad: command '" + command +
267 "' does not read parameter '" + name +
268 "', whose slot another of its parameters may "
273 void selectCommand(
const std::string &name,
const std::string &path) {
276 if (commandNames.empty())
277 throw std::runtime_error(
"ParameterScratchpad: '" + path +
278 "' lists no command to hold parameters for");
279 if (commandNames.size() > 1) {
280 std::string available;
281 for (
const std::string &entry : commandNames)
282 available += (available.empty() ?
"" :
", ") + entry;
283 throw std::runtime_error(
284 "ParameterScratchpad: '" + path +
"' lists " +
285 std::to_string(commandNames.size()) +
286 " commands, whose parameters may share slots: name the one this "
287 "scratchpad is for, of " +
290 command = commandNames.front();
292 auto it = commands.find(command);
293 if (it == commands.end())
294 throw std::runtime_error(
"ParameterScratchpad: '" + path +
295 "' has no command '" + name +
"'");
296 members = it->second;
300 for (
size_t i = 0; i < scratchpadSizeBytes / 4; i++) {
305 void parseParams(
const std::string &path) {
306 std::ifstream file(path);
307 if (!file.is_open()) {
308 throw std::runtime_error(
"ParameterScratchpad: cannot open '" + path +
331 unsigned numParams = 0, numSlots = 0;
332 file >> numParams >> numSlots;
334 throw std::runtime_error(
335 "ParameterScratchpad: missing parameter and slot counts in '" + path +
337 scratchpadSizeBytes = numSlots * 4;
339 for (
unsigned i = 0; i < numParams; i++) {
340 std::string name,
slot, type, kind, min, max;
341 file >> name >>
slot >> type >> kind >> min >> max;
343 throw std::runtime_error(
"ParameterScratchpad: malformed entry " +
344 std::to_string(i) +
" in '" + path +
"'");
345 if (paramMap.count(name) || unslotted.count(name))
346 throw std::runtime_error(
347 "ParameterScratchpad: duplicate parameter name '" + name +
"'");
349 unslotted.insert(name);
351 unsigned long idx = 0;
353 idx = std::stoul(
slot);
354 }
catch (
const std::logic_error &) {
355 throw std::runtime_error(
"ParameterScratchpad: malformed slot '" +
356 slot +
"' for parameter '" + name +
357 "' in '" + path +
"'");
360 throw std::runtime_error(
"ParameterScratchpad: state_table_idx " +
361 std::to_string(idx) +
" for '" + name +
362 "' is past the " + std::to_string(numSlots) +
363 " slots of '" + path +
"'");
364 paramMap[name] =
static_cast<uint8_t
>(idx);
366 if (kind !=
"core" && kind !=
"addr" && kind !=
"repeat")
367 throw std::runtime_error(
"ParameterScratchpad: invalid kind '" + kind +
368 "' for parameter '" + name +
"'");
373 ranges[name] = {std::stoi(min), std::stoi(max)};
374 }
catch (
const std::logic_error &) {
375 throw std::runtime_error(
"ParameterScratchpad: malformed range '" +
376 min +
" " + max +
"' for parameter '" +
377 name +
"' in '" + path +
"'");
383 unsigned numJointBounds = 0;
384 file >> numJointBounds;
386 throw std::runtime_error(
387 "ParameterScratchpad: missing joint bound count in '" + path +
"'");
388 for (
unsigned i = 0; i < numJointBounds; i++) {
390 file >> bound.length >> bound.offset >> bound.lengthStep >> bound.max;
392 throw std::runtime_error(
"ParameterScratchpad: malformed joint bound " +
393 std::to_string(i) +
" in '" + path +
"'");
394 for (
const std::string &name : {bound.length, bound.offset})
395 if (!ranges.count(name))
396 throw std::runtime_error(
"ParameterScratchpad: joint bound " +
397 std::to_string(i) +
" names '" + name +
398 "', which has no range, in '" + path +
"'");
399 jointBounds.push_back(bound);
402 unsigned numCommands = 0;
405 throw std::runtime_error(
406 "ParameterScratchpad: missing command count in '" + path +
"'");
407 for (
unsigned i = 0; i < numCommands; i++) {
409 unsigned numRead = 0;
410 file >> name >> numRead;
411 commandNames.push_back(name);
412 std::unordered_set<std::string> &reads = commands[name];
413 for (
unsigned j = 0; j < numRead; j++) {
416 if (file && !paramMap.count(param))
417 throw std::runtime_error(
"ParameterScratchpad: command '" + name +
418 "' names unknown parameter '" + param +
419 "' in '" + path +
"'");
423 throw std::runtime_error(
"ParameterScratchpad: malformed command " +
424 std::to_string(i) +
" in '" + path +
"'");
427 unsigned numMultiples = 0;
428 if (!(file >> numMultiples))
430 for (
unsigned i = 0; i < numMultiples; i++) {
432 int32_t multiple = 0;
433 file >> name >> multiple;
434 if (!file || multiple < 1)
435 throw std::runtime_error(
"ParameterScratchpad: malformed offset "
437 std::to_string(i) +
" in '" + path +
"'");
438 if (!paramMap.count(name))
439 throw std::runtime_error(
"ParameterScratchpad: offset multiple " +
443 name +
"' in '" + path +
"'");
444 offsetMultiples[name] = multiple;