Silly question: how to get max value in parallel for loop

My code tries to compute some values at each grid point

for i,j in ti.ndrange(nx, ny):
    value = computation(i,j)

and I want to get the maximum of these values. My code is

maximum = 0.0
for i,j in ti.ndrange(nx, ny):
    value = computation(i,j)
    maximum = ti.max(maximum,value)

But it does not give me the right answer, I suspect this is because the loop is parallel.

I work around this question by making the loop serialized. By putting some arbitrary if statement on the outermost scope, the for loop becomes serialized. This is because Taichi makes the outermost for loop parallelized.

(Parallel for-loops)[https://taichi.readthedocs.io/en/v0.7.25/hello.html?highlight=parallel%20loop#parallel-for-loops]

if k>0: # some arbitrary if statement 
    maximum = 0.0
    for i,j in ti.ndrange(nx, ny):
        value = computation(i,j)
        maximum = ti.max(maximum,value)

But I am still curious how to actually get the maximum with parallel for loop though.

def get_max():
  res = 0.0
  for i in x:
    ti.atomic_max(res, x[i])
  print(res)

No need be serial here, you can use atomic instrictions, they are gauranteed to not data race.

2 个赞

Wow, I didn’t think it will be that simple! I should’ve read the doc more carefully!

This is really helpful! Thank you!