Skip to content

Commit

Permalink
Add example code referenced in issue #452 (#556)
Browse files Browse the repository at this point in the history
* add example from issue #452

* MinLoc.rst: fix indentation

* builtinreducers/MinLoc.rst: use clang-format-8 (+ 4 spaces indent) in example

* builtinreducers/MinLoc.rst: rm "^I" identation, replace with spaces
  • Loading branch information
ajpowelsnl authored Sep 3, 2024
1 parent 057e4b5 commit 474237e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/source/API/core/builtinreducers/MinLoc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,44 @@ Additional Information
* Requires: ``Index`` has ``operator =`` defined. ``Kokkos::reduction_identity<Index>::min()`` is a valid expression.

* In order to use MinLoc with a custom type of either ``Scalar`` or ``Index``, a template specialization of ``Kokkos::reduction_identity<CustomType>`` must be defined. See `Built-In Reducers with Custom Scalar Types <../../../ProgrammingGuide/Custom-Reductions-Built-In-Reducers-with-Custom-Scalar-Types.html>`_ for details

Example
-------

.. code-block:: cpp
#include <Kokkos_Core.hpp>
struct Idx3D_t {
int value[3];
int& operator[](int i) { return value[i]; }
const int& operator[](int i) const { return value[i]; }
};
template <>
struct Kokkos::reduction_identity<Idx3D_t> {
static constexpr Idx3D_t min() { return {0, 0, 0}; }
};
int main(int argc, char* argv[]) {
Kokkos::initialize(argc, argv);
{
Kokkos::View<double***> a("A", 5, 5, 5);
Kokkos::deep_copy(a, 10);
a(2, 3, 1) = 5;
using MinLoc_t = Kokkos::MinLoc<double, Idx3D_t>;
using MinLocVal_t = typename MinLoc_t::value_type;
MinLocVal_t result;
Kokkos::parallel_reduce(
Kokkos::MDRangePolicy<Kokkos::Rank<3>>({0, 0, 0}, {5, 5, 5}),
KOKKOS_LAMBDA(int i, int j, int k, MinLocVal_t& val) {
if (a(i, j, k) < val.val) {
val.val = a(i, j, k);
val.loc[0] = i;
val.loc[1] = j;
val.loc[2] = k;
}
},
MinLoc_t(result));
printf("%lf %i %i %i\n", result.val, result.loc[0], result.loc[1],
result.loc[2]);
}
Kokkos::finalize();
}

0 comments on commit 474237e

Please sign in to comment.