2929#include < memory>
3030#include < type_traits>
3131#include < string> // stoi
32+ #ifdef MDSPAN_ENABLE_OPENMP
33+ #include < omp.h>
34+ #endif
3235
3336// mfh 2022/08/08: This is based on my comment on RAPIDS RAFT issue 725:
3437// https://github.com/rapidsai/raft/pull/725#discussion_r937991701
3538
3639namespace {
3740using Kokkos::aligned_accessor;
38- using Kokkos::detail::assume_aligned_method;
39- using Kokkos::detail::align_attribute_method;
40-
41- // We define aligned_pointer_t through a struct
42- // so we can check whether the byte alignment is valid.
43- // This makes it impossible to use the alias
44- // with an invalid byte alignment.
45- template <class ElementType , std::size_t byte_alignment>
46- struct aligned_pointer {
47- #if defined(__ICC)
48- // x86-64 ICC 2021.5.0 emits warning #3186 ("expected typedef declaration") here.
49- // No other compiler (including Clang, which has a similar type attribute) has this issue.
50- # pragma warning push
51- # pragma warning disable 3186
52- #endif
53-
54- using type = ElementType* MDSPAN_IMPL_ALIGN_VALUE_ATTRIBUTE ( byte_alignment );
55-
56- #if defined(__ICC)
57- # pragma warning pop
58- #endif
59- };
60-
61- template <class ElementType , std::size_t byte_alignment>
62- using aligned_pointer_t = typename aligned_pointer<ElementType, byte_alignment>::type;
6341
6442using test_value_type = float ;
6543constexpr std::size_t min_overalignment_factor = 8 ;
@@ -69,13 +47,6 @@ constexpr std::size_t min_byte_alignment = min_overalignment_factor * sizeof(flo
6947// Some compilers have trouble optimizing loops with unsigned or 64-bit index types.
7048using index_type = int ;
7149
72- template <class ElementType , std::size_t byte_alignment>
73- aligned_pointer_t <ElementType, byte_alignment>
74- bless (ElementType* ptr, std::integral_constant<std::size_t , byte_alignment> /* ba */ )
75- {
76- return MDSPAN_IMPL_ASSUME_ALIGNED ( ElementType, ptr, byte_alignment );
77- }
78-
7950template <class ElementType >
8051struct delete_raw {
8152 void operator ()(ElementType* p) const {
@@ -155,14 +126,14 @@ class aligned_array_allocation {
155126 num_elements (number_of_elements)
156127 {}
157128
158- aligned_pointer_t < ElementType, byte_alignment> data () const
129+ ElementType * data () const
159130 {
160- return MDSPAN_IMPL_ASSUME_ALIGNED ( ElementType, pointer, byte_alignment );
131+ return Kokkos::assume_aligned< byte_alignment >(pointer );
161132 }
162133
163134private:
164135 allocation_t <ElementType> allocation{nullptr , delete_raw<ElementType>{}};
165- aligned_pointer_t < ElementType, byte_alignment> pointer{nullptr };
136+ ElementType * pointer{nullptr };
166137 std::size_t num_elements{0 };
167138};
168139
@@ -321,9 +292,9 @@ auto benchmark_add_raw_1d(const std::size_t num_trials,
321292// Assume that x, y, and z all have the same alignment.
322293template <class ElementType , std::size_t byte_alignment>
323294void add_aligned_raw_1d (const index_type n,
324- aligned_pointer_t < const ElementType, byte_alignment> x,
325- aligned_pointer_t < const ElementType, byte_alignment> y,
326- aligned_pointer_t < ElementType, byte_alignment> z)
295+ const ElementType * MDSPAN_ALIGN ( byte_alignment) x,
296+ const ElementType * MDSPAN_ALIGN( byte_alignment) y,
297+ ElementType * MDSPAN_ALIGN( byte_alignment) z)
327298{
328299 for (index_type i = 0 ; i < n; ++i) {
329300 z[i] = x[i] + y[i];
@@ -337,19 +308,19 @@ auto benchmark_add_aligned_raw_1d(const std::size_t num_trials,
337308 const ElementType x[],
338309 const ElementType y[],
339310 ElementType z[],
340- std::integral_constant<std::size_t , byte_alignment> ba )
311+ std::integral_constant<std::size_t , byte_alignment>)
341312{
342313 TICK ();
343- auto x_blessed = bless (x, ba );
344- auto y_blessed = bless (y, ba );
345- auto z_blessed = bless (z, ba );
314+ auto x_blessed = Kokkos::assume_aligned< byte_alignment >(x );
315+ auto y_blessed = Kokkos::assume_aligned< byte_alignment >(y );
316+ auto z_blessed = Kokkos::assume_aligned< byte_alignment >(z );
346317 for (std::size_t trial = 0 ; trial < num_trials; ++trial) {
347318 add_aligned_raw_1d<ElementType, byte_alignment>(n, x_blessed, y_blessed, z_blessed);
348319 }
349320 return TOCK ();
350321}
351322
352- #ifdef _OPENMP
323+ #ifdef MDSPAN_ENABLE_OPENMP
353324template <class ElementType , std::size_t byte_alignment>
354325void add_omp_simd_aligned_mdspan_1d (aligned_mdspan_1d<const ElementType, byte_alignment> x,
355326 aligned_mdspan_1d<const ElementType, byte_alignment> y,
@@ -533,7 +504,7 @@ auto benchmark_add_omp_aligned_simd_aligned_raw_1d(
533504 }
534505 return TOCK ();
535506}
536- #endif // _OPENMP
507+ #endif // MDSPAN_ENABLE_OPENMP
537508
538509template <class ElementType >
539510void set_elements_of_arrays (const index_type n,
@@ -578,7 +549,7 @@ int main(int argc, char* argv[])
578549 benchmark_add_aligned_raw_1d (num_trials, n, x_aligned.data (),
579550 y_aligned.data (), z_aligned.data (),
580551 byte_alignment);
581- #ifdef _OPENMP
552+ #ifdef MDSPAN_ENABLE_OPENMP
582553 auto omp_simd_aligned_mdspan_result =
583554 benchmark_add_omp_simd_aligned_mdspan_1d (num_trials, n, x_aligned.data (),
584555 y_aligned.data (), z_aligned.data (),
@@ -609,7 +580,7 @@ int main(int argc, char* argv[])
609580 benchmark_add_raw_1d (num_trials, n, x_unaligned.data (),
610581 y_unaligned.data (), z_unaligned.data ());
611582
612- #ifdef _OPENMP
583+ #ifdef MDSPAN_ENABLE_OPENMP
613584 auto omp_simd_mdspan_result =
614585 benchmark_add_omp_simd_mdspan_1d (num_trials, n, x_unaligned.data (),
615586 y_unaligned.data (), z_unaligned.data ());
@@ -620,17 +591,13 @@ int main(int argc, char* argv[])
620591
621592 cout << " Number of trials: " << num_trials << endl
622593 << " Number of loop iterations per trial: " << n << endl
623- << " Way to declare a pointer value aligned, if any: "
624- << assume_aligned_method << endl
625- << " Way to declare a pointer type aligned, if any: "
626- << align_attribute_method << endl
627594 << " Total time in seconds for non-OpenMP loops:" << endl
628595 << " aligned mdspan: " << aligned_mdspan_result << endl
629596 << " unaligned mdspan: " << mdspan_result << endl
630597 << " aligned raw: " << aligned_raw_result << endl
631598 << " unaligned raw: " << raw_result << endl;
632599
633- #ifdef _OPENMP
600+ #ifdef MDSPAN_ENABLE_OPENMP
634601 cout << " Total time in seconds for OpenMP (omp simd) loops:" << endl
635602 << " omp_simd_aligned_mdspan: " << omp_simd_aligned_mdspan_result << endl
636603 << " omp_simd_mdspan: " << omp_simd_mdspan_result << endl
0 commit comments