Skip to content

Commit

Permalink
Merge pull request #91 from lmejn/feature/CUDA-ext
Browse files Browse the repository at this point in the history
CUDA Extension
  • Loading branch information
lmejn authored Nov 2, 2023
2 parents 5f86664 + 4a6d58a commit 78794e2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 32 deletions.
13 changes: 9 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
name = "Roentgen"
uuid = "b0d14063-c48a-4081-83d5-5a5ab48cb495"
authors = ["lmejn <33491793+lmejn@users.noreply.github.com>"]
version = "0.13.6"
version = "0.13.7"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
DICOM = "a26e6606-dd52-5f6a-a97f-4f611373d757"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Expand All @@ -29,6 +27,13 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"

[weakdeps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"

[extensions]
RoentgenCUDAExt = "CUDA"

[compat]
Adapt = "3"
CUDA = "4, 5"
Expand All @@ -54,4 +59,4 @@ SparseArrays = "1"
StaticArrays = "1"
Test = "1"
WriteVTK = "1"
julia = "1.8"
julia = "1.9"
32 changes: 32 additions & 0 deletions ext/RoentgenCUDAExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module RoentgenCUDAExt

using Roentgen

import Roentgen.AbstractBeamlet, Roentgen.AbstractExternalSurface, Roentgen.AbstractDoseAlgorithm

using CUDA
import Adapt

function dose_fluence_matrix(::Type{CuArray}, pos, beamlets::AbstractArray{<:AbstractBeamlet},
surf::AbstractExternalSurface, calc::AbstractDoseAlgorithm; kwargs...)
D = CUDA.zeros(length(pos), length(beamlets))
dose_fluence_matrix!(D, pos, beamlets, surf, calc; kwargs...)
end

function dose_fluence_matrix!(D::CuArray, pos, beamlets::AbstractArray{<:AbstractBeamlet},
surf::AbstractExternalSurface, calc::AbstractDoseAlgorithm;
maxradius=100.)
_assert_size(D, pos, beamlets)
D .= point_dose.(vec(pos), permutedims(vec(beamlets)), Ref(surf), Ref(calc), maxradius)
end

function Adapt.adapt_structure(to, surf::CylindricalSurface)
cu_rho = Adapt.adapt_structure(to, surf.rho)
CylindricalSurface(surf.y, surf.ϕ, cu_rho)
end

Adapt.@adapt_structure DoseVolume
Adapt.@adapt_structure LinearSurface
Adapt.@adapt_structure FinitePencilBeamKernel

end
2 changes: 0 additions & 2 deletions src/DoseCalculationAlgorithms/FinitePencilBeamKernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ function FinitePencilBeamKernel(parameters::AbstractMatrix, args...)
FinitePencilBeamKernel(SVector{5}.(eachcol(parameters)), args...)
end

Adapt.@adapt_structure FinitePencilBeamKernel

"""
FinitePencilBeamKernel(filename::String)
Expand Down
14 changes: 0 additions & 14 deletions src/DoseFluenceMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ function dose_fluence_matrix(T::Type{<:AbstractMatrix}, pos, beamlets::AbstractA
dose_fluence_matrix!(D, pos, beamlets, surf, calc; kwargs...)
end

function dose_fluence_matrix(::Type{CuArray}, pos, beamlets::AbstractArray{<:AbstractBeamlet},
surf::AbstractExternalSurface, calc::AbstractDoseAlgorithm;
kwargs...)
D = CUDA.zeros(length(pos), length(beamlets))
dose_fluence_matrix!(D, pos, beamlets, surf, calc; kwargs...)
end

function dose_fluence_matrix(::Type{SparseMatrixCSC}, pos, beamlets::AbstractArray{<:AbstractBeamlet},
surf::AbstractExternalSurface, calc::AbstractDoseAlgorithm;
kwargs...)
Expand Down Expand Up @@ -93,13 +86,6 @@ function dose_fluence_matrix!(D::AbstractArray, pos, beamlets::AbstractArray{<:A
D
end

function dose_fluence_matrix!(D::CuArray, pos, beamlets::AbstractArray{<:AbstractBeamlet},
surf::AbstractExternalSurface, calc::AbstractDoseAlgorithm;
maxradius=100.)
_assert_size(D, pos, beamlets)
D .= point_dose.(vec(pos), permutedims(vec(beamlets)), Ref(surf), Ref(calc), maxradius)
end

"""
_assert_size(D, pos, beamlets)
Expand Down
1 change: 0 additions & 1 deletion src/DoseVolume.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ function dose_fluence_matrix(T, vol::AbstractDoseVolume, beamlets, calc; kwargs.
kwargs...)
end

Adapt.@adapt_structure DoseVolume
7 changes: 0 additions & 7 deletions src/ExternalSurfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ position and normal at gantry angles.
"""
LinearSurface

Adapt.@adapt_structure LinearSurface

LinearSurface(p, n) = LinearSurface(vcat.(p, n))

function LinearSurface(ϕg, p, n)
Expand Down Expand Up @@ -363,11 +361,6 @@ function CylindricalSurface(mesh::SimpleMesh, y::AbstractRange, nϕ::Int=181)
CylindricalSurface(ϕ, y, I, center)
end

function Adapt.adapt_structure(to, surf::CylindricalSurface)
cu_rho = Adapt.adapt_structure(to, surf.rho)
CylindricalSurface(surf.y, surf.ϕ, cu_rho)
end

"""
CylindricalSurface(mesh::SimpleMesh, Δy::Real[, nϕ=181])
Expand Down
4 changes: 0 additions & 4 deletions src/Roentgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module Roentgen
using SparseArrays
using StaticArrays

# CUDA
using CUDA
import Adapt

# Coordinates
using CoordinateTransformations
using Rotations
Expand Down

0 comments on commit 78794e2

Please # to comment.