1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#include "Halide.h"
#include <stdio.h>
using namespace Halide;
int heap_peak = 0;
int num_mallocs = 0;
int malloc_avg = 0;
int stack_peak = 0;
void reset_stats() {
heap_peak = 0;
num_mallocs = 0;
malloc_avg = 0;
stack_peak = 0;
}
void my_print(JITUserContext *, const char *msg) {
float this_ms, this_threads, this_percentage;
int idx, this_heap_peak;
int this_num_mallocs, this_malloc_avg, this_stack_peak;
int val;
// printf("%s", msg);
val = sscanf(msg, " g_%d: %fms (%f%%) threads: %f peak: %d num: %d avg: %d",
&idx, &this_ms, &this_percentage, &this_threads, &this_heap_peak,
&this_num_mallocs, &this_malloc_avg);
if (val == 7) {
heap_peak = this_heap_peak;
num_mallocs = this_num_mallocs;
malloc_avg = this_malloc_avg;
}
val = sscanf(msg, " g_%d: %fms (%f%%) peak: %d num: %d avg: %d",
&idx, &this_ms, &this_percentage, &this_heap_peak,
&this_num_mallocs, &this_malloc_avg);
if (val == 6) {
heap_peak = this_heap_peak;
num_mallocs = this_num_mallocs;
malloc_avg = this_malloc_avg;
}
val = sscanf(msg, " g_%d: %fms (%f%%) threads: %f stack: %d",
&idx, &this_ms, &this_percentage, &this_threads, &this_stack_peak);
if (val == 5) {
stack_peak = this_stack_peak;
}
val = sscanf(msg, " g_%d: %fms (%f%%) stack: %d",
&idx, &this_ms, &this_percentage, &this_stack_peak);
if (val == 4) {
stack_peak = this_stack_peak;
}
}
// Return 0 if there is no error found
int check_error(int exp_heap_peak, int exp_num_mallocs,
int exp_malloc_avg, int exp_stack_peak) {
/*printf("Memory heap_peak: %d bytes, num_mallocs: %d, malloc_avg: %d, "
"stack_peak: %d\n", heap_peak, num_mallocs, malloc_avg, stack_peak);*/
if (heap_peak != exp_heap_peak) {
printf("Peak heap was %d instead of %d\n", heap_peak, exp_heap_peak);
return 1;
}
if (num_mallocs != exp_num_mallocs) {
printf("Num of mallocs was %d instead of %d\n", num_mallocs, exp_num_mallocs);
return 1;
}
if (malloc_avg != exp_malloc_avg) {
printf("Malloc average was %d instead of %d\n", malloc_avg, exp_malloc_avg);
return 1;
}
if (stack_peak != exp_stack_peak) {
printf("Stack peak was %d instead of %d\n", stack_peak, exp_stack_peak);
return 1;
}
return 0;
}
// Return 0 if there is no error found
int check_error_parallel(int min_heap_peak, int max_heap_peak, int exp_num_mallocs,
int exp_malloc_avg, int exp_stack_peak) {
/*printf("Memory heap_peak: %d bytes, num_mallocs: %d, malloc_avg: %d, "
"stack_peak: %d\n", heap_peak, num_mallocs, malloc_avg, stack_peak);*/
if (heap_peak < min_heap_peak || heap_peak > max_heap_peak) {
printf("Peak heap was %d which was outside the range of [%d, %d]\n",
heap_peak, min_heap_peak, max_heap_peak);
return 1;
}
if (num_mallocs != exp_num_mallocs) {
printf("Num of mallocs was %d instead of %d\n", num_mallocs, exp_num_mallocs);
return 1;
}
if (malloc_avg != exp_malloc_avg) {
printf("Malloc average was %d instead of %d\n", malloc_avg, exp_malloc_avg);
return 1;
}
if (stack_peak != exp_stack_peak) {
printf("Stack peak was %d instead of %d\n", stack_peak, exp_stack_peak);
return 1;
}
return 0;
}
int main(int argc, char **argv) {
Target target = get_jit_target_from_environment();
if (target.arch == Target::WebAssembly) {
printf("[SKIP] Performance tests are meaningless and/or misleading under WebAssembly interpreter.\n");
return 0;
}
Target t = get_jit_target_from_environment().with_feature(Target::Profile);
Var x("x"), y("y");
{
printf("Running simple stack allocation test...\n");
// On stack allocation (less than or equal to 1024*16 and buffer g1 size is constant 64x64)
const int size_x = 64;
const int size_y = 64;
Func f1("f_1"), g1("g_1");
g1(x, y) = x;
f1(x, y) = g1(x % size_x, y % size_y);
g1.compute_root();
f1.jit_handlers().custom_print = my_print;
reset_stats();
f1.realize({size_x, size_y}, t);
int stack_size = size_x * size_y * sizeof(int);
if (check_error(0, 0, 0, stack_size) != 0) {
return 1;
}
}
{
printf("Running simple heap allocation test 1...\n");
// On heap allocation (bigger than 1024*16)
const int size_x = 1000;
const int size_y = 1000;
Func f2("f_2"), g2("g_2");
g2(x, y) = x;
f2(x, y) = g2(x - 1, y) + g2(x, y - 1);
g2.compute_root();
f2.jit_handlers().custom_print = my_print;
reset_stats();
f2.realize({size_x, size_y}, t);
int total = (size_x + 1) * (size_y + 1) * sizeof(int);
if (check_error(total, 1, total, 0) != 0) {
return 1;
}
}
{
printf("Running heap allocate condition is always false test...\n");
// Allocate condiiton is always false
Func f3("f_3"), g3("g_3");
g3(x, y) = x * y;
f3(x, y) = select(1 == 2, g3(x - 1, y), 0);
g3.compute_root();
f3.jit_handlers().custom_print = my_print;
reset_stats();
f3.realize({1000, 1000}, t);
if (check_error(0, 0, 0, 0) != 0) {
return 1;
}
}
{
printf("Running stack allocate condition is always false test...\n");
// Allocate condiiton is always false
Func f3("f_3"), g3("g_3");
g3(x, y) = x * y;
f3(x, y) = select(1 == 2, g3((x - 1) % 10, y % 10), 0);
g3.compute_root();
f3.jit_handlers().custom_print = my_print;
reset_stats();
f3.realize({1000, 1000}, t);
if (check_error(0, 0, 0, 0) != 0) {
return 1;
}
}
{
printf("Running allocate with non-trivial condition test...\n");
const int size_x = 10000;
Param<bool> toggle1, toggle2;
Func g4("g_4"), f4("f_4"), f5("f_5"), f6("f_6");
g4(x) = sin(x);
f4(x) = g4(x) + 1;
f5(x) = g4(x) + 2;
f6(x) = select(toggle1, f4(x), 0) + select(toggle2, f5(x), 0);
g4.compute_root();
f4.compute_root();
f5.compute_root();
f6.jit_handlers().custom_print = my_print;
int total = 0;
reset_stats();
toggle1.set(true);
toggle2.set(true);
f6.realize({size_x}, t);
total = size_x * sizeof(float);
if (check_error(total, 1, total, 0) != 0) {
return 1;
}
reset_stats();
toggle1.set(true);
toggle2.set(false);
f6.realize({size_x}, t);
total = size_x * sizeof(float);
if (check_error(total, 1, total, 0) != 0) {
return 1;
}
reset_stats();
toggle1.set(false);
toggle2.set(true);
f6.realize({size_x}, t);
total = size_x * sizeof(float);
if (check_error(total, 1, total, 0) != 0) {
return 1;
}
reset_stats();
toggle1.set(false);
toggle2.set(false);
f6.realize({size_x}, t);
if (check_error(0, 0, 0, 0) != 0) {
return 1;
}
}
{
printf("Running allocate within loop test...\n");
const int size_x = 1200;
const int size_y = 1000;
Func f7("f_7"), f8("f_8"), g5("g_5");
g5(x, y) = x * y;
f7(x, y) = g5(x, y);
f8(x, y) = g5(x, y) + f7(x, y);
g5.store_at(f8, y).compute_at(f8, y);
f7.compute_at(f8, y);
f8.jit_handlers().custom_print = my_print;
reset_stats();
f8.realize({size_x, size_y}, t);
int peak = size_x * sizeof(int);
int total = size_x * size_y * sizeof(int);
if (check_error(peak, size_y, total / size_y, 0) != 0) {
return 1;
}
}
{
printf("Running parallel allocate test...\n");
const int size_x = 1200;
const int size_y = 1000;
Func f9("f_9"), f10("f_10"), g6("g_6");
g6(x, y) = x * y;
f9(x, y) = g6(x, y);
f10(x, y) = g6(x, y) + f9(x, y);
g6.store_at(f10, y).compute_at(f10, y);
f9.compute_at(f10, y);
f10.parallel(y);
f10.jit_handlers().custom_print = my_print;
reset_stats();
f10.realize({size_x, size_y}, t);
int min_heap_peak = size_x * sizeof(int);
int total = size_x * size_y * sizeof(int);
if (check_error_parallel(min_heap_peak, total, size_y, total / size_y, 0) != 0) {
return 1;
}
}
{
printf("Running simple heap allocation test 2...\n");
// On heap allocation (bigger than 1024*16 and buffer g7 size is constant 65x64)
const int size_x = 65;
const int size_y = 64;
Func f11("f_11"), g7("g_7");
g7(x, y) = x;
f11(x, y) = g7(x % size_x, y % size_y);
g7.compute_root();
f11.jit_handlers().custom_print = my_print;
reset_stats();
f11.realize({size_x, size_y}, t);
int total = size_x * size_y * sizeof(int);
if (check_error(total, 1, total, 0) != 0) {
return 1;
}
}
{
printf("Running parallel stack allocation test...\n");
const int size_x = 10;
const int size_y = 10;
Func f12("f_12"), g8("g_8");
g8(x, y) = x;
f12(x, y) = g8(x % size_x, y % size_y);
g8.store_at(f12, y).compute_at(f12, y);
f12.parallel(y);
f12.jit_handlers().custom_print = my_print;
reset_stats();
f12.realize({size_x, size_y}, t);
int stack_size = size_x * size_y * sizeof(int);
if (check_error(0, 0, 0, stack_size) != 0) {
return 1;
}
}
printf("Success!\n");
return 0;
}
|