MLIR-AIE
test_utils.h
Go to the documentation of this file.
1//===- test_utils.h ----------------------------000---*- C++ -*-===//
2//
3// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7// Copyright (C) 2024, Advanced Micro Devices, Inc.
8//
9//===----------------------------------------------------------------------===//
10
11// This file contains common helper functions for the generic host code
12
13#ifndef _TEST_UTILS_H_
14#define _TEST_UTILS_H_
15
16#include "cxxopts.hpp"
17#include <cfloat>
18#include <cmath>
19#include <cstdint>
20#include <fstream>
21#include <iomanip>
22#include <iostream>
23#include <sstream>
24#if defined(__STDCPP_BFLOAT16_T__)
25#include <stdfloat>
26#endif
27#include <string>
28#include <vector>
29
30namespace xrt {
31class device;
32class kernel;
33} // namespace xrt
34
35namespace test_utils {
36
38 std::string name);
39
41
42void parse_options(int argc, const char *argv[], cxxopts::Options &options,
43 cxxopts::ParseResult &result);
44
45std::vector<uint32_t> load_instr_sequence(std::string instr_path);
46std::vector<uint32_t> load_instr_binary(std::string instr_path);
47
48void init_xrt_load_kernel(xrt::device &device, xrt::kernel &kernel,
49 int verbosity, std::string xclbinFileName,
50 std::string kernelNameInXclbin);
51
52static inline std::int16_t random_int16_t(int32_t range = 0x10000) {
53 return (std::int16_t)rand() % range;
54}
55
56static inline std::int32_t random_int32_t(int32_t range = 0x10000) {
57 return (std::int32_t)rand() % range;
58}
59
60#if defined(__STDCPP_BFLOAT16_T__)
61static inline std::bfloat16_t random_bfloat16_t(std::bfloat16_t scale,
62 std::bfloat16_t bias) {
63 return std::bfloat16_t((scale * (float)rand() / (float)(RAND_MAX)) + bias);
64}
65#endif
66
67bool nearly_equal(float a, float b, float epsilon = 128 * FLT_EPSILON,
68 float abs_th = FLT_MIN);
69
70template <typename T>
71void print_matrix(const std::vector<T> matrix, int n_cols,
72 int n_printable_rows = 10, int n_printable_cols = 10,
73 std::ostream &ostream = std::cout,
74 const char col_sep[] = " ", const char elide_sym[] = " ... ",
75 int w = -1) {
76 assert(matrix.size() % n_cols == 0);
77
78 auto maxima = std::minmax_element(matrix.begin(), matrix.end());
79 T max_val = std::max(*maxima.first, std::abs(*maxima.second));
80 size_t n_digits = log10(max_val);
81 if (w == -1) {
82 w = n_digits;
83 }
84 int n_rows = matrix.size() / n_cols;
85
86 n_printable_rows = std::min(n_rows, n_printable_rows);
87 n_printable_cols = std::min(n_cols, n_printable_cols);
88
89 const bool elide_rows = n_printable_rows < n_rows;
90 const bool elide_cols = n_printable_cols < n_cols;
91
92 if (elide_rows || elide_cols) {
93 w = std::max((int)w, (int)strlen(elide_sym));
94 }
95
96 w += 3; // for decimal point and two decimal digits
97 ostream << std::fixed << std::setprecision(2);
98
99#define print_row(what) \
100 for (int col = 0; col < n_printable_cols / 2; col++) { \
101 ostream << std::right << std::setw(w) << (what); \
102 ostream << std::setw(0) << col_sep; \
103 } \
104 if (elide_cols) { \
105 ostream << std::setw(0) << elide_sym; \
106 } \
107 for (int col = n_printable_cols / 2 + 1; col < n_printable_cols; col++) { \
108 ostream << std::right << std::setw(w) << (what); \
109 ostream << std::setw(0) << col_sep; \
110 }
111
112 for (int row = 0; row < n_printable_rows / 2; row++) {
113 print_row(matrix[row * n_rows + col]);
114 ostream << std::endl;
115 }
116 if (elide_rows) {
117 print_row(elide_sym);
118 ostream << std::endl;
119 }
120 for (int row = n_printable_rows / 2 + 1; row < n_printable_rows; row++) {
121 print_row(matrix[row * n_rows + col]);
122 ostream << std::endl;
123 }
124
125#undef print_row
126}
127
128void write_out_trace(char *traceOutPtr, size_t trace_size, std::string path);
129
130} // namespace test_utils
131
132#endif // _TEST_UTILS_H_
bool nearly_equal(float a, float b, float epsilon=128 *FLT_EPSILON, float abs_th=FLT_MIN)
void print_matrix(const std::vector< T > matrix, int n_cols, int n_printable_rows=10, int n_printable_cols=10, std::ostream &ostream=std::cout, const char col_sep[]=" ", const char elide_sym[]=" ... ", int w=-1)
Definition test_utils.h:71
std::vector< uint32_t > load_instr_binary(std::string instr_path)
void init_xrt_load_kernel(xrt::device &device, xrt::kernel &kernel, int verbosity, std::string xclbinFileName, std::string kernelNameInXclbin)
void parse_options(int argc, const char *argv[], cxxopts::Options &options, cxxopts::ParseResult &result)
void write_out_trace(char *traceOutPtr, size_t trace_size, std::string path)
void check_arg_file_exists(const cxxopts::ParseResult &result, std::string name)
std::vector< uint32_t > load_instr_sequence(std::string instr_path)
void add_default_options(cxxopts::Options &options)
#define print_row(what)