Skip to content

Single precision computations and rudimentary GPU support#895

Merged
wahln merged 46 commits intodevfrom
feature/single_precision_dosecalc
Mar 24, 2026
Merged

Single precision computations and rudimentary GPU support#895
wahln merged 46 commits intodevfrom
feature/single_precision_dosecalc

Conversation

@wahln
Copy link
Copy Markdown
Contributor

@wahln wahln commented Mar 19, 2026

Since Matlab R2025 sparse matrices can have single precision value storage.

This PR enables this throughout all basic computations and adds rudimentary GPU support. This can benefit optimization quite substantially. Dose calculation is, however, to fragmented to be accelerated, apart from RayTracing. To enable sensible acceleration, RayTracing is now performed in a vectorized manner (similarly to our implementation in pyRadPlan.

CoPilot Summary:
This pull request introduces significant improvements to the matRad dose calculation engines, focusing on enhanced floating-point precision handling, initial GPU support, and improved code consistency. The most important changes are grouped below by theme.

Precision Handling and Consistency:

  • Added a new precision property (default 'single') to matRad_DoseEngineBase and propagated its use throughout the dose calculation pipeline, including casting arrays and matrices to the specified precision in matRad_PencilBeamEngineAbstract and matRad_PhotonPencilBeamSVDEngine. This ensures consistent floating-point operations and enables future support for mixed-precision or GPU computing. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
  • Updated allocation of matrices (including sparse matrices) to respect the desired precision and introduced logic to handle environments where single-precision sparse matrices are not supported, with a helper method allowsSinglePrecisionSparseDij. [1] [2] [3]

GPU Support:

  • Introduced an experimental enableGPU flag in MatRad_Config, and added logic in matRad_PencilBeamEngineAbstract to move CT and CST data to GPU when enabled. [1] [2]
  • Added a new utility function matRad_getCstFromGPU to gather CST data from the GPU and cast it to the desired precision.

Ray Tracing Refactor:

  • Refactored ray tracing in the dose engines to use a new matRad_RayTracerSiddon object, storing it as a property and using its methods for radiological depth calculations and SSD computation, improving modularity and code reuse. [1] [2] [3]

Minor Fixes and Documentation:

  • Fixed typos and improved comments in matRad_example15_brachy.m, and corrected a method call to use the proper case for isAvailable. [1] [2] [3]
  • Improved figure creation and display logic for better readability in example scripts.

These changes collectively modernize the matRad codebase, laying the groundwork for improved performance, precision, and hardware acceleration.

wahln and others added 23 commits September 17, 2021 17:39
…to feature/single_precision_dosecalc

# Conflicts:
#	matRad/rayTracing/matRad_rayTracing.m
# Conflicts:
#	matRad/gui/widgets/matRad_ViewingWidget.m
# Conflicts:
#	matRad/MatRad_Config.m
#	matRad/optimization/optimizer/matRad_OptimizerIPOPT.m
#	matRad/util/matRad_interp1.m
#	matRad_calcDoseInit.m
#	matRad_generateStf.m
# Conflicts:
#	matRad/rayTracing/matRad_rayTracing.m
# Conflicts:
#	examples/matRad_example3_photonsDAO.m
@wahln wahln self-assigned this Mar 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR propagates single-precision computations through core dose-calculation workflows, introduces initial GPU data movement helpers, and refactors ray tracing into a new vectorized Siddon-based class to enable future acceleration.

Changes:

  • Added precision handling (default 'single') across dose engines and related utilities.
  • Introduced experimental GPU support via MatRad_Config.enableGPU plus GPU move/gather helpers.
  • Refactored ray tracing to use new matRad_RayTracer / matRad_RayTracerSiddon classes and deprecated legacy ray-tracing functions.

Reviewed changes

Copilot reviewed 39 out of 39 changed files in this pull request and generated 16 comments.

Show a summary per file
File Description
test/util/test_interp1.m Adds a test for multi-dataset interpolation behavior.
test/optimizers/test_optimizerIPOPT.m Updates tests to new optimizer method names (isAvailable, getStatus).
test/optimizers/test_optimizerFmincon.m Updates tests to new optimizer method names and formatting.
submodules/ompMC Updates the ompMC submodule pointer.
matRad/util/octaveCompat/matRad_underlyingTypeCompat.m Adds Octave-compatible helper for determining underlying numeric type.
matRad/util/matRad_interp1.m Extends interpolation helper for mixed precision and GPU inputs.
matRad/steering/matRad_StfGeneratorParticleSingleBeamlet.m Switches steering ray tracing to the new Siddon tracer class.
matRad/steering/matRad_StfGeneratorParticleRayBixelAbstract.m Stores a ray tracer instance and initializes it during patient-geometry creation.
matRad/steering/matRad_StfGeneratorParticleIMPT.m Uses the new ray tracer for per-scenario ray traces.
matRad/steering/matRad_StfGeneratorBase.m Adds optional GPU movement of CT/CST during STF generation.
matRad/rayTracing/matRad_siddonRayTracer.m Deprecates old Siddon function and forwards to new tracer class.
matRad/rayTracing/matRad_rayTracing.m Deprecates old ray tracing and forwards to new tracer class.
matRad/rayTracing/matRad_RayTracerSiddon.m Adds new vectorized Siddon ray tracer implementation.
matRad/rayTracing/matRad_RayTracer.m Adds new base ray tracing infrastructure (candidate ray matrix + traceCube).
matRad/plotting/matRad_plotVoiContourSlice.m Casts inputs for contourc to double to support non-double cubes.
matRad/plotting/matRad_plotPlan3D.m Casts inputs for contourc to double to support non-double matrices.
matRad/plotting/matRad_plotIsoDoseLines.m Casts inputs for contourc to double to support single precision cubes.
matRad/plotting/matRad_computeVoiContours.m Casts inputs for contourc to double to support non-double masks.
matRad/optimization/optimizer/matRad_OptimizerSimulannealbnd.m Renames API to isAvailable/getStatus and formatting cleanup.
matRad/optimization/optimizer/matRad_OptimizerIPOPT.m Renames API, adds gather wrappers for GPU flows, and improves plotting robustness.
matRad/optimization/optimizer/matRad_OptimizerFmincon.m Renames API and improves option setup / wrappers.
matRad/optimization/optimizer/matRad_Optimizer.m Updates base optimizer API to isAvailable/getStatus.
matRad/matRad_fluenceOptimization.m Moves cst/dij to GPU when configured and updates optimizer availability checks.
matRad/gui/widgets/matRad_WorkflowWidget.m Updates GUI status call to getStatus().
matRad/gui/widgets/matRad_ViewingWidget.m Replaces legacy Siddon calls with new tracer usage in plotting path.
matRad/gpu/matRad_moveDijToGPU.m Adds helper to move selected dij quantities to GPU (optionally casting).
matRad/gpu/matRad_moveCtToGPU.m Adds helper to move CT cubes to GPU (optionally casting).
matRad/gpu/matRad_moveCstToGPU.m Adds helper to move CST voxel-index lists to GPU (optionally casting).
matRad/gpu/matRad_getDijFromGPU.m Adds helper to gather dij quantities back from GPU (optionally casting).
matRad/gpu/matRad_getCtFromGPU.m Adds helper to gather CT cubes back from GPU (optionally casting).
matRad/gpu/matRad_getCstFromGPU.m Adds helper to gather CST back from GPU (optionally casting).
matRad/geometry/matRad_cubeIndex2worldCoords.m Forces numeric arithmetic to double to reduce class/precision pitfalls.
matRad/doseCalc/matRad_computeSSD.m Switches SSD computation to use the new Siddon tracer.
matRad/doseCalc/+DoseEngines/matRad_PhotonPencilBeamSVDEngine.m Propagates precision into key intermediate allocations.
matRad/doseCalc/+DoseEngines/matRad_PencilBeamEngineAbstract.m Adds GPU movements, precision casts, and uses new tracer for ray tracing.
matRad/doseCalc/+DoseEngines/@matRad_DoseEngineBase/matRad_DoseEngineBase.m Adds precision property and sparse-single capability check helper.
matRad/doseCalc/+DoseEngines/@matRad_DoseEngineBase/initDoseCalc.m Casts computed world coordinates to engine precision.
matRad/MatRad_Config.m Adds experimental enableGPU flag.
examples/matRad_example15_brachy.m Formatting/typo fixes and updated availability API usage in example.
Comments suppressed due to low confidence (3)

matRad/util/matRad_interp1.m:1

  • Casting against class(x)/class(xi) will fail for GPU inputs because class(gpuArray) is 'gpuArray', which is not a numeric class for cast. Use the underlying numeric type (e.g., via matRad_underlyingTypeCompat or classUnderlying) and then move to GPU, or use 'like' semantics. Also, isgpuarray is MATLAB-specific and will error in Octave unless guarded (e.g., exist('isgpuarray','file')).
    matRad/util/matRad_interp1.m:1
  • Casting against class(x)/class(xi) will fail for GPU inputs because class(gpuArray) is 'gpuArray', which is not a numeric class for cast. Use the underlying numeric type (e.g., via matRad_underlyingTypeCompat or classUnderlying) and then move to GPU, or use 'like' semantics. Also, isgpuarray is MATLAB-specific and will error in Octave unless guarded (e.g., exist('isgpuarray','file')).
    test/util/test_interp1.m:1
  • This test name indicates a single-precision path but the inputs are created as doubles. Also, all(...,'all') is not supported in older MATLAB versions and is not supported in many Octave versions; it can make the test suite fail in those environments. Consider explicitly creating single inputs and using a compatibility-safe reduction (e.g., nested all(all(...))) or linearizing with (:) depending on test conventions in this repo.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread matRad/rayTracing/matRad_RayTracer.m Outdated
Comment thread matRad/rayTracing/matRad_RayTracer.m
Comment thread matRad/rayTracing/matRad_RayTracer.m Outdated
Comment thread matRad/rayTracing/matRad_RayTracer.m Outdated
Comment thread matRad/rayTracing/matRad_RayTracer.m
Comment thread matRad/gpu/matRad_moveCstToGPU.m Outdated
Comment thread matRad/gpu/matRad_moveCtToGPU.m Outdated
Comment thread matRad/optimization/optimizer/matRad_OptimizerIPOPT.m Outdated
Comment thread matRad/rayTracing/matRad_RayTracerSiddon.m
Comment thread matRad/rayTracing/matRad_RayTracerSiddon.m Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 68.17248% with 155 lines in your changes missing coverage. Please review.
✅ Project coverage is 54.86%. Comparing base (6117a65) to head (d185f63).
⚠️ Report is 47 commits behind head on dev.

Files with missing lines Patch % Lines
...Rad/optimization/optimizer/matRad_OptimizerIPOPT.m 37.20% 27 Missing ⚠️
matRad/rayTracing/matRad_RayTracer.m 83.62% 19 Missing ⚠️
matRad/gpu/matRad_moveCtToGPU.m 0.00% 11 Missing ⚠️
matRad/gpu/matRad_getCtFromGPU.m 0.00% 10 Missing ⚠️
matRad/matRad_fluenceOptimization.m 35.71% 9 Missing ⚠️
matRad/rayTracing/matRad_rayTracing.m 0.00% 9 Missing ⚠️
matRad/gpu/matRad_getDijFromGPU.m 0.00% 8 Missing ⚠️
matRad/gpu/matRad_moveDijToGPU.m 0.00% 8 Missing ⚠️
matRad/gpu/matRad_getCstFromGPU.m 0.00% 6 Missing ⚠️
matRad/gpu/matRad_moveCstToGPU.m 0.00% 6 Missing ⚠️
... and 16 more
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #895      +/-   ##
==========================================
- Coverage   54.94%   54.86%   -0.08%     
==========================================
  Files         310      320      +10     
  Lines       20303    20514     +211     
==========================================
+ Hits        11155    11255     +100     
- Misses       9148     9259     +111     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 20, 2026

Test Results

    3 files  ±0      3 suites  ±0   32m 17s ⏱️ - 10m 58s
  357 tests +3    357 ✅ +3  0 💤 ±0  0 ❌ ±0 
1 167 runs  +9  1 162 ✅ +9  5 💤 ±0  0 ❌ ±0 

Results for commit d185f63. ± Comparison against base commit 6117a65.

This pull request removes 7 and adds 10 tests. Note that renamed tests count towards both.
test_HongPB ‑ test_calcDoseHongPBVHEE_Focused
test_interp1 ‑ test_matRad_interp1_errors
test_interp1 ‑ test_matRad_interp1_extrapolation
test_interp1 ‑ test_matRad_interp1_extrapolation_nearest
test_interp1 ‑ test_matrad_interp1_values
test_rayTracer ‑ test_2DCube
test_rayTracer ‑ test_siddeonRayTracer
test_HongPB ‑ test_calcDoseHongPBVHEEfocused
test_HongPB ‑ test_traceDoseGrid
test_interp1 ‑ test_interp1Errors
test_interp1 ‑ test_interp1Extrapolation
test_interp1 ‑ test_interp1ExtrapolationNearest
test_interp1 ‑ test_interp1MultiSingle
test_interp1 ‑ test_interp1Values
test_rayTracer ‑ test_siddonRayTracer
test_rayTracer ‑ test_singleVsDouble
test_rayTracer ‑ test_vectorizedVsLoop

♻️ This comment has been updated with latest results.

wahln and others added 22 commits March 20, 2026 16:11
…set to true. Also reset it to false by default.
@github-actions
Copy link
Copy Markdown

Code Coverage

Package Line Rate Health
coverage Package 1 55%
Summary 55% (11248 / 20514)

@wahln wahln merged commit 2ccd0d2 into dev Mar 24, 2026
15 checks passed
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