Skip to content

Commit

Permalink
Reorganizing Halide apps and fixed blur's test/documentation (#598)
Browse files Browse the repository at this point in the history
I accidentally reused the same branch so it looks like there's a lot of
commits, but really it's just one small commit + merging main.
  • Loading branch information
skeqiqevian authored Mar 23, 2024
1 parent 1e700c9 commit 5c036d6
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 41 deletions.
5 changes: 0 additions & 5 deletions apps/blur/README.md

This file was deleted.

File renamed without changes.
10 changes: 10 additions & 0 deletions apps/x86/halide/blur/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Run the following command to compile and run.
```
exocc -o blur --stem blur blur.py
g++ -o png_process main.cpp blur/blur.c -lpng -mavx2 -fopenmp; ./png_process
```

The Halide schedule should be around 17x faster. Then run this to check output equivalence:
```
diff blur.png exo_blur_halide.png
```
File renamed without changes.
File renamed without changes
File renamed without changes
67 changes: 32 additions & 35 deletions apps/blur/main.cpp → apps/x86/halide/blur/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include <chrono>
#include <cstring>
#include <iostream>
#include <memory>
#include <png.h>
#include <vector>

bool read_png_file(const char *filename, std::vector<uint8_t> &buffer,
bool read_png_file(const char *filename, std::vector<uint16_t> &buffer,
int &width, int &height) {
FILE *fp = fopen(filename, "rb");
if (!fp)
Expand Down Expand Up @@ -50,7 +51,7 @@ bool read_png_file(const char *filename, std::vector<uint8_t> &buffer,
}

bool write_png_file(
const char *filename, const uint8_t *buffer, int width, int height) {
const char *filename, const uint16_t *buffer, int width, int height) {
FILE *fp = fopen(filename, "wb");
if (!fp)
return false;
Expand Down Expand Up @@ -95,27 +96,15 @@ bool write_png_file(
return true;
}

typedef void (*blurtype)(
void *ctxt, int_fast32_t n, int_fast32_t m, uint8_t *g, const uint8_t *inp);

int exec_parrot(blurtype func, std::string output_name, int width, int height,
uint8_t *parrot) {
uint8_t *parrot_blurred_0, *parrot_blurred_1;
parrot_blurred_0 =
(uint8_t *)malloc(sizeof(uint8_t) * (width + 4) * (height + 4));
parrot_blurred_1 =
(uint8_t *)malloc(sizeof(uint8_t) * (width + 4) * (height + 4));
typedef void (*blurtype)(void *ctxt, int_fast32_t n, int_fast32_t m,
uint16_t *g, const uint16_t *inp);

int exec_blur(blurtype func, std::string output_name, int width, int height,
uint16_t *img_in, uint16_t *img_out) {
auto start = std::chrono::steady_clock::now();
int iterations = 100;
for (int i = 0; i < iterations; i += 4) {
func(
nullptr, height, width, &parrot_blurred_0[2 * (width + 4) + 2], parrot);
func(nullptr, height, width, &parrot_blurred_1[2 * (width + 4) + 2],
parrot_blurred_0);
func(nullptr, height, width, &parrot_blurred_0[2 * (width + 4) + 2],
parrot_blurred_1);
func(nullptr, height, width, parrot_blurred_1, parrot_blurred_0);
for (int i = 0; i < iterations; i++) {
func(nullptr, width, height, img_out, img_in);
}
auto stop = std::chrono::steady_clock::now();
float time = (float)std::chrono::duration_cast<std::chrono::microseconds>(
Expand All @@ -124,13 +113,7 @@ int exec_parrot(blurtype func, std::string output_name, int width, int height,
printf("%s: %f microseconds\n", output_name.c_str(), time);

std::string file_name = output_name + std::string(".png");

uint8_t *parrot_write;
parrot_write = (uint8_t *)malloc(sizeof(uint8_t) * width * height);
for (int i = 0; i < height; i++)
memcpy(&parrot_write[i * width], &parrot_blurred_1[i * (width + 4)],
sizeof(uint8_t) * width);
if (!write_png_file(file_name.c_str(), parrot_write, width, height)) {
if (!write_png_file(file_name.c_str(), img_out, width, height)) {
std::cerr << "Error writing PNG file." << std::endl;
}

Expand All @@ -139,23 +122,37 @@ int exec_parrot(blurtype func, std::string output_name, int width, int height,

int main() {
const char *read_file = "gray_scaled.png";
std::vector<uint8_t> buffer;
std::vector<uint16_t> buffer;
int width, height;

if (read_png_file(read_file, buffer, width, height)) {
printf("width: %d\n", (int)width);
printf("height: %d\n", (int)height);

uint8_t *parrot;
parrot = (uint8_t *)malloc(sizeof(uint8_t) * (width + 4) * (height + 4));
std::vector<uint16_t> img_in((width + 2) * (height + 2));
for (int i = 0; i < height; i++) {
memcpy(&parrot[(i + 2) * (width + 4) + 2], &buffer[i * width],
sizeof(uint8_t) * width);
memcpy(&img_in[(i + 1) * (width + 2) + 1], &buffer[i * width],
sizeof(uint16_t) * width);
}

std::vector<uint16_t> ref_img_out(width * height);
std::vector<uint16_t> img_out(width * height);
exec_blur(blur, "blur", width, height, img_in.data(), ref_img_out.data());
exec_blur(exo_blur_halide, "exo_blur_halide", width, height, img_in.data(),
img_out.data());

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (ref_img_out[y * width + x] != img_out[y * width + x]) {
std::cerr << "Mismatch at (" << x << ", " << y
<< "): " << ref_img_out[y * width + x]
<< " != " << img_out[y * width + x] << std::endl;
return 1;
}
}
}

exec_parrot(blur_staged, "blur_staged", width, height, parrot);
exec_parrot(blur_inline, "blur_inline", width, height, parrot);
exec_parrot(blur_tiled, "blur_tiled", width, height, parrot);
std::cout << "Results match!" << std::endl;
} else {
std::cerr << "Error reading PNG file." << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ def test_gemmini_conv(golden):


def test_blur(golden):
module_file = REPO_ROOT / "apps" / "blur" / "blur.py"
module_file = REPO_ROOT / "apps" / "x86" / "halide" / "blur" / "blur.py"
assert _test_app(module_file) == golden

0 comments on commit 5c036d6

Please sign in to comment.