def transition(sVals, action, bounds, trans, goal): dx = hcl.scalar(0, "dx") dy = hcl.scalar(0, "dy") mag = hcl.scalar(0, "mag") # Check if moving from a goal state dx[0] = sVals[0] - goal[0, 0] dy[0] = sVals[1] - goal[0, 1] mag[0] = hcl.sqrt((dx[0] * dx[0]) + (dy[0] * dy[0])) with hcl.if_( hcl.and_(mag[0] <= 1.0, sVals[2] <= goal[1, 1], sVals[2] >= goal[1, 0])): trans[0, 0] = 0 # Check if moving from an obstacle with hcl.elif_( hcl.or_(sVals[0] < bounds[0, 0] + 0.2, sVals[0] > bounds[0, 1] - 0.2)): trans[0, 0] = 0 with hcl.elif_( hcl.or_(sVals[1] < bounds[1, 0] + 0.2, sVals[1] > bounds[1, 1] - 0.2)): trans[0, 0] = 0 # Standard move with hcl.else_(): trans[0, 0] = 1.0 trans[0, 1] = sVals[0] + (0.6 * action[0] * hcl.cos(sVals[2])) trans[0, 2] = sVals[1] + (0.6 * action[0] * hcl.sin(sVals[2])) trans[0, 3] = sVals[2] + (0.6 * action[1]) # Adjust for periodic dimension with hcl.while_(trans[0, 3] > 3.141592653589793): trans[0, 3] -= 6.283185307179586 with hcl.while_(trans[0, 3] < -3.141592653589793): trans[0, 3] += 6.283185307179586
def kernel(A): i = hcl.scalar(0) with hcl.while_(True): with hcl.if_(i[0] > 5): hcl.break_() A[i[0]] = i[0] i[0] += 1
def kernel(matrix_1, matrix_2): first_matrix = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y], "first_matrix") return_matrix = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y] + 7, "return_matrix") ax = hcl.scalar(0) with hcl.while_(ax.v < 3): matrix_A = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y] + 7, "matrix_A") with hcl.for_(0, 2, name="for_loop_in") as h: matrix_B = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y] + 8, "matrix_B") with hcl.if_(matrix_1[0, 2] >= 0): matrix_C = hcl.compute((m, k), lambda x, y : matrix_1[x, x] + matrix_2[x, x] + 9, "matrix_C") hcl.assert_(matrix_1[0, 0]> 0, "assert message in the if statement %d", matrix_C[0, 0]) matrix_D = hcl.compute((m, k), lambda x, y : matrix_1[x, x] + matrix_2[x, x] + 9, "matrix_D") hcl.print(0, "in if statement\n") hcl.assert_(matrix_1[0, 0]> 1, "assert message for loop") matrix_F = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y] + 8, "matrix_F") hcl.print(0, "in for loop\n") hcl.assert_(matrix_1[0, 0]> 2, "assert error, matrix_A[1, 1]: %d matrix_A[2, 1]: %d matrix_A[3, 1]: %d", [matrix_A[1, 1], matrix_A[2, 1], matrix_A[3, 1]]) hcl.print(0, "in the while loop\n") ax.v = ax.v + 1 hcl.assert_(matrix_1[0, 0]> 3, "assert message end") matrix_E = hcl.compute((m, k), lambda x, y : matrix_1[x, y] + matrix_2[x, y] + 10, "matrix_E") hcl.print(0, "this should not be printed\n") return return_matrix
def kernel(A): with hcl.Stage(): i = hcl.local(0) with hcl.while_(True): with hcl.if_(i[0] > 5): hcl.break_() A[i[0]] = i[0] i[0] += 1
def popcount(value): #Calculate the number of one in a binary number count = hcl.scalar(0, "count") numb = hcl.scalar(value, "numb", dtype=hcl.UInt(in_bw)) with hcl.while_(numb.v > 0): count.v += 1 numb.v &= numb.v - 1 return count.v
def insertion_sort(A): # Introduce a stage. with hcl.Stage("S"): # for i in range(1, A.shape[0]) # We can name the axis with hcl.for_(1, A.shape[0], name="i") as i: key = hcl.local(A[i], "key") j = hcl.local(i - 1, "j") # while(j >= 0 && key < A[j]) with hcl.while_(hcl.and_(j >= 0, key < A[j])): A[j + 1] = A[j] j[0] -= 1 A[j + 1] = key[0]
def kernel(A): with hcl.Stage(): a = hcl.scalar(0) with hcl.while_(a[0] < 10): A[a[0]] = a[0] a[0] += 1
def solve_Qopt(Qopt, actions, intermeds, trans, interpV, gamma, epsilon, iVals, sVals, bounds, goal, ptsEachDim, count, maxIters, useNN, fillVal): reSweep = hcl.scalar(1, "reSweep") oldQ = hcl.scalar(0, "oldV") newQ = hcl.scalar(0, "newV") with hcl.while_(hcl.and_(reSweep[0] == 1, count[0] < maxIters[0])): reSweep[0] = 0 # Perform value iteration by sweeping in direction 1 with hcl.Stage("Sweep_1"): with hcl.for_(0, Qopt.shape[0], name="i") as i: with hcl.for_(0, Qopt.shape[1], name="j") as j: with hcl.for_(0, Qopt.shape[2], name="k") as k: with hcl.for_(0, Qopt.shape[3], name="a") as a: oldQ[0] = Qopt[i, j, k, a] updateQopt(i, j, k, a, iVals, sVals, Qopt, actions, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN, fillVal) newQ[0] = Qopt[i, j, k, a] evaluateConvergence(newQ, oldQ, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 2 with hcl.Stage("Sweep_2"): # For all states with hcl.for_(1, Qopt.shape[0] + 1, name="i") as i: with hcl.for_(1, Qopt.shape[1] + 1, name="j") as j: with hcl.for_(1, Qopt.shape[2] + 1, name="k") as k: i2 = Qopt.shape[0] - i j2 = Qopt.shape[1] - j k2 = Qopt.shape[2] - k # For all actions with hcl.for_(0, Qopt.shape[3], name="a") as a: oldQ[0] = Qopt[i2, j2, k2, a] updateQopt(i2, j2, k2, a, iVals, sVals, Qopt, actions, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN, fillVal) newQ[0] = Qopt[i2, j2, k2, a] evaluateConvergence(newQ, oldQ, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 3 with hcl.Stage("Sweep_3"): # For all states with hcl.for_(1, Qopt.shape[0] + 1, name="i") as i: with hcl.for_(0, Qopt.shape[1], name="j") as j: with hcl.for_(0, Qopt.shape[2], name="k") as k: i2 = Qopt.shape[0] - i # For all actions with hcl.for_(0, Qopt.shape[3], name="a") as a: oldQ[0] = Qopt[i2, j, k, a] updateQopt(i2, j, k, a, iVals, sVals, Qopt, actions, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN, fillVal) newQ[0] = Qopt[i2, j, k, a] evaluateConvergence(newQ, oldQ, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 4 with hcl.Stage("Sweep_4"): # For all states with hcl.for_(0, Qopt.shape[0], name="i") as i: with hcl.for_(1, Qopt.shape[1] + 1, name="j") as j: with hcl.for_(0, Qopt.shape[2], name="k") as k: j2 = Qopt.shape[1] - j # For all actions with hcl.for_(0, Qopt.shape[3], name="a") as a: oldQ[0] = Qopt[i, j2, k, a] updateQopt(i, j2, k, a, iVals, sVals, Qopt, actions, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN, fillVal) newQ[0] = Qopt[i, j2, k, a] evaluateConvergence(newQ, oldQ, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 5 with hcl.Stage("Sweep_5"): # For all states with hcl.for_(0, Qopt.shape[0], name="i") as i: with hcl.for_(0, Qopt.shape[1], name="j") as j: with hcl.for_(1, Qopt.shape[2] + 1, name="k") as k: k2 = Qopt.shape[2] - k # For all actions with hcl.for_(0, Qopt.shape[3], name="a") as a: oldQ[0] = Qopt[i, j, k2, a] updateQopt(i, j, k2, a, iVals, sVals, Qopt, actions, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN, fillVal) newQ[0] = Qopt[i, j, k2, a] evaluateConvergence(newQ, oldQ, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 6 with hcl.Stage("Sweep_6"): # For all states with hcl.for_(1, Qopt.shape[0] + 1, name="i") as i: with hcl.for_(1, Qopt.shape[1] + 1, name="j") as j: with hcl.for_(0, Qopt.shape[2], name="k") as k: i2 = Qopt.shape[0] - i j2 = Qopt.shape[1] - j # For all actions with hcl.for_(0, Qopt.shape[3], name="a") as a: oldQ[0] = Qopt[i2, j2, k, a] updateQopt(i2, j2, k, a, iVals, sVals, Qopt, actions, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN, fillVal) newQ[0] = Qopt[i2, j2, k, a] evaluateConvergence(newQ, oldQ, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 7 with hcl.Stage("Sweep_7"): # For all states with hcl.for_(1, Qopt.shape[0] + 1, name="i") as i: with hcl.for_(0, Qopt.shape[1], name="j") as j: with hcl.for_(1, Qopt.shape[2] + 1, name="k") as k: i2 = Qopt.shape[0] - i k2 = Qopt.shape[2] - k # For all actions with hcl.for_(0, Qopt.shape[3], name="a") as a: oldQ[0] = Qopt[i2, j, k2, a] updateQopt(i2, j, k2, a, iVals, sVals, Qopt, actions, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN, fillVal) newQ[0] = Qopt[i2, j, k2, a] evaluateConvergence(newQ, oldQ, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 8 with hcl.Stage("Sweep_8"): # For all states with hcl.for_(0, Qopt.shape[0], name="i") as i: with hcl.for_(1, Qopt.shape[1] + 1, name="j") as j: with hcl.for_(1, Qopt.shape[2] + 1, name="k") as k: j2 = Qopt.shape[1] - j k2 = Qopt.shape[2] - k # For all actions with hcl.for_(0, Qopt.shape[3], name="a") as a: oldQ[0] = Qopt[i, j2, k2, a] updateQopt(i, j2, k2, a, iVals, sVals, Qopt, actions, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN, fillVal) newQ[0] = Qopt[i, j2, k2, a] evaluateConvergence(newQ, oldQ, epsilon, reSweep) count[0] += 1
def smith_waterman(seqA, seqB, consA, consB): def similarity_score(a, b): return hcl.select(a == b, 1, penalty) def find_max(A, len_): max_ = hcl.local(A[0], "max") act_ = hcl.local(0, "act") with hcl.for_(0, len_) as i: with hcl.if_(A[i] > max_[0]): max_[0] = A[i] act_[0] = i return max_[0], act_[0] matrix_max = hcl.local(0, "maxtrix_max") i_max = hcl.local(0, "i_max") j_max = hcl.local(0, "j_max") matrix = hcl.compute((lenA + 1, lenB + 1), lambda x, y: 0, "matrix") action = hcl.compute(matrix.shape, lambda x, y: 3, "action") def populate_matrix(i, j): trace_back = hcl.compute((4, ), lambda x: 0, "trace_back") with hcl.if_(hcl.and_(i != 0, j != 0)): trace_back[0] = matrix[i-1, j-1] + \ similarity_score(seqA[i-1], seqB[j-1]) trace_back[1] = matrix[i - 1, j] + penalty trace_back[2] = matrix[i, j - 1] + penalty trace_back[3] = 0 matrix[i, j], action[i, j] = find_max(trace_back, 4) with hcl.if_(matrix[i, j] > matrix_max[0]): matrix_max[0] = matrix[i, j] i_max[0] = i j_max[0] = j P = hcl.mutate((lenA + 1, lenB + 1), lambda i, j: populate_matrix(i, j)) def align(curr_i, curr_j, next_i, next_j): outA = hcl.local(0, "a") outB = hcl.local(0, "b") with hcl.if_(next_i[0] == curr_i[0]): outA[0] = 0 with hcl.else_(): outA[0] = seqA[curr_i[0] - 1] with hcl.if_(next_j[0] == curr_j[0]): outB[0] = 0 with hcl.else_(): outB[0] = seqB[curr_j[0] - 1] return outA[0], outB[0] def get_next(action, i, j): act_ = hcl.local(action[i][j], "act") next_i = hcl.local(0, "next_i") next_j = hcl.local(0, "next_j") with hcl.if_(act_[0] == 0): next_i[0] = i - 1 next_j[0] = j - 1 with hcl.elif_(act_[0] == 1): next_i[0] = i - 1 next_j[0] = j with hcl.elif_(act_[0] == 2): next_i[0] = i next_j[0] = j - 1 with hcl.else_(): next_i[0] = i next_j[0] = j return next_i[0], next_j[0] with hcl.Stage("T"): curr_i = hcl.local(i_max[0], "curr_i") curr_j = hcl.local(j_max[0], "curr_j") next_i = hcl.local(0, "next_i") next_j = hcl.local(0, "next_j") next_i[0], next_j[0] = get_next(action, curr_i[0], curr_j[0]) tick = hcl.local(0, "tick") with hcl.while_( hcl.or_(curr_i[0] != next_i[0], curr_j[0] != next_j[0])): consA[tick[0]], consB[tick[0]] = \ align(curr_i, curr_j, next_i, next_j) curr_i[0], curr_j[0] = next_i[0], next_j[0] next_i[0], next_j[0] = get_next(action, curr_i[0], curr_j[0]) tick[0] += 1
def solve_Vopt(Vopt, actions, intermeds, trans, interpV, gamma, epsilon, iVals, sVals, bounds, goal, ptsEachDim, count, maxIters, useNN): reSweep = hcl.scalar(1, "reSweep") oldV = hcl.scalar(0, "oldV") newV = hcl.scalar(0, "newV") with hcl.while_(hcl.and_(reSweep[0] == 1, count[0] < maxIters[0])): reSweep[0] = 0 # Perform value iteration by sweeping in direction 1 with hcl.Stage("Sweep_1"): with hcl.for_(0, Vopt.shape[0], name="i") as i: with hcl.for_(0, Vopt.shape[1], name="j") as j: with hcl.for_(0, Vopt.shape[2], name="k") as k: with hcl.for_(0, Vopt.shape[3], name="l") as l: with hcl.for_(0, Vopt.shape[4], name="m") as m: oldV[0] = Vopt[i, j, k, l, m] updateVopt(MDP_object, i, j, k, l, m, iVals, sVals, actions, Vopt, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN) newV[0] = Vopt[i, j, k, l, m] evaluateConvergence( newV, oldV, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 2 with hcl.Stage("Sweep_2"): with hcl.if_(useNN[0] == 1): with hcl.for_(1, Vopt.shape[0] + 1, name="i") as i: with hcl.for_(1, Vopt.shape[1] + 1, name="j") as j: with hcl.for_(1, Vopt.shape[2] + 1, name="k") as k: with hcl.for_(0, Vopt.shape[3], name="l") as l: with hcl.for_(0, Vopt.shape[4], name="m") as m: i2 = Vopt.shape[0] - i j2 = Vopt.shape[1] - j k2 = Vopt.shape[2] - k oldV[0] = Vopt[i2, j2, k2, l, m] updateVopt(MDP_object, i2, j2, k2, l, m, iVals, sVals, actions, Vopt, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN) newV[0] = Vopt[i2, j2, k2, l, m] evaluateConvergence( newV, oldV, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 3 with hcl.Stage("Sweep_3"): with hcl.if_(useNN[0] == 1): with hcl.for_(1, Vopt.shape[0] + 1, name="i") as i: with hcl.for_(0, Vopt.shape[1], name="j") as j: with hcl.for_(0, Vopt.shape[2], name="k") as k: with hcl.for_(0, Vopt.shape[3], name="l") as l: with hcl.for_(0, Vopt.shape[4], name="m") as m: i2 = Vopt.shape[0] - i oldV[0] = Vopt[i2, j, k, l, m] updateVopt(MDP_object, i2, j, k, l, m, iVals, sVals, actions, Vopt, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN) newV[0] = Vopt[i2, j, k, l, m] evaluateConvergence( newV, oldV, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 4 with hcl.Stage("Sweep_4"): with hcl.if_(useNN[0] == 1): with hcl.for_(0, Vopt.shape[0], name="i") as i: with hcl.for_(1, Vopt.shape[1] + 1, name="j") as j: with hcl.for_(0, Vopt.shape[2], name="k") as k: with hcl.for_(0, Vopt.shape[3], name="l") as l: with hcl.for_(0, Vopt.shape[4], name="m") as m: j2 = Vopt.shape[1] - j oldV[0] = Vopt[i, j2, k, l, m] updateVopt(MDP_object, i, j2, k, l, m, iVals, sVals, actions, Vopt, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN) newV[0] = Vopt[i, j2, k, l, m] evaluateConvergence( newV, oldV, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 5 with hcl.Stage("Sweep_5"): with hcl.if_(useNN[0] == 1): with hcl.for_(0, Vopt.shape[0], name="i") as i: with hcl.for_(0, Vopt.shape[1], name="j") as j: with hcl.for_(1, Vopt.shape[2] + 1, name="k") as k: with hcl.for_(0, Vopt.shape[3], name="l") as l: with hcl.for_(0, Vopt.shape[4], name="m") as m: k2 = Vopt.shape[2] - k oldV[0] = Vopt[i, j, k2, l, m] updateVopt(MDP_object, i, j, k2, l, m, iVals, sVals, actions, Vopt, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN) newV[0] = Vopt[i, j, k2, l, m] evaluateConvergence( newV, oldV, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 6 with hcl.Stage("Sweep_6"): with hcl.if_(useNN[0] == 1): with hcl.for_(1, Vopt.shape[0] + 1, name="i") as i: with hcl.for_(1, Vopt.shape[1] + 1, name="j") as j: with hcl.for_(0, Vopt.shape[2], name="k") as k: with hcl.for_(0, Vopt.shape[3], name="l") as l: with hcl.for_(0, Vopt.shape[4], name="m") as m: i2 = Vopt.shape[0] - i j2 = Vopt.shape[1] - j oldV[0] = Vopt[i2, j2, k, l, m] updateVopt(MDP_object, i2, j2, k, l, m, iVals, sVals, actions, Vopt, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN) newV[0] = Vopt[i2, j2, k, l, m] evaluateConvergence( newV, oldV, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 7 with hcl.Stage("Sweep_7"): with hcl.if_(useNN[0] == 1): with hcl.for_(1, Vopt.shape[0] + 1, name="i") as i: with hcl.for_(0, Vopt.shape[1], name="j") as j: with hcl.for_(1, Vopt.shape[2] + 1, name="k") as k: with hcl.for_(0, Vopt.shape[3], name="l") as l: with hcl.for_(0, Vopt.shape[4], name="m") as m: i2 = Vopt.shape[0] - i k2 = Vopt.shape[2] - k oldV[0] = Vopt[i2, j, k2, l, m] updateVopt(MDP_object, i2, j, k2, l, m, iVals, sVals, actions, Vopt, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN) newV[0] = Vopt[i2, j, k2, l, m] evaluateConvergence( newV, oldV, epsilon, reSweep) count[0] += 1 # Perform value iteration by sweeping in direction 8 with hcl.Stage("Sweep_8"): with hcl.if_(useNN[0] == 1): with hcl.for_(0, Vopt.shape[0], name="i") as i: with hcl.for_(1, Vopt.shape[1] + 1, name="j") as j: with hcl.for_(1, Vopt.shape[2] + 1, name="k") as k: with hcl.for_(0, Vopt.shape[3], name="l") as l: with hcl.for_(0, Vopt.shape[4], name="m") as m: j2 = Vopt.shape[1] - j k2 = Vopt.shape[2] - k oldV[0] = Vopt[i, j2, k2, l, m] updateVopt(MDP_object, i, j2, k2, l, m, iVals, sVals, actions, Vopt, intermeds, trans, interpV, gamma, bounds, goal, ptsEachDim, useNN) newV[0] = Vopt[i, j2, k2, l, m] evaluateConvergence( newV, oldV, epsilon, reSweep) count[0] += 1