Skip to content

Instantly share code, notes, and snippets.

@jrk
Created February 17, 2014 22:07
Show Gist options
  • Save jrk/9060243 to your computer and use it in GitHub Desktop.
Save jrk/9060243 to your computer and use it in GitHub Desktop.
Halide test for error behavior when ulimits prevent sufficient memory allocation
// NOTE: these setrlimit APIs are not implemented in OS X. This should only actually catch failure on Linux.
// TODO: also test overflow on output, intermediate allocations
#include <Halide.h>
#include <stdio.h>
#include <sys/resource.h>
using namespace Halide;
size_t memory_limit_bytes = 1024*1024*128; // 128MB - plenty for Halide, not enough for buffers
int test_width = memory_limit_bytes/4; // In floats, this should be enough memory to overflow on input
int main(int argc, char **argv) {
// Set limits on core, address space as done by OpenTuner
rlimit core = {1,1},
as = {memory_limit_bytes, memory_limit_bytes};
setrlimit(RLIMIT_CORE, &core);
setrlimit(RLIMIT_AS, &as);
fprintf(stderr, "Construct pipeline...");
Func f;
ImageParam input(Float(32), 1);
f(_) = input(_)*2.0f;
fprintf(stderr, "done\n");
fprintf(stderr, "Compile...");
f.compile_jit();
fprintf(stderr, "done\n");
fprintf(stderr, "Allocate inputs...");
f.infer_input_bounds(test_width);
fprintf(stderr, "done\n");
// Dig out the allocation
Buffer inbuf = input.parameter().get_buffer();
float* host = (float*)inbuf.host_ptr();
fprintf(stderr, "Buffer @ %p: %d -> %d\n", inbuf.host_ptr(), inbuf.min(0), inbuf.extent(0));
#if 0 // this segfaults, as expected
fprintf(stderr, "Host[0] = %f\n", host[0]);
#else // this doesn't!
fprintf(stderr, "Realize...");
f.realize(test_width);
fprintf(stderr, "done!\n");
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment