-
Hi, I'm developing an algorithm in which I have a set of N points (Nx3 numpy array input, converted to Point3f). I need to loop over these points, and for each one of them spawn a set of R rays and get the first intersection. Along with each point, I have a linear array of N integers, belonging to each of the N points. It's similar to having an outer loop over pixels (in this case my points) and an inner loop over rays. I can use a regular python for loop, but I was wondering if there's a way to use a recorded loop for this. The issue I have is that from the point of view of the input points, N is the width of the data. However, the actual width is R, as I want to process one point at a time in a loop. I've been playing around with this and made some observations:
I'm starting to think this isn't what the loop is intended for. Should I basically have a python loop over my original numpy array? Related question: it's unclear to me what becomes part of the compiled kernel, specifically wrt. the inputs. For instance, if I use a python loop in the above, do I compile a kernel for each iteration? Or can mitsuba somehow figure out that it's the same kernel with different inputs? Or, more likely, does it become one giant kernel with all the input data baked into it? TLDR; looking for some guidance around when to use recorded vs regular loops. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @tomas16
Indeed, if I understood your problem correctly, you don't need a loop at all. One way to think about writing Mitsuba/Dr.Jit code, is that every variable and operation is vectorized. We are basically only doing operations on arrays. n_points = get_points # dr.shape(n_points) == [3, N]
repeated_points = dr.repeat(n_points, R) # # dr.shape(repeated_points) == [3, N * R]
rays = spawn_ray(repeated points) # dr.width(rays) == N*R
si = scene.ray_intersect(rays) The idea here is to "unroll" the loop into array operations. We gain in parallelization, by doing this.
If you haven't already, I'd recommend going through this introduction to Dr.Jit. It goes over kernel caching and recorded loops. |
Beta Was this translation helpful? Give feedback.
Hi @tomas16
Indeed, if I understood your problem correctly, you don't need a loop at all. One way to think about writing Mitsuba/Dr.Jit code, is that every variable and operation is vectorized. We are basically only doing operations on arrays.
The most efficient implementation of your setup would be something like this:
The idea her…