Skip to content

Fix OpenMP data races and add speed improvements in Rcpp src/ functions#30

Merged
asgr merged 3 commits intomasterfrom
copilot/improve-rcpp-function-speed
Feb 21, 2026
Merged

Fix OpenMP data races and add speed improvements in Rcpp src/ functions#30
asgr merged 3 commits intomasterfrom
copilot/improve-rcpp-function-speed

Conversation

Copy link
Contributor

Copilot AI commented Feb 21, 2026

Several Rcpp functions contained OpenMP data races producing undefined behavior under multi-threaded execution, plus avoidable redundant work in single-threaded hot paths.

Race condition fixes

  • sum_segim.cpp (profoundSegimFlux): Threads wrote to shared fluxes[k] without synchronization. Replaced with thread-local reduction arrays merged via #pragma omp critical.
  • this_in_that.cpp (vec_this_in_vec_that, mat_this_in_vec_that): Parallel writes of true to the same ref_ID index is UB. Removed #pragma omp from the lookup-table setup loop — it's O(N) and fast enough single-threaded.
  • ellip_cover.cpp / aper_cover.cpp (profoundEllipWeight, profoundAperWeight): Overlapping sources processed on different threads would race on weight(i,j) +=. Added #pragma omp atomic on each accumulation (avoids the memory cost of per-thread weight matrices).

Performance improvements

  • akima.cpp (interpolateLinearGrid): The y-bracket index search (top_index/bottom_index) was O(ncol_tiles) per output pixel, running redundantly inside the outer row loop. Hoisted to a precomputation pass outside the row loop — reduces total index-search work from O(rows × cols × tiles) → O(cols × tiles).

  • skygrid.cpp (Cadacs_FindSkyCellValues): Replaced Rcpp::NumericVector(skyN) (zero-initializes all elements) + indexed fill with std::vector::reserve(skyN) + push_back, eliminating the zero-initialization pass.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: asgr <5617132+asgr@users.noreply.github.com>
Copilot AI changed the title [WIP] Enhance speed of Rcpp functions in src folder Fix OpenMP data races and add speed improvements in Rcpp src/ functions Feb 21, 2026
Copilot AI requested a review from asgr February 21, 2026 15:36
Copy link
Owner

@asgr asgr left a comment

Choose a reason for hiding this comment

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

Looks good, and I've learnt a bit about OpenMP races.

@asgr asgr marked this pull request as ready for review February 21, 2026 16:08
@asgr asgr merged commit bace063 into master Feb 21, 2026
10 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