1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
#include "HalideBuffer.h"
#include "HalideTraceUtils.h"
#include "halide_image_io.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <map>
#include <string>
#include <vector>
/** \file
*
* A tool which can read a binary Halide trace file, and dump files
* containing the final pixel values recorded for each traced Func.
*
* Currently dumps into supported Halide image formats.
*/
using namespace Halide;
using namespace Internal;
using Halide::Runtime::Buffer;
using std::map;
using std::string;
using std::vector;
struct BufferOutputOpts {
enum OutputType {
PNG = 0,
JPG,
PGM,
TMP,
MAT
};
enum OutputType type;
};
struct FuncInfo {
int min_coords[16];
int max_coords[16];
int dimensions;
halide_type_t type;
Buffer<> values;
FuncInfo() = default;
FuncInfo(Packet *p) {
int real_dims = p->dimensions / p->type.lanes;
if (real_dims > 16) {
fprintf(stderr, "Error: found trace packet with dimensionality > 16. Aborting.\n");
exit(1);
}
for (int i = 0; i < real_dims; i++) {
min_coords[i] = INT32_MAX;
max_coords[i] = INT32_MIN;
}
dimensions = real_dims;
type = p->type;
type.lanes = 1;
}
void add_preprocess(Packet *p) {
int real_dims = p->dimensions / p->type.lanes;
int lanes = p->type.lanes;
halide_type_t scalar_type = p->type;
scalar_type.lanes = 1;
if (scalar_type != type) {
fprintf(stderr, "Error: packet type doesn't match previous packets of same Func. Aborting.\n");
exit(1);
}
if (real_dims != dimensions) {
fprintf(stderr, "Error: packet dimensionality doesn't match previous packets of same Func. Aborting.\n");
exit(1);
}
for (int lane = 0; lane < lanes; lane++) {
for (int i = 0; i < real_dims; i++) {
const int coord = p->coordinates()[lanes * i + lane];
min_coords[i] = std::min(min_coords[i], coord);
max_coords[i] = std::max(max_coords[i], coord);
}
}
}
void allocate() {
std::vector<int> extents;
for (int i = 0; i < dimensions; i++) {
extents.push_back(max_coords[i] - min_coords[i] + 1);
}
values = Buffer<>(type, extents);
if (values.data() == nullptr) {
fprintf(stderr, "Memory allocation failure. Aborting.\n");
exit(1);
}
}
void add(Packet *p) {
halide_type_t scalar_type = p->type;
scalar_type.lanes = 1;
if (scalar_type == halide_type_of<float>()) {
add_typed<float>(p);
} else if (scalar_type == halide_type_of<double>()) {
add_typed<double>(p);
} else if (scalar_type == halide_type_of<uint8_t>()) {
add_typed<uint8_t>(p);
} else if (scalar_type == halide_type_of<uint16_t>()) {
add_typed<uint16_t>(p);
} else if (scalar_type == halide_type_of<uint32_t>()) {
add_typed<uint32_t>(p);
} else if (scalar_type == halide_type_of<uint64_t>()) {
add_typed<uint64_t>(p);
} else if (scalar_type == halide_type_of<int8_t>()) {
add_typed<int8_t>(p);
} else if (scalar_type == halide_type_of<int16_t>()) {
add_typed<int16_t>(p);
} else if (scalar_type == halide_type_of<int32_t>()) {
add_typed<int32_t>(p);
} else if (scalar_type == halide_type_of<int64_t>()) {
add_typed<int64_t>(p);
} else if (scalar_type == halide_type_of<bool>()) {
add_typed<bool>(p);
} else {
printf("Packet with unknown type\n");
exit(1);
}
}
template<typename T>
void add_typed(Packet *p) {
Buffer<T> &buf = values.as<T>();
int lanes = p->type.lanes;
if (!allocated()) {
fprintf(stderr, "Packet storage not allocated. Aborting.\n");
exit(1);
}
if (p->dimensions < 0) {
fprintf(stderr, "Negative dimensionality found. Aborting.\n");
exit(1);
}
for (int lane = 0; lane < lanes; lane++) {
int coord[16];
for (int i = 0; i < dimensions; i++) {
coord[i] = p->coordinates()[lanes * i + lane] - min_coords[i];
}
buf(coord) = p->get_value_as<T>(lane);
}
}
bool allocated() const {
return (values.data() != nullptr);
}
};
bool check_and_continue(bool condition, const char *msg) {
if (!condition) {
fprintf(stderr, "Failed to dump func: %s\n", msg);
}
return condition;
}
void dump_func(string name, FuncInfo &func, BufferOutputOpts output_opts) {
// Remove special characters
for (char &c : name) {
if (!std::isalnum(c)) {
c = '_';
}
}
string filename;
switch (output_opts.type) {
case BufferOutputOpts::PNG:
filename = name + ".png";
break;
case BufferOutputOpts::JPG:
filename = name + ".jpg";
break;
case BufferOutputOpts::PGM:
filename = name + ".pgm";
break;
case BufferOutputOpts::TMP:
filename = name + ".tmp";
break;
case BufferOutputOpts::MAT:
filename = name + ".mat";
break;
default:
exit(1);
}
printf("[INFO] Dumping func '%s' to file: %s\n", name.c_str(), filename.c_str());
// Rely on save_image to do type-checking
Halide::Tools::convert_and_save_image<Buffer<>, check_and_continue>(func.values, filename);
}
void finish_dump(map<string, FuncInfo> &func_info, BufferOutputOpts output_opts) {
printf("\nTrace stats:\n");
printf(" Funcs:\n");
for (auto &pair : func_info) {
const string &name = pair.first;
FuncInfo &info = pair.second;
printf(" %s:\n", name.c_str());
// Type
printf(" Type: ");
if (info.type.code == halide_type_code_t::halide_type_int) {
printf("int%d\n", info.type.bits);
} else if (info.type.code == halide_type_code_t::halide_type_uint) {
printf("uint%d\n", info.type.bits);
} else if (info.type.code == halide_type_code_t::halide_type_float) {
printf("float%d\n", info.type.bits);
} else {
fprintf(stderr, "Unsupported Func type. Aborting.\n");
exit(1);
}
// Dimensions
printf(" Dimensions: %d\n", info.dimensions);
// Size of the func
printf(" Size: ");
for (int idx = 0; idx < info.dimensions; idx++) {
if (idx > 0) {
printf("x");
}
printf("%d", (info.max_coords[idx] - info.min_coords[idx]) + 1);
}
printf("\n");
// Minima
printf(" Minimum stored to in each dim: {");
for (int idx = 0; idx < info.dimensions; idx++) {
if (idx > 0) {
printf(", ");
}
printf("%d", info.min_coords[idx]);
}
printf("}\n");
// Maxima
printf(" Maximum stored to in each dim: {");
for (int idx = 0; idx < info.dimensions; idx++) {
if (idx > 0) {
printf(", ");
}
printf("%d", info.max_coords[idx]);
}
printf("}\n");
}
for (auto &pair : func_info) {
string name = pair.first;
FuncInfo &info = pair.second;
dump_func(name, info, output_opts);
}
printf("Done.\n");
}
void usage(char *const *argv) {
const string usage =
"Usage: " + string(argv[0]) +
" -i trace_file -t {png,jpg,pgm,tmp,mat}\n"
"\n"
"This tool reads a binary trace produced by Halide, and dumps all\n"
"Funcs into individual image files in the current directory.\n"
"To generate a suitable binary trace, use Func::trace_stores(), or the\n"
"target features trace_stores and trace_realizations, and run with\n"
"HL_TRACE_FILE=<filename>.\n";
fprintf(stderr, "%s\n", usage.c_str());
exit(1);
}
int main(int argc, char *const *argv) {
char *buf_filename = nullptr;
char *buf_imagetype = nullptr;
BufferOutputOpts outputopts;
for (int i = 1; i < argc - 1; i++) {
string arg = argv[i];
if (arg == "-t") {
i++;
buf_imagetype = argv[i];
} else if (arg == "-i") {
i++;
buf_filename = argv[i];
}
}
if (buf_filename == nullptr) {
usage(argv);
}
if (buf_imagetype == nullptr) {
usage(argv);
}
string imagetype(buf_imagetype);
if (imagetype == "jpg") {
outputopts.type = BufferOutputOpts::JPG;
} else if (imagetype == "png") {
outputopts.type = BufferOutputOpts::PNG;
} else if (imagetype == "pgm") {
outputopts.type = BufferOutputOpts::PGM;
} else if (imagetype == "tmp") {
outputopts.type = BufferOutputOpts::TMP;
} else if (imagetype == "mat") {
outputopts.type = BufferOutputOpts::MAT;
} else {
usage(argv);
}
FILE *file_desc = fopen(buf_filename, "r");
if (file_desc == nullptr) {
fprintf(stderr, "[Error opening file: %s. Exiting.\n", argv[1]);
exit(1);
}
printf("[INFO] Starting parse of binary trace...\n");
int packet_count = 0;
map<string, FuncInfo> func_info;
printf("[INFO] First pass...\n");
for (;;) {
Packet p;
if (!p.read_from_filedesc(file_desc)) {
printf("[INFO] Finished pass 1 after %d packets.\n", packet_count);
break;
}
// Packet read was successful.
packet_count++;
if ((packet_count % 100000) == 0) {
printf("[INFO] Pass 1: Read %d packets so far.\n", packet_count);
}
// Check if this was a store packet.
if ((p.event == halide_trace_store) || (p.event == halide_trace_load)) {
if (func_info.find(string(p.func())) == func_info.end()) {
printf("[INFO] Found Func with tracked accesses: %s\n", p.func());
func_info[string(p.func())] = FuncInfo(&p);
}
func_info[string(p.func())].add_preprocess(&p);
}
}
packet_count = 0;
fseek(file_desc, 0, SEEK_SET);
if (ferror(file_desc)) {
fprintf(stderr, "Error: couldn't seek back to beginning of trace file. Aborting.\n");
exit(1);
}
for (auto &pair : func_info) {
pair.second.allocate();
}
for (;;) {
Packet p;
if (!p.read_from_filedesc(file_desc)) {
printf("[INFO] Finished pass 2 after %d packets.\n", packet_count);
if (file_desc != nullptr) {
fclose(file_desc);
}
finish_dump(func_info, outputopts);
exit(0);
}
// Packet read was successful.
packet_count++;
if ((packet_count % 100000) == 0) {
printf("[INFO] Pass 2: Read %d packets so far.\n", packet_count);
}
// Check if this was a store packet.
if ((p.event == halide_trace_store) || (p.event == halide_trace_load)) {
if (func_info.find(string(p.func())) == func_info.end()) {
fprintf(stderr, "Unable to find Func on 2nd pass. Aborting.\n");
exit(1);
}
func_info[string(p.func())].add(&p);
}
}
}
|