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
30#include "xrt/xrt_device.h"
31#include "xrt/xrt_kernel.h"
32
33namespace test_utils {
34
36 std::string name);
37
39
40void parse_options(int argc, const char *argv[], cxxopts::Options &options,
41 cxxopts::ParseResult &result);
42
43std::vector<uint32_t> load_instr_sequence(std::string instr_path);
44std::vector<uint32_t> load_instr_binary(std::string instr_path);
45
46void init_xrt_load_kernel(xrt::device &device, xrt::kernel &kernel,
47 int verbosity, std::string xclbinFileName,
48 std::string kernelNameInXclbin);
49
50static inline std::int16_t random_int16_t(int32_t range = 0x10000) {
51 return (std::int16_t)rand() % range;
52}
53
54static inline std::int32_t random_int32_t(int32_t range = 0x10000) {
55 return (std::int32_t)rand() % range;
56}
57
58#if defined(__STDCPP_BFLOAT16_T__)
59static inline std::bfloat16_t random_bfloat16_t(std::bfloat16_t scale,
60 std::bfloat16_t bias) {
61 return std::bfloat16_t((scale * (float)rand() / (float)(RAND_MAX)) + bias);
62}
63#endif
64
65bool nearly_equal(float a, float b, float epsilon = 128 * FLT_EPSILON,
66 float abs_th = FLT_MIN);
67
68template <typename T>
69void print_matrix(const std::vector<T> matrix, int n_cols,
70 int n_printable_rows = 10, int n_printable_cols = 10,
71 std::ostream &ostream = std::cout,
72 const char col_sep[] = " ", const char elide_sym[] = " ... ",
73 int w = -1) {
74 assert(matrix.size() % n_cols == 0);
75
76 auto maxima = std::minmax_element(matrix.begin(), matrix.end());
77 T max_val = std::max(*maxima.first, std::abs(*maxima.second));
78 size_t n_digits = log10(max_val);
79 if (w == -1) {
80 w = n_digits;
81 }
82 int n_rows = matrix.size() / n_cols;
83
84 n_printable_rows = std::min(n_rows, n_printable_rows);
85 n_printable_cols = std::min(n_cols, n_printable_cols);
86
87 const bool elide_rows = n_printable_rows < n_rows;
88 const bool elide_cols = n_printable_cols < n_cols;
89
90 if (elide_rows || elide_cols) {
91 w = std::max((int)w, (int)strlen(elide_sym));
92 }
93
94 w += 3; // for decimal point and two decimal digits
95 ostream << std::fixed << std::setprecision(2);
96
97#define print_row(what) \
98 for (int col = 0; col < n_printable_cols / 2; col++) { \
99 ostream << std::right << std::setw(w) << (what); \
100 ostream << std::setw(0) << col_sep; \
101 } \
102 if (elide_cols) { \
103 ostream << std::setw(0) << elide_sym; \
104 } \
105 for (int col = n_printable_cols / 2 + 1; col < n_printable_cols; col++) { \
106 ostream << std::right << std::setw(w) << (what); \
107 ostream << std::setw(0) << col_sep; \
108 }
109
110 for (int row = 0; row < n_printable_rows / 2; row++) {
111 print_row(matrix[row * n_rows + col]);
112 ostream << std::endl;
113 }
114 if (elide_rows) {
115 print_row(elide_sym);
116 ostream << std::endl;
117 }
118 for (int row = n_printable_rows / 2 + 1; row < n_printable_rows; row++) {
119 print_row(matrix[row * n_rows + col]);
120 ostream << std::endl;
121 }
122
123#undef print_row
124}
125
126void write_out_trace(char *traceOutPtr, size_t trace_size, std::string path);
127
128} // namespace test_utils
129
130#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:69
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)