onnx2versal
Loading...
Searching...
No Matches
graph_utils.h
1#ifndef __GRAPH_UTILS_H__
2#define __GRAPH_UTILS_H__
3
4#include <adf.h>
5
6// for 16384B (note 32768B DDR per AIE)
7#define TILE_BYTES 32768
8#define MAX_PARAM_BYTES 16384
9#define MAX_FLOAT_PARAMS MAX_PARAM_BYTES / sizeof(float)
10
11#ifdef EXTERNAL_IO
12#define PLIO64_ARG(TXT_PATH) adf::plio_64_bits
13#else
14#define PLIO64_ARG(TXT_PATH) adf::plio_64_bits, TXT_PATH
15#endif
16
17#define adfCheck(stmt, log_name) \
18 do { \
19 adf::return_code ret = stmt; \
20 if (ret != adf::ok) \
21 printf("ERROR [%s]: failed\n", log_name); \
22 } while (0)
23
24
25// uses 2/2 performance counters
26void get_graph_throughput_by_port(
27 adf::graph graph,
28 const std::string& log_name,
29 adf::IoAttr& plio_port,
30 const size_t window_len,
31 const size_t sample_bytesize,
32 const size_t iter_cnt
33) {
34 adf::event::handle ehdl = adf::event::start_profiling(plio_port, adf::event::io_stream_start_to_bytes_transferred_cycles, window_len * iter_cnt * sample_bytesize);
35
36 adfCheck(graph.run(iter_cnt), "run graph");
37 adfCheck(graph.wait(), "wait graph");
38
39 if (ehdl != adf::event::invalid_handle) {
40 long long cycles = adf::event::read_profiling(ehdl);
41 adf::event::stop_profiling(ehdl);
42 printf("%s: Cycles %lld Throughput %f samples/s\n", log_name.c_str(), cycles, (double) window_len * iter_cnt / (cycles * 1e-9)); //samples/second
43 } else {
44 printf("%s: ERROR: Invalid handle. Only two performance counter in a AIE-PL interface tile. Event profile is not supported for x86sim.\n", log_name.c_str());
45 }
46}
47
48// uses 2/2 performance counters, run first if multiple profiles are done sequentially as counters wait till net receives first data
49void get_graph_latency(
50 adf::graph graph,
51 const std::string& log_name,
52 adf::IoAttr& plio_input_port,
53 adf::IoAttr& plio_output_port,
54 const size_t iter_cnt
55) {
56 adf::event::handle ehdl = adf::event::start_profiling(plio_input_port, plio_output_port, adf::event::io_stream_start_difference_cycles);
57
58 adfCheck(graph.run(iter_cnt), "run graph");
59 adfCheck(graph.wait(), "wait graph");
60
61 if (ehdl != adf::event::invalid_handle) {
62 long long cycles = adf::event::read_profiling(ehdl);
63 adf::event::stop_profiling(ehdl); // performance counter is released and cleared
64 printf("[%s]: AIE Latency: %lld cycles, %fs (@1Ghz)\n", log_name.c_str(), cycles, (double) cycles * 1e-9);
65 } else {
66 printf("%s: ERROR: Invalid handle. Only two performance counter in a AIE-PL interface tile. Event profile is not supported for x86sim.\n", log_name.c_str());
67 }
68}
69
70// Only useful for system test, uses 2/2 performance counters
71void get_graph_bandwidth_by_port(
72 adf::graph graph,
73 const std::string& log_name,
74 adf::IoAttr& plio_input_port,
75 adf::IoAttr& plio_output_port,
76 const size_t input_window_len,
77 const size_t output_window_len,
78 const size_t iter_cnt
79) {
80 adf::event::handle in_ehdl = adf::event::start_profiling(plio_input_port, adf::event::io_total_stream_running_to_idle_cycles);
81 adf::event::handle out_ehdl = adf::event::start_profiling(plio_output_port, adf::event::io_total_stream_running_to_idle_cycles);
82
83 adfCheck(graph.run(iter_cnt), "run graph");
84 adfCheck(graph.wait(), "wait graph");
85
86 long long cycles;
87 if (in_ehdl != adf::event::invalid_handle) {
88 cycles = adf::event::read_profiling(in_ehdl);
89 adf::event::stop_profiling(in_ehdl);
90 printf("%s: Cycles %lld Bandwidth %f, ==1: graph faster than PLIO, < 1: input port stalled\n", log_name.c_str(), cycles, (double) input_window_len * iter_cnt / cycles);
91 } else {
92 printf("%s: ERROR: Invalid handle. Only two performance counter in a AIE-PL interface tile. Event profile is not supported for x86sim.\n", log_name.c_str());
93 }
94
95 if (out_ehdl != adf::event::invalid_handle) {
96 cycles = adf::event::read_profiling(out_ehdl);
97 adf::event::stop_profiling(out_ehdl);
98 printf("%s: Cycles %lld Bandwidth %f, ==1: graph faster than PLIO, < 1: output port stalled\n", log_name.c_str(), cycles, (double) output_window_len * iter_cnt / cycles);
99 } else {
100 printf("%s: ERROR: Invalid handle. Only two performance counter in a AIE-PL interface tile. Event profile is not supported for x86sim.\n", log_name.c_str());
101 }
102}
103
104#endif // __GRAPH_UTILS_H__