Early termination for any/all boolean reduction functions #365
-
Here is the current definition of
In imperative languages, implementations of def all(f, xs):
for x in xs:
if not f(x):
return False # return False on first occurrence of f(x) == False
return True Can Dex support this too - either in the language or as an optimization on the IR? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In principle, I think this could be implemented fairly easily using a while loop instead of a for loop (and this would only work for short-circuit-able reductions, so I think early-termination is possibly less friendly to parallel hardware like GPUs, since it requires dynamic branching. (Although, that being said, if we really care about parallel hardware for There's also a bigger question of "how early" we want to terminate. Using a while loop would avoid looping over the indices after the first
where we use a regular arrow instead of a table arrow, that might be more efficient, because we wouldn't even bother computing the value for indices we aren't going to check. Although maybe an IR pass could convert the table arrow into (effectively) a regular arrow if it saw that the table was never used again (I think there might be an optimization like this already, in fact). |
Beta Was this translation helpful? Give feedback.
In principle, I think this could be implemented fairly easily using a while loop instead of a for loop (and this would only work for short-circuit-able reductions, so
any
andall
would no longer be special-cases ofreduce
).I think early-termination is possibly less friendly to parallel hardware like GPUs, since it requires dynamic branching. (Although, that being said, if we really care about parallel hardware for
any
andall
then we would eventually want to implement them usingwithAccum
or similar instead of usingwithState
.)There's also a bigger question of "how early" we want to terminate. Using a while loop would avoid looping over the indices after the first
False
, but probably wo…