Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example code referenced in issue #452 #556

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 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,64 @@ 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>
Copy link
Contributor

@masterleinad masterleinad Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use four whitespaces instead of one tab?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure. I'll have to do it by hand, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like %s/\t/ /g in vi.

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();
}




















Loading