Skip to content

Roland sipos/zero out optimization - #44

Open
roland-sipos wants to merge 3 commits into
developfrom
roland-sipos/zero-out-optimization
Open

roland-sipos wants to merge 3 commits into
developfrom
roland-sipos/zero-out-optimization

Conversation

@roland-sipos

@roland-sipos roland-sipos commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

Description

Addresses issue #43

Changes Made

  1. AVX Threshold Processor
    File: AVXThresholdProcessor.cpp
    Optimization: Replaced _mm256_blendv_epi8(_mm256_setzero_si256(), signal, mask) with _mm256_and_si256(signal, mask).
    Latency/Throughput Improvement: Reduced a variable-blend instruction down to a single bitwise AND instruction, increasing throughput and port availability (releasing Port 5 constraints).

  2. AVX Frugal Pedestal Subtract Processor
    File: AVXFrugalPedestalSubtractProcessor.cpp
    Optimizations: Replaced the two 3-instruction blocks generating to_add = (is_gt ? 1 : 0) + (is_lt ? -1 : 0) (which materialized zero, 1, -1 and ran blendv) with _mm256_sub_epi16(is_lt, is_gt). Since comparison masks contain elements that are either 0 or 0xFFFF (which is signed -1), is_lt - is_gt mathematically computes the exact desired addition/subtraction.
    Replaced _mm256_blendv_epi8(m_accum, _mm256_setzero_si256(), need_reset) with _mm256_andnot_si256(need_reset, m_accum).
    Latency/Throughput Improvement: Avoided 4 slow blendv instructions, 2 zero-vector initializations, and 4 constant-vector initializations. The operations now use single-cycle arithmetic subtraction and bitwise ANDNOT.

  3. AVX Pipeline
    File: AVXPipeline.cpp
    Optimizations: Replaced _mm256_blendv_epi8(_mm256_setzero_si256(), m_ones_register, active) with _mm256_and_si256(m_ones_register, active).
    Replaced 5 blendv calls masking fields in generate_tps with fast _mm256_and_si256.
    Replaced 5 blendv calls resetting fields in generate_tps with fast _mm256_andnot_si256.
    Latency/Throughput Improvement: Eliminated 11 slow variable-blend (blendv) intrinsics in the critical path of trigger primitive generation.

Standalone Benchmark and Verification App
A self-contained validation and performance benchmark app is located in /test/app/test_benchmark_tpg.cxx

You can build this benchmark application on any Linux system with GCC and AVX2 support without needing the DUNE-DAQ environment:

Compile

g++ -O3 -mavx2 test/apps/test_benchmark_tpg.cxx -o tpglibs_benchmark

Run

./tpglibs_benchmark

Type of change

  • Documentation (non-breaking change that adds or improves the documentation)
  • New feature or enhancement (non-breaking change which adds functionality)
  • Optimization (non-breaking change that improves code/performance)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (whatever its nature)

Testing checklist

  • Unit tests pass (e.g. dbt-build --unittest)
  • Minimal system quicktest passes (pytest -s minimal_system_quick_test.py)
  • Full set of integration tests pass (dunedaq_integtest_bundle.sh)
  • Python tests pass if applicable (e.g. python -m pytest)
  • Pre-commit hooks run successfully if applicable (e.g. pre-commit run --all-files)

Comments here on the testing

Further checks

  • Code is commented where needed, particularly in hard-to-understand areas
  • Code style is correct (dbt-build --lint, and/or see https://dune-daq-sw.readthedocs.io/en/latest/packages/styleguide/)
  • If applicable, new tests have been added or an issue has been opened to tackle that in the future.
    (Indicate issue here: # (issue))

@ShyamB97

Copy link
Copy Markdown
Collaborator

Report of the performance testing comparing base to the changes made:
TPG optimisation test results.pdf

Report of the TP quantities comparing base to the changes made:
TP_comparison_report.pdf

Overall, there is a significant performance improvement with the changes, and the TP quantities from both runs are consistent with one another.

@ShyamB97 ShyamB97 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.

Based on the testing I think the changes are ready and PR can be opened for merge.

@ShyamB97
ShyamB97 marked this pull request as ready for review August 18, 2026 08:29
@ShyamB97

Copy link
Copy Markdown
Collaborator

Based on discussion in the readout WG meeting. two key points were raised by Alessandro:

  • bit-wise comparison of the TPs was not done, just the global properties. This should be done to be confident there is no change to generated TPs on the same ADC patterns (this should make use of tpg_test_processsor_app.cxx)
  • Considering to keep the current implementation and the optimised implementation in the event there are edge cases not considered. Then, when we have confidence we can decide to fully make the transition.

Thus, it would be appropriate to defer this to next release.

I would appreciate comments from @roland-sipos on this and discussion on slack with the relevant people would be good.

@roland-sipos

Copy link
Copy Markdown
Member Author

Thanks for the test reports!
The PR has a commit ( f981d93 ) with a micro-bench and test application that does bit-wise comparison on randomized vectors. I think that shows that the modifications are not causing content discrepancy and only replaces instruction combinations with built-in ones that essentially do the same.

@denizergonul denizergonul added the enhancement New feature or request label Sep 8, 2026
@ShyamB97

Copy link
Copy Markdown
Collaborator

Following discussion in the readout meeting two item came up related to this PR:

  • Roland wrote a text app to check this which likely covers the comparisons needed, thought it would be good to (quickly) check the existing test application can cover the same tests and perform bit-wise comparisons of two different algorithm.
  • Rather than changing the current pipeline and processors, can new variants be made instead? This would allow switching the algorithm implementation at configuration, and then also do direct comparisons of the two algorithms in the same test bench.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Optimization]: AVX2 Intrinsics: Blend vs. Andnot

3 participants