How can I reduce memory allocation and execution time while ray tracing using VoxelRayTracers package?


I am trying to use VoxelRayTracers.jl package to trace a ray through cubic voxels.

I want to improve the performance of ray tracing by reducing execution time because in my application I have to use many rays in three dimensions.

I wrote a simple example to show my case. I only used one ray, but variable itr represents number of rays. Then I have to sum traced voxel values in each ray.

using VoxelRayTracers
using BenchmarkTools
edges = (-2.0:0.1:2.0, -2.0:0.1:2.0)
ray = (position=[4.946152422706632, 3.433012701892219], velocity=[-0.8660254037844393,-0.5])
all_val = rand(41,41)
itr = 400000

function exit_data(ray, edges, val, itr)
      for i in 1:itr
            out = 0.0
            for hit in eachtraversal(ray, edges)
              out += val[hit.voxelindex]

I have checked execution time and memory allocation by using @btime

@btime exit_data($ray, $edges, $all_val, $itr)

and the result is as below:
710.617 ms (2949120 allocations: 145.00 MiB)

In my real case, I have more than 1.5M rays and points will be 3 dimensions… so, execution time and memory allocation will be very high… Moreover, I have to iterate this whole process more than 1000 times using another algorithm. So, can anyone suggest some ideas to improve performance in terms of execution time and memory allocation?

Try using static vectors here. This should eliminate many of the allocations inside eachtraversal if I understand correctly.


Its a bug. Inference could not handle this line, not sure why. I found a workaround anyway, fixed on master and tagged a release. There should be no need to use a static vector.


@mohamed82008 : Thanks for your reply. I tried with static vectors like ray = (position=@SVector[4.946152422706632, 3.433012701892219], velocity=@SVector[-0.8660254037844393,-0.5]) , but it give similar results with @btime exit_data($ray, $edges, $all_val, $itr)

@jw3126 : Thanks for your fix. Now, I am getting timing values as 506.445 ms (0 allocations: 0 bytes) and time of execution also improved by 200ms.

Thanks Again,

1 Like