Skip to content

Commit

Permalink
Overwork several thinks in mem access strategy
Browse files Browse the repository at this point in the history
- thanks to @j-stephan
  • Loading branch information
SimeonEhrig committed Feb 15, 2022
1 parent 44bd5db commit f3aaa68
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 63 deletions.
38 changes: 25 additions & 13 deletions include/vikunja/access/BaseStrategy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace vikunja::MemAccess
//! Constructor.
//!
//! \param index The index.
//! \param maximum The first index outside of the iterator memory.
//! \param maximum value of the index (not inclusive).
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE BaseStrategy(TIdx const index, TIdx const maximum)
: m_index(index)
, m_maximum(maximum)
Expand All @@ -37,34 +37,46 @@ namespace vikunja::MemAccess

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE BaseStrategy(const BaseStrategy& other) = default;

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator==(const BaseStrategy& other) const -> bool
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr friend auto operator==(
BaseStrategy const& st,
const BaseStrategy& other) -> bool
{
return (this->m_index == other.m_index) && (this->m_maximum == other.m_maximum);
return (st.m_index == other.m_index) && (st.m_maximum == other.m_maximum);
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator!=(const BaseStrategy& other) const -> bool
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr friend auto operator!=(
BaseStrategy const& st,
const BaseStrategy& other) -> bool
{
return !operator==(other);
return !operator==(st, other);
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator<(const BaseStrategy& other) const -> bool
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr friend auto operator<(
BaseStrategy const& st,
const BaseStrategy& other) -> bool
{
return m_index < other.m_index;
return st.m_index < other.m_index;
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator>(const BaseStrategy& other) const -> bool
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr friend auto operator>(
BaseStrategy const& st,
const BaseStrategy& other) -> bool
{
return m_index > other.m_index;
return st.m_index > other.m_index;
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator<=(const BaseStrategy& other) const -> bool
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr friend auto operator<=(
BaseStrategy const& st,
const BaseStrategy& other) -> bool
{
return m_index <= other.m_index;
return st.m_index <= other.m_index;
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator>=(const BaseStrategy& other) const -> bool
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr friend auto operator>=(
BaseStrategy const& st,
const BaseStrategy& other) -> bool
{
return m_index >= other.m_index;
return st.m_index >= other.m_index;
}

//-----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ namespace vikunja::MemAccess
{
/**
* A policy based memory access strategy that splits the data access into chunks. Depending on the memory access
* policy, these chunks can access the data sequential or in a striding pattern.
* policy, these chunks can access the data sequentially or in a striding pattern.
* @tparam MemAccessPolicy The memory access policy to use.
* @tparam TAcc The alpaka accelerator type.
* @tparam TIdx The index type
*
* The memory access policy should provide three values:
* - The startIndex of the iterator, which is the first index to use.
* - The endIndex of the iterator, which is the last index to use.
* - The stepSize of the iterator, which tells how far the iterator should move.
* - The startIndex which is the first index to use.
* - The endIndex which is the last index to use.
* - The stepSize which specifies how large the distance is between an index position and its successor.
*/
template<typename MemAccessPolicy, typename TAcc, typename TIdx>
class PolicyBasedBlockStrategy : public BaseStrategy<TIdx>
class BlockStrategy : public BaseStrategy<TIdx>
{
private:
TIdx m_step; /**< The step size of this iterator. */
TIdx m_step; /**< The step size of this strategy. */
public:
/**
* Create a policy based block iterator
* Create a policy based block strategy accessor
* @param acc The accelerator type to use.
* @param problemSize The size of the original iterator.
* @param problemSize The size of the original strategy.
* @param blockSize The size of the blocks.
*/
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE PolicyBasedBlockStrategy(TAcc const& acc, TIdx problemSize, TIdx blockSize)
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE BlockStrategy(TAcc const& acc, TIdx problemSize, TIdx blockSize)
: BaseStrategy<TIdx>(
MemAccessPolicy::getStartIndex(acc, problemSize, blockSize),
MemAccessPolicy::getEndIndex(acc, problemSize, blockSize))
, m_step(MemAccessPolicy::getStepSize(acc, problemSize, blockSize))
{
}

ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE PolicyBasedBlockStrategy(const PolicyBasedBlockStrategy& other) = default;
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE BlockStrategy(const BlockStrategy& other) = default;

//-----------------------------------------------------------------------------
//! Returns a memory access object with the index set to the last item.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto end() const -> PolicyBasedBlockStrategy
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto end() const -> BlockStrategy
{
PolicyBasedBlockStrategy ret(*this);
BlockStrategy ret(*this);
ret.m_index = this->m_maximum;
return ret;
}
Expand All @@ -62,7 +62,7 @@ namespace vikunja::MemAccess
//! Increments the internal index to the next one.
//!
//! Returns a reference to the next index.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator++() -> PolicyBasedBlockStrategy&
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator++() -> BlockStrategy&
{
this->m_index += this->m_step;
return *this;
Expand All @@ -73,7 +73,7 @@ namespace vikunja::MemAccess
//! next one.
//!
//! Returns a reference to the current index.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator++(int) -> PolicyBasedBlockStrategy
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator++(int) -> BlockStrategy
{
auto ret(*this);
this->m_index += this->m_step;
Expand All @@ -85,7 +85,7 @@ namespace vikunja::MemAccess
//! element.
//!
//! Returns a reference to the previous index.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator--() -> PolicyBasedBlockStrategy&
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator--() -> BlockStrategy&
{
this->m_index -= this->m_step;
return *this;
Expand All @@ -96,7 +96,7 @@ namespace vikunja::MemAccess
//! previous one.
//!
//! Returns a reference to the current index.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator--(int) -> PolicyBasedBlockStrategy
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator--(int) -> BlockStrategy
{
auto ret(*this);
this->m_index -= this->m_step;
Expand All @@ -107,7 +107,7 @@ namespace vikunja::MemAccess
//! Returns the index + a supplied offset.
//!
//! \param n The offset.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator+(uint64_t n) const -> PolicyBasedBlockStrategy
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator+(uint64_t n) const -> BlockStrategy
{
auto ret(*this);
ret.m_index += n * m_step;
Expand All @@ -118,7 +118,7 @@ namespace vikunja::MemAccess
//! Returns the index - a supplied offset.
//!
//! \param n The offset.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator-(uint64_t n) const -> PolicyBasedBlockStrategy
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator-(uint64_t n) const -> BlockStrategy
{
auto ret(*this);
ret.m_index -= n * m_step;
Expand All @@ -131,7 +131,7 @@ namespace vikunja::MemAccess
//! \param offset The offset.
//!
//! Returns the current object offset by the offset.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator+=(uint64_t offset) -> PolicyBasedBlockStrategy&
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator+=(uint64_t offset) -> BlockStrategy&
{
this->m_index += offset * this->m_step;
return *this;
Expand All @@ -143,7 +143,7 @@ namespace vikunja::MemAccess
//! \param offset The offset.
//!
//! Returns the current object offset by the offset.
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator-=(uint64_t offset) -> PolicyBasedBlockStrategy&
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE auto operator-=(uint64_t offset) -> BlockStrategy&
{
this->m_index -= offset * this->m_step;
return *this;
Expand All @@ -153,15 +153,15 @@ namespace vikunja::MemAccess
namespace policies
{
/**
* A memory policy for the PolicyBlockBasedIterator that provides grid striding memory access.
* A memory policy for the BlockStrategy that provides grid striding memory access.
*/
struct GridStridingMemAccessPolicy
{
template<typename TAcc, typename TIdx>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static auto getStartIndex(
TAcc const& acc,
TIdx const& problemSize __attribute__((unused)),
TIdx const& blockSize __attribute__((unused))) -> TIdx const
TIdx const& /* problemSize */,
TIdx const& /* blockSize */) -> TIdx const
{
constexpr TIdx xIndex = alpaka::Dim<TAcc>::value - 1u;
return alpaka::getIdx<alpaka::Grid, alpaka::Threads>(acc)[xIndex];
Expand All @@ -171,15 +171,15 @@ namespace vikunja::MemAccess
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static auto getEndIndex(
TAcc const& acc,
TIdx const& problemSize,
TIdx const& blockSize __attribute__((unused))) -> TIdx const
TIdx const& /* blockSize */) -> TIdx const
{
return problemSize;
}

template<typename TAcc, typename TIdx>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static auto getStepSize(
TAcc const& acc,
TIdx const& problemSize __attribute__((unused)),
TIdx const& /* problemSize */,
TIdx const& blockSize) -> TIdx const
{
constexpr TIdx xIndex = alpaka::Dim<TAcc>::value - 1u;
Expand All @@ -191,7 +191,7 @@ namespace vikunja::MemAccess
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static auto isValidThreadResult(
TAcc const& acc,
TIdx const& problemSize,
TIdx const& blockSize __attribute__((unused))) -> bool const
TIdx const& /* blockSize */) -> bool const
{
constexpr TIdx xIndex = alpaka::Dim<TAcc>::value - 1u;
auto threadIndex = (alpaka::getIdx<alpaka::Block, alpaka::Threads>(acc)[xIndex]);
Expand All @@ -207,7 +207,7 @@ namespace vikunja::MemAccess
};

/**
* A memory access policy for the PolicyBlockBasedIterator that provides linear memory access.
* A memory access policy for the BlockStrategy that provides linear memory access.
*/
struct LinearMemAccessPolicy
{
Expand Down Expand Up @@ -241,18 +241,18 @@ namespace vikunja::MemAccess

template<typename TAcc, typename TIdx>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static constexpr auto getStepSize(
TAcc const& acc __attribute__((unused)),
TIdx const& problemSize __attribute__((unused)),
TIdx const& blockSize __attribute__((unused))) -> TIdx const
TAcc const& /* acc */,
TIdx const& /* problemSize */,
TIdx const& /* blockSize */) -> TIdx const
{
return 1;
}

template<typename TAcc, typename TIdx>
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE static constexpr auto isValidThreadResult(
TAcc const& acc __attribute__((unused)),
TIdx const& problemSize __attribute__((unused)),
TIdx const& blockSize __attribute__((unused))) -> bool const
TAcc const& /* acc */,
TIdx const& /* problemSize */,
TIdx const& /* blockSize */) -> bool const
{
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions include/vikunja/reduce/detail/BlockThreadReduceKernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#pragma once

#include <vikunja/access/PolicyBasedBlockStrategy.hpp>
#include <vikunja/access/BlockStrategy.hpp>

#include <alpaka/alpaka.hpp>

Expand Down Expand Up @@ -104,9 +104,9 @@ namespace vikunja

using MemPolicy = TMemAccessPolicy;
// Create an iterator with the specified memory access policy that wraps the input iterator.
using MenIndex = vikunja::MemAccess::PolicyBasedBlockStrategy<MemPolicy, TAcc, TIdx>;
MenIndex iter(acc, n, TBlockSize);
MenIndex end = iter.end();
using MemIndex = vikunja::MemAccess::BlockStrategy<MemPolicy, TAcc, TIdx>;
MemIndex iter(acc, n, TBlockSize);
MemIndex end = iter.end();

auto startIndex
= MemPolicy::getStartIndex(acc, static_cast<TIdx>(n), static_cast<TIdx>(TBlockSize));
Expand Down
2 changes: 1 addition & 1 deletion include/vikunja/reduce/reduce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#pragma once

#include <vikunja/access/PolicyBasedBlockStrategy.hpp>
#include <vikunja/access/BlockStrategy.hpp>
#include <vikunja/operators/operators.hpp>
#include <vikunja/reduce/detail/BlockThreadReduceKernel.hpp>
#include <vikunja/reduce/detail/SmallProblemReduceKernel.hpp>
Expand Down
10 changes: 5 additions & 5 deletions include/vikunja/transform/detail/BlockThreadTransformKernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#pragma once

#include <vikunja/access/PolicyBasedBlockStrategy.hpp>
#include <vikunja/access/BlockStrategy.hpp>

#include <alpaka/alpaka.hpp>

Expand Down Expand Up @@ -41,8 +41,8 @@ namespace vikunja
TIdx const& n,
TFunc const& func) const
{
using MenIndex = vikunja::MemAccess::PolicyBasedBlockStrategy<TMemAccessPolicy, TAcc, TIdx>;
for(MenIndex iter(acc, n, TBlockSize), end = iter.end(); iter < end; ++iter)
using MemIndex = vikunja::MemAccess::BlockStrategy<TMemAccessPolicy, TAcc, TIdx>;
for(MemIndex iter(acc, n, TBlockSize), end = iter.end(); iter < end; ++iter)
{
destination[*iter] = TOperator::run(acc, func, source[*iter]);
}
Expand All @@ -63,8 +63,8 @@ namespace vikunja
TIdx const& n,
TFunc const& func) const
{
using MenIndex = vikunja::MemAccess::PolicyBasedBlockStrategy<TMemAccessPolicy, TAcc, TIdx>;
for(MenIndex iter(acc, n, TBlockSize), end = iter.end(); iter < end; ++iter)
using MemIndex = vikunja::MemAccess::BlockStrategy<TMemAccessPolicy, TAcc, TIdx>;
for(MemIndex iter(acc, n, TBlockSize), end = iter.end(); iter < end; ++iter)
{
destination[*iter] = TOperator::run(acc, func, source[*iter], sourceSecond[*iter]);
}
Expand Down
2 changes: 1 addition & 1 deletion include/vikunja/transform/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#pragma once

#include <vikunja/access/PolicyBasedBlockStrategy.hpp>
#include <vikunja/access/BlockStrategy.hpp>
#include <vikunja/operators/operators.hpp>
#include <vikunja/transform/detail/BlockThreadTransformKernel.hpp>
#include <vikunja/workdiv/BlockBasedWorkDiv.hpp>
Expand Down
8 changes: 4 additions & 4 deletions test/include/vikunja/test/AlpakaSetup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace vikunja
}

/**
* @brief Allocate 1D memory on host.
* @brief Allocate 1D memory on the host.
*
* @tparam TData Type of the memory.
* @param size Size of the memory.
Expand All @@ -66,7 +66,7 @@ namespace vikunja
}

/**
* @brief Allocate ND memory on host.
* @brief Allocate ND memory on the host.
*
* @tparam TData Type of the memory.
* @tparam TExtentDim alpaka::Vec<N, Idx>
Expand All @@ -80,7 +80,7 @@ namespace vikunja
}

/**
* @brief Allocate 1D memory on device.
* @brief Allocate 1D memory on the device.
*
* @tparam TData Type of the memory.
* @param size Size of the memory.
Expand All @@ -94,7 +94,7 @@ namespace vikunja
}

/**
* @brief Allocate ND memory on device.
* @brief Allocate ND memory on the device.
*
* @tparam TData Type of the memory.
* @tparam TExtentDim alpaka::Vec<N, Idx>
Expand Down
2 changes: 1 addition & 1 deletion test/include/vikunja/test/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#pragma once

#include <vikunja/access/PolicyBasedBlockStrategy.hpp>
#include <vikunja/access/BlockStrategy.hpp>

#include <alpaka/alpaka.hpp>
#include <alpaka/example/ExampleDefaultAcc.hpp>
Expand Down
1 change: 0 additions & 1 deletion test/unit/access/src/BaseStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

#include <vikunja/access/BaseStrategy.hpp>
#include <vikunja/access/PolicyBasedBlockStrategy.hpp>

#include <alpaka/alpaka.hpp>

Expand Down

0 comments on commit f3aaa68

Please sign in to comment.