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

Add kernel-based matrix cumsum #416

Merged
merged 6 commits into from
Jun 21, 2024
Merged

Add kernel-based matrix cumsum #416

merged 6 commits into from
Jun 21, 2024

Conversation

rkierulf
Copy link
Collaborator

I added a kernel implementation of cumsum(A::Matrix, dims=2). Since Metal and oneAPI don't yet support cumsum and findall, we are working around the issue for now by copying to the CPU, doing the operations there and then copying back. It looks like the 2D cumsum in the cumtrapz function of KomaMRIBase is the main place where this could be significantly slower than an actual GPU implementation (the other cumsum and findall operations are on 1D arrays), so I wrote a simple kernel equivalent to avoid having to copy to the CPU.

Copy link

codecov bot commented Jun 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.19%. Comparing base (3178902) to head (6a69454).
Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #416      +/-   ##
==========================================
+ Coverage   88.22%   89.19%   +0.96%     
==========================================
  Files          49       49              
  Lines        2811     2803       -8     
==========================================
+ Hits         2480     2500      +20     
+ Misses        331      303      -28     
Flag Coverage Δ
base 86.42% <ø> (ø)
core 85.68% <100.00%> (+6.28%) ⬆️
files 93.70% <ø> (ø)
komamri 93.98% <ø> (ø)
plots 89.27% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
KomaMRICore/ext/KomaAMDGPUExt.jl 66.66% <ø> (+24.99%) ⬆️
.../src/simulation/Bloch/BlochDictSimulationMethod.jl 87.50% <100.00%> (ø)
...Core/src/simulation/Bloch/BlochSimulationMethod.jl 100.00% <100.00%> (ø)
...omaMRICore/src/simulation/Bloch/KernelFunctions.jl 100.00% <100.00%> (ø)
KomaMRICore/src/simulation/Functors.jl 47.36% <100.00%> (+14.93%) ⬆️
KomaMRICore/src/simulation/SimulatorCore.jl 94.59% <100.00%> (ø)

... and 3 files with indirect coverage changes

@cncastillo
Copy link
Member

cncastillo commented Jun 21, 2024

Niiice! the first kernel!! 😄 🎊 How is the performance of KA vs the regular cumsum? Feel free to merge if it passes all the tests.

If you get a codecov error, there are some low-hanging fruit tests like gpu and print_devices that you can add quite easily (verifying that gpu changes an array to the backend's array, and that the print_devices does not fail @test true). If not, I can merge it tomorrow.

@rkierulf
Copy link
Collaborator Author

I added some more tests to increase code coverage. Although it is still not reaching the target, a lot of the remaining uncovered lines are for custom GPU logic for the MotionModel structs which is being eliminated in #408 . Once this is done, I think it should reach the goal.

Copy link
Member

@cncastillo cncastillo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a few comments that could increase the codecov further If it's not enough.

@@ -0,0 +1,48 @@
using KernelAbstractions: @index, @kernel
Copy link
Member

@cncastillo cncastillo Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if the newly added CPU backend adds codecov for the kernel?

Also, add the comments to exclude the coverage for the metal extension.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it does since on the CPU it forwards the call to cumtrapz in KomaMRIBase. I forgot about code coverage not working for Metal, so I added the comments to exclude.

Comment on lines 395 to 401
x = ones(Float32, 1000)
if USE_GPU
x = x |> gpu
@test KA.get_backend(x) isa KA.GPU
else
@test true
end
Copy link
Member

@cncastillo cncastillo Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to check that

Use_gpu = true

  • cpu(gpu(x)) is a CPU array

Use_gpu = false

  • cpu and gpu are no-ops

@rkierulf rkierulf merged commit d624e8f into master Jun 21, 2024
18 checks passed
@rkierulf rkierulf deleted the cumsum branch June 21, 2024 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants