Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPU linear operators #163

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
5c2e71c
init design (no working implementation)
Abdelrahman912 Dec 18, 2024
8636a49
init setup
Abdelrahman912 Dec 20, 2024
34d8b79
make cuda work for the test kernel
Abdelrahman912 Dec 21, 2024
f7bd306
add gpuy subdof and init gpu update operator
Abdelrahman912 Jan 6, 2025
7eedd51
add subdof iterator
Abdelrahman912 Jan 6, 2025
16846e4
minor fix in sdh iterator
Abdelrahman912 Jan 6, 2025
5787e82
first working example (not refined)
Abdelrahman912 Jan 9, 2025
c58a14f
working example (not refined)
Abdelrahman912 Jan 9, 2025
324dbd0
Merge branch 'main' into gpu-operators
Abdelrahman912 Jan 9, 2025
139fc83
minor adjustment
Abdelrahman912 Jan 9, 2025
babc06c
minor refinement
Abdelrahman912 Jan 13, 2025
fe064cf
init add coeffs
Abdelrahman912 Jan 13, 2025
6bdae08
add to extension (doesn't dispatch to ext tho)
Abdelrahman912 Jan 14, 2025
c5c8b0b
minor edit
Abdelrahman912 Jan 14, 2025
4d3f7de
move to ext (working implementation)
Abdelrahman912 Jan 15, 2025
ac9c807
minor fix
Abdelrahman912 Jan 15, 2025
0d075a8
add coesfficients tests for gpu
Abdelrahman912 Jan 17, 2025
863ab05
minor refinements in coefficients
Abdelrahman912 Jan 17, 2025
0daf36c
minor fix
Abdelrahman912 Jan 17, 2025
7ba3139
restructure PR913
Abdelrahman912 Jan 21, 2025
077572f
change gpu -> device
Abdelrahman912 Jan 21, 2025
34c4536
some restructuring for memory allocation
Abdelrahman912 Jan 24, 2025
0e41a01
fix extension
Abdelrahman912 Jan 25, 2025
f512b88
optimize global mem
Abdelrahman912 Jan 25, 2025
cfc7cdd
minor fix for cuda op
Abdelrahman912 Jan 25, 2025
b59927d
pre kernel launch adapt for dh
Abdelrahman912 Feb 1, 2025
eb93c8d
Merge branch 'main' into gpu-operators
Abdelrahman912 Feb 1, 2025
d4d3bab
minor fix
Abdelrahman912 Feb 1, 2025
23f5413
init fix dh adapt error
Abdelrahman912 Feb 5, 2025
5e5216d
fix memory leak
Abdelrahman912 Feb 6, 2025
d50f7fa
pr review fix
Abdelrahman912 Feb 7, 2025
32c5763
init qp
Abdelrahman912 Feb 11, 2025
7bcdd88
minor fix
Abdelrahman912 Feb 11, 2025
8fe74d2
change operators and fuse mem alloc
Abdelrahman912 Feb 12, 2025
b6be5d4
subdof loop (i.e. get rid of unsafe cuda convert)
Abdelrahman912 Feb 13, 2025
110c7ca
Merge branch 'gpu-operators' into add-quadpoints-in-quadvalues
Abdelrahman912 Feb 13, 2025
ba95bae
use quad points instead of quad values
Abdelrahman912 Feb 13, 2025
97c6b00
minor fix
Abdelrahman912 Feb 14, 2025
69530cc
fix 1
Abdelrahman912 Feb 17, 2025
7339d1c
add complex test function and other stuff
Abdelrahman912 Feb 17, 2025
59fa5b4
fix CI (hopefully!)
Abdelrahman912 Feb 18, 2025
72a8f0e
minor fix
Abdelrahman912 Feb 18, 2025
569ef85
add n_threads and n_blocks as kwargs + change cuda default strategy n…
Abdelrahman912 Feb 18, 2025
a2a8bcd
fix SVector
Abdelrahman912 Feb 18, 2025
a0e28f4
add benchmarks + solve major bugs in memory allocation
Abdelrahman912 Feb 19, 2025
2215395
remove nested closures
Abdelrahman912 Feb 19, 2025
81a6529
add line + remove unnecessary code
Abdelrahman912 Feb 19, 2025
0c1d58f
Merge branch 'main' into gpu-operators
Abdelrahman912 Feb 19, 2025
d1bd648
add benchmarks
Abdelrahman912 Feb 20, 2025
291f3be
change analytical function + add PEALinearOperator benchmark
Abdelrahman912 Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add complex test function and other stuff
  • Loading branch information
Abdelrahman912 committed Feb 17, 2025
commit 7339d1c2ad65c4b9543ed7d17f8d2da79b249e05
17 changes: 8 additions & 9 deletions ext/cuda/cuda_memalloc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ end


function (dsf::DynamicSharedMemFunction{N, Tv, Ti})() where {N, Tv, Ti}
mem_size = dsf.mem_size
offset = dsf.offset
(; mem_size, offset) = dsf
return CUDA.@cuDynamicSharedMem(Tv, mem_size, offset)
end

Expand Down Expand Up @@ -69,17 +68,17 @@ end
# Global Memory Allocation #
#############################

struct KeFeGlobalMem{LOCAL_MATRICES, LOCAL_VECTORS} <: AbstractDeviceGlobalMem
Kes::LOCAL_MATRICES ## global level allocation (i.e. memory for all blocks -> 3rd order tensor)
fes::LOCAL_VECTORS ## global level allocation (i.e. memory for all blocks -> 2nd order tensor)
struct KeFeGlobalMem{MatricesType, VectorsType} <: AbstractDeviceGlobalMem
Kes::MatricesType ## global level allocation (i.e. memory for all blocks -> 3rd order tensor)
fes::VectorsType ## global level allocation (i.e. memory for all blocks -> 2nd order tensor)
end

struct FeGlobalMem{LOCAL_VECTORS} <: AbstractDeviceGlobalMem
fes::LOCAL_VECTORS ## global level allocation (i.e. memory for all blocks -> 2nd order tensor)
struct FeGlobalMem{VectorsType} <: AbstractDeviceGlobalMem
fes::VectorsType ## global level allocation (i.e. memory for all blocks -> 2nd order tensor)
end

struct KeGlobalMem{LOCAL_MATRICES} <: AbstractDeviceGlobalMem
Kes::LOCAL_MATRICES ## global level allocation (i.e. memory for all blocks -> 3rd order tensor)
struct KeGlobalMem{MatricesType} <: AbstractDeviceGlobalMem
Kes::MatricesType ## global level allocation (i.e. memory for all blocks -> 3rd order tensor)
end


Expand Down
9 changes: 2 additions & 7 deletions src/ferrite-addons/PR883.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct QuadratureValuesIterator{VT,XT}
return new{V, Nothing}(v, nothing)
end
function QuadratureValuesIterator(v::V, cell_coords::VT) where {V, VT <: AbstractArray}
#reinit!(v, cell_coords)
reinit!(v, cell_coords)
return new{V, VT}(v, cell_coords)
end
end
Expand Down Expand Up @@ -257,11 +257,7 @@ Ferrite.getnquadpoints(cv::StaticCellValues) = length(cv.weights)
Ferrite.getnbasefunctions(cv::StaticCellValues) = getnbasefunctions(cv.fv)
Ferrite.getngeobasefunctions(cv::StaticCellValues) = getnbasefunctions(cv.gm)

@inline function Ferrite.reinit!(cv::StaticCellValues{<:Any, <:Any, <:AbstractVector}, cell_coords::AbstractVector)
copyto!(cv.x, cell_coords)
#TODO: Also allow the cell::AbstracCell to be given and updated
end
@inline function Ferrite.reinit!(::StaticCellValues{<:Any, <:Any, Nothing}, ::AbstractVector)
@inline function Ferrite.reinit!(::StaticCellValues, ::AbstractVector)
nothing # Nothing to do on reinit if x is not saved.
end

Expand All @@ -277,7 +273,6 @@ end
return _quadrature_point_values(fe_v, q_point, cell_coords, detJ -> -1)
end


function _quadrature_point_values(fe_v::StaticCellValues, q_point::Int, cell_coords::AbstractVector, neg_detJ_err_fun::Function)
#q_point bounds checked, ok to use @inbounds
@inbounds begin
Expand Down
8 changes: 4 additions & 4 deletions src/ferrite-addons/gpu/device_dofhandler.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

# # Utility which holds partial information for assembly.
struct DeviceSubDofHandler{Ti<:Integer,VEC_IP,IndexType, IndexVectorType <: AbstractVector{IndexType},DHDataType} <: Ferrite.AbstractDofHandler
struct DeviceSubDofHandler{Ti<:Integer,IPVectorType,IndexType, IndexVectorType <: AbstractVector{IndexType},DHDataType} <: Ferrite.AbstractDofHandler
cellset::IndexVectorType
field_names::IndexVectorType
field_interpolations::VEC_IP
field_interpolations::IPVectorType
ndofs_per_cell::Ti
dh_data::DHDataType #DeviceDofHandlerData
end


# Utility which holds partial information for assembly.
struct DeviceDofHandlerData{sdim, G<:Ferrite.AbstractGrid{sdim}, IndexType, IndexVectorType <: AbstractVector{IndexType},Ti<: Integer} <: Ferrite.AbstractDofHandler
grid::G
struct DeviceDofHandlerData{sdim, GridType<:Ferrite.AbstractGrid{sdim}, IndexType, IndexVectorType <: AbstractVector{IndexType},Ti<: Integer} <: Ferrite.AbstractDofHandler
grid::GridType
cell_dofs::IndexVectorType
cell_dofs_offset::IndexVectorType
cell_to_subdofhandler::IndexVectorType
Expand Down
4 changes: 2 additions & 2 deletions src/ferrite-addons/gpu/device_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ end
function DeviceGrid(
cells::CellDataType,
nodes::NodeDataType
) where {C <: Ferrite.AbstractCell, CellDataType <: AbstractArray{C, 1}, NodeDataType <: AbstractArray{Node{dim, T}}} where {dim, T}
return DeviceGrid{dim,C,T, CellDataType, NodeDataType}(cells, nodes)
) where {C<:Ferrite.AbstractCell,CellDataType<:AbstractArray{C,1},NodeDataType<:AbstractArray{Node{dim,T}}} where {dim,T}
return DeviceGrid{dim,C,T,CellDataType,NodeDataType}(cells, nodes)
end

Ferrite.get_coordinate_type(::DeviceGrid{sdim, <:Any, T,<:Any,<:Any}) where {sdim, T} = Vec{sdim, T} # Node is baked into the mesh type.
Expand Down
16 changes: 8 additions & 8 deletions src/ferrite-addons/gpu/device_iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ abstract type AbstractDeviceCellIterator end
ncells(iterator::AbstractDeviceCellIterator) = iterator.n_cells ## any subtype has to have `n_cells` field


struct DeviceCellIterator{SDH <: DeviceSubDofHandler, Ti <: Integer, CellMem<: AbstractCellMem} <: AbstractDeviceCellIterator
sdh::SDH
struct DeviceCellIterator{SDHType <: DeviceSubDofHandler, Ti <: Integer, CellMemType<: AbstractCellMem} <: AbstractDeviceCellIterator
sdh::SDHType
n_cells::Ti # depends whether we are iterating over all cells (i.e. all the dh) or a subset of cells (i.e. subdh)
cell_mem::CellMem
cell_mem::CellMemType
end

struct DeviceOutOfBoundCellIterator <: AbstractDeviceCellIterator end # used to handle the case for out of bound threads
Expand All @@ -24,12 +24,12 @@ struct DeviceOutOfBoundCellIterator <: AbstractDeviceCellIterator end # used to

abstract type AbstractDeviceCellCache end

struct DeviceCellCache{Ti <: Integer, DOFS <: AbstractVector{Ti}, NN, NODES <: SVector{NN, Ti}, X, COORDS <: SVector{X}, CellMem<: AbstractCellMem} <: AbstractDeviceCellCache
coords::COORDS
dofs::DOFS
struct DeviceCellCache{Ti <: Integer, DofsType <: AbstractVector{Ti}, NN, NodesType <: SVector{NN, Ti}, X, CoordinatesType <: SVector{X}, CellMemType<: AbstractCellMem} <: AbstractDeviceCellCache
coords::CoordinatesType
dofs::DofsType
cellid::Ti
nodes::NODES
cell_mem::CellMem
nodes::NodesType
cell_mem::CellMemType
end

@inline function cellke(::AbstractDeviceCellCache)
Expand Down
8 changes: 2 additions & 6 deletions test/gpu/test_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ cs = CartesianCoordinateSystem(grid)


protocol = AnalyticalTransmembraneStimulationProtocol(
AnalyticalCoefficient((x,t) -> 1.f0, CoordinateSystemCoefficient(cs)),
AnalyticalCoefficient((x,t) -> sin(2Ο€ * t) * exp(-norm(x)^2), CoordinateSystemCoefficient(cs)),
[SVector((0.f0, 1.f0))]
)





linop = Thunderbolt.LinearOperator(
zeros(ndofs(dh)),
protocol,
Expand All @@ -26,12 +23,11 @@ linop = Thunderbolt.LinearOperator(
)

Thunderbolt.update_operator!(linop,0.0)
#@test linop.b β‰ˆ [0.25, 0.5, 1.0, 0.5, 0.25, 0.5, 0.5, 0.25, 0.25]


cuda_strategy = Thunderbolt.CudaAssemblyStrategy(Float32, Int32)
cuda_op = Thunderbolt.init_linear_operator(cuda_strategy,protocol, qrc, dh);
Thunderbolt.update_operator!(cuda_op,0.0)
Thunderbolt.update_operator!(cuda_op,0.f0)


@test Vector(cuda_op.b) β‰ˆ linop.b