From: Luke Kenneth Casson Leighton Date: Sat, 25 Jun 2022 18:48:48 +0000 (+0100) Subject: move unit test for parallel prefix reduce to separate module X-Git-Tag: opf_rfc_ls005_v1~1523 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=9a2edf869c36f660dcb156d26eb423ca0727fdca;p=libreriscv.git move unit test for parallel prefix reduce to separate module test_preduce.py --- diff --git a/openpower/sv/preduce.py b/openpower/sv/preduce.py index b369778ee..87120c4d8 100644 --- a/openpower/sv/preduce.py +++ b/openpower/sv/preduce.py @@ -1,42 +1,3 @@ -from copy import copy - -def preduce(vl, vec, pred): - vec = copy(vec) - step = 1 - print(" start", step, pred, vec) - while step < vl: - step *= 2 - for i in range(0, vl, step): - other = i + step // 2 - other_pred = other < vl and pred[other] - if pred[i] and other_pred: - vec[i] += vec[other] - elif other_pred: - vec[i] = vec[other] - print(" row", step, pred, vec) - return vec - - -def preducei(vl, vec, pred): - vec = copy(vec) - step = 1 - ix = list(range(vl)) # indices move rather than copy data - print(" start", step, pred, vec) - while step < vl: - step *= 2 - for i in range(0, vl, step): - other = i + step // 2 - ci = ix[i] - oi = ix[other] if other < vl else None - other_pred = other < vl and pred[oi] - if pred[ci] and other_pred: - vec[ci] += vec[oi] - elif other_pred: - ix[i] = oi # leave data in-place, copy index instead - print(" row", step, pred, vec, ix) - return vec - - def preduce_yield(vl, vec, pred): step = 1 ix = list(range(vl)) @@ -52,75 +13,6 @@ def preduce_yield(vl, vec, pred): elif other_pred: ix[i] = oi - def preduce_y(vl, vec, pred): for i, other in preduce_yield(vl, vec, pred): vec[i] += vec[other] - - -if __name__ == '__main__': - vec = [1, 2, 3, 4, 9, 5, 6] - prd = [0, 1, 1, 1, 0, 0, 1] - print (vec) - res = preduce(len(vec), vec, prd) - print (res) - res2 = preducei(len(vec), vec, prd) - print (res2) - print () - preduce_y(len(vec), vec, prd) - print (vec) - print () - assert vec == res2 - - vec = [1, 2, 3, 4, 9, 5, 6] - prd = [1, 0, 0, 1, 1, 0, 1] - print (vec) - res = preduce(len(vec), vec, prd) - print (res) - res2 = preducei(len(vec), vec, prd) - print (res2) - print () - preduce_y(len(vec), vec, prd) - print (vec) - print () - assert vec == res2 - - vec = [1, 2, 3, 4, 9, 5, 6, 8] - prd = [0, 0, 0, 0, 1, 1, 1, 1] - print (vec) - res = preduce(len(vec), vec, prd) - print (res) - res2 = preducei(len(vec), vec, prd) - print (res2) - print () - preduce_y(len(vec), vec, prd) - print (vec) - print () - assert vec == res2 - - vec = [1, 2, 3, 4, 9, 5, 6, 8] - prd = [0, 1, 0, 0, 0, 1, 0, 1] - print (vec) - res = preduce(len(vec), vec, prd) - print (res) - res2 = preducei(len(vec), vec, prd) - print (res2) - print () - preduce_y(len(vec), vec, prd) - print (vec) - print () - assert vec == res2 - - vec = [1, 2, 3, 4, 9, 5, 6] - prd = [1, 0, 1, 1, 0, 0, 1] - print (vec) - res = preduce(len(vec), vec, prd) - print (res) - res2 = preducei(len(vec), vec, prd) - print (res2) - print () - preduce_y(len(vec), vec, prd) - print (vec) - print () - assert vec == res2 - diff --git a/openpower/sv/test_preduce.py b/openpower/sv/test_preduce.py new file mode 100644 index 000000000..d5148428c --- /dev/null +++ b/openpower/sv/test_preduce.py @@ -0,0 +1,107 @@ +from preduce import preduce_y +from copy import copy + +def preduce(vl, vec, pred): + vec = copy(vec) + step = 1 + print(" start", step, pred, vec) + while step < vl: + step *= 2 + for i in range(0, vl, step): + other = i + step // 2 + other_pred = other < vl and pred[other] + if pred[i] and other_pred: + vec[i] += vec[other] + elif other_pred: + vec[i] = vec[other] + print(" row", step, pred, vec) + return vec + + +def preducei(vl, vec, pred): + vec = copy(vec) + step = 1 + ix = list(range(vl)) # indices move rather than copy data + print(" start", step, pred, vec) + while step < vl: + step *= 2 + for i in range(0, vl, step): + other = i + step // 2 + ci = ix[i] + oi = ix[other] if other < vl else None + other_pred = other < vl and pred[oi] + if pred[ci] and other_pred: + vec[ci] += vec[oi] + elif other_pred: + ix[i] = oi # leave data in-place, copy index instead + print(" row", step, pred, vec, ix) + return vec + + + +if __name__ == '__main__': + vec = [1, 2, 3, 4, 9, 5, 6] + prd = [0, 1, 1, 1, 0, 0, 1] + print (vec) + res = preduce(len(vec), vec, prd) + print (res) + res2 = preducei(len(vec), vec, prd) + print (res2) + print () + preduce_y(len(vec), vec, prd) + print (vec) + print () + assert vec == res2 + + vec = [1, 2, 3, 4, 9, 5, 6] + prd = [1, 0, 0, 1, 1, 0, 1] + print (vec) + res = preduce(len(vec), vec, prd) + print (res) + res2 = preducei(len(vec), vec, prd) + print (res2) + print () + preduce_y(len(vec), vec, prd) + print (vec) + print () + assert vec == res2 + + vec = [1, 2, 3, 4, 9, 5, 6, 8] + prd = [0, 0, 0, 0, 1, 1, 1, 1] + print (vec) + res = preduce(len(vec), vec, prd) + print (res) + res2 = preducei(len(vec), vec, prd) + print (res2) + print () + preduce_y(len(vec), vec, prd) + print (vec) + print () + assert vec == res2 + + vec = [1, 2, 3, 4, 9, 5, 6, 8] + prd = [0, 1, 0, 0, 0, 1, 0, 1] + print (vec) + res = preduce(len(vec), vec, prd) + print (res) + res2 = preducei(len(vec), vec, prd) + print (res2) + print () + preduce_y(len(vec), vec, prd) + print (vec) + print () + assert vec == res2 + + vec = [1, 2, 3, 4, 9, 5, 6] + prd = [1, 0, 1, 1, 0, 0, 1] + print (vec) + res = preduce(len(vec), vec, prd) + print (res) + res2 = preducei(len(vec), vec, prd) + print (res2) + print () + preduce_y(len(vec), vec, prd) + print (vec) + print () + assert vec == res2 +