Closed hecmay closed 4 years ago
What's your HeteroCL code? The IR also looks incorrect to me. The break
statement should be inside the if
statement?
What's your HeteroCL code? The IR also looks incorrect to me. The
break
statement should be inside theif
statement?
You are right. I forgot to check the break
statement . It should break right after finding the insertion point.
The current code looks like this:
def kernel(A, B):
hlib.function.sort(A, B)
A = hcl.placeholder((10, 3), name="A")
B = hcl.placeholder((10, 3), name="B")
s = hcl.create_schedule([A, B], kernel)
The implementation is in hlib.function
def sort(a, b, axis=1, init_value=1e3, name="sort"):
""" sort in axis with ascending order """
assert axis<len(a.shape) and len(a.shape)<=2, "invalid axis"
assert a.shape == b.shape, "shape mismatch"
size = a.shape[axis] # 2d insert sorting
def sreduce(x, Y):
with hcl.for_(0, size, name="i") as i:
with hcl.if_(x < Y[i]):
with hcl.for_(size-1,i,-1) as j:
Y[j] = Y[j-1]
Y[i] = x
hcl.break_()
def sort2d(A, B):
init = hcl.compute((size,), lambda x: init_value)
my_sort = hcl.reducer(init, sreduce)
r = hcl.reduce_axis(0, size, name="rdx")
if axis == 1:
hcl.update(B, lambda x, _y:
my_sort(A[x, r], axis=r), name=name)
else: # sort in y axis
hcl.update(B, lambda _x, y:
my_sort(A[r, y], axis=r), name=name)
# return decorated function
module = hcl.def_([a.shape, b.shape], name=name)(sort2d)
module(a, b)
I created a HCl reducer to realize a sorting algorithm, and wrap it with a HeteroCL module. The gaol is to sort a 10x3 array in axis 1. I checked the IR line by line and did not find any wrong logic inside. But the simulation result is not correct in each row.
The Halide IR:
the input array:
And the sorted result is wrong for most rows (for some rows the result is correct). I guess there might be something wrong with memory in LLVM JIT complication?