From e33a02d077a2bbba89c541dc80f4a3c01e66e648 Mon Sep 17 00:00:00 2001 From: lucafedeli88 Date: Thu, 22 Sep 2022 11:42:57 +0200 Subject: [PATCH 1/5] add upper and lower bound algorithms to AMReX_Algorithm --- Src/Base/AMReX_Algorithm.H | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Src/Base/AMReX_Algorithm.H b/Src/Base/AMReX_Algorithm.H index b5a5f4973c7..fce2405d7da 100644 --- a/Src/Base/AMReX_Algorithm.H +++ b/Src/Base/AMReX_Algorithm.H @@ -157,6 +157,58 @@ namespace amrex return hi; } + template + AMREX_GPU_DEVICE + ItType upper_bound (ItType first, ItType last, const ValType& val) + { +#if AMREX_DEVICE_COMPILE + ItType it; + size_t count, step; + count = last-first; + while(count>0){ + it = first; + step = count/2; + it += step; + if (!(val<*it)){ + first = ++it; + count -= step + 1; + } + else{ + count = step; + } + } + + return first; +#else + return std::upper_bound(first, last, val); +#endif + } + + template + AMREX_GPU_DEVICE + ItType lower_bound (ItType first, ItType last, const ValType& val) + { +#ifdef AMREX_DEVICE_COMPILE + size_t count = last-first; + do{ + auto it = first; + const auto step = count/2; + it += step; + if (!(val<=*it)){ + first = ++it; + count -= step + 1; + } + else{ + count = step; + } + } while(count>0); + + return first; +#else + return std::lower_bound(first, last, val); +#endif + } + namespace detail { struct clzll_tag {}; From f56a1df3fb84bf7e2282519b0176c47ee3fb8d7b Mon Sep 17 00:00:00 2001 From: lucafedeli88 Date: Fri, 23 Sep 2022 01:04:52 +0200 Subject: [PATCH 2/5] use AMREX_GPU_HOST_DEVICE --- Src/Base/AMReX_Algorithm.H | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Src/Base/AMReX_Algorithm.H b/Src/Base/AMReX_Algorithm.H index fce2405d7da..5cd60703c14 100644 --- a/Src/Base/AMReX_Algorithm.H +++ b/Src/Base/AMReX_Algorithm.H @@ -158,7 +158,7 @@ namespace amrex } template - AMREX_GPU_DEVICE + AMREX_GPU_HOST_DEVICE ItType upper_bound (ItType first, ItType last, const ValType& val) { #if AMREX_DEVICE_COMPILE @@ -185,7 +185,7 @@ namespace amrex } template - AMREX_GPU_DEVICE + AMREX_GPU_HOST_DEVICE ItType lower_bound (ItType first, ItType last, const ValType& val) { #ifdef AMREX_DEVICE_COMPILE From 28be07de33328b9ea8c3a40bfe5eeacb105d5b8a Mon Sep 17 00:00:00 2001 From: lucafedeli88 Date: Fri, 23 Sep 2022 01:07:30 +0200 Subject: [PATCH 3/5] use std::ptrdiff_t --- Src/Base/AMReX_Algorithm.H | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Src/Base/AMReX_Algorithm.H b/Src/Base/AMReX_Algorithm.H index 5cd60703c14..f6e4bda190c 100644 --- a/Src/Base/AMReX_Algorithm.H +++ b/Src/Base/AMReX_Algorithm.H @@ -163,7 +163,7 @@ namespace amrex { #if AMREX_DEVICE_COMPILE ItType it; - size_t count, step; + std::ptrdiff_t count, step; count = last-first; while(count>0){ it = first; @@ -189,7 +189,7 @@ namespace amrex ItType lower_bound (ItType first, ItType last, const ValType& val) { #ifdef AMREX_DEVICE_COMPILE - size_t count = last-first; + std::ptrdiff_t count = last-first; do{ auto it = first; const auto step = count/2; From acac786c36e5f5a52549d6c7b228e37d6e486e5d Mon Sep 17 00:00:00 2001 From: lucafedeli88 Date: Fri, 23 Sep 2022 03:19:42 +0200 Subject: [PATCH 4/5] change implementation --- Src/Base/AMReX_Algorithm.H | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Src/Base/AMReX_Algorithm.H b/Src/Base/AMReX_Algorithm.H index f6e4bda190c..7717a8c5c9f 100644 --- a/Src/Base/AMReX_Algorithm.H +++ b/Src/Base/AMReX_Algorithm.H @@ -162,12 +162,10 @@ namespace amrex ItType upper_bound (ItType first, ItType last, const ValType& val) { #if AMREX_DEVICE_COMPILE - ItType it; - std::ptrdiff_t count, step; - count = last-first; + std::ptrdiff_t count = last-first; while(count>0){ - it = first; - step = count/2; + auto it = first; + const auto step = count/2; it += step; if (!(val<*it)){ first = ++it; @@ -190,7 +188,8 @@ namespace amrex { #ifdef AMREX_DEVICE_COMPILE std::ptrdiff_t count = last-first; - do{ + while(count>0) + { auto it = first; const auto step = count/2; it += step; @@ -201,7 +200,7 @@ namespace amrex else{ count = step; } - } while(count>0); + } return first; #else From 92ae4906f85bccc4498b9d73ae1a2b032cfc3870 Mon Sep 17 00:00:00 2001 From: lucafedeli88 Date: Fri, 23 Sep 2022 19:36:52 +0200 Subject: [PATCH 5/5] use only < --- Src/Base/AMReX_Algorithm.H | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Src/Base/AMReX_Algorithm.H b/Src/Base/AMReX_Algorithm.H index 7717a8c5c9f..18c9b59b28b 100644 --- a/Src/Base/AMReX_Algorithm.H +++ b/Src/Base/AMReX_Algorithm.H @@ -167,7 +167,7 @@ namespace amrex auto it = first; const auto step = count/2; it += step; - if (!(val<*it)){ + if (!(val < *it)){ first = ++it; count -= step + 1; } @@ -193,7 +193,7 @@ namespace amrex auto it = first; const auto step = count/2; it += step; - if (!(val<=*it)){ + if (*it < val){ first = ++it; count -= step + 1; }