From 7ee4eb5c8bbfd6ed7c8fd4e7d4b9580c882c7929 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Thu, 1 Jul 2021 18:59:13 +0100 Subject: [PATCH] add matrix remap example --- openpower/sv/remap.mdwn | 6 ++-- openpower/sv/remapmatrix.py | 38 +++++++++++++++++++++ openpower/sv/remapyield.py | 67 +++++++++++++++++++++---------------- 3 files changed, 80 insertions(+), 31 deletions(-) create mode 100644 openpower/sv/remapmatrix.py diff --git a/openpower/sv/remap.mdwn b/openpower/sv/remap.mdwn index 4c625febc..966a98138 100644 --- a/openpower/sv/remap.mdwn +++ b/openpower/sv/remap.mdwn @@ -125,9 +125,9 @@ assuming that instruction is of the form FRT,FRA,FRC,FRB. Form: SVM-Form SV "Matrix" Form (see [[isatables/fields.text]]) -| 0.5|6.10 |11.15 |16..20 | 21..30 |31| name | -| -- | -- | --- | ------ | ------ |--| -------- | -|OPCD| SVxd | SVyd | SVRM | XO |/ | svremap | +| 0.5|6.10 |11.15 |16..20 | 21..25 | 26..30 |31| name | +| -- | -- | --- | ----- | ------ | ------ |--| -------- | +|OPCD| SVxd | SVyd | SVzd | SVRM | XO |/ | svremap | Fields: diff --git a/openpower/sv/remapmatrix.py b/openpower/sv/remapmatrix.py new file mode 100644 index 000000000..7c94b8947 --- /dev/null +++ b/openpower/sv/remapmatrix.py @@ -0,0 +1,38 @@ +from remapyield import iterate_indices + +def matrix_demo(): + X = [[1, 2, 3], + [3, 4, 5], + ] + Y = [[6, 7], + [8, 9], + [10, 11], + ] + # 3x3 matrix + X = [[12,7,3], + [4 ,5,6], + [7 ,8,9]] + # 3x4 matrix + Y = [[5,8,1,2], + [6,7,3,0], + [4,5,9,1]] + xdim1 = len(X[0]) + ydim1 = len(X) + xdim2 = len(Y[0]) + ydim2 = len(Y) + # set up result matrix of correct size + result = [] + for _ in range(ydim1): + result.append([0]*xdim2) + # iterate through rows of X + for i in range(len(X)): + # iterate through columns of Y + for j in range(len(Y[0])): + # iterate through rows of Y + for k in range(len(Y)): + result[i][j] += X[i][k] * Y[k][j] + for r in result: + print (r) + +if __name__ == '__main__': + matrix_demo() diff --git a/openpower/sv/remapyield.py b/openpower/sv/remapyield.py index c3a5f5301..4cca1f39a 100644 --- a/openpower/sv/remapyield.py +++ b/openpower/sv/remapyield.py @@ -1,48 +1,59 @@ # a "yield" version of the REMAP algorithm. a little easier to read # than the Finite State Machine version -# set the dimension sizes here -xdim = 3 -ydim = 2 -zdim = 1 - -# set total (can repeat, e.g. VL=x*y*z*4) -VL = xdim * ydim * zdim - -lims = [xdim, ydim, zdim] -idxs = [0,0,0] # starting indices -order = [1,0,2] # experiment with different permutations, here -offset = 0 # experiment with different offset, here -invxyz = [0,1,0] # inversion if desired - # python "yield" can be iterated. use this to make it clear how # the indices are generated by using natural-looking nested loops -def iterate_indices(): +def iterate_indices(SVSHAPE): # get indices to iterate over, in the required order - xd = lims[order[2]] - yd = lims[order[1]] - zd = lims[order[0]] + xd = SVSHAPE.lims[SVSHAPE.order[2]] + yd = SVSHAPE.lims[SVSHAPE.order[1]] + zd = SVSHAPE.lims[SVSHAPE.order[0]] # create lists of indices to iterate over in each dimension x_r = list(range(xd)) y_r = list(range(yd)) z_r = list(range(zd)) # invert the indices if needed - if invxyz[order[2]]: x_r.reverse() - if invxyz[order[1]]: y_r.reverse() - if invxyz[order[0]]: z_r.reverse() + if SVSHAPE.invxyz[SVSHAPE.order[2]]: x_r.reverse() + if SVSHAPE.invxyz[SVSHAPE.order[1]]: y_r.reverse() + if SVSHAPE.invxyz[SVSHAPE.order[0]]: z_r.reverse() # start an infinite (wrapping) loop + skip = 0 while True: for x in x_r: # loop over 3rd order dimension for y in y_r: # loop over 2nd order dimension for z in z_r: # loop over 1st order dimension + # skip the first entries up to offset + if skip < SVSHAPE.offset: + skip += 1 + continue # construct the (up to) 3D remap schedule yield (x + y * xd + z * xd * yd) -# enumerate over the iterator function, getting new indices -for idx, new_idx in enumerate(iterate_indices()): - if idx < offset: - continue - if idx >= offset + VL: - break - print ("%d->%d" % (idx, new_idx)) +def demo(): + # set the dimension sizes here + xdim = 3 + ydim = 2 + zdim = 1 + + # set total (can repeat, e.g. VL=x*y*z*4) + VL = xdim * ydim * zdim + + # set up an SVSHAPE + class SVSHAPE: + pass + SVSHAPE0 = SVSHAPE() + SVSHAPE0.lims = [xdim, ydim, zdim] + SVSHAPE0.idxs = [0,0,0] # starting indices + SVSHAPE0.order = [1,0,2] # experiment with different permutations, here + SVSHAPE0.offset = 0 # experiment with different offset, here + SVSHAPE0.invxyz = [0,1,0] # inversion if desired + + # enumerate over the iterator function, getting new indices + for idx, new_idx in enumerate(iterate_indices(SVSHAPE0)): + if idx >= VL: + break + print ("%d->%d" % (idx, new_idx)) +# run the demo +if __name__ == '__main__': + demo() -- 2.30.2