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
|
#include "Halide.h"
#include <stdio.h>
using namespace Halide;
int main(int argc, char **argv) {
Target target = get_jit_target_from_environment();
if (1) {
// Test a tuple reduction on the gpu
Func f;
Var x, y, xo, yo, xi, yi;
f(x, y) = Tuple(x + y, x - y);
// Updates to a reduction are atomic.
f(x, y) = Tuple(f(x, y)[1] * 2, f(x, y)[0] * 2);
// now equals ((x - y)*2, (x + y)*2)
if (target.has_gpu_feature()) {
f.gpu_tile(x, y, xo, yo, xi, yi, 16, 16);
f.update().gpu_tile(x, y, xo, yo, xi, yi, 16, 16);
} else if (target.has_feature(Target::HVX)) {
f.hexagon(y).vectorize(x, 32);
f.update().hexagon(y).vectorize(x, 32);
}
Realization result = f.realize({1024, 1024});
Buffer<int> a = result[0], b = result[1];
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
int correct_a = (x - y) * 2;
int correct_b = (x + y) * 2;
if (a(x, y) != correct_a || b(x, y) != correct_b) {
printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
x, y, a(x, y), b(x, y), correct_a, correct_b);
return 1;
}
}
}
}
if (1) {
// Now test one that alternates between cpu and gpu per update step
Func f;
Var x, y, xo, yo, xi, yi;
f(x, y) = Tuple(x + y, x - y);
for (size_t i = 0; i < 10; i++) {
// Swap the tuple elements and increment both
f(x, y) = Tuple(f(x, y)[1] + 1, f(x, y)[0] + 1);
}
// Schedule the pure step and the odd update steps on the gpu
if (target.has_gpu_feature()) {
f.gpu_tile(x, y, xo, yo, xi, yi, 16, 16);
} else if (target.has_feature(Target::HVX)) {
f.hexagon(y).vectorize(x, 32);
}
for (int i = 0; i < 10; i++) {
f.update(i).unscheduled();
if (i & 1) {
if (target.has_gpu_feature()) {
f.update(i).gpu_tile(x, y, xo, yo, xi, yi, 16, 16);
} else if (target.has_feature(Target::HVX)) {
f.update(i).hexagon(y).vectorize(x, 32);
}
}
}
Realization result = f.realize({1024, 1024});
Buffer<int> a = result[0], b = result[1];
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
int correct_a = (x + y) + 10;
int correct_b = (x - y) + 10;
if (a(x, y) != correct_a || b(x, y) != correct_b) {
printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
x, y, a(x, y), b(x, y), correct_a, correct_b);
return 1;
}
}
}
}
if (1) {
// Same as above, but switches which steps are gpu and cpu
Func f;
Var x, y, xo, yo, xi, yi;
f(x, y) = Tuple(x + y, x - y);
for (size_t i = 0; i < 10; i++) {
// Swap the tuple elements and increment both
f(x, y) = Tuple(f(x, y)[1] + 1, f(x, y)[0] + 1);
}
// Schedule the even update steps on the gpu
for (int i = 0; i < 10; i++) {
f.update(i).unscheduled();
if (i & 1) {
if (target.has_gpu_feature()) {
f.update(i).gpu_tile(x, y, xo, yo, xi, yi, 16, 16);
} else if (target.has_feature(Target::HVX)) {
f.update(i).hexagon(y).vectorize(x, 32);
}
}
}
Realization result = f.realize({1024, 1024});
Buffer<int> a = result[0], b = result[1];
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
int correct_a = (x + y) + 10;
int correct_b = (x - y) + 10;
if (a(x, y) != correct_a || b(x, y) != correct_b) {
printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
x, y, a(x, y), b(x, y), correct_a, correct_b);
return 1;
}
}
}
}
if (1) {
// In this one, each step only uses one of the tuple elements
// of the previous step, so only that buffer should get copied
// back to host or copied to device.
Func f;
Var x, y, xo, yo, xi, yi;
f(x, y) = Tuple(x + y - 1000, x - y + 1000);
for (size_t i = 0; i < 10; i++) {
f(x, y) = Tuple(f(x, y)[1] - 1, f(x, y)[1] + 1);
}
// Schedule the even update steps on the gpu
for (int i = 0; i < 10; i++) {
f.update(i).unscheduled();
if ((i & 1) == 0) {
if (target.has_gpu_feature()) {
f.update(i).gpu_tile(x, y, xo, yo, xi, yi, 16, 16);
} else if (target.has_feature(Target::HVX)) {
f.update(i).hexagon(y).vectorize(x, 32);
}
}
}
Realization result = f.realize({1024, 1024});
Buffer<int> a = result[0], b = result[1];
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
int correct_a = (x - y + 1000) + 8;
int correct_b = (x - y + 1000) + 10;
if (a(x, y) != correct_a || b(x, y) != correct_b) {
printf("result(%d, %d) = (%d, %d) instead of (%d, %d)\n",
x, y, a(x, y), b(x, y), correct_a, correct_b);
return 1;
}
}
}
}
printf("Success!\n");
return 0;
}
|