from copy import copy
def preduce(vl, vec, pred):
+ vec = copy(vec) # must not damage predicate
pred = copy(pred) # must not damage predicate
step = 1
+ print(" start", step, pred, vec)
while step < vl:
step *= 2
for i in range(0, vl, step):
vec[i] = vec[other]
pred[i] |= other_pred
print(" row", step, pred, vec)
+ return vec
+
+def preducei(vl, vec, pred):
+ pred = copy(pred) # must not damage predicate
+ step = 1
+ ix = list(range(vl))
+ 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
+ pred[ci] |= other_pred
+ print(" row", step, pred, vec, ix)
if __name__ == '__main__':
vec = [1, 2, 3, 4, 9, 5, 6]
- prd = [1, 1, 1, 1, 0, 1, 1]
+ prd = [0, 1, 1, 1, 0, 0, 1]
print (vec)
- preduce(len(vec), vec, prd)
+ res = preduce(len(vec), vec, prd)
+ print (res)
+ preducei(len(vec), vec, prd)
print (vec)
print ()
+ assert vec == res