1 #ifndef _TIRAMISU_UTILS 2 #define _TIRAMISU_UTILS 5 #include "tiramisu/debug.h" 13 double median(std::vector<std::chrono::duration<double, std::milli>> scores);
14 void print_time(
const std::string &file_name,
const std::string &kernel_name,
15 const std::vector<std::string> &header_text,
16 const std::vector<double> &time_vector);
25 for (
int z = 0; z < buf.channels(); z++)
27 for (
int y = 0; y < buf.height(); y++)
29 for (
int x = 0; x < buf.width(); x++)
40 std::string channels_size = ((buf.channels()>1)?std::to_string(buf.channels())+
",":
"");
41 std::string heigth_size = ((buf.height()>1)?std::to_string(buf.height())+
",":
"");
42 std::string width_size = ((buf.width()>=0)?std::to_string(buf.width()):
"");
44 std::cout <<
"Printing " << buf.name() <<
"[" << channels_size << heigth_size << width_size <<
"]: " << std::endl;
46 for (
int z = 0; z < buf.channels(); z++)
48 for (
int y = 0; y < buf.height(); y++)
50 for (
int x = 0; x < buf.width(); x++)
52 if (std::is_same<T, uint8_t>::value)
53 std::cout << (int)buf(x, y, z);
55 std::cout << buf(x, y, z);
57 if (x != buf.width() - 1)
62 std::cout << ((buf.height()>1)?
"\n":
"");
68 inline void copy_buffer(
const Halide::Buffer<T> &from, Halide::Buffer<T> &to)
70 if ((from.dimensions() > to.dimensions()) || (from.channels() > to.channels()) ||
71 (from.height() > to.height()) || (from.width() > to.width()))
73 ERROR(
"'from' has bigger dimension size than 'to'. 'from' size: " +
74 std::to_string(from.dimensions()) +
", 'to' size: " +
75 std::to_string(to.dimensions()) +
"\n",
true);
78 for (
int z = 0; z < from.channels(); z++)
80 for (
int y = 0; y < from.height(); y++)
82 for (
int x = 0; x < from.width(); x++)
84 to(x, y, z) = from(x, y, z);
92 const Halide::Buffer<T> &expected,
float threshold)
94 if ((result.dimensions() != expected.dimensions()) ||
95 (result.channels() != expected.channels()) ||
96 (result.height() != expected.height()) ||
97 (result.width() != expected.width()))
99 ERROR(
"result has different dimension size from expected\n",
true);
102 for (
int z = 0; z < result.channels(); z++) {
103 for (
int y = 0; y < result.height(); y++) {
104 for (
int x = 0; x < result.width(); x++) {
105 if ((
float) (
result(x, y, z) - expected(x, y, z)) > threshold) {
106 ERROR(
"\033[1;31mTest " + test +
" failed. At (" + std::to_string(x) +
107 ", " + std::to_string(y) +
", " + std::to_string(z) +
"), expected: " +
108 std::to_string(expected(x, y, z)) +
", got: " +
109 std::to_string(
result(x, y, z)) +
".\033[0m\n",
false);
115 tiramisu::str_dump(
"\033[1;32mTest " + test +
" succeeded.\033[0m\n");
120 const Halide::Buffer<T> &expected)
127 const Halide::Buffer<T> &expected,
int box)
129 for (
int n = 0; n < result.extent(3); n++) {
130 for (
int z = 0; z < result.extent(2); z++) {
131 for (
int y = 0; y < result.extent(1)-box; y++) {
132 for (
int x = 0; x < result.extent(0)-box; x++) {
134 std::cout <<
"Comparing " <<
result(x, y, z, n) <<
" and " 135 << expected(x, y, z, n) <<
" at position " <<
136 "(" + std::to_string(x) +
"/" + std::to_string(result.extent(0)-box) +
", " + std::to_string(y) +
"/" + std::to_string(result.extent(1)-box) +
137 ", " + std::to_string(z) +
"/" + std::to_string(result.extent(2)) +
", " + std::to_string(n) +
"/" + std::to_string(result.extent(3)) +
")" 140 if (
result(x, y, z, n) != expected(x, y, z, n)) {
141 ERROR(
"\033[1;31mTest " + test +
" failed. At (" + std::to_string(x) +
142 ", " + std::to_string(y) +
", " + std::to_string(z) +
", " + std::to_string(n)+
"), expected: " +
143 std::to_string(expected(x, y, z, n)) +
", got: " +
144 std::to_string(
result(x, y, z, n)) +
".\033[0m\n",
false);
151 tiramisu::str_dump(
"\033[1;32mTest " + test +
" succeeded.\033[0m\n");
156 const Halide::Buffer<T> &expected)
166 for (
int z = 0; z < result.channels(); z++) {
167 for (
int y = 0; y < result.height(); y++) {
168 for (
int x = 0; x < result.width(); x++) {
169 if (
result(x, y, z) != expected(x, y, z)) {
170 ERROR(
"\033[1;31mTest " + test +
" failed. At (" + std::to_string(x) +
171 ", " + std::to_string(y) +
", " + std::to_string(z) +
"), expected: " +
172 std::to_string(expected(x, y, z)) +
", got: " +
173 std::to_string(
result(x, y, z)) +
".\033[0m\n",
true);
179 tiramisu::str_dump(
"\033[1;32mTest " + test +
" succeeded.\033[0m\n");
182 template <
typename T>
185 std::ifstream
result(
"/tmp/" + test +
"_all_ranks.txt");
186 if (result.is_open()) {
188 for (
int z = 0; z < expected_result.channels(); z++) {
189 for (
int y = 0; y < expected_result.height(); y++) {
190 for (
int x = 0; x < expected_result.width(); x++) {
191 std::getline(result, line);
192 if (line != std::to_string(expected_result(x, y, z))) {
193 ERROR(
"\033[1;31mTest " + test +
" failed. At (" + std::to_string(x) +
194 ", " + std::to_string(y) +
", " + std::to_string(z) +
"), expected: " +
195 std::to_string(expected_result(x, y, z)) +
", got: " +
196 line +
".\033[0m\n",
true);
207 template <
typename T,
typename C>
209 std::ofstream output_file;
210 output_file.open(
"/tmp/" + test +
"_rank_" + std::to_string(rank) +
".txt");
211 for (
int z = 0; z < result.channels(); z++) {
212 for (
int y = 0; y < result.height(); y++) {
213 for (
int x = 0; x < result.width(); x++) {
214 output_file << static_cast<C>(
result(x, y, z)) << std::endl;
230 tiramisu::str_dump(
"\033[1;32mTest " + test +
" succeeded.\033[0m\n");
232 ERROR(
"\033[1;31mTest " + test +
" failed.\033[0m\n",
false);
243 for (
int y = 0; y < buf.height(); y++)
245 for (
int x = 0; x < buf.width(); x++)
247 buf(x, y) = (y % 2 == 0) ? val1 : val2;
259 start_timing = std::chrono::system_clock::now();
264 end_timing = std::chrono::system_clock::now();
269 std::chrono::duration<double> elapsed_seconds = end_timing -
start_timing;
270 auto elapsed_micro_seconds = std::chrono::duration_cast<std::chrono::microseconds>(elapsed_seconds);
271 std::cout << bench_name <<
": " << elapsed_micro_seconds.count() <<
" micro-seconds\n";
275 template <
typename T>
283 optional(T value) : value(value), has_value(true) {}
285 explicit operator bool()
const std::chrono::time_point< std::chrono::system_clock > start_timing
void compare_4D_buffers(const std::string &test, const Halide::Buffer< T > &result, const Halide::Buffer< T > &expected, int box)
void print(std::string bench_name)
void copy_buffer(const Halide::Buffer< T > &from, Halide::Buffer< T > &to)
void store_dist_results(const std::string &test, int rank, const Halide::Buffer< T > &result)
void print_test_results(const std::string &test, bool success)
success: a boolean indicating whether the test succeeded.
void print_time(const std::string &file_name, const std::string &kernel_name, const std::vector< std::string > &header_text, const std::vector< double > &time_vector)
void combine_dist_results(const std::string &test, std::vector< int > dims, int num_ranks)
void init_buffer(Halide::Buffer< T > &buf, T val)
void compare_buffers(const std::string &test, const Halide::Buffer< T > &result, const Halide::Buffer< T > &expected)
void print_buffer(const Halide::Buffer< T > &buf)
void compare_dist_buffers(const std::string &test, const Halide::Buffer< T > &expected_result)
void init_2D_buffer_interleaving(Halide::Buffer< T > &buf, T val1, T val2)
Create an array {val1, val2, val1, val2, val1, val2, val1, val2, ...}.
void compare_buffers_approximately(const std::string &test, const Halide::Buffer< T > &result, const Halide::Buffer< T > &expected, float threshold)
std::chrono::time_point< std::chrono::system_clock > end_timing
double median(std::vector< std::chrono::duration< double, std::milli >> scores)