Created
October 9, 2015 06:23
-
-
Save sonsongithub/520840d2bf66ed645570 to your computer and use it in GitHub Desktop.
Sample source for Halide - http://halide-lang.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Halide | |
#include "Halide.h" | |
// Include some support code for loading pngs. | |
#include "halide_image_io.h" | |
// for performance test | |
#include <sys/time.h> | |
double currentTime() { | |
timeval t; | |
gettimeofday(&t, NULL); | |
return t.tv_sec * 1000.0 + t.tv_usec / 1000.0f; | |
} | |
int main(int argc, char **argv) { | |
int count = 100; | |
Halide::Image<uint8_t> input = Halide::Tools::load_image("rgb.png"); | |
Halide::Func blur, clamped, blur_x; | |
Halide::Var x, y, c; | |
// clampled image because output image is smaller than input image. | |
clamped(x,y,c) = input(clamp(x,1,input.width()-2),clamp(y,1,input.height()-2),c); | |
// Filtering | |
blur_x(x,y,c) = 0.2f * clamped(x-2, y, c) + 0.2f * clamped(x-1, y, c) + 0.2f * clamped(x, y, c) + 0.2f * clamped(x+1, y, c) + 0.2f * clamped(x+2, y, c); | |
Halide::Expr value = 0.2f * blur_x(x, y-2, c) + 0.2f * blur_x(x, y-1, c) + 0.2f * blur_x(x, y, c) + 0.2f * blur_x(x, y+1, c) + 0.2f * blur_x(x, y+2, c); | |
// Cast it back to an 8-bit unsigned integer. | |
value = Halide::cast<uint8_t>(value); | |
// Define the function. | |
blur(x, y, c) = value; | |
// do it | |
Halide::Image<uint8_t> output = blur.realize(input.width(), input.height(), input.channels()); | |
// save image | |
Halide::Tools::save_image(output, "blured.png"); | |
// performance test | |
{ | |
Halide::Func temp = blur; | |
double t = currentTime(); | |
for (int i = 0; i < count; i++) { | |
Halide::Image<uint8_t> output = blur.realize(input.width(), input.height(), input.channels()); | |
} | |
printf("%f[msec] = Normal\n", (currentTime()- t)/count); | |
} | |
{ | |
Halide::Var yi; | |
Halide::Func temp = blur; | |
temp.split(y, y, yi, 8).parallel(y).vectorize(x, 4); | |
double t = currentTime(); | |
for (int i = 0; i < count; i++) { | |
Halide::Image<uint8_t> output = temp.realize(input.width(), input.height(), input.channels()); | |
} | |
printf("%f[msec] = Compute oncurrently. \n", (currentTime()- t)/count); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment