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

Conversation

ajpowelsnl
Copy link
Contributor

This PR adds example code from Compiler Explorer to MinLoc built-in reducer.

Copy link
Member

@dalg24 dalg24 left a comment

Choose a reason for hiding this comment

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

The indentation of the entire code block does not look right

@ajpowelsnl
Copy link
Contributor Author

The indentation of the entire code block does not look right

Fixed indentation (w/ spaces), so it now looks like example in View.

Comment on lines 117 to 151
#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();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

clang-format-8 gives

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

(without the preceding whitespace).

Copy link
Contributor Author

@ajpowelsnl ajpowelsnl Jul 29, 2024

Choose a reason for hiding this comment

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

So I tried your clang-formatted version (above), but it did not render correctly, and had many warnings (e.g., "WARNING: Block quote ends without a blank line; unexpected unindent"). It is possible that we do not have our project set up correctly / completely for C++ (see #451). Example formatting can be a topic for tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

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

We need to tab over the output of clang format, directives need to be tabbed


.. 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.

docs/source/API/core/builtinreducers/MinLoc.rst Outdated Show resolved Hide resolved
@ajpowelsnl ajpowelsnl force-pushed the feature/example_for_reducers branch from 19e9804 to dc69c6f Compare July 30, 2024 22:58
Copy link
Contributor

@masterleinad masterleinad left a comment

Choose a reason for hiding this comment

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

Fine with me.

@dalg24 dalg24 merged commit 474237e into kokkos:main Sep 3, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants