Skip to content

Commit

Permalink
Merge pull request #431 from Sbte/reduce
Browse files Browse the repository at this point in the history
Some small fixes for the parallel_reduce documentation.
  • Loading branch information
fnrizzi authored Jul 5, 2023
2 parents 73d6113 + 47e09f7 commit fd433b3
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions docs/source/API/core/parallel-dispatch/parallel_reduce.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Usage

.. code-block:: cpp
Kokkos::parallel_reduce( name, policy, functor, reducer... );
Kokkos::parallel_reduce( name, policy, functor, result...);
Kokkos::parallel_reduce( name, policy, functor);
Kokkos::parallel_reduce( policy, functor, reducer...);
Kokkos::parallel_reduce( policy, functor, result...);
Kokkos::parallel_reduce( policy, functor);
Kokkos::parallel_reduce(name, policy, functor, reducer...);
Kokkos::parallel_reduce(name, policy, functor, result...);
Kokkos::parallel_reduce(name, policy, functor);
Kokkos::parallel_reduce(policy, functor, reducer...);
Kokkos::parallel_reduce(policy, functor, result...);
Kokkos::parallel_reduce(policy, functor);
Dispatches parallel work defined by ``functor`` according to the *ExecutionPolicy* and performs a reduction of the contributions provided by workers as defined by the execution policy. The optional label name is used by profiling and debugging tools. The reduction type is either a ``sum``, is defined by the ``reducer`` or is deduced from an optional ``join`` operator on the functor. The reduction result is stored in ``result``, or through the ``reducer`` handle. It is also provided to the ``functor.final()`` function if such a function exists. Multiple ``reducers`` can be used in a single ``parallel_reduce`` and thus, it is possible to compute the ``min`` and the ``max`` values in a single ``parallel_reduce``.

Expand Down Expand Up @@ -79,7 +79,7 @@ Parameters:
- `TeamThreadRange <../policies/TeamThreadRange.html>`_: defines a 1D iteration range to be executed by a thread-team. Only valid inside a parallel region executed through a ``TeamPolicy`` or a ``TaskTeam``.
- `ThreadVectorRange <../policies/ThreadVectorRange.html>`_: defines a 1D iteration range to be executed through vector parallelization dividing the threads within a team. Only valid inside a parallel region executed through a ``TeamPolicy`` or a ``TaskTeam``.
* FunctorType: A valid functor with (at minimum) an ``operator()`` with a matching signature for the ``ExecPolicy`` combined with the reduced type.
* ReducerArgument: Either a class fullfilling the "Reducer" concept or a ``Kokkos::View``
* ReducerArgument: Either a class fulfilling the "Reducer" concept or a ``Kokkos::View``.
* ReducerArgumentNonConst: A scalar type or an array type; see below for functor requirements.

Requirements:
Expand All @@ -106,7 +106,7 @@ Requirements:
+ ReducerValueType must match the array signature.
+ the functor must define FunctorType::value_type the same as ReducerValueType.
+ the functor must declare a public member variable ``int value_count`` which is the length of the array.
+ the functor must implement the function ``void init( ReducerValueType dst [] ) const``.
+ the functor must implement the function ``void init( ReducerValueType dst[] ) const``.
+ the functor must implement the function ``void join( ReducerValueType dst[], ReducerValueType src[] ) const``.
+ If the functor implements the ``final`` function, the argument must also match those of init and join.

Expand All @@ -125,74 +125,74 @@ Further examples are provided in the `Custom Reductions <../../../ProgrammingGui

.. code-block:: cpp
#include<Kokkos_Core.hpp>
#include<cstdio>
#include <Kokkos_Core.hpp>
#include <cstdio>
int main(int argc, char* argv[]) {
Kokkos::initialize(argc,argv);
Kokkos::initialize(argc, argv);
int N = atoi(argv[1]);
double result;
Kokkos::parallel_reduce("Loop1", N, KOKKOS_LAMBDA (const int& i, double& lsum ) {
Kokkos::parallel_reduce("Loop1", N, KOKKOS_LAMBDA (const int& i, double& lsum) {
lsum += 1.0*i;
},result);
}, result);
printf("Result: %i %lf\n",N,result);
printf("Result: %i %lf\n", N, result);
Kokkos::finalize();
}
.. code-block:: cpp
#include<Kokkos_Core.hpp>
#include<cstdio>
#include <Kokkos_Core.hpp>
#include <cstdio>
int main(int argc, char* argv[]) {
Kokkos::initialize(argc,argv);
Kokkos::initialize(argc, argv);
int N = atoi(argv[1]);
double sum, min;
Kokkos::parallel_reduce("Loop1", N, KOKKOS_LAMBDA (const int& i, double& lsum, double& lmin ) {
Kokkos::parallel_reduce("Loop1", N, KOKKOS_LAMBDA (const int& i, double& lsum, double& lmin) {
lsum += 1.0*i;
lmin = lmin < 1.0*i ? lmin : 1.0*i;
},sum,Min<double>(min));
}, sum, Kokkos::Min<double>(min));
printf("Result: %i %lf %lf\n",N,sum,min);
printf("Result: %i %lf %lf\n", N, sum, min);
Kokkos::finalize();
}
.. code-block:: cpp
#include<Kokkos_Core.hpp>
#include<cstdio>
#include <Kokkos_Core.hpp>
#include <cstdio>
struct TagMax {};
struct TagMin {};
struct Foo {
KOKKOS_INLINE_FUNCTION
void operator() (const TagMax, const Kokkos::TeamPolicy<>::member_type& team, double& lmax) const {
if( team.league_rank % 17 + team.team_rank % 13 > lmax )
lmax = team.league_rank % 17 + team.team_rank % 13;
if (team.league_rank() % 17 + team.team_rank() % 13 > lmax)
lmax = team.league_rank() % 17 + team.team_rank() % 13;
}
KOKKOS_INLINE_FUNCTION
void operator() (const TagMin, const Kokkos::TeamPolicy<>::member_type& team, double& lmin ) const {
if( team.league_rank % 17 + team.team_rank % 13 < lmin )
lmin = team.league_rank % 17 + team.team_rank % 13;
void operator() (const TagMin, const Kokkos::TeamPolicy<>::member_type& team, double& lmin) const {
if (team.league_rank() % 17 + team.team_rank() % 13 < lmin)
lmin = team.league_rank() % 17 + team.team_rank() % 13;
}
};
int main(int argc, char* argv[]) {
Kokkos::initialize(argc,argv);
Kokkos::initialize(argc, argv);
int N = atoi(argv[1]);
Foo foo;
double max,min;
double max, min;
Kokkos::parallel_reduce(Kokkos::TeamPolicy<TagMax>(N,Kokkos::AUTO), foo, Kokkos::Max<double>(max));
Kokkos::parallel_reduce("Loop2", Kokkos::TeamPolicy<TagMin>(N,Kokkos::AUTO), foo, Kokkos::Min<double>(min));
Kokkos::fence();
printf("Result: %lf %lf\n",min,max);
printf("Result: %lf %lf\n", min, max);
Kokkos::finalize();
}

0 comments on commit fd433b3

Please sign in to comment.