In the future it would be nice for closures to auto-capture addresses and do 
escape analysis / lifetime analysis.

Currently it's tedious to pass `var` parameters and `seq` buffers to closures 
for compute.

For example this is a parallel "max" computation using Weave, and you need to 
capture the address of the var and the lock, and it would be even more verbose 
if `Matrix` was a seq, we would need to capture M[0, 0].addr as a workaround.

<https://github.com/mratsim/weave/blob/b6255afa5816ee431dbf2f59cc6bc605d8d657b8/benchmarks/logsumexp/weave_logsumexp.nim#L242-L265>
    
    
    proc maxWeaveStaged[T: SomeFloat](M: Matrix[T]) : T =
      var max = T(-Inf)
      let maxAddr = max.addr
      
      var lock: Lock
      lock.initLock()
      let lockAddr = lock.addr
      
      parallelForStaged i in 0 ..< M.nrows:
        captures:{maxAddr, lockAddr, M}
        awaitable: maxLoop
        prologue:
          var localMax = T(-Inf)
        loop:
          for j in 0 ..< M.ncols:
            localMax = max(localMax, M[i, j])
            loadBalance(Weave)
        epilogue:
          lockAddr[].acquire()
          maxAddr[] = max(maxAddr[], localMax)
          lockAddr[].release()
      
      let waslastThread = sync(maxLoop)
      lock.deinitLock()
    
    
    Run

Reply via email to