Skip to content

SPECTGPU - #1687

Merged
KrisThielemans merged 37 commits into
UCL:masterfrom
danieldeidda:GPUSPECT
Nov 30, 2026
Merged

KrisThielemans merged 37 commits into
UCL:masterfrom
danieldeidda:GPUSPECT

Conversation

@danieldeidda

Copy link
Copy Markdown
Collaborator

…eton classe, atm a lot of copy from niftyGPU

Changes in this pull request

Testing performed

Related issues

Checklist before requesting a review

  • [] I have performed a self-review of my code
  • [] I have added docstrings/doxygen in line with the guidance in the developer guide
  • [] I have implemented unit tests that cover any new or modified functionality (if applicable)
  • [] The code builds and runs on my machine
  • [] documentation/release_XXX.md has been updated with any functionality change (if applicable)

Contribution Notes

Please tick the following:

  • The content of this Pull Request (the Contribution) is intentionally submitted for inclusion in STIR (the Work) under the terms and conditions of the Apache-2.0 License.
  • I (or my institution) have signed the STIR Contribution License Agreement (not required for small changes).

danieldeidda and others added 9 commits March 4, 2026 17:01
…calls the kernels; we need to double check that the kernel can write into the sino but probably we need to dodevice_to_host(stir_sino,cuda_array_created_by_forward)
… that calls the kernels; we need to double check that the kernel can write into the sino but probably we need to dodevice_to_host(stir_sino,cuda_array_created_by_forward)
@KrisThielemans
KrisThielemans marked this pull request as draft March 23, 2026 15:46
@KrisThielemans KrisThielemans self-assigned this Mar 23, 2026
addition of wrappers for actual_FP/BP
removal of most of helper
currently FP and BP work pretty well and are very close to spectUB rough speed estimate on debug mode were: SPECTUB 3min36s SPECTGPU 20s
3x3x3 gaussian kernel was fine for FP but introduced holes artifact in BP it was necessary to change the kernel to a 5x5x7 with sigma 2 to make a BPimage very close to SPECTUB. Speed above is related to 5x5x7.
addition of wrappers for actual_FP/BP
removal of most of helper
currently FP and BP work pretty well and are very close to spectUB rough speed estimate on debug mode were: SPECTUB 3min36s SPECTGPU 20s
3x3x3 gaussian kernel was fine for FP but introduced holes artifact in BP it was necessary to change the kernel to a 5x5x7 with sigma 2 to make a BPimage very close to SPECTUB. Speed above is related to 5x5x7.
@codacy-production

codacy-production Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 65 complexity · 15 duplication

Metric Results
Complexity 65
Duplication 15

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@danieldeidda
danieldeidda marked this pull request as ready for review September 9, 2026 17:28
@danieldeidda

Copy link
Copy Markdown
Collaborator Author
image the forward projction of a 150x150x200 image into a sino of 150, 80, 200 (tg,view,ax) values are pretty much identical.

the back projection on the other hand shows some scaling factor which I suppose we expect?
image

adapted PairProjectorByBinUsingSPECTGPU to be able to run reconstruction
@danieldeidda

Copy link
Copy Markdown
Collaborator Author

Very shallow tests on Attenuation and PSF but is seems that it is doing what I would expect it to be doing. But of course more tests are needed. Actual unit test are also needed like adjointness, etc.

reconstruction also seem to work fairly well:
the following is a reconstruction of simulated data (no noise) 2 iteration with

  1. PSF+AC (PSF is just random to see the effect on recon and speed)
  2. AC
  3. no corrections
image image

currently running with OMP off. when turning OMP on I get an assert aerror on ptr_access

@KrisThielemans

Copy link
Copy Markdown
Collaborator

Hi. looks like great progress! Obviously, I'm interested to know some timings :-)

the back projection on the other hand shows some scaling factor which I suppose we expect?

no, we don't. The SPECTUB projector backprojector is adjoint (as it uses the matrix), so has the correct scale.

@danieldeidda

Copy link
Copy Markdown
Collaborator Author

I thought there was some scaling in SPECTUB as I always need to multiply by the number of views to get images in the right scale. Anyway in debug mode with no OMP around 300s 2 iterations fully 3D PSF and AC I am reconstructing with SPECTUB now but it's been running for a while and not finished the first iter yet. GPU recon with no correction is 90 s and 160s only AC

@danieldeidda

danieldeidda commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

comparison with SPECTUB
image

I though SPECTUB would have some sort of scaling factor as we always need to multiply by the number of views to get "quantitative" values interestingly enough the reconstruction has the same scale.
image

Rough times in debug mode with OMP=off 2 iterations are:
GPU:
PSFAC 300s (CYAN)
AC 150s
no corr 90s
SPECTUB:
PSFAC 3400s (purple)

Added Hei Yin Jowett Chan as an author to the file.
Jowett: Revert authorship added to the wrong file
@danieldeidda

Copy link
Copy Markdown
Collaborator Author

when OMP is on: the following debugging track is followed:
image
`array_to_device(elemT* dev_data, const Array<num_dimensions, elemT>& stir_array)
{
if (stir_array.is_contiguous())

{
  info("array_to_device contiguous", 100);

  cudaMemcpy(dev_data, stir_array.get_const_full_data_ptr(), stir_array.size_all() * sizeof(elemT), cudaMemcpyHostToDevice);

  **stir_array.release_const_full_data_ptr();**

}`

the assert happens when calling realease_const_full_data_ptr() which is called by SPECTGPUBackwardProjectorCUDA.cu: array_to_device(dev_image, stir_image);

@KrisThielemans

Copy link
Copy Markdown
Collaborator

interesting. only with OMP?

I think it's indeed the case that the get/release pair currently is not thread-safe, i.e. would have to be in a critical section to make it safe. I don't know about cudaMemCpy and thread-safety.

There's various issues here:

  • do we want to make it thread-safe?
  • does it matter for const access?
  • why are we copying the same image multiple times to the GPU? (That seems wrong/inefficient).

I guess the last one is the main one.

@danieldeidda

Copy link
Copy Markdown
Collaborator Author

Yes that's it! I think that is because the image needs to be updated as the function is called for every view. But then I should copy outside of this call maybe in start_accumulating and free in get_output? so that the BP just updates the output image

@KrisThielemans

Copy link
Copy Markdown
Collaborator

@MatteoNeelColombo wrote:

2. run_SPECTGPU_tests.sh. The OSEM results are 11 % and 6 % off from the images in org/. I get exactly the same results with the commit where org/ was updated, so I think those images were made with slightly different code. Could you regenerate them?

@danieldeidda wrote:

may be because there was that bug with the setter of sigma0.

git history:

  • commit bc28f3a Date: Fri Sep 25
    • fixed ProjectorByBinPairUsingSPECTGPU::post_processing() to use fwd->set_num_sigmas(_num_sigmas) as opposed to _sigma0.
    • check-in of org (replacing the original one with a rebase such that it isn't any longer in the git history)
  • commit c9638c1 Date: Mon Oct 19
    • replaced *.v in org

I think @MatteoNeelColombo is saying that the test fails with current (i.e. 19 Oct) org, but works with old (25 Sep) org, which seems to make no sense.

@danieldeidda can you check what's going on?

@KrisThielemans

Copy link
Copy Markdown
Collaborator

@danieldeidda I see that you are using _num_sigmas, get/set_num_sigmas accordingly, and "number of PSF sigmas" parsing key. On the other hand, SPECTUB (and presumably pinhole) uses maximum_number_of_sigmas etc. Are these the same thing?

I think both namings aren't great (it took me a while to understand it isn't a "number"), but I also think we should be consistent. Can we therefore use the SPECTUB naming? This would mean changing C++, python and .par of course.

@MatteoNeelColombo

MatteoNeelColombo commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What I meant is that the current org/ images can't be reproduced with the code in the PR, not even with the code of the commit that added them (c9638c1). Maybe I'm missing something on how to produce them.

i ran run_SPECTGPU_tests.sh in recon_test_pack/SPECT/SPECTGPU. Both versions give the same images. FBP matches org/, the two OSEM ones don't.
I think the simplest thing would be to regenerate them with the current code, as @danieldeidda suggested.

@MatteoNeelColombo

MatteoNeelColombo commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

SPECTGPU_OSEM_current_code.zip
@danieldeidda as Kris suggested, here are the two OSEM images I get with the current code by running recon_test_pack/SPECT/SPECTGPU/run_SPECTGPU_tests.sh (after the invert_axis x the script does, so they can go straight into org/).

@KrisThielemans KrisThielemans modified the milestones: v6.5, v6.6 Nov 9, 2026
@danieldeidda

Copy link
Copy Markdown
Collaborator Author

SPECTGPU_OSEM_current_code.zip @danieldeidda as Kris suggested, here are the two OSEM images I get with the current code by running recon_test_pack/SPECT/SPECTGPU/run_SPECTGPU_tests.sh (after the invert_axis x the script does, so they can go straight into org/).

interesting for me that test runs ok with the latest commit :O

@danieldeidda

Copy link
Copy Markdown
Collaborator Author

I finally am able to reproduce @MatteoNeelColombo issue with that test by using another workstation.

danieldeidda and others added 5 commits November 23, 2026 15:50
- all the allocations happen at setup so that in the loop we only initialise and assigne values. this has significantly speeded up performonce.
- all arrays are now CuVec but at the end of the reconstruction we still have a free problem.
The structure has a sestructor that calls resize(0) for all the cuvecs but the error is still there
- the new stack structure create some race problem when using OMP because many threads where
accessing the same object, so a thread_local AllocatedStack was used for everythread
in the BP part given that each thread contribute to the the same image it was also necessary
to add an accumulation step as critical section, so a kernel for arrays accumulation was created;
updated timing given that spectUB becomes really fast once the matrix is created:
SPECTUB 2 iter 231 s (223 matrix + 4.2 s per full iter)
SPECTGPU 2 iter: 8.7s (6.4 matrix + 2.3 per full iter)
- reducing double exp() calculation in pull kernel for a little speed up;
- creating OSMAPOSL and PoissonLogLikelihood tests (these are running with
the caveat that i had to make epsilon for the hessian product with input 0.001)
- general clean up;
- moving cu_allocate and cu_free into cuda utilities and fix cuda malloc errors
- adressing all comments
- clean-up
  - making allocatedStack its own file
  - change names of some critical sections and minor things
  - adding sub-namespace for all SPECTGPU functionality and adressing comments
  - adressing comments, adapting to std::format, and adding info on user's guide
- Enabling python setup of SPECTGPU with no parfile
- fix src/swig/stir_projectors.i
- tests
  - running FBP for SPECTGPU
  - updating recontest data due to previous bug in setting number of sigma incorrectly
  - fixing test error for SPECTUB
  - comment out SPECTUB gradient test for the moment as this fails (not related to this PR)

Co-authored-by: Kris Thielemans <KrisThielemans@users.noreply.github.com>
Co-authored-by: Matteo Colombo <150334955+ColomboMatte0@users.noreply.github.com>
@KrisThielemans

Copy link
Copy Markdown
Collaborator

@danieldeidda merged some commits to avoid have too many updates to the *.v files. Will push now

@KrisThielemans

Copy link
Copy Markdown
Collaborator

I will need to update libparallelproj version.

@danieldeidda

Copy link
Copy Markdown
Collaborator Author

@KrisThielemans as expected all tests passed and no modification occurred in the rebase

@codacy-production

codacy-production Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 2 high · 1 medium · 4 minor

Alerts:
⚠ 7 issues (≤ 0 issues of at least minor severity)

Results:
7 new issues

Category Results
BestPractice 1 medium
ErrorProne 2 high
CodeStyle 4 minor

View in Codacy

🟢 Metrics 114 complexity · 26 duplication

Metric Results
Complexity 114
Duplication 26

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@KrisThielemans KrisThielemans left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I've merged master to get rid of #1794. It's looking good...

@KrisThielemans KrisThielemans left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I've added a bit more in the release notes. See if you agree.

I also suggest adding this in the class doc (with the backprojector referring tot he forward projector (use \see) and mention the adjoint.

I'd suggest that you put all suggestions in 1 commit (batch them up) and commit with [ci skip].

At the end, I (or you) can squash these last commits into 1.

Comment thread documentation/release_6.6.htm Resolved
Comment thread documentation/release_6.6.htm Resolved
Comment thread documentation/release_6.6.htm Resolved
Comment thread documentation/release_6.6.htm Resolved
@danieldeidda

Copy link
Copy Markdown
Collaborator Author

I also suggest adding this in the class doc (with the backprojector referring tot he forward projector (use \see) and mention the adjoint.

I am not sure I am following this

@KrisThielemans KrisThielemans left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I've fixed those things (and a few Codacy warnings)

@KrisThielemans
KrisThielemans merged commit e622618 into UCL:master Nov 30, 2026
12 of 13 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in 2026-03 AIRBI Hackathon Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants