Skip to content

Commit

Permalink
Algorithms : reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJoube committed Sep 15, 2024
1 parent b3f1029 commit 126e170
Show file tree
Hide file tree
Showing 7 changed files with 558 additions and 266 deletions.
38 changes: 31 additions & 7 deletions include/kwk/algorithm/algos/reduce.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//==================================================================================================
//======================================================================================================================
/**
KIWAKU - Containers Well Made
Copyright : KIWAKU Project Contributors
SPDX-License-Identifier: BSL-1.0
**/
//==================================================================================================
//======================================================================================================================
#pragma once

#include <kwk/concepts/container.hpp>
#include <kwk/context/context.hpp>
#include <kwk/algorithm/algos/for_each.hpp>
#include <kwk/detail/abi.hpp>
#include <cstddef>
Expand All @@ -22,22 +23,45 @@ min, max, minmax -> valeurs

namespace kwk
{

// Reduce is not a required part of Contexts (unlike map)
// A custom overloaded reduce(my_context, ...) is required to use a custom context.
template<typename Context, typename Func, concepts::container In>
constexpr auto reduce(Context& ctx, In const& in, Func f, auto init)
{
ctx.map( [&](auto const& i) { init = f(init, i); }
, ctx.in(in)
);
return init;
}

template<typename Func, concepts::container In>
constexpr auto reduce(In const& in, Func f, auto init)
{
kwk::for_each([&](auto... is) { init = f(init, in(is...)); }, in.shape() );
return init;
return kwk::reduce(cpu, in, f, init);
}

template<typename Context, typename Func, concepts::container In>
constexpr auto reduce(Context& ctx, In const& in, Func f)
{
return kwk::reduce(ctx, in, f, typename In::value_type{});
}

template<typename Func, concepts::container In>
constexpr auto reduce(In const& in, Func f)
{
return kwk::reduce(in, f, typename In::value_type{});
return kwk::reduce(cpu, in, f);
}

template<typename Context, concepts::container In>
constexpr auto reduce(Context& ctx, In const& in)
{
return kwk::reduce(ctx, in, [](auto a, auto e) { return a+e; });
}

template<concepts::container In>
constexpr auto reduce(In const& in)
{
return kwk::reduce(in, [](auto a, auto e) { return a+e; });
return kwk::reduce(cpu, in);
}
}
} // namespace kwk
84 changes: 84 additions & 0 deletions test/algorithm/algos/context/cpu/reduce.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//======================================================================================================================
/*
KIWAKU - Containers Well Made
Copyright : KIWAKU Contributors & Maintainers
SPDX-License-Identifier: BSL-1.0
*/
//======================================================================================================================
#include <kwk/algorithm/algos/for_each.hpp>
#include <kwk/algorithm/algos/reduce.hpp>
#include <kwk/container.hpp>
#include "test.hpp"
#include "../generic/reduce.hpp"

// TODO: update these tests

TTS_CASE("Check for kwk::reduce(in) 1D")
{
kwk::test::reduce_in_1D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in) 1D - larger")
{
kwk::test::reduce_in_1D_larger(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in) 2D")
{
kwk::test::reduce_in_2D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in) 3D")
{
kwk::test::reduce_in_3D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in) 4D")
{
kwk::test::reduce_in_4D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in, func) 1D")
{
kwk::test::reduce_in_func_1D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in, func) 2D")
{
kwk::test::reduce_in_func_1D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in, func) 3D")
{
kwk::test::reduce_in_func_3D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in, func) 4D")
{
kwk::test::reduce_in_func_4D(kwk::cpu);
};

TTS_CASE("Check for float kwk::reduce(in, func)")
{
kwk::test::reduce_in_func_float(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in, func, init) 1D")
{
kwk::test::reduce_in_func_init_1D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in, func, init) 2D")
{
kwk::test::reduce_in_func_init_2D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in, func, init) 3D")
{
kwk::test::reduce_in_func_init_3D(kwk::cpu);
};

TTS_CASE("Check for kwk::reduce(in, func, init) 4D")
{
kwk::test::reduce_in_func_init_4D(kwk::cpu);
};
Loading

0 comments on commit 126e170

Please sign in to comment.