From 9b7be06b06af1fef59a7b6330924e271ba2820e4 Mon Sep 17 00:00:00 2001 From: julianlitz Date: Thu, 14 May 2026 22:49:23 +0200 Subject: [PATCH 1/2] Optimize performance in PolarGrid Indexing --- include/PolarGrid/polargrid.inl | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/include/PolarGrid/polargrid.inl b/include/PolarGrid/polargrid.inl index 67b61842..b0465be2 100755 --- a/include/PolarGrid/polargrid.inl +++ b/include/PolarGrid/polargrid.inl @@ -82,10 +82,24 @@ KOKKOS_INLINE_FUNCTION int PolarGrid::wrapThetaIndex(const int unwrapped_theta_i // This effectively computes unwrapped_theta_index % ntheta(), because it discards all higher bits. // // If ntheta is not a power of two, we use the standard modulo approach to handle wrapping. - int theta_index = is_ntheta_PowerOfTwo_ ? unwrapped_theta_index & (ntheta() - 1) - : (unwrapped_theta_index % ntheta() + ntheta()) % ntheta(); - assert(0 <= theta_index && theta_index < ntheta()); - return theta_index; + if (is_ntheta_PowerOfTwo_) { + const int theta_index = unwrapped_theta_index & (ntheta() - 1); + assert(0 <= theta_index && theta_index < ntheta()); + return theta_index; + } + else { + // For non-power-of-two ntheta, we use a simple iterative approach to wrap the index. + // This is efficient for small deviations from the valid range, which is common in practice. + int theta_index = unwrapped_theta_index; + while (theta_index >= ntheta()) { + theta_index -= ntheta(); + } + while (theta_index < 0) { + theta_index += ntheta(); + } + assert(0 <= theta_index && theta_index < ntheta()); + return theta_index; + } } KOKKOS_INLINE_FUNCTION int PolarGrid::index(const int r_index, const int unwrapped_theta_index) const @@ -106,7 +120,7 @@ KOKKOS_INLINE_FUNCTION void PolarGrid::multiIndex(const int node_index, int& r_i assert(0 <= node_index && node_index < numberOfNodes()); if (node_index < numberCircularSmootherNodes()) { r_index = node_index / ntheta(); - theta_index = wrapThetaIndex(node_index); + theta_index = node_index - ntheta() * r_index; } else { theta_index = (node_index - numberCircularSmootherNodes()) / lengthRadialSmoother(); From 04a195967498362225f73e94084bdf02c4ee20e5 Mon Sep 17 00:00:00 2001 From: Julian Litz <91479202+julianlitz@users.noreply.github.com> Date: Fri, 15 May 2026 10:07:08 +0200 Subject: [PATCH 2/2] Update polargrid.inl --- include/PolarGrid/polargrid.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/PolarGrid/polargrid.inl b/include/PolarGrid/polargrid.inl index b0465be2..61419e2f 100755 --- a/include/PolarGrid/polargrid.inl +++ b/include/PolarGrid/polargrid.inl @@ -120,7 +120,7 @@ KOKKOS_INLINE_FUNCTION void PolarGrid::multiIndex(const int node_index, int& r_i assert(0 <= node_index && node_index < numberOfNodes()); if (node_index < numberCircularSmootherNodes()) { r_index = node_index / ntheta(); - theta_index = node_index - ntheta() * r_index; + theta_index = node_index % ntheta(); } else { theta_index = (node_index - numberCircularSmootherNodes()) / lengthRadialSmoother();