Skip to content

Commit

Permalink
add asserts for bounds checking to Array1D and Array2D (AMReX-Codes#1230
Browse files Browse the repository at this point in the history
)

## Summary

Array1D and Array2D did not check if the index was in bounds.  This adds an `AMREX_ASSERT` that catches out of bounds issues when compiled with `DEBUG = TRUE`.

## Additional background

## Checklist

The proposed changes:
- [ ] fix a bug or incorrect behavior in AMReX
- [ ] add new capabilities to AMReX
- [ ] changes answers in the test suite to more than roundoff level
- [ ] are likely to significantly affect the results of downstream AMReX users
- [ ] are described in the proposed changes to the AMReX documentation, if appropriate
  • Loading branch information
zingale authored Aug 5, 2020
1 parent e2dac9c commit 30fe5a5
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Src/Base/AMReX_Array.H
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ namespace amrex {
{
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
const T& operator() (int i) const noexcept {
AMREX_ASSERT(i >= XLO && i <= XHI);
return arr[i-XLO];
}

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T& operator() (int i) noexcept {
AMREX_ASSERT(i >= XLO && i <= XHI);
return arr[i-XLO];
}

Expand All @@ -94,27 +96,31 @@ namespace amrex {
typename std::enable_if<std::is_same<O,Order::F>::value,int>::type=0>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
const T& operator() (int i, int j) const noexcept {
AMREX_ASSERT(i >= XLO && i <= XHI && j >= YLO && j <= YHI);
return arr[i+j*(XHI-XLO+1)-(YLO*(XHI-XLO+1)+XLO)];
}

template <typename O=ORDER,
typename std::enable_if<std::is_same<O,Order::F>::value,int>::type=0>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T& operator() (int i, int j) noexcept {
AMREX_ASSERT(i >= XLO && i <= XHI && j >= YLO && j <= YHI);
return arr[i+j*(XHI-XLO+1)-(YLO*(XHI-XLO+1)+XLO)];
}

template <typename O=ORDER,
typename std::enable_if<std::is_same<O,Order::C>::value,int>::type=0>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
const T& operator() (int i, int j) const noexcept {
AMREX_ASSERT(i >= XLO && i <= XHI && j >= YLO && j <= YHI);
return arr[j+i*(YHI-YLO+1)-(XLO*(YHI-YLO+1)+YLO)];
}

template <typename O=ORDER,
typename std::enable_if<std::is_same<O,Order::C>::value,int>::type=0>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T& operator() (int i, int j) noexcept {
AMREX_ASSERT(i >= XLO && i <= XHI && j >= YLO && j <= YHI);
return arr[j+i*(YHI-YLO+1)-(XLO*(YHI-YLO+1)+YLO)];
}

Expand Down

0 comments on commit 30fe5a5

Please sign in to comment.