|
| 1 | +using ImplicitBVH |
| 2 | +using ImplicitBVH: BBox, BSphere |
| 3 | +import AcceleratedKernels as AK |
| 4 | + |
| 5 | +using Profile |
| 6 | +using PProf |
| 7 | + |
| 8 | +using Metal |
| 9 | +# using AtomixMetal |
| 10 | + |
| 11 | +using Random |
| 12 | +Random.seed!(0) |
| 13 | + |
| 14 | + |
| 15 | +function get_interacting_pairs(particle_centers::AbstractMatrix, cutoff::AbstractFloat) |
| 16 | + |
| 17 | + # Construct bounding sphere around each particle of `cutoff` radius |
| 18 | + num_particles = size(particle_centers, 2) |
| 19 | + bounding_volumes = similar(particle_centers, BSphere{Float32}, num_particles) |
| 20 | + AK.foreachindex(bounding_volumes) do i |
| 21 | + bounding_volumes[i] = BSphere{Float32}( |
| 22 | + (particle_centers[1, i], particle_centers[2, i], particle_centers[3, i]), |
| 23 | + cutoff, |
| 24 | + ) |
| 25 | + end |
| 26 | + |
| 27 | + # Construct BVH, merging BSpheres into BBoxes, and using 32-bit Morton indices |
| 28 | + bvh = BVH(bounding_volumes, BBox{Float32}, UInt32, default_start_level(num_particles)) |
| 29 | + |
| 30 | + # Traverse BVH - this returns a BVHTraversal |
| 31 | + contacting_pairs = traverse(bvh) |
| 32 | + |
| 33 | + # Return Vector{Tuple{Int32, Int32}} of particle index pairs |
| 34 | + contacting_pairs.contacts |
| 35 | +end |
| 36 | + |
| 37 | + |
| 38 | +particle_centers = rand(Float32, 3, 12_486) |
| 39 | +# particle_centers = MtlArray(particle_centers) |
| 40 | +interacting_pairs = get_interacting_pairs(particle_centers, 0.0312f0) |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +# Collect an allocation profile |
| 46 | +Profile.Allocs.clear() |
| 47 | +Profile.Allocs.@profile get_interacting_pairs(particle_centers, 0.0312f0) |
| 48 | +PProf.Allocs.pprof() |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +# Example output: |
| 53 | +# julia> @show interacting_pairs |
| 54 | +# interacting_pairs = Tuple{Int32, Int32}[(369, 667), (427, 974), ...] |
| 55 | + |
| 56 | + |
| 57 | +# using BenchmarkTools |
| 58 | +# @benchmark get_interacting_pairs(particle_centers, 0.0312f0) |
| 59 | + |
| 60 | + |
| 61 | + |
0 commit comments