#include "util.h" #include #include #include #include // Defined in main.cpp extern int random_seed; extern int N; extern int print_max; extern int MAX_LEN; extern char *parameter_fname; extern char *output_fname; extern char *input_fname; void *read_binary(const char *filename, size_t *size) { size_t size_; FILE *f = fopen(filename, "rb"); CHECK_ERROR(f != NULL, "Failed to read %s", filename); fseek(f, 0, SEEK_END); size_ = ftell(f); rewind(f); void *buf = malloc(size_); size_t ret = fread(buf, 1, size_, f); fclose(f); CHECK_ERROR(size_ == ret, "Failed to read %ld bytes from %s", size_, filename); if (size != NULL) *size = size_; return buf; } void WriteFile(const char *filename, size_t size, void *buf) { FILE *f = fopen(filename, "wb"); CHECK_ERROR(f != NULL, "Failed to write %s", filename); size_t ret = fwrite(buf, 1, size, f); fclose(f); CHECK_ERROR(size == ret, "Failed to write %ld bytes to %s", size, filename); } double get_time() { struct timespec tv; clock_gettime(CLOCK_MONOTONIC, &tv); return tv.tv_sec + tv.tv_nsec * 1e-9; } void print_usage_exit(int argc, char **argv) { printf("Usage %s [parameter bin] [output] [N] [seed] [input bin]\n", argv[0]); printf(" parameter bin: File conatining DNN parameters\n"); printf(" output: File to write results\n"); printf(" N: Number of images to style\n"); printf(" seed: Random seed\n"); printf(" input bin : input image file\n"); EXIT(0); } void check_and_parse_args(int argc, char **argv) { if (argc != 6) print_usage_exit(argc, argv); int c; while ((c = getopt(argc, argv, "h")) != -1) { switch (c) { case 'h': break; default: print_usage_exit(argc, argv); } } parameter_fname = argv[1]; output_fname = argv[2]; N = atoi(argv[3]); random_seed = atoi(argv[4]); input_fname = argv[5]; } void print_first_few_result(float *output, int print_max, double elapsed_time){ // Print first few results int print_cnt = N < print_max ? N : print_max; printf("First %d results are:", print_cnt); for (int i = 0; i < print_cnt; i++) { printf("[%d th result] %f\n", i, output[i]); } // Write the results to file printf("Writing to %s ...", output_fname); fflush(stdout); FILE *output_fp = (FILE *)fopen(output_fname, "w"); for (int i = 0; i < N; i++) { fprintf(output_fp, "%f\n", output[i]); } fclose(output_fp); printf("Done!\n"); // Print elapsed time printf("Elapsed time: %.6f seconds\n", elapsed_time); printf("Throughput: %.3f images/sec\n", (double)N / elapsed_time); }