Repeated random numbers

Dear Taichi friends,
I am trying to use taichi for an MPM simulation where I seed ellipsoids. Today I realised that many supposedly random particles have identical 3d coordinates! I’ve been trying to figure out the error, but without success… Here’s a minimal version of the code (simplified from the taichi_elements mpm_solver code). I’m also pasting the output, where you can see that many particles have identical 3d coordinates.

Thank you in advance for your help!

import numpy as np
from sklearn.neighbors import KDTree
import taichi as ti
ti.require_version(0, 7, 10)
@ti.data_oriented
class RandomTest:
  @ti.func
  def random_point_in_unit_sphere(self):
    ret = ti.Vector.zero(dt=ti.f32, n=self.dim)
    while True:
      for i in ti.static(range(self.dim)):
        ret[i] = ti.random(ti.f32) * 2 - 1
      if ret.norm_sqr() <= 1:
        break
    return ret
  @ti.kernel
  def seed_sphere(self, new_particles: ti.i32):
      for ind in range(self.n_particles[None], self.n_particles[None] + new_particles):
        self.x[ind] = self.random_point_in_unit_sphere()
  @ti.kernel
  def copy_dynamic_nd(self, np_x: ti.ext_arr(), input_x: ti.template()):
    for i in self.x:
      for j in ti.static(range(self.dim)):
        np_x[i, j] = input_x[i][j]
  def particle_info(self):
    np_x = np.ndarray((self.n_particles[None], self.dim), dtype=np.float32)
    self.copy_dynamic_nd(np_x, self.x)
    return np_x
  def __init__(self):
    self.dim = 3
    self.n_particles = ti.field(ti.i32, shape=())
    self.x = ti.Vector.field(self.dim, ti.f32)
    ti.root.dynamic(ti.i, 2**24, 8192).place(self.x)
    # seeding 100,000 random particles
    self.seed_sphere(100000)
    self.n_particles[None] += 100000

# instantiate the class
rtest = RandomTest()
np_x = rtest.particle_info()

# find 10 neighbours using a KD tree
tree = KDTree(np_x)
dist, ind = tree.query(np_x, k=10)

# print the 10 closest neighbours for the first particle
print(ind[0])
print(np_x[ind[0]])

Here’s the result:

[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/n2/lrm77s050d1bccp2614f8w300000gq/T/taichi-vmdmubus
[Taichi] version 0.7.10, llvm 10.0.0, commit 0f0205fc, osx, python 3.7.9
[Taichi] materializing...
[    0 84283 84527 84313 84495 87624 87433 87377 87525 87491]
[[-0.81590587 -0.394037   -0.19519258]
 [-0.8269088  -0.40203518 -0.21380717]
 [-0.8269088  -0.40203518 -0.21380717]
 [-0.8269088  -0.40203518 -0.21380717]
 [-0.8269088  -0.40203518 -0.21380717]
 [-0.8057588  -0.36902577 -0.19828415]
 [-0.8057588  -0.36902577 -0.19828415]
 [-0.8057588  -0.36902577 -0.19828415]
 [-0.8057588  -0.36902577 -0.19828415]
 [-0.8057588  -0.36902577 -0.19828415]]

It looks like the script hasn’t called ti.init() before running the kernel. This may result in uninitialized random states from which all CPU/GPU threads will produce the same random number sequence.

In the latest version, not calling ti.init() will result in an error to ensure that the runtime is properly initialized.

2 个赞

Argh!! That was it! Thank you so much!