Skip to content

Commit

Permalink
Add a linspace-like function in AMReX_Algorithm.H (AMReX-Codes#3698)
Browse files Browse the repository at this point in the history
## Summary

I would like to propose to add a linspace-like function in
AMReX_Algorithm.H , since filling a container with equally spaced
numbers is a task that occurs rather frequently. This PR proposes a
possible implementation.
The container is modified in place only if it has at least 2 elements. 

## Additional background

## Checklist

The proposed changes:
- [ ] fix a bug or incorrect behavior in AMReX
- [X] 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
- [ ] include documentation in the code and/or rst files, if appropriate
  • Loading branch information
lucafedeli88 authored Jan 16, 2024
1 parent b96b731 commit 03f5936
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Src/Base/AMReX_Algorithm.H
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@ namespace amrex
))
}

template<typename ItType, typename ValType,
typename std::enable_if<
std::is_floating_point<typename std::iterator_traits<ItType>::value_type>::value &&
std::is_floating_point<ValType>::value,
int>::type = 0>
AMREX_GPU_HOST_DEVICE
void linspace (ItType first, const ItType& last, const ValType& start, const ValType& stop)
{
const std::ptrdiff_t count = last-first;
if (count >= 2){
const auto delta = (stop - start)/(count - 1);
for (std::ptrdiff_t i = 0; i < count-1; ++i){
*(first++) = start + i*delta;
}
*first = stop;
}
}

namespace detail {

struct clzll_tag {};
Expand Down

0 comments on commit 03f5936

Please sign in to comment.