49 const std::vector<uint32_t> &widths,
bool write,
53 const std::vector<uint32_t> &widths,
bool read,
56 const std::vector<uint32_t> &widths,
bool read,
bool write);
58 const std::vector<uint32_t> &widths,
59 uint32_t xferCount,
bool read,
bool write);
64 uint32_t xferCount,
bool read,
67 uint32_t addAmt, uint32_t numItems);
69 uint32_t addAmt, uint32_t numItems);
71 uint32_t xTrans, uint32_t yTrans,
74 uint32_t xTrans, uint32_t yTrans,
75 uint32_t numCoords,
size_t batchSizeLimit);
77 uint32_t xTrans, uint32_t yTrans,
83constexpr std::array<uint32_t, 5>
defaultWidths = {32, 64, 128, 256, 512};
96 const char *unit =
"B/s";
97 double value = bytesPerSec;
98 if (bytesPerSec >= 1e9) {
100 value = bytesPerSec / 1e9;
101 }
else if (bytesPerSec >= 1e6) {
103 value = bytesPerSec / 1e6;
104 }
else if (bytesPerSec >= 1e3) {
106 value = bytesPerSec / 1e3;
108 std::ostringstream oss;
109 oss.setf(std::ios::fixed);
111 oss << value <<
" " << unit;
117 const char *units[] = {
"B",
"KB",
"MB",
"GB",
"TB"};
118 double v = (double)bytes;
120 while (v >= 1024.0 && u < 4) {
124 std::ostringstream oss;
125 oss.setf(std::ios::fixed);
126 oss.precision(u == 0 ? 0 : 2);
127 oss << v <<
" " << units[u];
134 return std::to_string(us) +
" us";
135 double ms = us / 1000.0;
137 std::ostringstream oss;
138 oss.setf(std::ios::fixed);
139 oss.precision(ms < 10.0 ? 2 : (ms < 100.0 ? 1 : 0));
143 double sec = ms / 1000.0;
144 std::ostringstream oss;
145 oss.setf(std::ios::fixed);
146 oss.precision(sec < 10.0 ? 3 : 2);
155 void *ptr = _aligned_malloc(size, alignment);
157 throw std::bad_alloc();
160 void *ptr = std::aligned_alloc(alignment, size);
162 throw std::bad_alloc();
175int main(
int argc,
const char *argv[]) {
177 cli.description(
"Test an ESI system running the ESI tester image.");
178 cli.require_subcommand(1);
180 CLI::App *callback_test =
181 cli.add_subcommand(
"callback",
"initiate callback test");
182 uint32_t cb_iters = 1;
183 callback_test->add_option(
"-i,--iters", cb_iters,
184 "Number of iterations to run");
186 CLI::App *hostmemtestSub =
187 cli.add_subcommand(
"hostmem",
"Run the host memory test");
189 bool hmWrite =
false;
192 hostmemtestSub->add_flag(
"-w,--write", hmWrite,
193 "Enable host memory write test");
194 hostmemtestSub->add_flag(
"-r,--read", hmRead,
"Enable host memory read test");
195 hostmemtestSub->add_option(
196 "--widths", hostmemWidths,
199 CLI::App *dmatestSub = cli.add_subcommand(
"dma",
"Run the DMA test");
200 bool dmaRead =
false;
201 bool dmaWrite =
false;
203 dmatestSub->add_flag(
"-w,--write", dmaWrite,
"Enable dma write test");
204 dmatestSub->add_flag(
"-r,--read", dmaRead,
"Enable dma read test");
205 dmatestSub->add_option(
"--widths", dmaWidths,
209 CLI::App *bandwidthSub =
210 cli.add_subcommand(
"bandwidth",
"Run the bandwidth test");
211 uint32_t xferCount = 1000;
212 bandwidthSub->add_option(
"-c,--count", xferCount,
213 "Number of transfers to perform");
214 bool bandwidthRead =
false;
215 bool bandwidthWrite =
false;
218 bandwidthSub->add_option(
"--widths", bandwidthWidths,
219 "Width of the transfers to perform (default: " +
221 bandwidthSub->add_flag(
"-w,--write", bandwidthWrite,
222 "Enable bandwidth write");
223 bandwidthSub->add_flag(
"-r,--read", bandwidthRead,
"Enable bandwidth read");
225 CLI::App *hostmembwSub =
226 cli.add_subcommand(
"hostmembw",
"Run the host memory bandwidth test");
227 uint32_t hmBwCount = 1000;
228 bool hmBwRead =
false;
229 bool hmBwWrite =
false;
231 hostmembwSub->add_option(
"-c,--count", hmBwCount,
232 "Number of hostmem transfers");
233 hostmembwSub->add_option(
234 "--widths", hmBwWidths,
236 hostmembwSub->add_flag(
"-w,--write", hmBwWrite,
237 "Measure hostmem write bandwidth");
238 hostmembwSub->add_flag(
"-r,--read", hmBwRead,
239 "Measure hostmem read bandwidth");
241 CLI::App *loopbackSub =
242 cli.add_subcommand(
"loopback",
"Test LoopbackInOutAdd function service");
243 uint32_t loopbackIters = 10;
244 bool loopbackPipeline =
false;
245 loopbackSub->add_option(
"-i,--iters", loopbackIters,
246 "Number of function invocations (default 10)");
247 loopbackSub->add_flag(
"-p,--pipeline", loopbackPipeline,
248 "Pipeline all calls then collect results");
250 CLI::App *aggBwSub = cli.add_subcommand(
252 "Aggregate hostmem bandwidth across four units (readmem*, writemem*)");
253 uint32_t aggWidth = 512;
254 uint32_t aggCount = 1000;
255 bool aggRead =
false;
256 bool aggWrite =
false;
257 aggBwSub->add_option(
259 "Bit width (default 512; other widths ignored if absent)");
260 aggBwSub->add_option(
"-c,--count", aggCount,
"Flits per unit (default 1000)");
261 aggBwSub->add_flag(
"-r,--read", aggRead,
"Include read units");
262 aggBwSub->add_flag(
"-w,--write", aggWrite,
"Include write units");
264 CLI::App *streamingAddSub = cli.add_subcommand(
265 "streaming_add",
"Test StreamingAdder function service with list input");
266 uint32_t streamingAddAmt = 5;
267 uint32_t streamingNumItems = 5;
268 bool streamingTranslate =
false;
269 streamingAddSub->add_option(
"-a,--add", streamingAddAmt,
270 "Amount to add to each element (default 5)");
271 streamingAddSub->add_option(
"-n,--num-items", streamingNumItems,
272 "Number of random items in the list (default 5)");
273 streamingAddSub->add_flag(
"-t,--translate", streamingTranslate,
274 "Use message translation (list translation)");
276 CLI::App *coordTranslateSub = cli.add_subcommand(
278 "Test CoordTranslator function service with list of coordinates");
279 uint32_t coordXTrans = 10;
280 uint32_t coordYTrans = 20;
281 uint32_t coordNumItems = 5;
282 coordTranslateSub->add_option(
"-x,--x-translation", coordXTrans,
283 "X translation amount (default 10)");
284 coordTranslateSub->add_option(
"-y,--y-translation", coordYTrans,
285 "Y translation amount (default 20)");
286 coordTranslateSub->add_option(
"-n,--num-coords", coordNumItems,
287 "Number of random coordinates (default 5)");
289 CLI::App *serialCoordTranslateSub = cli.add_subcommand(
291 "Test SerialCoordTranslator function service with list of coordinates");
292 uint32_t serialBatchSize = 240;
293 serialCoordTranslateSub->add_option(
"-x,--x-translation", coordXTrans,
294 "X translation amount (default 10)");
295 serialCoordTranslateSub->add_option(
"-y,--y-translation", coordYTrans,
296 "Y translation amount (default 20)");
297 serialCoordTranslateSub->add_option(
298 "-n,--num-coords", coordNumItems,
299 "Number of random coordinates (default 5)");
300 serialCoordTranslateSub
301 ->add_option(
"-b,--batch-size", serialBatchSize,
302 "Coordinates per header (default 240, max 65535)")
303 ->check(CLI::Range(1u, 0xFFFFu));
305 CLI::App *autoSerialCoordTranslateSub = cli.add_subcommand(
306 "auto_serial_coords",
307 "Test AutoSerialCoordTranslator (uses ListWindowToParallel/Serial "
308 "converters under the hood)");
309 uint32_t autoCoordXTrans = 10;
310 uint32_t autoCoordYTrans = 20;
311 uint32_t autoCoordNumItems = 5;
312 autoSerialCoordTranslateSub->add_option(
"-x,--x-translation", autoCoordXTrans,
313 "X translation amount (default 10)");
314 autoSerialCoordTranslateSub->add_option(
"-y,--y-translation", autoCoordYTrans,
315 "Y translation amount (default 20)");
316 autoSerialCoordTranslateSub->add_option(
317 "-n,--num-coords", autoCoordNumItems,
318 "Number of random coordinates (default 5)");
320 CLI::App *channelTestSub = cli.add_subcommand(
321 "channel",
"Test ChannelService to_host and from_host");
322 uint32_t channelIters = 10;
323 channelTestSub->add_option(
"-i,--iters", channelIters,
324 "Number of loopback iterations (default 10)");
326 if (
int rc = cli.
esiParse(argc, argv))
328 if (!cli.get_help_ptr()->empty())
335 ctxt.
getLogger().
info(
"esitester",
"Connected to accelerator.");
336 Manifest manifest(ctxt, info.getJsonManifest());
339 acc->getServiceThread()->addPoll(*accel);
341 if (*callback_test) {
343 }
else if (*hostmemtestSub) {
344 hostmemTest(acc, accel, hostmemWidths, hmWrite, hmRead);
345 }
else if (*loopbackSub) {
347 }
else if (*dmatestSub) {
348 dmaTest(acc, accel, dmaWidths, dmaRead, dmaWrite);
349 }
else if (*bandwidthSub) {
350 bandwidthTest(acc, accel, bandwidthWidths, xferCount, bandwidthRead,
352 }
else if (*hostmembwSub) {
355 }
else if (*aggBwSub) {
358 }
else if (*streamingAddSub) {
359 if (streamingTranslate)
364 }
else if (*coordTranslateSub) {
366 }
else if (*serialCoordTranslateSub) {
368 coordNumItems, serialBatchSize);
369 }
else if (*autoSerialCoordTranslateSub) {
372 }
else if (*channelTestSub) {
377 }
catch (std::exception &e) {
382 std::cout <<
"Exiting successfully\n";
387 uint32_t iterations) {
390 throw std::runtime_error(
"No cb_test child found in accelerator");
391 auto &ports = cb_test->second->getPorts();
392 auto cmd_port = ports.find(
AppID(
"cmd"));
393 if (cmd_port == ports.end())
394 throw std::runtime_error(
"No cmd port found in cb_test child");
397 throw std::runtime_error(
"cb_test cmd port is not MMIO");
399 auto f = ports.find(
AppID(
"cb"));
400 if (f == ports.end())
401 throw std::runtime_error(
"No cb port found in accelerator");
405 throw std::runtime_error(
"cb port is not a CallService::Callback");
407 std::atomic<uint32_t> callbackCount = 0;
410 callbackCount.fetch_add(1);
411 conn->getLogger().
debug(
412 [&](std::string &subsystem, std::string &msg,
413 std::unique_ptr<std::map<std::string, std::any>> &details) {
414 subsystem =
"ESITESTER";
415 msg =
"Received callback";
416 details = std::make_unique<std::map<std::string, std::any>>();
417 details->emplace(
"data", data);
419 std::cout <<
"callback: " << *data.as<uint64_t>() << std::endl;
424 for (uint32_t i = 0; i < iterations; ++i) {
425 conn->getLogger().info(
"esitester",
"Issuing callback command iteration " +
426 std::to_string(i) +
"/" +
427 std::to_string(iterations));
428 cmdMMIO->write(0x10, i);
430 for (uint32_t wait = 0; wait < 1000; ++wait) {
431 if (callbackCount.load() > i)
433 std::this_thread::sleep_for(std::chrono::milliseconds(1));
435 if (callbackCount.load() <= i)
436 throw std::runtime_error(
"Callback test failed. No callback received");
444 std::cout <<
"Running hostmem WRITE test with width " << width << std::endl;
445 uint64_t *dataPtr =
static_cast<uint64_t *
>(region.
getPtr());
446 auto check = [&](
bool print) {
448 for (
size_t i = 0; i < 9; ++i) {
450 printf(
"[write] dataPtr[%zu] = 0x%016lx\n", i, dataPtr[i]);
451 if (i < (width + 63) / 64 && dataPtr[i] == 0xFFFFFFFFFFFFFFFFull)
457 auto writeMemChildIter = acc->getChildren().find(
AppID(
"writemem", width));
458 if (writeMemChildIter == acc->getChildren().end())
459 throw std::runtime_error(
460 "hostmem write test failed. No writemem child found");
461 auto &writeMemPorts = writeMemChildIter->second->getPorts();
463 auto cmdPortIter = writeMemPorts.find(
AppID(
"cmd", width));
464 if (cmdPortIter == writeMemPorts.end())
465 throw std::runtime_error(
466 "hostmem write test failed. No (cmd,width) MMIO port");
469 throw std::runtime_error(
470 "hostmem write test failed. (cmd,width) port not MMIO");
472 auto issuedPortIter = writeMemPorts.find(
AppID(
"addrCmdIssued"));
473 if (issuedPortIter == writeMemPorts.end())
474 throw std::runtime_error(
475 "hostmem write test failed. addrCmdIssued missing");
476 auto *addrCmdIssuedPort =
478 if (!addrCmdIssuedPort)
479 throw std::runtime_error(
480 "hostmem write test failed. addrCmdIssued not telemetry");
481 addrCmdIssuedPort->connect();
483 auto responsesPortIter = writeMemPorts.find(
AppID(
"addrCmdResponses"));
484 if (responsesPortIter == writeMemPorts.end())
485 throw std::runtime_error(
486 "hostmem write test failed. addrCmdResponses missing");
487 auto *addrCmdResponsesPort =
489 if (!addrCmdResponsesPort)
490 throw std::runtime_error(
491 "hostmem write test failed. addrCmdResponses not telemetry");
492 addrCmdResponsesPort->connect();
494 for (
size_t i = 0, e = 9; i < e; ++i)
495 dataPtr[i] = 0xFFFFFFFFFFFFFFFFull;
497 cmdMMIO->write(0x10,
reinterpret_cast<uint64_t
>(region.
getDevicePtr()));
498 cmdMMIO->write(0x18, 1);
499 cmdMMIO->write(0x20, 1);
501 for (
int i = 0; i < 100; ++i) {
502 auto issued = addrCmdIssuedPort->readInt();
503 auto responses = addrCmdResponsesPort->readInt();
504 if (issued == 1 && responses == 1) {
508 std::this_thread::sleep_for(std::chrono::microseconds(100));
512 throw std::runtime_error(
"hostmem write test (" + std::to_string(width) +
513 " bits) timeout waiting for completion");
516 throw std::runtime_error(
"hostmem write test failed (" +
517 std::to_string(width) +
" bits)");
523 std::cout <<
"Running hostmem READ test with width " << width << std::endl;
524 auto readMemChildIter = acc->getChildren().find(
AppID(
"readmem", width));
525 if (readMemChildIter == acc->getChildren().end())
526 throw std::runtime_error(
527 "hostmem read test failed. No readmem child found");
529 auto &readMemPorts = readMemChildIter->second->getPorts();
530 auto addrCmdPortIter = readMemPorts.find(
AppID(
"cmd", width));
531 if (addrCmdPortIter == readMemPorts.end())
532 throw std::runtime_error(
533 "hostmem read test failed. No AddressCommand MMIO port");
537 throw std::runtime_error(
538 "hostmem read test failed. AddressCommand port not MMIO");
540 auto lastReadPortIter = readMemPorts.find(
AppID(
"lastReadLSB"));
541 if (lastReadPortIter == readMemPorts.end())
542 throw std::runtime_error(
"hostmem read test failed. lastReadLSB missing");
546 throw std::runtime_error(
547 "hostmem read test failed. lastReadLSB not telemetry");
548 lastReadPort->connect();
550 auto issuedPortIter = readMemPorts.find(
AppID(
"addrCmdIssued"));
551 if (issuedPortIter == readMemPorts.end())
552 throw std::runtime_error(
"hostmem read test failed. addrCmdIssued missing");
553 auto *addrCmdIssuedPort =
555 if (!addrCmdIssuedPort)
556 throw std::runtime_error(
557 "hostmem read test failed. addrCmdIssued not telemetry");
558 addrCmdIssuedPort->connect();
560 auto responsesPortIter = readMemPorts.find(
AppID(
"addrCmdResponses"));
561 if (responsesPortIter == readMemPorts.end())
562 throw std::runtime_error(
563 "hostmem read test failed. addrCmdResponses missing");
564 auto *addrCmdResponsesPort =
566 if (!addrCmdResponsesPort)
567 throw std::runtime_error(
568 "hostmem read test failed. addrCmdResponses not telemetry");
569 addrCmdResponsesPort->connect();
571 for (
size_t i = 0; i < 8; ++i) {
572 auto *dataPtr =
static_cast<uint64_t *
>(region.
getPtr());
573 dataPtr[0] = 0x12345678ull << i;
574 dataPtr[1] = 0xDEADBEEFull << i;
576 addrCmdMMIO->write(0x10,
reinterpret_cast<uint64_t
>(region.
getDevicePtr()));
577 addrCmdMMIO->write(0x18, 1);
578 addrCmdMMIO->write(0x20, 1);
580 for (
int waitLoop = 0; waitLoop < 100; ++waitLoop) {
581 auto issued = addrCmdIssuedPort->readInt();
582 auto responses = addrCmdResponsesPort->readInt();
583 if (issued == 1 && responses == 1) {
587 std::this_thread::sleep_for(std::chrono::milliseconds(10));
590 throw std::runtime_error(
"hostmem read (" + std::to_string(width) +
591 " bits) timeout waiting for completion");
592 uint64_t captured = lastReadPort->readInt();
593 uint64_t expected = dataPtr[0];
595 expected &= ((1ull << width) - 1);
596 if (captured != expected)
597 throw std::runtime_error(
"hostmem read test (" + std::to_string(width) +
598 " bits) failed. Expected " +
605 const std::vector<uint32_t> &widths,
bool write,
610 auto scratchRegion = hostmem->allocate(1024 * 1024,
611 {.writeable =
true});
612 uint64_t *dataPtr =
static_cast<uint64_t *
>(scratchRegion->getPtr());
613 conn->getLogger().info(
"esitester",
614 "Running host memory test with region size " +
615 std::to_string(scratchRegion->getSize()) +
616 " bytes at 0x" +
toHex(dataPtr));
617 for (
size_t i = 0; i < scratchRegion->getSize() / 8; ++i)
619 scratchRegion->flush();
622 for (
size_t width : widths) {
628 }
catch (std::exception &e) {
629 conn->getLogger().error(
"esitester",
"Hostmem test failed for width " +
630 std::to_string(width) +
": " +
636 throw std::runtime_error(
"Hostmem test failed");
637 std::cout <<
"Hostmem test passed" << std::endl;
642 Logger &logger = conn->getLogger();
643 logger.
info(
"esitester",
644 "== Running DMA read test with width " + std::to_string(width));
647 acc->resolvePort({
AppID(
"tohostdma", width),
AppID(
"cmd")}, lastPath);
649 throw std::runtime_error(
"dma read test failed. No tohostdma[" +
650 std::to_string(width) +
"] found");
653 throw std::runtime_error(
"dma read test failed. MMIO port is not MMIO");
656 acc->resolvePort({
AppID(
"tohostdma", width),
AppID(
"out")}, lastPath);
660 size_t xferCount = 24;
663 toHostMMIO->write(0, xferCount);
664 for (
size_t i = 0; i < xferCount; ++i) {
667 uint64_t val = *data.as<uint64_t>();
669 throw std::runtime_error(
"dma read test failed. Out of order data");
672 logger.
debug(
"esitester",
673 "Cycle count [" + std::to_string(i) +
"] = 0x" + data.toHex());
676 std::cout <<
" DMA read test for " << width <<
" bits passed" << std::endl;
681 Logger &logger = conn->getLogger();
682 logger.
info(
"esitester",
683 "Running DMA write test with width " + std::to_string(width));
686 acc->resolvePort({
AppID(
"fromhostdma", width),
AppID(
"cmd")}, lastPath);
687 if (!fromHostMMIOPort)
688 throw std::runtime_error(
"dma read test for " +
toString(width) +
689 " bits failed. No fromhostdma[" +
690 std::to_string(width) +
"] found");
693 throw std::runtime_error(
"dma write test for " +
toString(width) +
694 " bits failed. MMIO port is not MMIO");
697 acc->resolvePort({
AppID(
"fromhostdma", width),
AppID(
"in")}, lastPath);
699 throw std::runtime_error(
"dma write test for " +
toString(width) +
700 " bits failed. No out port found");
704 size_t xferCount = 24;
705 uint8_t *data =
new uint8_t[width];
706 for (
size_t i = 0; i < width / 8; ++i)
708 fromHostMMIO->read(8);
709 fromHostMMIO->write(0, xferCount);
710 for (
size_t i = 1; i < xferCount + 1; ++i) {
717 std::this_thread::sleep_for(std::chrono::milliseconds(10));
719 }
while (!successWrite && ++attempts < 100);
721 throw std::runtime_error(
"dma write test for " +
toString(width) +
722 " bits failed. Write failed");
723 uint64_t lastReadMMIO;
724 for (
size_t a = 0; a < 20; ++a) {
725 lastReadMMIO = fromHostMMIO->read(8);
726 if (lastReadMMIO == i)
728 std::this_thread::sleep_for(std::chrono::milliseconds(10));
730 throw std::runtime_error(
"dma write for " +
toString(width) +
731 " bits test failed. Read from MMIO failed");
736 std::cout <<
" DMA write test for " << width <<
" bits passed" << std::endl;
740 const std::vector<uint32_t> &widths,
bool read,
744 for (
size_t width : widths)
747 }
catch (std::exception &e) {
749 std::cerr <<
"DMA write test for " << width
750 <<
" bits failed: " << e.what() << std::endl;
753 for (
size_t width : widths)
756 throw std::runtime_error(
"DMA test failed");
757 std::cout <<
"DMA test passed" << std::endl;
765 size_t width,
size_t xferCount) {
769 acc->resolvePort({
AppID(
"tohostdma", width),
AppID(
"cmd")}, lastPath);
771 throw std::runtime_error(
"bandwidth test failed. No tohostdma[" +
772 std::to_string(width) +
"] found");
775 throw std::runtime_error(
"bandwidth test failed. MMIO port is not MMIO");
778 acc->resolvePort({
AppID(
"tohostdma", width),
AppID(
"out")}, lastPath);
782 Logger &logger = conn->getLogger();
783 logger.
info(
"esitester",
"Starting read bandwidth test with " +
784 std::to_string(xferCount) +
" x " +
785 std::to_string(width) +
" bit transfers");
787 auto start = std::chrono::high_resolution_clock::now();
788 toHostMMIO->write(0, xferCount);
789 for (
size_t i = 0; i < xferCount; ++i) {
792 [i, &data](std::string &subsystem, std::string &msg,
793 std::unique_ptr<std::map<std::string, std::any>> &details) {
794 subsystem =
"esitester";
795 msg =
"Cycle count [" + std::to_string(i) +
"] = 0x" + data.toHex();
798 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
799 std::chrono::high_resolution_clock::now() - start);
801 (double)xferCount * (width / 8.0) * 1e6 / (double)duration.count();
802 logger.
info(
"esitester",
803 " Bandwidth test: " + std::to_string(xferCount) +
" x " +
804 std::to_string(width) +
" bit transfers in " +
805 std::to_string(duration.count()) +
" microseconds");
810 size_t width,
size_t xferCount) {
814 acc->resolvePort({
AppID(
"fromhostdma", width),
AppID(
"cmd")}, lastPath);
815 if (!fromHostMMIOPort)
816 throw std::runtime_error(
"bandwidth test failed. No fromhostdma[" +
817 std::to_string(width) +
"] found");
820 throw std::runtime_error(
"bandwidth test failed. MMIO port is not MMIO");
823 acc->resolvePort({
AppID(
"fromhostdma", width),
AppID(
"in")}, lastPath);
827 Logger &logger = conn->getLogger();
828 logger.
info(
"esitester",
"Starting write bandwidth test with " +
829 std::to_string(xferCount) +
" x " +
830 std::to_string(width) +
" bit transfers");
831 std::vector<uint8_t> dataVec(width / 8);
832 for (
size_t i = 0; i < width / 8; ++i)
835 auto start = std::chrono::high_resolution_clock::now();
836 fromHostMMIO->write(0, xferCount);
837 for (
size_t i = 0; i < xferCount; ++i) {
840 [i, &data](std::string &subsystem, std::string &msg,
841 std::unique_ptr<std::map<std::string, std::any>> &details) {
842 subsystem =
"esitester";
843 msg =
"Cycle count [" + std::to_string(i) +
"] = 0x" + data.toHex();
846 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
847 std::chrono::high_resolution_clock::now() - start);
849 (double)xferCount * (width / 8.0) * 1e6 / (double)duration.count();
850 logger.
info(
"esitester",
851 " Bandwidth test: " + std::to_string(xferCount) +
" x " +
852 std::to_string(width) +
" bit transfers in " +
853 std::to_string(duration.count()) +
" microseconds");
858 const std::vector<uint32_t> &widths,
859 uint32_t xferCount,
bool read,
bool write) {
861 for (uint32_t w : widths)
864 for (uint32_t w : widths)
875 uint32_t width, uint32_t xferCount) {
876 Logger &logger = conn->getLogger();
877 logger.
info(
"esitester",
"Starting hostmem WRITE bandwidth test: " +
878 std::to_string(xferCount) +
" x " +
879 std::to_string(width) +
" bits");
881 auto writeMemChildIter = acc->getChildren().find(
AppID(
"writemem", width));
882 if (writeMemChildIter == acc->getChildren().end())
883 throw std::runtime_error(
"hostmem write bandwidth: writemem child missing");
884 auto &writeMemPorts = writeMemChildIter->second->getPorts();
886 auto cmdPortIter = writeMemPorts.find(
AppID(
"cmd", width));
887 if (cmdPortIter == writeMemPorts.end())
888 throw std::runtime_error(
"hostmem write bandwidth: cmd MMIO missing");
891 throw std::runtime_error(
"hostmem write bandwidth: cmd not MMIO");
893 auto issuedIter = writeMemPorts.find(
AppID(
"addrCmdIssued"));
894 auto respIter = writeMemPorts.find(
AppID(
"addrCmdResponses"));
895 auto cycleCount = writeMemPorts.find(
AppID(
"addrCmdCycles"));
896 if (issuedIter == writeMemPorts.end() || respIter == writeMemPorts.end() ||
897 cycleCount == writeMemPorts.end())
898 throw std::runtime_error(
"hostmem write bandwidth: telemetry missing");
904 if (!issuedPort || !respPort || !cyclePort)
905 throw std::runtime_error(
906 "hostmem write bandwidth: telemetry type mismatch");
908 issuedPort->connect();
913 uint64_t *dataPtr =
static_cast<uint64_t *
>(region.
getPtr());
914 size_t words = region.
getSize() / 8;
915 for (
size_t i = 0; i < words; ++i)
916 dataPtr[i] = i + 0xA5A50000;
919 auto start = std::chrono::high_resolution_clock::now();
921 uint64_t devPtr =
reinterpret_cast<uint64_t
>(region.
getDevicePtr());
922 cmdMMIO->write(0x10, devPtr);
923 cmdMMIO->write(0x18, xferCount);
924 cmdMMIO->write(0x20, 1);
927 bool completed =
false;
928 for (
int wait = 0; wait < 100000; ++wait) {
929 uint64_t respNow = respPort->
readInt();
930 if (respNow == xferCount) {
934 std::this_thread::sleep_for(std::chrono::microseconds(50));
937 throw std::runtime_error(
"hostmem write bandwidth timeout");
938 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
939 std::chrono::high_resolution_clock::now() - start);
941 (double)xferCount * (width / 8.0) * 1e6 / (double)duration.count();
942 uint64_t cycles = cyclePort->
readInt();
943 double bytesPerCycle = (double)xferCount * (width / 8.0) / (double)cycles;
944 std::cout <<
"[WRITE] Hostmem bandwidth (" << std::to_string(width)
946 << std::to_string(xferCount) <<
" flits in "
947 << std::to_string(duration.count()) <<
" us, "
948 << std::to_string(cycles) <<
" cycles, " << bytesPerCycle
949 <<
" bytes/cycle" << std::endl;
955 uint32_t width, uint32_t xferCount) {
956 Logger &logger = conn->getLogger();
957 logger.
info(
"esitester",
"Starting hostmem READ bandwidth test: " +
958 std::to_string(xferCount) +
" x " +
959 std::to_string(width) +
" bits");
961 auto readMemChildIter = acc->getChildren().find(
AppID(
"readmem", width));
962 if (readMemChildIter == acc->getChildren().end())
963 throw std::runtime_error(
"hostmem read bandwidth: readmem child missing");
964 auto &readMemPorts = readMemChildIter->second->getPorts();
966 auto cmdPortIter = readMemPorts.find(
AppID(
"cmd", width));
967 if (cmdPortIter == readMemPorts.end())
968 throw std::runtime_error(
"hostmem read bandwidth: cmd MMIO missing");
971 throw std::runtime_error(
"hostmem read bandwidth: cmd not MMIO");
973 auto issuedIter = readMemPorts.find(
AppID(
"addrCmdIssued"));
974 auto respIter = readMemPorts.find(
AppID(
"addrCmdResponses"));
975 auto cyclePort = readMemPorts.find(
AppID(
"addrCmdCycles"));
976 if (issuedIter == readMemPorts.end() || respIter == readMemPorts.end() ||
977 cyclePort == readMemPorts.end())
978 throw std::runtime_error(
"hostmem read bandwidth: telemetry missing");
984 if (!issuedPort || !respPort || !cycleCntPort)
985 throw std::runtime_error(
"hostmem read bandwidth: telemetry type mismatch");
986 issuedPort->connect();
991 uint64_t *dataPtr =
static_cast<uint64_t *
>(region.
getPtr());
992 size_t words64 = region.
getSize() / 8;
993 for (
size_t i = 0; i < words64; ++i)
994 dataPtr[i] = 0xCAFEBABE0000ull + i;
996 uint64_t devPtr =
reinterpret_cast<uint64_t
>(region.
getDevicePtr());
997 auto start = std::chrono::high_resolution_clock::now();
999 cmdMMIO->write(0x10, devPtr);
1000 cmdMMIO->write(0x18, xferCount);
1001 cmdMMIO->write(0x20, 1);
1003 bool timeout =
true;
1004 for (
int wait = 0; wait < 100000; ++wait) {
1005 uint64_t respNow = respPort->
readInt();
1006 if (respNow == xferCount) {
1010 std::this_thread::sleep_for(std::chrono::microseconds(50));
1013 throw std::runtime_error(
"hostmem read bandwidth timeout");
1014 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
1015 std::chrono::high_resolution_clock::now() - start);
1016 double bytesPerSec =
1017 (double)xferCount * (width / 8.0) * 1e6 / (double)duration.count();
1018 uint64_t cycles = cycleCntPort->
readInt();
1019 double bytesPerCycle = (double)xferCount * (width / 8.0) / (double)cycles;
1020 std::cout <<
"[ READ] Hostmem bandwidth (" << width
1022 <<
" flits in " << duration.count() <<
" us, " << cycles
1023 <<
" cycles, " << bytesPerCycle <<
" bytes/cycle" << std::endl;
1028 const std::vector<uint32_t> &widths,
bool read,
1031 hostmemSvc->
start();
1032 auto region = hostmemSvc->allocate(1024 * 1024 * 1024,
1033 {.writeable =
true});
1034 for (uint32_t w : widths) {
1043 uint32_t iterations,
bool pipeline) {
1044 Logger &logger = conn->getLogger();
1047 throw std::runtime_error(
"Loopback test: no 'loopback' child");
1048 auto &ports = loopbackChild->second->getPorts();
1049 auto addIter = ports.find(
AppID(
"add"));
1050 if (addIter == ports.end())
1051 throw std::runtime_error(
"Loopback test: no 'add' port");
1056 throw std::runtime_error(
1057 "Loopback test: 'add' port not a FuncService::Function");
1058 funcPort->connect();
1059 if (iterations == 0) {
1060 logger.
info(
"esitester",
"Loopback add test: 0 iterations (skipped)");
1063 std::mt19937_64 rng(0xC0FFEE);
1064 std::uniform_int_distribution<uint32_t> dist(0, (1u << 24) - 1);
1067 auto start = std::chrono::high_resolution_clock::now();
1068 for (uint32_t i = 0; i < iterations; ++i) {
1069 uint32_t argVal = dist(rng);
1070 uint32_t expected = (argVal + 11) & 0xFFFF;
1071 uint8_t argBytes[3] = {
1072 static_cast<uint8_t
>(argVal & 0xFF),
1073 static_cast<uint8_t
>((argVal >> 8) & 0xFF),
1074 static_cast<uint8_t
>((argVal >> 16) & 0xFF),
1077 MessageData resMsg = funcPort->call(argMsg).get();
1078 uint16_t got = *resMsg.
as<uint16_t>();
1079 std::cout <<
"[loopback] i=" << i <<
" arg=0x" <<
esi::toHex(argVal)
1082 if (got != expected)
1083 throw std::runtime_error(
"Loopback mismatch (non-pipelined)");
1085 auto end = std::chrono::high_resolution_clock::now();
1086 auto us = std::chrono::duration_cast<std::chrono::microseconds>(end - start)
1088 double callsPerSec = (double)iterations * 1e6 / (
double)us;
1089 logger.
info(
"esitester",
"Loopback add test passed (non-pipelined, " +
1090 std::to_string(iterations) +
" calls, " +
1091 std::to_string(us) +
" us, " +
1092 std::to_string(callsPerSec) +
" calls/s)");
1095 std::vector<std::future<MessageData>> futures;
1096 futures.reserve(iterations);
1097 std::vector<uint32_t> expectedVals;
1098 expectedVals.reserve(iterations);
1100 auto issueStart = std::chrono::high_resolution_clock::now();
1101 for (uint32_t i = 0; i < iterations; ++i) {
1102 uint32_t argVal = dist(rng);
1103 uint32_t expected = (argVal + 11) & 0xFFFF;
1104 uint8_t argBytes[3] = {
1105 static_cast<uint8_t
>(argVal & 0xFF),
1106 static_cast<uint8_t
>((argVal >> 8) & 0xFF),
1107 static_cast<uint8_t
>((argVal >> 16) & 0xFF),
1109 futures.emplace_back(funcPort->call(
MessageData(argBytes, 3)));
1110 expectedVals.emplace_back(expected);
1112 auto issueEnd = std::chrono::high_resolution_clock::now();
1114 for (uint32_t i = 0; i < iterations; ++i) {
1116 uint16_t got = *resMsg.
as<uint16_t>();
1117 uint16_t exp = (uint16_t)expectedVals[i];
1118 std::cout <<
"[loopback-pipelined] i=" << i <<
" got=0x"
1121 throw std::runtime_error(
"Loopback mismatch (pipelined) idx=" +
1124 auto collectEnd = std::chrono::high_resolution_clock::now();
1126 auto issueUs = std::chrono::duration_cast<std::chrono::microseconds>(
1127 issueEnd - issueStart)
1129 auto totalUs = std::chrono::duration_cast<std::chrono::microseconds>(
1130 collectEnd - issueStart)
1133 double issueRate = (double)iterations * 1e6 / (
double)issueUs;
1134 double completionRate = (double)iterations * 1e6 / (
double)totalUs;
1136 logger.
info(
"esitester",
"Loopback add test passed (pipelined). Issued " +
1137 std::to_string(iterations) +
" in " +
1138 std::to_string(issueUs) +
" us (" +
1139 std::to_string(issueRate) +
1140 " calls/s), total " + std::to_string(totalUs) +
1141 " us (" + std::to_string(completionRate) +
1142 " calls/s effective)");
1148 uint32_t xferCount,
bool read,
1150 Logger &logger = conn->getLogger();
1151 if (!read && !write) {
1152 std::cout <<
"aggbandwidth: nothing to do (enable --read and/or --write)\n";
1157 "Aggregate hostmem bandwidth start width=" + std::to_string(width) +
1158 " count=" + std::to_string(xferCount) +
1159 " read=" + (read ?
"Y" :
"N") +
" write=" + (write ?
"Y" :
"N"));
1162 hostmemSvc->
start();
1166 bool isRead =
false;
1167 bool isWrite =
false;
1168 std::unique_ptr<esi::services::HostMem::HostMemRegion> region;
1172 bool launched =
false;
1175 uint64_t duration_us = 0;
1176 uint64_t cycleCount = 0;
1177 std::chrono::high_resolution_clock::time_point start;
1179 std::vector<Unit> units;
1180 const std::vector<std::string> readPrefixes = {
"readmem",
"readmem_0",
1181 "readmem_1",
"readmem_2"};
1182 const std::vector<std::string> writePrefixes = {
"writemem",
"writemem_0",
1183 "writemem_1",
"writemem_2"};
1185 auto addUnits = [&](
const std::vector<std::string> &pref,
bool doRead,
1187 for (
auto &p : pref) {
1189 auto childIt = acc->getChildren().find(
id);
1190 if (childIt == acc->getChildren().end())
1192 auto &ports = childIt->second->getPorts();
1193 auto cmdIt = ports.find(
AppID(
"cmd", width));
1194 auto respIt = ports.find(
AppID(
"addrCmdResponses"));
1195 auto cycIt = ports.find(
AppID(
"addrCmdCycles"));
1196 if (cmdIt == ports.end() || respIt == ports.end() || cycIt == ports.end())
1201 if (!cmd || !resp || !cyc)
1208 u.isWrite = doWrite;
1209 u.region = hostmemSvc->allocate(1024 * 1024 * 1024, {.writeable =
true});
1211 uint64_t *ptr =
static_cast<uint64_t *
>(u.region->getPtr());
1212 size_t words = u.region->getSize() / 8;
1213 for (
size_t i = 0; i < words; ++i)
1215 (p[0] ==
'w' ? (0xA5A500000000ull + i) : (0xCAFEBABE0000ull + i));
1220 u.bytes = uint64_t(xferCount) * (width / 8);
1221 units.emplace_back(std::move(u));
1225 addUnits(readPrefixes,
true,
false);
1227 addUnits(writePrefixes,
false,
true);
1228 if (units.empty()) {
1229 std::cout <<
"aggbandwidth: no matching units present for width " << width
1234 auto wallStart = std::chrono::high_resolution_clock::now();
1236 for (
auto &u : units) {
1237 uint64_t devPtr =
reinterpret_cast<uint64_t
>(u.region->getDevicePtr());
1238 u.cmd->write(0x10, devPtr);
1239 u.cmd->write(0x18, xferCount);
1240 u.cmd->write(0x20, 1);
1241 u.start = std::chrono::high_resolution_clock::now();
1246 const uint64_t timeoutLoops = 200000;
1249 bool allDone =
true;
1250 for (
auto &u : units) {
1253 if (u.resp->readInt() == xferCount) {
1254 auto end = std::chrono::high_resolution_clock::now();
1256 std::chrono::duration_cast<std::chrono::microseconds>(end - u.start)
1258 u.cycleCount = u.cycles->readInt();
1266 if (++loops >= timeoutLoops)
1267 throw std::runtime_error(
"aggbandwidth: timeout");
1268 std::this_thread::sleep_for(std::chrono::microseconds(50));
1270 auto wallUs = std::chrono::duration_cast<std::chrono::microseconds>(
1271 std::chrono::high_resolution_clock::now() - wallStart)
1274 uint64_t totalBytes = 0;
1275 uint64_t totalReadBytes = 0;
1276 uint64_t totalWriteBytes = 0;
1277 for (
auto &u : units) {
1278 totalBytes += u.bytes;
1280 totalReadBytes += u.bytes;
1282 totalWriteBytes += u.bytes;
1283 double unitBps = (double)u.bytes * 1e6 / (
double)u.duration_us;
1284 std::cout <<
"[agg-unit] " << u.prefix <<
"[" << width <<
"] "
1285 << (u.isRead ?
"READ" : (u.isWrite ?
"WRITE" :
"UNK"))
1286 <<
" bytes=" <<
humanBytes(u.bytes) <<
" (" << u.bytes <<
" B)"
1287 <<
" time=" <<
humanTimeUS(u.duration_us) <<
" (" << u.duration_us
1288 <<
" us) cycles=" << u.cycleCount
1294 totalReadBytes ? (double)totalReadBytes * 1e6 / (
double)wallUs : 0.0;
1295 double aggWriteBps =
1296 totalWriteBytes ? (double)totalWriteBytes * 1e6 / (
double)wallUs : 0.0;
1297 double aggCombinedBps =
1298 totalBytes ? (double)totalBytes * 1e6 / (
double)wallUs : 0.0;
1300 std::cout <<
"[agg-total] units=" << units.size()
1301 <<
" read_bytes=" <<
humanBytes(totalReadBytes) <<
" ("
1302 << totalReadBytes <<
" B)"
1304 <<
" write_bytes=" <<
humanBytes(totalWriteBytes) <<
" ("
1305 << totalWriteBytes <<
" B)"
1307 <<
" combined_bytes=" <<
humanBytes(totalBytes) <<
" ("
1308 << totalBytes <<
" B)"
1310 <<
" wall_time=" <<
humanTimeUS(wallUs) <<
" (" << wallUs <<
" us)"
1312 logger.
info(
"esitester",
"Aggregate hostmem bandwidth test complete");
1318#pragma pack(push, 1)
1326 "StreamingAddArg must be 9 bytes packed");
1331#pragma pack(push, 1)
1338 "StreamingAddResult must be 5 bytes packed");
1344 uint32_t addAmt, uint32_t numItems) {
1345 Logger &logger = conn->getLogger();
1346 logger.
info(
"esitester",
"Starting streaming add test with add_amt=" +
1347 std::to_string(addAmt) +
1348 ", num_items=" + std::to_string(numItems));
1351 std::mt19937 rng(0xDEADBEEF);
1352 std::uniform_int_distribution<uint32_t> dist(0, 1000000);
1353 std::vector<uint32_t> inputData;
1354 inputData.reserve(numItems);
1355 for (uint32_t i = 0; i < numItems; ++i)
1356 inputData.push_back(dist(rng));
1359 auto streamingAdderChild =
1361 if (streamingAdderChild == accel->
getChildren().end())
1362 throw std::runtime_error(
1363 "Streaming add test: no 'streaming_adder' child found");
1365 auto &ports = streamingAdderChild->second->getPorts();
1366 auto addIter = ports.find(
AppID(
"streaming_add"));
1367 if (addIter == ports.end())
1368 throw std::runtime_error(
1369 "Streaming add test: no 'streaming_add' port found");
1381 for (
size_t i = 0; i < inputData.size(); ++i) {
1384 arg.
input = inputData[i];
1385 arg.
last = (i == inputData.size() - 1) ? 1 : 0;
1387 MessageData(
reinterpret_cast<const uint8_t *
>(&arg),
sizeof(arg)));
1388 logger.
debug(
"esitester",
"Sent {add_amt=" + std::to_string(arg.
addAmt) +
1389 ", input=" + std::to_string(arg.
input) +
1390 ", last=" + (arg.
last ?
"true" :
"false") +
1395 std::vector<uint32_t> results;
1396 bool lastSeen =
false;
1399 resultPort.
read(resMsg);
1401 throw std::runtime_error(
1402 "Streaming add test: unexpected result message size");
1406 lastSeen = res->
last != 0;
1407 results.push_back(res->data);
1408 logger.
debug(
"esitester",
"Received result=" + std::to_string(res->data) +
1409 " (last=" + (lastSeen ?
"true" :
"false") +
1414 if (results.size() != inputData.size())
1415 throw std::runtime_error(
1416 "Streaming add test: result size mismatch. Expected " +
1417 std::to_string(inputData.size()) +
", got " +
1418 std::to_string(results.size()));
1421 std::cout <<
"Streaming add test results:" << std::endl;
1422 for (
size_t i = 0; i < inputData.size(); ++i) {
1423 uint32_t expected = inputData[i] + addAmt;
1424 std::cout <<
" input[" << i <<
"]=" << inputData[i] <<
" + " << addAmt
1425 <<
" = " << results[i] <<
" (expected " << expected <<
")";
1426 if (results[i] != expected) {
1427 std::cout <<
" MISMATCH!";
1430 std::cout << std::endl;
1437 throw std::runtime_error(
"Streaming add test failed: result mismatch");
1439 logger.
info(
"esitester",
"Streaming add test passed");
1440 std::cout <<
"Streaming add test passed" << std::endl;
1459#pragma pack(push, 1)
1467 uint32_t *
inputData() {
return reinterpret_cast<uint32_t *
>(
this + 1); }
1469 return reinterpret_cast<const uint32_t *
>(
this + 1);
1488#pragma pack(push, 1)
1494 uint32_t *
data() {
return reinterpret_cast<uint32_t *
>(
this + 1); }
1496 return reinterpret_cast<const uint32_t *
>(
this + 1);
1510 uint32_t numItems) {
1511 Logger &logger = conn->getLogger();
1512 logger.
info(
"esitester",
1513 "Starting streaming add test (translated) with add_amt=" +
1514 std::to_string(addAmt) +
1515 ", num_items=" + std::to_string(numItems));
1518 std::mt19937 rng(0xDEADBEEF);
1519 std::uniform_int_distribution<uint32_t> dist(0, 1000000);
1520 std::vector<uint32_t> inputData;
1521 inputData.reserve(numItems);
1522 for (uint32_t i = 0; i < numItems; ++i)
1523 inputData.push_back(dist(rng));
1526 auto streamingAdderChild =
1528 if (streamingAdderChild == accel->
getChildren().end())
1529 throw std::runtime_error(
1530 "Streaming add test: no 'streaming_adder' child found");
1532 auto &ports = streamingAdderChild->second->getPorts();
1533 auto addIter = ports.find(
AppID(
"streaming_add"));
1534 if (addIter == ports.end())
1535 throw std::runtime_error(
1536 "Streaming add test: no 'streaming_add' port found");
1551 size_t allocSize = ((argSize + alignment - 1) / alignment) * alignment;
1554 throw std::bad_alloc();
1556 std::unique_ptr<void,
decltype(argDeleter)> argBuffer(argRaw, argDeleter);
1559 arg->addAmt = addAmt;
1560 for (uint32_t i = 0; i < numItems; ++i)
1561 arg->inputData()[i] = inputData[i];
1563 logger.
debug(
"esitester",
1564 "Sending translated argument: " + std::to_string(argSize) +
1565 " bytes, list_length=" + std::to_string(arg->inputLength) +
1566 ", add_amt=" + std::to_string(arg->addAmt));
1569 argPort.
write(
MessageData(
reinterpret_cast<const uint8_t *
>(arg), argSize));
1574 resultPort.
read(resMsg);
1576 logger.
debug(
"esitester",
"Received translated result: " +
1577 std::to_string(resMsg.
getSize()) +
" bytes");
1580 throw std::runtime_error(
1581 "Streaming add test (translated): result too small");
1583 const auto *result =
1588 throw std::runtime_error(
1589 "Streaming add test (translated): result data truncated");
1592 if (result->dataLength != inputData.size())
1593 throw std::runtime_error(
1594 "Streaming add test (translated): result size mismatch. Expected " +
1595 std::to_string(inputData.size()) +
", got " +
1596 std::to_string(result->dataLength));
1599 std::cout <<
"Streaming add test results:" << std::endl;
1600 for (
size_t i = 0; i < inputData.size(); ++i) {
1601 uint32_t expected = inputData[i] + addAmt;
1602 std::cout <<
" input[" << i <<
"]=" << inputData[i] <<
" + " << addAmt
1603 <<
" = " << result->data()[i] <<
" (expected " << expected <<
")";
1604 if (result->data()[i] != expected) {
1605 std::cout <<
" MISMATCH!";
1608 std::cout << std::endl;
1615 throw std::runtime_error(
1616 "Streaming add test (translated) failed: result mismatch");
1618 logger.
info(
"esitester",
"Streaming add test passed (translated)");
1619 std::cout <<
"Streaming add test passed" << std::endl;
1630#pragma pack(push, 1)
1636static_assert(
sizeof(
Coord) == 8,
"Coord must be 8 bytes packed");
1647#pragma pack(push, 1)
1657 return reinterpret_cast<const Coord *
>(
this + 1);
1674#pragma pack(push, 1)
1682 return reinterpret_cast<const Coord *
>(
this + 1);
1695 uint32_t xTrans, uint32_t yTrans,
1696 uint32_t numCoords) {
1697 Logger &logger = conn->getLogger();
1698 logger.
info(
"esitester",
"Starting coord translate test with x_trans=" +
1699 std::to_string(xTrans) +
1700 ", y_trans=" + std::to_string(yTrans) +
1701 ", num_coords=" + std::to_string(numCoords));
1706 std::mt19937 rng(0xDEADBEEF);
1707 std::uniform_int_distribution<uint32_t> dist(0, 1000000);
1708 std::vector<Coord> inputCoords;
1709 inputCoords.reserve(numCoords);
1710 for (uint32_t i = 0; i < numCoords; ++i) {
1714 inputCoords.push_back(c);
1718 auto coordTranslatorChild =
1720 if (coordTranslatorChild == accel->
getChildren().end())
1721 throw std::runtime_error(
1722 "Coord translate test: no 'coord_translator' child found");
1724 auto &ports = coordTranslatorChild->second->getPorts();
1725 auto translateIter = ports.find(
AppID(
"translate_coords"));
1726 if (translateIter == ports.end())
1727 throw std::runtime_error(
1728 "Coord translate test: no 'translate_coords' port found");
1734 throw std::runtime_error(
1735 "Coord translate test: 'translate_coords' port not a "
1736 "FuncService::Function");
1737 funcPort->connect();
1743 size_t allocSize = ((argSize + alignment - 1) / alignment) * alignment;
1746 throw std::bad_alloc();
1748 std::unique_ptr<void,
decltype(argDeleter)> argBuffer(argRaw, argDeleter);
1751 arg->xTranslation = xTrans;
1752 arg->yTranslation = yTrans;
1753 for (uint32_t i = 0; i < numCoords; ++i)
1754 arg->coords()[i] = inputCoords[i];
1758 "Sending coord translate argument: " + std::to_string(argSize) +
1759 " bytes, coords_length=" + std::to_string(arg->coordsLength) +
1760 ", x_trans=" + std::to_string(arg->xTranslation) +
1761 ", y_trans=" + std::to_string(arg->yTranslation));
1766 ->call(
MessageData(
reinterpret_cast<const uint8_t *
>(arg), argSize))
1770 logger.
debug(
"esitester",
"Received coord translate result: " +
1771 std::to_string(resMsg.
getSize()) +
" bytes");
1774 throw std::runtime_error(
"Coord translate test: result too small");
1776 const auto *result =
1780 throw std::runtime_error(
"Coord translate test: result data truncated");
1783 if (result->coordsLength != inputCoords.size())
1784 throw std::runtime_error(
1785 "Coord translate test: result size mismatch. Expected " +
1786 std::to_string(inputCoords.size()) +
", got " +
1787 std::to_string(result->coordsLength));
1790 std::cout <<
"Coord translate test results:" << std::endl;
1791 for (
size_t i = 0; i < inputCoords.size(); ++i) {
1792 uint32_t expectedX = inputCoords[i].x + xTrans;
1793 uint32_t expectedY = inputCoords[i].y + yTrans;
1794 std::cout <<
" coord[" << i <<
"]=(" << inputCoords[i].x <<
","
1795 << inputCoords[i].y <<
") + (" << xTrans <<
"," << yTrans
1796 <<
") = (" << result->
coords()[i].
x <<
","
1797 << result->coords()[i].y <<
")";
1798 if (result->coords()[i].x != expectedX ||
1799 result->coords()[i].y != expectedY) {
1800 std::cout <<
" MISMATCH! (expected (" << expectedX <<
"," << expectedY
1804 std::cout << std::endl;
1808 throw std::runtime_error(
"Coord translate test failed: result mismatch");
1810 logger.
info(
"esitester",
"Coord translate test passed");
1811 std::cout <<
"Coord translate test passed" << std::endl;
1818#pragma pack(push, 1)
1861 coords.emplace_back(x, y);
1869 return {
reinterpret_cast<const uint8_t *
>(&
header),
sizeof(
header)};
1871 return {
reinterpret_cast<const uint8_t *
>(
coords.data()),
1874 return {
reinterpret_cast<const uint8_t *
>(&
footer),
sizeof(
footer)};
1876 throw std::out_of_range(
"SerialCoordInput: invalid segment index");
1898 coords.emplace_back(x, y);
1905 return {
reinterpret_cast<const uint8_t *
>(&
header),
sizeof(
header)};
1907 return {
reinterpret_cast<const uint8_t *
>(
coords.data()),
1910 throw std::out_of_range(
"SerialCoordBurst: invalid segment index");
1914#pragma pack(push, 1)
1952 detail::getMessageDataRef<SerialCoordOutputBatch>(*msg, scratch);
1953 const uint8_t *bytes = flat.
getBytes();
1958 while (offset < size) {
1960 size_t chunkSize = std::min(needed, size - offset);
1962 bytes + offset + chunkSize);
1963 offset += chunkSize;
1975 if (batchCount == 0) {
1977 auto batch = std::make_unique<SerialCoordOutputBatch>();
1980 decoded.push_back(std::move(batch));
2004 uint32_t yTrans, uint32_t numCoords,
2005 size_t batchSizeLimit) {
2006 Logger &logger = conn->getLogger();
2007 logger.
info(
"esitester",
"Starting Serial coord translate test");
2010 std::mt19937 rng(0xDEADBEEF);
2011 std::uniform_int_distribution<uint32_t> dist(0, 1000000);
2012 std::vector<Coord> inputCoords;
2013 inputCoords.reserve(numCoords);
2014 for (uint32_t i = 0; i < numCoords; ++i)
2015 inputCoords.push_back({dist(rng), dist(rng)});
2019 throw std::runtime_error(
"Serial coord translate test: no "
2020 "'coord_translator_serial' child found");
2022 auto &ports = child->second->getPorts();
2023 auto portIter = ports.find(
AppID(
"translate_coords_serial"));
2024 if (portIter == ports.end())
2025 throw std::runtime_error(
2026 "Serial coord translate test: no 'translate_coords_serial' port found");
2029 portIter->second.getRawWrite(
"arg"));
2044 while (sent < numCoords) {
2045 size_t batchSize = std::min(batchSizeLimit, numCoords - sent);
2050 auto batch = std::make_unique<SerialCoordBurst>();
2051 batch->xTranslation(sent == 0 ? xTrans : 0);
2052 batch->yTranslation(sent == 0 ? yTrans : 0);
2054 for (
size_t i = 0; i < batchSize; ++i) {
2055 batch->appendCoord(inputCoords[sent + i].x, inputCoords[sent + i].y);
2057 argPort.
write(batch);
2061 auto footerBurst = std::make_unique<SerialCoordBurst>();
2062 argPort.
write(footerBurst);
2071 std::vector<uint8_t> rxBuf;
2073 while (rxBuf.size() < frameSize) {
2075 resultRaw.
read(data);
2076 rxBuf.insert(rxBuf.end(), data.getBytes(),
2077 data.getBytes() + data.getSize());
2079 std::memcpy(&out, rxBuf.data(), frameSize);
2080 rxBuf.erase(rxBuf.begin(), rxBuf.begin() + frameSize);
2083 std::vector<Coord> results;
2084 results.reserve(numCoords);
2088 uint16_t batchCount = hdr.header.coordsCount;
2089 if (batchCount == 0)
2091 for (uint16_t i = 0; i < batchCount; ++i) {
2094 results.push_back({frame.data.y, frame.data.x});
2100 std::cout <<
"Serial coord translate test results:" << std::endl;
2101 if (results.size() != inputCoords.size()) {
2102 std::cout <<
"Result size mismatch. Expected " << inputCoords.size()
2103 <<
", got " << results.size() << std::endl;
2106 for (
size_t i = 0; i < std::min(inputCoords.size(), results.size()); ++i) {
2107 uint32_t expX = inputCoords[i].x + xTrans;
2108 uint32_t expY = inputCoords[i].y + yTrans;
2109 std::cout <<
" coord[" << i <<
"]=(" << inputCoords[i].x <<
","
2110 << inputCoords[i].y <<
") + (" << xTrans <<
"," << yTrans
2111 <<
") = (" << results[i].x <<
"," << results[i].y
2112 <<
") (expected (" << expX <<
"," << expY <<
"))";
2113 if (results[i].x != expX || results[i].y != expY) {
2114 std::cout <<
" MISMATCH!";
2117 std::cout << std::endl;
2124 throw std::runtime_error(
"Serial coord translate test failed");
2126 logger.
info(
"esitester",
"Serial coord translate test passed");
2127 std::cout <<
"Serial coord translate test passed" << std::endl;
2147 uint32_t yTrans, uint32_t numCoords) {
2148 Logger &logger = conn->getLogger();
2149 logger.
info(
"esitester",
"Starting Auto serial coord translate test");
2152 std::mt19937 rng(0xDEADBEEF);
2153 std::uniform_int_distribution<uint32_t> dist(0, 1000000);
2154 std::vector<Coord> inputCoords;
2155 inputCoords.reserve(numCoords);
2156 for (uint32_t i = 0; i < numCoords; ++i)
2157 inputCoords.push_back({dist(rng), dist(rng)});
2159 auto child = accel->
getChildren().find(
AppID(
"coord_translator_auto_serial"));
2161 throw std::runtime_error(
"Auto serial coord translate test: no "
2162 "'coord_translator_auto_serial' child found");
2164 auto &ports = child->second->getPorts();
2165 auto portIter = ports.find(
AppID(
"translate_coords_auto_serial"));
2166 if (portIter == ports.end())
2167 throw std::runtime_error(
"Auto serial coord translate test: no "
2168 "'translate_coords_auto_serial' port found");
2173 portIter->second.getRawWrite(
"arg"));
2189 auto batch = std::make_unique<SerialCoordInput>();
2190 batch->xTranslation(xTrans);
2191 batch->yTranslation(yTrans);
2192 for (uint32_t i = 0; i < numCoords; ++i)
2193 batch->appendCoord(inputCoords[i].x, inputCoords[i].y);
2194 argPort.
write(batch);
2200 std::vector<uint8_t> rxBuf;
2202 while (rxBuf.size() < frameSize) {
2204 resultRaw.
read(data);
2205 rxBuf.insert(rxBuf.end(), data.getBytes(),
2206 data.getBytes() + data.getSize());
2208 std::memcpy(&out, rxBuf.data(), frameSize);
2209 rxBuf.erase(rxBuf.begin(), rxBuf.begin() + frameSize);
2215 std::vector<Coord> results;
2216 results.reserve(numCoords);
2220 uint16_t burstCount = hdr.header.coordsCount;
2221 if (burstCount == 0)
2223 if (results.size() + burstCount > numCoords)
2224 throw std::runtime_error(
2225 "Auto serial coord translate test: bursts overflow expected total " +
2226 std::to_string(numCoords));
2227 for (uint32_t i = 0; i < burstCount; ++i) {
2230 results.push_back({frame.data.y, frame.data.x});
2233 if (results.size() != numCoords)
2234 throw std::runtime_error(
"Auto serial coord translate test: got " +
2235 std::to_string(results.size()) +
2236 " coords across all bursts " +
"(expected " +
2237 std::to_string(numCoords) +
")");
2243 std::cout <<
"Auto serial coord translate test results:" << std::endl;
2244 for (
size_t i = 0; i < inputCoords.size(); ++i) {
2245 uint32_t expX = inputCoords[i].x + xTrans;
2246 uint32_t expY = inputCoords[i].y + yTrans;
2247 std::cout <<
" coord[" << i <<
"]=(" << inputCoords[i].x <<
","
2248 << inputCoords[i].y <<
") + (" << xTrans <<
"," << yTrans
2249 <<
") = (" << results[i].x <<
"," << results[i].y
2250 <<
") (expected (" << expX <<
"," << expY <<
"))";
2251 if (results[i].x != expX || results[i].y != expY) {
2252 std::cout <<
" MISMATCH!";
2255 std::cout << std::endl;
2259 throw std::runtime_error(
"Auto serial coord translate test failed");
2261 logger.
info(
"esitester",
"Auto serial coord translate test passed");
2262 std::cout <<
"Auto serial coord translate test passed" << std::endl;
2266 uint32_t iterations) {
2267 Logger &logger = conn->getLogger();
2271 throw std::runtime_error(
"Channel test: no 'channel_test' child");
2272 auto &ports = channelChild->second->getPorts();
2275 auto cmdIter = ports.find(
AppID(
"cmd"));
2276 if (cmdIter == ports.end())
2277 throw std::runtime_error(
"Channel test: no 'cmd' port");
2280 throw std::runtime_error(
"Channel test: 'cmd' is not MMIO");
2283 auto producerIter = ports.find(
AppID(
"producer"));
2284 if (producerIter == ports.end())
2285 throw std::runtime_error(
"Channel test: no 'producer' port");
2286 auto *producerPort =
2289 throw std::runtime_error(
2290 "Channel test: 'producer' is not a ChannelService::ToHost");
2291 producerPort->connect();
2295 cmdMMIO->write(0x0, iterations);
2297 for (uint32_t i = 0; i < iterations; ++i) {
2298 MessageData recvData = producerPort->read().get();
2299 uint32_t got = *recvData.
as<uint32_t>();
2300 std::cout <<
"[channel] producer i=" << i <<
" got=" << got << std::endl;
2302 throw std::runtime_error(
"Channel producer: expected " +
2303 std::to_string(i) +
", got " +
2304 std::to_string(got));
2306 logger.
info(
"esitester",
"Channel test: producer passed (" +
2307 std::to_string(iterations) +
2308 " incrementing values)");
2311 auto loopbackInIter = ports.find(
AppID(
"loopback_in"));
2312 if (loopbackInIter == ports.end())
2313 throw std::runtime_error(
"Channel test: no 'loopback_in' port");
2314 auto *fromHostPort =
2317 throw std::runtime_error(
2318 "Channel test: 'loopback_in' is not a ChannelService::FromHost");
2319 fromHostPort->connect();
2321 auto loopbackOutIter = ports.find(
AppID(
"loopback_out"));
2322 if (loopbackOutIter == ports.end())
2323 throw std::runtime_error(
"Channel test: no 'loopback_out' port");
2324 auto *loopbackOutPort =
2326 if (!loopbackOutPort)
2327 throw std::runtime_error(
2328 "Channel test: 'loopback_out' is not a ChannelService::ToHost");
2329 loopbackOutPort->connect();
2331 std::mt19937_64 rng(0xDEADBEEF);
2332 std::uniform_int_distribution<uint32_t> dist(0, UINT32_MAX);
2334 for (uint32_t i = 0; i < iterations; ++i) {
2335 uint32_t sendVal = dist(rng);
2337 MessageData recvData = loopbackOutPort->read().get();
2338 uint32_t recvVal = *recvData.
as<uint32_t>();
2339 std::cout <<
"[channel] loopback i=" << i <<
" sent=0x"
2342 if (recvVal != sendVal)
2343 throw std::runtime_error(
"Channel loopback mismatch at i=" +
2347 logger.
info(
"esitester",
"Channel test: loopback passed (" +
2348 std::to_string(iterations) +
" iterations)");
2349 std::cout <<
"Channel test passed" << std::endl;
static void print(TypedAttr val, llvm::raw_ostream &os)
TypeDeserializer(OutputCallback output)
Base::OutputCallback OutputCallback
std::vector< uint8_t > partialFrameBytes
Base::DecodedOutputs DecodedOutputs
DecodedOutputs decode(std::unique_ptr< SegmentedMessageData > &msg) override
Decode one raw message into zero or more typed outputs.
std::vector< Coord > accumulated
Abstract class representing a connection to an accelerator.
Top level accelerator class.
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
T * getAs() const
Cast this Bundle port to a subclass which is actually useful.
ReadChannelPort & getRawRead(const std::string &name) const
WriteChannelPort & getRawWrite(const std::string &name) const
Get access to the raw byte streams of a channel.
Common options and code for ESI runtime tools.
Context & getContext()
Get the context.
AcceleratorConnection * connect()
Connect to the accelerator using the specified backend and connection.
int esiParse(int argc, const char **argv)
Run the parser.
AcceleratorConnections, Accelerators, and Manifests must all share a context.
const std::map< AppID, Instance * > & getChildren() const
Access the module's children by ID.
virtual void error(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report an error.
virtual void info(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report an informational message.
void debug(const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details=nullptr)
Report a debug message.
Class to parse a manifest.
Accelerator * buildAccelerator(AcceleratorConnection &acc) const
A concrete flat message backed by a single vector of bytes.
const uint8_t * getBytes() const
const T * as() const
Cast to a type.
size_t getSize() const
Get the size of the data in bytes.
static MessageData from(T &t)
Cast from a type to its raw bytes.
Helper base class for stateful deserializers which may emit zero, one, or many typed outputs for each...
detail::TypedReadOwnedCallback< SerialCoordOutputBatch > OutputCallback
std::vector< std::unique_ptr< SerialCoordOutputBatch > > DecodedOutputs
A ChannelPort which reads data from the accelerator.
virtual void connect(ReadCallback callback, const ConnectOptions &options={})
virtual void disconnect() override
Disconnect the channel.
virtual void read(MessageData &outData)
Specify a buffer to read into.
Abstract multi-segment message.
void connect(const ChannelPort::ConnectOptions &opts={std::nullopt, false})
void write(const T &data)
A ChannelPort which sends data to the accelerator.
virtual void disconnect() override
void write(const MessageData &data)
A very basic blocking write API.
bool tryWrite(const MessageData &data)
A basic non-blocking write API.
virtual void connect(const ConnectOptions &options={}) override
Set up a connection to the accelerator.
A function call which gets attached to a service port.
A port which writes data to the accelerator (from_host).
A port which reads data from the accelerator (to_host).
A function call which gets attached to a service port.
virtual void start()
In cases where necessary, enable host memory services.
A "slice" of some parent MMIO space.
Information about the Accelerator system.
A telemetry port which gets attached to a service port.
void connect()
Connect to a particular telemetry port. Offset should be non-nullopt.
static void * alignedAllocCompat(std::size_t alignment, std::size_t size)
static void hostmemWriteTest(Accelerator *acc, esi::services::HostMem::HostMemRegion ®ion, uint32_t width)
Test the hostmem write functionality.
static void aggregateHostmemBandwidthTest(AcceleratorConnection *, Accelerator *, uint32_t width, uint32_t xferCount, bool read, bool write)
static void dmaTest(AcceleratorConnection *, Accelerator *, const std::vector< uint32_t > &widths, bool read, bool write)
static void hostmemBandwidthTest(AcceleratorConnection *conn, Accelerator *acc, uint32_t xferCount, const std::vector< uint32_t > &widths, bool read, bool write)
static void callbackTest(AcceleratorConnection *, Accelerator *, uint32_t iterations)
static void bandwidthTest(AcceleratorConnection *, Accelerator *, const std::vector< uint32_t > &widths, uint32_t xferCount, bool read, bool write)
static void serialCoordTranslateTest(AcceleratorConnection *, Accelerator *, uint32_t xTrans, uint32_t yTrans, uint32_t numCoords, size_t batchSizeLimit)
constexpr std::array< uint32_t, 5 > defaultWidths
static void hostmemReadBandwidthTest(AcceleratorConnection *conn, Accelerator *acc, esi::services::HostMem::HostMemRegion ®ion, uint32_t width, uint32_t xferCount)
static void bandwidthReadTest(AcceleratorConnection *conn, Accelerator *acc, size_t width, size_t xferCount)
static void channelTest(AcceleratorConnection *, Accelerator *, uint32_t iterations)
static std::string formatBandwidth(double bytesPerSec)
static void autoSerialCoordTranslateTest(AcceleratorConnection *, Accelerator *, uint32_t xTrans, uint32_t yTrans, uint32_t numCoords)
static void hostmemWriteBandwidthTest(AcceleratorConnection *conn, Accelerator *acc, esi::services::HostMem::HostMemRegion ®ion, uint32_t width, uint32_t xferCount)
static void alignedFreeCompat(void *ptr)
static void dmaWriteTest(AcceleratorConnection *conn, Accelerator *acc, size_t width)
static void bandwidthWriteTest(AcceleratorConnection *conn, Accelerator *acc, size_t width, size_t xferCount)
static std::string humanBytes(uint64_t bytes)
static void streamingAddTest(AcceleratorConnection *, Accelerator *, uint32_t addAmt, uint32_t numItems)
Test the StreamingAdder module.
static void loopbackAddTest(AcceleratorConnection *, Accelerator *, uint32_t iterations, bool pipeline)
static void dmaReadTest(AcceleratorConnection *conn, Accelerator *acc, size_t width)
static void streamingAddTranslatedTest(AcceleratorConnection *, Accelerator *, uint32_t addAmt, uint32_t numItems)
static void hostmemTest(AcceleratorConnection *, Accelerator *, const std::vector< uint32_t > &widths, bool write, bool read)
static std::string humanTimeUS(uint64_t us)
int main(int argc, const char *argv[])
static void coordTranslateTest(AcceleratorConnection *, Accelerator *, uint32_t xTrans, uint32_t yTrans, uint32_t numCoords)
static std::string defaultWidthsStr()
static void hostmemReadTest(Accelerator *acc, esi::services::HostMem::HostMemRegion ®ion, uint32_t width)
std::string toString(const std::any &a)
'Stringify' a std::any. This is used to log std::any values by some loggers.
std::string toHex(void *val)
Translated argument struct for CoordTranslator.
std::span< const Coord > coordsSpan() const
const Coord * coords() const
static size_t allocSize(size_t numCoords)
Coord * coords()
Get pointer to trailing coords array.
std::span< Coord > coordsSpan()
Get span view of coords (requires coordsLength to be set first).
Translated result struct for CoordTranslator.
static size_t allocSize(size_t numCoords)
std::span< Coord > coordsSpan()
Get span view of coords (requires coordsLength to be set first).
const Coord * coords() const
Coord * coords()
Get pointer to trailing coords array.
std::span< const Coord > coordsSpan() const
Test the CoordTranslator module using message translation.
void yTranslation(uint32_t yTrans)
void appendCoord(uint32_t x, uint32_t y)
std::vector< SerialCoordData > coords
void xTranslation(uint32_t xTrans)
Segment segment(size_t idx) const override
Get a segment by index.
size_t numSegments() const override
Number of segments in the message.
SerialCoordData(uint32_t x, uint32_t y)
Deserialized result batch from the serial coord translator.
std::vector< Coord > coords
Packed struct representing a parallel window argument for StreamingAdder.
Packed struct representing a parallel window result for StreamingAdder.
Test the StreamingAdder module using message translation.
uint32_t * inputData()
Get pointer to trailing input data array.
static size_t allocSize(size_t numItems)
std::span< uint32_t > inputDataSpan()
Get span view of input data (requires inputLength to be set first).
std::span< const uint32_t > inputDataSpan() const
const uint32_t * inputData() const
Translated result struct for StreamingAdder.
uint32_t * data()
Get pointer to trailing result data array.
std::span< uint32_t > dataSpan()
Get span view of result data (requires dataLength to be set first).
static size_t allocSize(size_t numItems)
std::span< const uint32_t > dataSpan() const
const uint32_t * data() const
A contiguous, non-owning view of bytes within a SegmentedMessageData.
RAII memory region for host memory.
virtual void * getDevicePtr() const
Sometimes the pointer the device sees is different from the pointer the host sees.
virtual void * getPtr() const =0
Get a pointer to the host memory.
virtual void flush()
Flush the memory region to ensure that the device sees the latest contents.
virtual std::size_t getSize() const =0
SerialCoordOutputData data
SerialCoordOutputHeader header