-
Notifications
You must be signed in to change notification settings - Fork 1
Add GPU backend selection with load_cuda(), load_metal(), load_amdgpu(), load_oneapi() #23
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
Merged
cncastillo
merged 9 commits into
JuliaHealth:main
from
anvika-singhal:gpu-backend-selection
Jul 31, 2026
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
64b2fc6
Add GPU backend selection functionality and example
anvika-singhal 8d11922
Fix linting errors
anvika-singhal b97ff99
Fix linting errors
anvika-singhal e2a8572
Simplify example, updated README to have gpu selection
anvika-singhal 44a5fb4
Fix linting errors
anvika-singhal c105885
Install GPU backends on first use
cncastillo 91d9c36
Merge branch 'JuliaHealth:main' into gpu-backend-selection
anvika-singhal d5a0ae6
Merge remote-tracking branch 'upstream/main' into gpu-backend-selection
anvika-singhal 05378c0
Merge branch 'gpu-backend-selection' of https://github.com/anvika-sin…
anvika-singhal 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 hidden or 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,58 @@ | ||
| """Simulate MRI acquisition with optional GPU acceleration. | ||
|
|
||
| This example demonstrates GPU backend selection: | ||
| - Uncomment km.load_cuda() (or load_metal/load_amdgpu/load_oneapi) to enable GPU | ||
| - Once a backend is loaded, gpu=true is used by default | ||
| - Without a backend, simulation runs on CPU automatically | ||
|
|
||
| The simulation produces identical results on CPU and GPU (within numerical precision). | ||
| """ | ||
|
|
||
| import numpy as np | ||
|
|
||
| import komamripy as km | ||
|
|
||
| # Optional: Load a GPU backend to enable GPU acceleration. | ||
| # Uncomment ONE of these (backend must be installed): | ||
| # km.load_cuda() # NVIDIA GPUs | ||
| # km.load_metal() # Apple Silicon | ||
| # km.load_amdgpu() # AMD GPUs | ||
| # km.load_oneapi() # Intel GPUs (experimental) | ||
|
|
||
| # If no backend is loaded, simulation runs on CPU automatically. | ||
|
|
||
| print("Creating phantom...") | ||
| coords = np.linspace(-40e-3, 40e-3, 64) | ||
| xx, yy = np.meshgrid(coords, coords) | ||
| radius = np.sqrt(xx**2 + yy**2) | ||
| mask = radius <= 32e-3 | ||
|
|
||
| x = xx[mask] | ||
| y = yy[mask] | ||
| z = np.zeros_like(x) | ||
|
|
||
| phantom = km.Phantom( | ||
| name="circle", | ||
| x=x, | ||
| y=y, | ||
| z=z, | ||
| ρ=np.ones_like(x), | ||
| T1=np.ones_like(x), | ||
| T2=0.1 * np.ones_like(x), | ||
| T2s=0.1 * np.ones_like(x), | ||
| ) | ||
|
|
||
| print("Setting up acquisition...") | ||
| sys = km.Scanner() | ||
| seq = km.PulseDesigner.EPI_example() | ||
|
|
||
| print("Simulating...") | ||
| sim_params = {"return_type": "mat"} # gpu=true by default if backend loaded | ||
| signal = km.simulate(phantom, seq, sys, sim_params=sim_params) | ||
| signal = np.asarray(signal).reshape(-1) | ||
|
|
||
| print("Simulation complete!") | ||
| print(f"Signal shape: {signal.shape}") | ||
| signal_min = np.abs(signal).min() | ||
| signal_max = np.abs(signal).max() | ||
| print(f"Signal magnitude range: [{signal_min:.2e}, {signal_max:.2e}]") |
This file contains hidden or 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 hidden or 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,36 @@ | ||
| """GPU backend selection for komamripy. | ||
|
|
||
| Maps Python functions to Julia backend loading: | ||
| - load_cuda() → using CUDA | ||
| - load_metal() → using Metal | ||
| - load_amdgpu() → using AMDGPU | ||
| - load_oneapi() → using oneAPI | ||
|
|
||
| Once a backend is loaded, KomaMRI.jl automatically uses it with gpu=true by default. | ||
| """ | ||
|
|
||
| from ._session import get_julia | ||
|
|
||
|
|
||
| def load_cuda() -> None: | ||
| """Load CUDA backend: using CUDA""" | ||
| jl = get_julia() | ||
| jl.seval("using CUDA") | ||
|
|
||
|
|
||
| def load_metal() -> None: | ||
| """Load Metal backend: using Metal""" | ||
| jl = get_julia() | ||
| jl.seval("using Metal") | ||
|
|
||
|
|
||
| def load_amdgpu() -> None: | ||
| """Load AMDGPU backend: using AMDGPU""" | ||
| jl = get_julia() | ||
| jl.seval("using AMDGPU") | ||
|
|
||
|
|
||
| def load_oneapi() -> None: | ||
| """Load oneAPI backend: using oneAPI (experimental)""" | ||
| jl = get_julia() | ||
| jl.seval("using oneAPI") |
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.
Uh oh!
There was an error while loading. Please reload this page.