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
|
#include "Halide.h"
using namespace Halide;
using namespace Halide::Internal;
class Checker : public IRMutator {
Stmt visit(const Atomic *op) override {
count_atomics++;
if (!op->mutex_name.empty()) {
count_atomics_with_mutexes++;
}
return IRMutator::visit(op);
}
public:
int count_atomics = 0;
int count_atomics_with_mutexes = 0;
};
int main(int argc, char **argv) {
if (get_jit_target_from_environment().arch == Target::WebAssembly) {
printf("[SKIP] Skipping test for WebAssembly as it does not support atomics yet.\n");
return 0;
}
{
Func f, g;
Var x, y;
f(x, y) = {x, y};
f(x, y) = {f(x, y)[0] + x,
f(x, y)[1] + y};
// The summation is independent in the two tuple components,
// so it can just be two atomic add instructions. No CAS loop
// required.
f.compute_root().update().parallel(y).atomic();
g(x, y) = f(x, y)[0] + f(x, y)[1];
Checker checker;
g.add_custom_lowering_pass(&checker, []() {});
Buffer<int> out = g.realize({128, 128});
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) {
int correct = 2 * x + 2 * y;
if (out(x, y) != correct) {
printf("out(%d, %d) = %d instead of %d\n",
x, y, out(x, y), correct);
return 1;
}
}
}
if (checker.count_atomics != 2 || checker.count_atomics_with_mutexes != 0) {
printf("Expected two atomic nodes, neither of them with mutexes\n");
return 1;
}
}
{
Func f, g;
Var x, y;
f(x, y) = {x, y};
f(x, y) = {f(x, y)[1] + x,
f(x, y)[0] + y};
// The summation is coupled across the two tuple components
// and there are two stores, so we need a mutex.
f.compute_root().update().parallel(y).atomic();
g(x, y) = f(x, y)[0] + f(x, y)[1];
Checker checker;
g.add_custom_lowering_pass(&checker, []() {});
Buffer<int> out = g.realize({128, 128});
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) {
int correct = 2 * x + 2 * y;
if (out(x, y) != correct) {
printf("out(%d, %d) = %d instead of %d\n",
x, y, out(x, y), correct);
return 1;
}
}
}
if (checker.count_atomics != 1 || checker.count_atomics_with_mutexes != 1) {
printf("Expected one atomic node, with mutex\n");
return 1;
}
}
{
Func f, g;
Var x, y;
f(x, y) = {x, y, 0};
f(x, y) = {f(x, y)[1] + x,
f(x, y)[0] + y,
f(x, y)[2] + 1};
// The summation is coupled across the first two tuple
// components and there are two stores, so we need a mutex
// there. The last store could in principle be a separate atomic
// add, but we instead just pack it into the critical section.
f.compute_root().update().parallel(y).atomic();
g(x, y) = f(x, y)[0] + f(x, y)[1] + f(x, y)[2];
Checker checker;
g.add_custom_lowering_pass(&checker, []() {});
Buffer<int> out = g.realize({128, 128});
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) {
int correct = 2 * x + 2 * y + 1;
if (out(x, y) != correct) {
printf("out(%d, %d) = %d instead of %d\n",
x, y, out(x, y), correct);
return 1;
}
}
}
if (checker.count_atomics != 1 || checker.count_atomics_with_mutexes != 1) {
printf("Expected one atomic nodes, with mutex\n");
return 1;
}
}
{
Func f, g;
Var x, y;
f(x, y) = {x, y, x, y};
f(x, y) = {f(x, y)[1] + x,
f(x, y)[0] + y,
f(x, y)[3] + x,
f(x, y)[2] + y};
// The summation is coupled across the first two tuple
// components and the last two components, but they're
// independent so they *could* get two critical sections, but
// it would be on the same mutex, so we just pack them all
// into one critical section.
f.compute_root().update().parallel(y).atomic();
g(x, y) = f(x, y)[0] + f(x, y)[1] + f(x, y)[2] + f(x, y)[3];
Checker checker;
g.add_custom_lowering_pass(&checker, []() {});
Buffer<int> out = g.realize({128, 128});
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) {
int correct = 4 * x + 4 * y;
if (out(x, y) != correct) {
printf("out(%d, %d) = %d instead of %d\n",
x, y, out(x, y), correct);
return 1;
}
}
}
if (checker.count_atomics != 1 || checker.count_atomics_with_mutexes != 1) {
printf("Expected one atomic node, with mutex\n");
return 1;
}
}
{
Func f, g;
Var x, y;
f(x, y) = {x, y};
RDom r(0, 65);
// Update even rows
f(x, r * 2) = {f(x, r * 2 + 1)[1] + x,
f(x, r * 2 - 1)[0] + r * 2};
// Update odd rows using even rows
f(x, r * 2 + 1) = {f(x, r * 2)[1] + x,
f(x, r * 2 + 2)[0] + r * 2 + 1};
// The tuple components have cross-talk, but the loads
// couldn't possibly alias with the stores because of the
// even/odd split. We can just use four atomic adds safely.
f.compute_root();
f.update(0)
.parallel(r)
.atomic();
f.update(1)
.parallel(r)
.atomic();
g(x, y) = f(x, y)[0] + f(x, y)[1];
Checker checker;
g.add_custom_lowering_pass(&checker, []() {});
Buffer<int> out = g.realize({128, 128});
for (int y = 0; y < 128; y++) {
for (int x = 0; x < 128; x++) {
int correct = 2 * x + 2 * y + 1;
if (y & 1) {
// The odd rows happen after the even rows, so
// they get another dose of x + y.
correct += x + y;
}
if (out(x, y) != correct) {
printf("out(%d, %d) = %d instead of %d\n",
x, y, out(x, y), correct);
// return 1;
}
}
}
if (checker.count_atomics != 4 || checker.count_atomics_with_mutexes != 0) {
printf("Expected four atomic nodes, with no mutexes\n");
return 1;
}
}
printf("Success!\n");
return 0;
}
|