-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
987aef1
Add kernel-based matrix cumsum
rkierulf 64cccd1
Fix some minor issues
rkierulf 786cbf9
Add tests to increase code coverage
rkierulf fad789d
Merge branch 'master' of https://github.com/JuliaHealth/KomaMRI.jl in…
rkierulf 15ec737
Fix print devices for AMD
rkierulf 6a69454
Expand tests and exclude Metal and kernel functions from coverage / f…
rkierulf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using KernelAbstractions: @index, @kernel | ||
|
||
## COV_EXCL_START | ||
|
||
""" | ||
cumsum2_kernel | ||
|
||
Simple kernel function, computes the cumulative sum of each row of a matrix. Operates | ||
in-place on the input matrix without allocating additional memory. | ||
|
||
# Arguments | ||
- 'A': matrix to compute cumsum on | ||
""" | ||
@kernel function cumsum_matrix_rows_kernel!(A) | ||
i = @index(Global) | ||
|
||
for k ∈ 2:size(A, 2) | ||
@inbounds A[i, k] += A[i, k-1] | ||
end | ||
end | ||
|
||
## COV_EXCL_STOP | ||
|
||
""" | ||
cumtrapz | ||
|
||
A more efficient GPU implementation of the cumtrapz method defined in TrapezoidalIntegration.jl. | ||
Uses a kernel to compute cumsum along the second dimension. | ||
|
||
# Arguments | ||
- `Δt`: (`1 x NΔt ::Matrix{Float64}`, `[s]`) delta time 1-row array | ||
- `x`: (`Ns x (NΔt+1) ::Matrix{Float64}`, `[T]`) magnitude of the field Gx * x + Gy * y + | ||
Gz * z | ||
|
||
# Returns | ||
- `y`: (`Ns x NΔt ::Matrix{Float64}`, `[T*s]`) matrix where every column is the | ||
cumulative integration over time of (Gx * x + Gy * y + Gz * z) * Δt for every spin of a | ||
phantom | ||
""" | ||
function KomaMRIBase.cumtrapz(Δt::AbstractArray{T}, x::AbstractArray{T}, backend::KA.GPU) where {T<:Real} | ||
y = (x[:, 2:end] .+ x[:, 1:end-1]) .* (Δt / 2) | ||
cumsum_matrix_rows_kernel!(backend)(y, ndrange=size(y,1)) | ||
KA.synchronize(backend) | ||
return y | ||
end | ||
|
||
#If on CPU, forward call to cumtrapz in KomaMRIBase | ||
KomaMRIBase.cumtrapz(Δt::AbstractArray{T}, x::AbstractArray{T}, backend::KA.CPU) where {T<:Real} = KomaMRIBase.cumtrapz(Δt, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using TestItems, TestItemRunner | ||
|
||
### NOTE: by default, tests are run on the CPU with the number of threads set to | ||
# Threads.nthreads(). To run on a specific GPU backend, pass the name of the | ||
# trigger package ("AMDGPU", "CUDA", "Metal", or "oneAPI") as a test argument. | ||
# Threads.nthreads(). To run on a specific GPU backend, add the name of the | ||
# backend package ("AMDGPU", "CUDA", "Metal", or "oneAPI") to the test/Project.toml | ||
# file in KomaMRICore and pass the name as a test argument. | ||
# | ||
# Example: | ||
# | ||
|
@@ -385,3 +386,20 @@ end | |
# For the time being, always pass the test | ||
@test true | ||
end | ||
|
||
@testitem "GPU Functions" tags=[:core] begin | ||
using Suppressor | ||
import KernelAbstractions as KA | ||
include("initialize.jl") | ||
|
||
x = ones(Float32, 1000) | ||
if USE_GPU | ||
x = x |> gpu | ||
@test KA.get_backend(x) isa KA.GPU | ||
else | ||
@test true | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to check that Use_gpu = true
Use_gpu = false
|
||
|
||
@suppress print_devices() | ||
@test true | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.