Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Halide simple example
// Modified from tutorials/lesson_02.cpp and test/vectorize.cpp
// On linux, you can compile and run it like so:
// g++ -I ../include -L ../bin -lHalide -lpthread -ldl -lpng blur.cpp
// LD_LIBRARY_PATH=../bin ./lesson_02
// On os x:
// g++ -I ../include -L ../bin -lHalide `libpng-config --cflags --ldflags` blur.cpp
// DYLD_LIBRARY_PATH=../bin ./lesson_02
// The only Halide header file you need is Halide.h. It includes all of Halide.
#include <Halide.h>
using Halide::Image;
#ifdef _WIN32
extern "C" bool QueryPerformanceCounter(uint64_t *);
extern "C" bool QueryPerformanceFrequency(uint64_t *);
double currentTime() {
uint64_t t, freq;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&freq);
return (t * 1000.0) / freq;
}
#else
#include <sys/time.h>
double currentTime() {
timeval t;
gettimeofday(&t, NULL);
return t.tv_sec * 1000.0 + t.tv_usec / 1000.0f;
}
#endif
#include "../apps/support/image_io.h"
int main(int argc, char **argv) {
Halide::Image<uint8_t> image = load<uint8_t>("../apps/images/rgb.png");
Halide::Func brighter,brighter_par;
Halide::Var x, y, xi, yi, c;
Halide::Func input;
int w = image.width();
int h = image.height();
int ch = image.channels();
printf("Input: %d x %d x %d\n",w,h,ch);
//Define algorithm.
brighter(x, y, c) = Halide::cast<uint8_t>(min(image(x, y, c) * 1.5f, 255));
brighter_par(x,y,c) = brighter(x,y,c);
// Specify a schedule.
brighter_par.split(y, y, yi, 8).parallel(y).vectorize(x, 8);
// brighter_par.vectorize(x,4).parallel(y);
//By invoking "realize" first, JIT is done before time measurement. (JIT takes a few seconds.)
Image<uint8_t> out = brighter.realize(w,h,ch);
Image<uint8_t> out_par = brighter_par.realize(w,h,ch);
printf("Start measurement.\n");
//Execution time measurement.
double t1 = currentTime();
const int repeat = 1000;
for (int i = 0; i < repeat; i++) {
brighter.realize(out);
}
double t2 = currentTime();
for (int i = 0; i < repeat; i++) {
brighter_par.realize(out_par);
}
double t3 = currentTime();
printf("Vectorized vs scalar (average over %d times): %.2f ms %.2f ms. Speedup = %1.3f\n", repeat,
((t3-t2)/repeat), ((t2-t1)/repeat), (t2-t1)/(t3-t2));
save(out, "brighter.png");
save(out_par, "brighter_par.png");
printf("Success!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment