Skip to content

Add padding callback examples demonstrating load and store callbacks - #361

Open
ThibaultGH wants to merge 7 commits into
NVIDIA:mainfrom
ThibaultGH:feature/zero-padding-callback-examples
Open

Add padding callback examples demonstrating load and store callbacks#361
ThibaultGH wants to merge 7 commits into
NVIDIA:mainfrom
ThibaultGH:feature/zero-padding-callback-examples

Conversation

@ThibaultGH

Copy link
Copy Markdown

Add padding callback examples demonstrating load and store callbacks

This PR adds new examples to demonstrate the use of both load and store callbacks with cuFFT, complementing the existing windowing examples that only show load callbacks.

New Examples

  • r2c_c2r_padding_legacy_callback_example.cu: Legacy API with load/store callbacks
  • r2c_c2r_padding_lto_callback_example.cpp: LTO API with load/store callbacks
  • r2c_c2r_padding_reference.cu: Reference implementation without callbacks

Key Differences from Windowing Examples

  • Load callback on forward FFT (R2C): Implements zero-padding by returning 0 for indices beyond the original signal size
  • Store callback on inverse FFT (C2R): Truncates output to original size and normalizes
  • Demonstrates complete round-trip with both callback types

Implementation Notes

  • Followed the existing code structure and naming conventions from the windowing examples
  • Happy to make adjustments if the structure or implementation doesn't match NVIDIA's preferences
  • Currently only includes legacy and LTO (nvcc) variants
  • Note: LTO+NVRTC variant not included in this PR, but can be added later if needed

Testing

  • All examples build cleanly with no warnings
  • All examples run successfully with L2 error ≈ 0 (within threshold)
  • Existing windowing examples remain unaffected

@greptile-apps

greptile-apps Bot commented Jul 3, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds zero-padding examples to the existing cuFFT callback sample directory, demonstrating both load and store callbacks together (forward R2C zero-pads the input; inverse C2R truncates and normalises the output). It also renames the existing windowing files to carry a windowing_ prefix for clarity.

  • New files: legacy and LTO callback variants (r2c_c2r_padding_legacy_callback_example.cu, r2c_c2r_padding_lto_callback_example.cpp) plus matching device-side callback files, a reference implementation, and a shared header.
  • Renames: all existing r2c_c2r_* sources and build targets gain a windowing_ prefix; CMakeLists.txt and Makefile are updated throughout.
  • Build system: CMakeLists.txt is updated correctly; the Makefile link rule for the padding LTO target inadvertently passes generated .h files to the linker via $^, which will cause a link-time failure when building with make.

Confidence Score: 4/5

  • The new CUDA padding examples are logically correct; the Makefile link rule for the padding LTO target passes generated header files to the linker, which will prevent make from building that target successfully.
  • The callback logic, plan setup, and cleanup are all sound across both new examples. However, the Makefile link rule for r2c_c2r_padding_lto_callback_example lists build/r2c_c2r_padding_load_callback_fatbin.h and build/r2c_c2r_padding_store_callback_fatbin.h as regular prerequisites and uses $^ in the recipe, so both header files are forwarded to the linker — a pattern the analogous windowing target avoids. Users building via make (rather than CMake) will hit a linker error for this target.
  • cuFFT/lto_callback_window_1d/Makefile — the link rule for the padding LTO target needs the .h files removed or moved to order-only prerequisites.

Important Files Changed

Filename Overview
cuFFT/lto_callback_window_1d/Makefile Adds padding targets but the link rule for r2c_c2r_padding_lto_callback_example incorrectly lists generated .h files in $^ which will cause a linker failure when building via make.
cuFFT/lto_callback_window_1d/CMakeLists.txt Renames windowing targets and adds two new padding targets; CMake correctly handles generated .h files in target_sources as dependency-only (no linking issue here).
cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_legacy_callback_example.cu New legacy callback example demonstrating load/store padding; load callback correctly zeros padded indices, store callback normalises and truncates output. Error path cleanup is complete.
cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_lto_callback_example.cpp New LTO callback example; callbacks are correctly registered before cufftMakePlan1d as required by the LTO API, and cleanup paths are thorough.
cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_reference.cu Reference implementation uses explicit zero-padding and normalisation without callbacks; logic matches what the callback variants implement, suitable for L2 comparison.
cuFFT/lto_callback_window_1d/src/callback_params.h Adds PaddingCallbackParams struct and padding constants; padding_batches constant is declared but neither example uses it (both hardcode 1), which is a minor inconsistency.

Sequence Diagram

sequenceDiagram
    participant Host
    participant GPU as GPU (device_signals)
    participant CUFFT as cuFFT Engine
    participant LoadCB as Load Callback (R2C)
    participant StoreCB as Store Callback (C2R)
    participant Complex as device_complex

    Host->>GPU: cudaMemcpy input_signals[0..signal_size)
    Host->>CUFFT: cufftExecR2C(forward_plan, device_signals, device_complex)
    loop for each index 0..padded_signal_size-1
        CUFFT->>LoadCB: read index
        alt "index < signal_size"
            LoadCB-->>CUFFT: return device_signals[index]
        else "index >= signal_size"
            LoadCB-->>CUFFT: return 0.0f  (zero-pad)
        end
    end
    CUFFT->>Complex: write R2C output

    Host->>CUFFT: cufftExecC2R(inverse_plan, device_complex, device_signals)
    loop for each index 0..padded_signal_size-1
        CUFFT->>StoreCB: write index, value
        alt "index < signal_size"
            StoreCB->>GPU: "out[index] = value / padded_signal_size"
        else "index >= signal_size"
            StoreCB->>GPU: "out[index] = 0.0f"
        end
    end

    Host->>GPU: cudaMemcpy output_signals[0..signal_size)
Loading

Reviews (4): Last reviewed commit: "fix(Makefile): update targets to match w..." | Re-trigger Greptile

Comment thread cuFFT/lto_callback_window_1d/src/common.cpp Outdated
Comment thread cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_lto_load_callback_device.cu Outdated
@JanuszL JanuszL added the cuFFT label Jul 3, 2026
Thibault Cimic added 6 commits July 3, 2026 15:25
- Rename all windowing example files to include 'windowing' in their names:
  - r2c_c2r_legacy_callback_example.cu -> r2c_c2r_windowing_legacy_callback_example.cu
  - r2c_c2r_lto_callback_example.cpp -> r2c_c2r_windowing_lto_callback_example.cpp
  - r2c_c2r_lto_nvrtc_callback_example.cpp -> r2c_c2r_windowing_lto_nvrtc_callback_example.cpp
  - r2c_c2r_lto_callback_device.cu -> r2c_c2r_windowing_lto_callback_device.cu
  - r2c_c2r_reference.* -> r2c_c2r_windowing_reference.*
- Update CMakeLists.txt, README.md, and includes to use new names
- Rename executables to include windowing prefix

Signed-off-by: Thibault Cimic <thibault.cimic@cea.fr>
- Add compute_error_windowing for complex buffer layout (windowing)
- Add compute_error_padding for real buffer layout (padding)
- Update windowing examples to use compute_error_windowing
- Update common.h with both error computation functions

Signed-off-by: Thibault Cimic <thibault.cimic@cea.fr>
New files:
- r2c_c2r_padding_legacy_callback_example.cu: Legacy API with load/store callbacks
- r2c_c2r_padding_lto_callback_example.cpp: LTO API with load/store callbacks
- r2c_c2r_padding_lto_load_callback_device.cu: LTO load callback device code
- r2c_c2r_padding_lto_store_callback_device.cu: LTO store callback device code
- r2c_c2r_padding_reference.cu/h: Reference implementation without callbacks

Modified files:
- CMakeLists.txt: Add padding targets and fatbin generation
- README.md: Document padding examples
- callback_params.h: Add padding parameters
- common.h: Add compute_error_padding function

Signed-off-by: Thibault Cimic <thibault.cimic@cea.fr>
In the error path when reference_r2c_padding_c2r() fails, the code was
only freeing host memory but not CUDA resources. This adds the missing:
- cufftDestroy() for both forward and inverse plans
- cudaFree() for device_signals, device_complex, and device_params

Files:
- cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_legacy_callback_example.cu
- cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_lto_callback_example.cpp

Signed-off-by: Thibault Cimic <thibault.cimic@cea.fr>
The PaddingCallbackParams struct was duplicated in 4 files. This consolidates
it into callback_params.h and updates all files to include the header.

Files:
- cuFFT/lto_callback_window_1d/src/callback_params.h (added struct)
- cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_legacy_callback_example.cu
- cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_lto_callback_example.cpp
- cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_lto_load_callback_device.cu
- cuFFT/lto_callback_window_1d/src/r2c_c2r_padding_lto_store_callback_device.cu

Signed-off-by: Thibault Cimic <thibault.cimic@cea.fr>
The init_input_signals_padding function was never called in any example and
had a hidden bug (used += without zeroing the buffer first). This removes
the unused function and its declaration.

Files:
- cuFFT/lto_callback_window_1d/src/common.cpp
- cuFFT/lto_callback_window_1d/src/common.h

Signed-off-by: Thibault Cimic <thibault.cimic@cea.fr>

@sylvesterkaczmarek sylvesterkaczmarek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could the Makefile be updated together with the CMake build?

This PR renames the existing sources/targets to r2c_c2r_windowing_*, but the Makefile still builds r2c_c2r_lto_callback_example, r2c_c2r_lto_nvrtc_callback_example, r2c_c2r_legacy_callback_example and the old r2c_c2r_reference objects.

Since those source names are being replaced in this PR, the documented make build path will no longer work even though the CMake path does. The Makefile should follow the windowing renames and probably add the new padding targets as well.

…ing examples

Update Makefile to reflect the file renames from the windowing examples
and add build rules for the new padding examples.

Changes:
- Rename all windowing targets to include 'windowing_' prefix
- Add build rules for padding LTO and legacy callback examples
- Add fatbin generation for padding load/store callbacks
- Update references to use new filenames

This ensures the make build path works alongside the CMake path.

Signed-off-by: Thibault Cimic <thibault.cimic@cea.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants