Skip to content

Commit d3bfb59

Browse files
authored
Merge pull request #62 from lindsayad/remove-use-of-std
Remove use of std functions in dynamicsparsenumberbase.h
2 parents 4537914 + e828276 commit d3bfb59

4 files changed

Lines changed: 178 additions & 53 deletions

File tree

m4/kokkos.m4

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ AC_DEFUN([CONFIGURE_KOKKOS],
1616
1717
AS_IF([test "x$KOKKOS_PREFIX" != "x"], [
1818
KOKKOS_CPPFLAGS="-DMETAPHYSICL_KOKKOS_COMPILATION -I$KOKKOS_PREFIX/include"
19-
KOKKOS_CXXFLAGS="--forward-unknown-to-host-compiler $KOKKOS_CXXFLAGS"
20-
KOKKOS_LDFLAGS="--forward-unknown-to-host-compiler -L$KOKKOS_PREFIX/lib -Wl,-rpath,$KOKKOS_PREFIX/lib"
19+
KOKKOS_LDFLAGS="-L$KOKKOS_PREFIX/lib -Wl,-rpath,$KOKKOS_PREFIX/lib"
2120
KOKKOS_LIBS="-lkokkoscore"
2221
2322
KOKKOS_CFG="$KOKKOS_PREFIX/include/KokkosCore_config.h"
@@ -32,6 +31,21 @@ AC_DEFUN([CONFIGURE_KOKKOS],
3231
[have_kokkos_openmp=yes],
3332
[have_kokkos_openmp=no])
3433
34+
AS_IF(["$GREP" -F -q '#define KOKKOS_ENABLE_CXX26' \
35+
"$KOKKOS_CFG"],
36+
[kokkos_cxx_standard=26],
37+
[AS_IF(["$GREP" -F -q '#define KOKKOS_ENABLE_CXX23' \
38+
"$KOKKOS_CFG"],
39+
[kokkos_cxx_standard=23],
40+
[AS_IF(["$GREP" -F -q '#define KOKKOS_ENABLE_CXX20' \
41+
"$KOKKOS_CFG"],
42+
[kokkos_cxx_standard=20],
43+
[kokkos_cxx_standard=])])])
44+
45+
AS_IF([test "x$kokkos_cxx_standard" != "x"], [
46+
KOKKOS_CXXFLAGS="-std=c++$kokkos_cxx_standard $KOKKOS_CXXFLAGS"
47+
])
48+
3549
if test "x$have_kokkos_openmp" = "xyes"; then
3650
KOKKOS_CXXFLAGS="-fopenmp $KOKKOS_CXXFLAGS"
3751
KOKKOS_LDFLAGS="-fopenmp $KOKKOS_LDFLAGS"
@@ -43,7 +57,8 @@ AC_DEFUN([CONFIGURE_KOKKOS],
4357
AS_IF([test "x$NVCC" = "xno"],
4458
[AC_MSG_ERROR([nvcc not found. Install CUDA.])])
4559
KOKKOS_CXX="$NVCC"
46-
KOKKOS_CXXFLAGS="-x cu $KOKKOS_CXXFLAGS"
60+
KOKKOS_CXXFLAGS="--forward-unknown-to-host-compiler -x cu $KOKKOS_CXXFLAGS"
61+
KOKKOS_LDFLAGS="--forward-unknown-to-host-compiler $KOKKOS_LDFLAGS"
4762
4863
dnl
4964
dnl credit to ChatGPT for the ensuing parsing of arch's from kokkos config
@@ -99,13 +114,16 @@ AC_DEFUN([CONFIGURE_KOKKOS],
99114
AS_IF([test "x$HIPCC" = "xno"],
100115
[AC_MSG_ERROR([hipcc not found; install ROCm HIP.])])
101116
KOKKOS_CXX="$HIPCC"
117+
KOKKOS_CXXFLAGS="--forward-unknown-to-host-compiler $KOKKOS_CXXFLAGS"
118+
KOKKOS_LDFLAGS="--forward-unknown-to-host-compiler $KOKKOS_LDFLAGS"
102119
;;
103120
sycl)
104121
AC_PATH_PROG([ICPX],[icpx],[no],[$PATH])
105122
AS_IF([test "x$ICPX" = "xno"],
106123
[AC_MSG_ERROR([icpx (oneAPI) not found for SYCL backend.])])
107124
KOKKOS_CXX="$ICPX"
108-
KOKKOS_CXXFLAGS="-fsycl $KOKKOS_CXXFLAGS"
125+
KOKKOS_CXXFLAGS="--forward-unknown-to-host-compiler -fsycl $KOKKOS_CXXFLAGS"
126+
KOKKOS_LDFLAGS="--forward-unknown-to-host-compiler $KOKKOS_LDFLAGS"
109127
;;
110128
openmp)
111129
KOKKOS_CXX="$CXX"

src/numerics/include/metaphysicl/dynamicsparsenumberbase.h

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "metaphysicl/dynamicsparsenumberbase_decl.h"
3333

34+
#include "metaphysicl/metaphysicl_algorithm.h"
3435
#include "metaphysicl/metaphysicl_common.h"
3536
#include "metaphysicl/metaphysicl_math.h"
3637

@@ -142,42 +143,15 @@ Indices&
142143
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::nude_indices()
143144
{ return _indices; }
144145

145-
template <typename C, typename Val>
146-
METAPHYSICL_INLINE auto lower_bound_pointer(C & container, const Val & value)
147-
{
148-
// Adapting https://en.cppreference.com/w/cpp/algorithm/lower_bound.html implementation to use
149-
// pointers for portability
150-
typedef decltype(container.data()) Ptr;
151-
Ptr lower_bound_ptr = container.data(), work_ptr;
152-
std::ptrdiff_t num_remaining_elements = container.size(), step;
153-
while (num_remaining_elements > 0)
154-
{
155-
work_ptr = lower_bound_ptr;
156-
step = num_remaining_elements / 2;
157-
work_ptr += step;
158-
if (*work_ptr < value)
159-
{
160-
lower_bound_ptr = ++work_ptr;
161-
// We can discard both the current element and the other half of the range
162-
num_remaining_elements -= step + 1;
163-
}
164-
else
165-
// Discard the other half of the range
166-
num_remaining_elements = step;
167-
}
168-
169-
return lower_bound_ptr;
170-
}
171-
172146
template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
173147
METAPHYSICL_INLINE
174148
std::size_t
175149
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::runtime_index_query(index_value_type i) const
176150
{
177-
auto ptr = MetaPhysicL::lower_bound_pointer(_indices, i);
178-
if (ptr == (_indices.data() + _indices.size()) || *ptr != i)
151+
auto it = detail::lower_bound(_indices.begin(), _indices.end(), i);
152+
if (it == _indices.end() || *it != i)
179153
return MetaPhysicL::numeric_limits<std::size_t>::max();
180-
std::size_t offset = ptr - _indices.data();
154+
std::size_t offset = it - _indices.begin();
181155
metaphysicl_assert_equal_to(_indices[offset], i);
182156
return offset;
183157
}
@@ -187,8 +161,7 @@ METAPHYSICL_INLINE
187161
std::size_t
188162
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::runtime_index_of(index_value_type i) const
189163
{
190-
auto it =
191-
std::lower_bound(_indices.begin(), _indices.end(), i);
164+
auto it = detail::lower_bound(_indices.begin(), _indices.end(), i);
192165
metaphysicl_assert(it != _indices.end());
193166
std::size_t offset = it - _indices.begin();
194167
metaphysicl_assert_equal_to(_indices[offset], i);
@@ -245,8 +218,7 @@ METAPHYSICL_INLINE
245218
typename DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::value_type&
246219
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::insert(unsigned int i)
247220
{
248-
auto upper_it =
249-
std::lower_bound(_indices.begin(), _indices.end(), i);
221+
auto upper_it = detail::lower_bound(_indices.begin(), _indices.end(), i);
250222
std::size_t offset = upper_it - _indices.begin();
251223

252224
// If we don't have entry i, insert it. Yes this is O(N).
@@ -255,8 +227,8 @@ DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::insert(unsigned
255227
{
256228
std::size_t old_size = this->size();
257229
this->resize(old_size+1);
258-
std::copy_backward(_indices.begin()+offset, _indices.begin()+old_size, _indices.end());
259-
std::copy_backward(_data.begin()+offset, _data.begin()+old_size, _data.end());
230+
detail::copy_backward(_indices.begin()+offset, _indices.begin()+old_size, _indices.end());
231+
detail::copy_backward(_data.begin()+offset, _data.begin()+old_size, _data.end());
260232
_indices[offset] = i;
261233
_data[offset] = 0;
262234
}
@@ -553,10 +525,10 @@ void
553525
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::sparsity_trim (const value_type tolerance)
554526
{
555527
metaphysicl_assert
556-
(std::adjacent_find(_indices.begin(), _indices.end()) ==
528+
(detail::adjacent_find(_indices.begin(), _indices.end()) ==
557529
_indices.end());
558530
#ifdef METAPHYSICL_HAVE_CXX11
559-
metaphysicl_assert(std::is_sorted(_indices.begin(), _indices.end()));
531+
metaphysicl_assert(detail::is_sorted(_indices.begin(), _indices.end()));
560532
#endif
561533
metaphysicl_assert(tolerance >= 0);
562534

@@ -567,7 +539,7 @@ DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::sparsity_trim (
567539
auto index_it = _indices.begin();
568540
auto data_it = _data.begin();
569541
for (; index_it != _indices.end(); ++index_it, ++data_it)
570-
if (std::abs(*data_it) > tolerance)
542+
if (MetaPhysicL::math::abs(*data_it) > tolerance)
571543
++used_indices;
572544
}
573545
#endif
@@ -584,7 +556,7 @@ DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::sparsity_trim (
584556

585557
for (auto i_it = _indices.begin();
586558
i_it != _indices.end(); ++i_it, ++d_it)
587-
if (std::abs(*d_it) > tolerance)
559+
if (MetaPhysicL::math::abs(*d_it) > tolerance)
588560
{
589561
*mi_it = *i_it;
590562
*md_it = *d_it;
@@ -790,18 +762,18 @@ if_else(
790762
const DynamicSparseNumberBase<Data2, Indices2, SubType, SubTypeArgs2...> & if_false)
791763
{
792764
metaphysicl_assert
793-
(std::adjacent_find(condition.nude_indices().begin(), condition.nude_indices().end()) ==
765+
(detail::adjacent_find(condition.nude_indices().begin(), condition.nude_indices().end()) ==
794766
condition.nude_indices().end());
795767
metaphysicl_assert
796-
(std::adjacent_find(if_true.nude_indices().begin(), if_true.nude_indices().end()) ==
768+
(detail::adjacent_find(if_true.nude_indices().begin(), if_true.nude_indices().end()) ==
797769
if_true.nude_indices().end());
798770
metaphysicl_assert
799-
(std::adjacent_find(if_false.nude_indices().begin(), if_false.nude_indices().end()) ==
771+
(detail::adjacent_find(if_false.nude_indices().begin(), if_false.nude_indices().end()) ==
800772
if_false.nude_indices().end());
801773
#ifdef METAPHYSICL_HAVE_CXX11
802-
metaphysicl_assert(std::is_sorted(condition.nude_indices().begin(), condition.nude_indices().end()));
803-
metaphysicl_assert(std::is_sorted(if_true.nude_indices().begin(), if_true.nude_indices().end()));
804-
metaphysicl_assert(std::is_sorted(if_false.nude_indices().begin(), if_false.nude_indices().end()));
774+
metaphysicl_assert(detail::is_sorted(condition.nude_indices().begin(), condition.nude_indices().end()));
775+
metaphysicl_assert(detail::is_sorted(if_true.nude_indices().begin(), if_true.nude_indices().end()));
776+
metaphysicl_assert(detail::is_sorted(if_false.nude_indices().begin(), if_false.nude_indices().end()));
805777
#endif
806778

807779
typedef
@@ -947,10 +919,10 @@ if_else(
947919
}
948920

949921
metaphysicl_assert
950-
(std::adjacent_find(returnval.nude_indices().begin(), returnval.nude_indices().end()) ==
922+
(detail::adjacent_find(returnval.nude_indices().begin(), returnval.nude_indices().end()) ==
951923
returnval.nude_indices().end());
952924
#ifdef METAPHYSICL_HAVE_CXX11
953-
metaphysicl_assert(std::is_sorted(returnval.nude_indices().begin(), returnval.nude_indices().end()));
925+
metaphysicl_assert(detail::is_sorted(returnval.nude_indices().begin(), returnval.nude_indices().end()));
954926
#endif
955927

956928
return returnval;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#ifndef METAPHYSICL_ALGORITHM_H
2+
#define METAPHYSICL_ALGORITHM_H
3+
4+
#include "metaphysicl/metaphysicl_device.h"
5+
6+
#include <algorithm>
7+
#include <type_traits>
8+
9+
namespace MetaPhysicL::detail {
10+
11+
#ifdef METAPHYSICL_KOKKOS_COMPILATION
12+
13+
template <typename T, typename U>
14+
METAPHYSICL_INLINE constexpr bool less(const T & lhs, const U & rhs)
15+
{
16+
typedef typename std::remove_const<typename std::remove_reference<T>::type>::type lhs_type;
17+
typedef typename std::remove_const<typename std::remove_reference<U>::type>::type rhs_type;
18+
19+
if constexpr (std::is_integral<lhs_type>::value && std::is_integral<rhs_type>::value)
20+
{
21+
if constexpr (std::is_signed<lhs_type>::value == std::is_signed<rhs_type>::value)
22+
return lhs < rhs;
23+
else if constexpr (std::is_signed<lhs_type>::value)
24+
{
25+
if (lhs < 0)
26+
return true;
27+
return static_cast<typename std::make_unsigned<lhs_type>::type>(lhs) < rhs;
28+
}
29+
else
30+
{
31+
if (rhs < 0)
32+
return false;
33+
return lhs < static_cast<typename std::make_unsigned<rhs_type>::type>(rhs);
34+
}
35+
}
36+
else
37+
return lhs < rhs;
38+
}
39+
40+
template <typename ForwardIt, typename T>
41+
METAPHYSICL_INLINE ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T & value)
42+
{
43+
auto count = last - first;
44+
while (count > 0)
45+
{
46+
const auto step = count / 2;
47+
auto it = first + step;
48+
if (less(*it, value))
49+
{
50+
first = ++it;
51+
count -= step + 1;
52+
}
53+
else
54+
count = step;
55+
}
56+
57+
return first;
58+
}
59+
60+
template <typename BidirectionalIt1, typename BidirectionalIt2>
61+
METAPHYSICL_INLINE BidirectionalIt2 copy_backward(BidirectionalIt1 first,
62+
BidirectionalIt1 last,
63+
BidirectionalIt2 result)
64+
{
65+
while (first != last)
66+
*(--result) = *(--last);
67+
return result;
68+
}
69+
70+
template <typename ForwardIt>
71+
METAPHYSICL_INLINE ForwardIt adjacent_find(ForwardIt first, ForwardIt last)
72+
{
73+
if (first == last)
74+
return last;
75+
76+
auto next = first;
77+
++next;
78+
for (; next != last; ++first, ++next)
79+
if (*first == *next)
80+
return first;
81+
82+
return last;
83+
}
84+
85+
template <typename ForwardIt>
86+
METAPHYSICL_INLINE bool is_sorted(ForwardIt first, ForwardIt last)
87+
{
88+
if (first != last)
89+
{
90+
auto next = first;
91+
while (++next != last)
92+
{
93+
if (less(*next, *first))
94+
return false;
95+
first = next;
96+
}
97+
}
98+
99+
return true;
100+
}
101+
102+
#else
103+
104+
template <typename ForwardIt, typename T>
105+
METAPHYSICL_INLINE ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T & value)
106+
{
107+
return std::lower_bound(first, last, value);
108+
}
109+
110+
template <typename BidirectionalIt1, typename BidirectionalIt2>
111+
METAPHYSICL_INLINE BidirectionalIt2 copy_backward(BidirectionalIt1 first,
112+
BidirectionalIt1 last,
113+
BidirectionalIt2 result)
114+
{
115+
return std::copy_backward(first, last, result);
116+
}
117+
118+
template <typename ForwardIt>
119+
METAPHYSICL_INLINE ForwardIt adjacent_find(ForwardIt first, ForwardIt last)
120+
{
121+
return std::adjacent_find(first, last);
122+
}
123+
124+
template <typename ForwardIt>
125+
METAPHYSICL_INLINE bool is_sorted(ForwardIt first, ForwardIt last)
126+
{
127+
return std::is_sorted(first, last);
128+
}
129+
130+
#endif
131+
132+
} // namespace MetaPhysicL::detail
133+
134+
#endif // METAPHYSICL_ALGORITHM_H

test/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ kokkos_sparse_derivs_unit_LDFLAGS = $(AM_LDFLAGS) $(KOKKOS_LDFLAGS)
169169
kokkos_sparse_derivs_unit_LDADD = $(KOKKOS_LIBS) $(LIBS)
170170

171171
.K.o:
172-
$(LIBTOOL) --tag=CXX --mode=compile $(KOKKOS_CXX) \
172+
$(KOKKOS_CXX) \
173173
$(kokkos_sparse_derivs_unit_CPPFLAGS) \
174174
$(kokkos_sparse_derivs_unit_CXXFLAGS) \
175+
-x c++ \
175176
-c $< -o $@
176177

177178
# Force this target’s link step to use the Kokkos compiler/wrapper

0 commit comments

Comments
 (0)