Here are a few comments: 1. Computing max via atomics is non-trivial. Reserving it for the `epilogue` is a good idea. 2. `proc comp_max() : float = rand(1.0)` is problematic, `rand` uses a global mutable RNG state underneath, you cannot access that via multiple thread as it will at best require locking and unlocking the RNG at every generation, rendering multithreading useless, and at worst, multiple threads will compete and corrupt the RNG state.
Here is a version using a parallel reducer. It requires a special import as I'm not sure about the API yet. import random, weave, weave/parallel_reduce proc maxReduce(n: int): float64 = var waitableMax: Flowvar[float64] parallelReduceImpl i in 0 .. n, stride = 1: reduce(waitableMax): prologue: var threadLocalRNG = initRand(1234) var localMax = -Inf fold: # Note all thread will start with the same RNG sequence. # Multithreaded RNG is a non-trivial problem, so showing a simple example # since the question is about max localMax = max(localMax, threadLocalRNG.rand(1.0)) merge(remoteMax): localMax = max(localMax, sync(remoteMax)) return localMax result = sync(waitableMax) init(Weave) let max1M = maxReduce(1000000) echo "maxReduce(1000000): ", max1M exit(Weave) Run Version(s) with parallelForStaged coming next.