Skip to content

Commit

Permalink
use std::uint32_t, move ConfigSet to core, and del throw in constexpr
Browse files Browse the repository at this point in the history
delete throw in constexpr because it fails in gcc <= 5.x

Co-authored-by: Aditya Kashi <aditya.kashi@kit.edu>
Co-authored-by: Terry Cojean <terry.cojean@kit.edu>
Co-authored-by: Tobias Ribizel <ribizel@kit.edu>
  • Loading branch information
4 people committed May 28, 2021
1 parent 6ab97cd commit 7f72418
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 243 deletions.
209 changes: 209 additions & 0 deletions core/base/types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, the Ginkgo authors
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#ifndef GKO_CORE_BASE_TYPES_HPP_
#define GKO_CORE_BASE_TYPES_HPP_


#include <array>
#include <cstdint>
#include <type_traits>


namespace gko {
namespace detail {


/**
* mask gives the integer with Size activated bits in the end
*
* @tparam Size the number of activated bits
* @tparam ValueType the type of mask, which uses std::uint32_t as default
*
* @return the ValueType with Size activated bits in the end
*/
template <int Size, typename ValueType = std::uint32_t>
constexpr std::enable_if_t<(Size < sizeof(ValueType) * 8), ValueType> mask()
{
return (ValueType{1} << Size) - 1;
}

/**
* @copydoc mask()
*
* @note this is special case for the Size = the number of bits of ValueType
*/
template <int Size, typename ValueType = std::uint32_t>
constexpr std::enable_if_t<Size == sizeof(ValueType) * 8, ValueType> mask()
{
return ~ValueType{};
}


/**
* shift calculates the number of bits for shifting
*
* @tparam current_shift the current position of shifting
* @tparam num_groups the number of elements in array
*
* @return the number of shifting bits
*
* @note this is the last case of nested template
*/
template <int current_shift, int num_groups>
constexpr std::enable_if_t<(num_groups == current_shift + 1), int> shift(
const std::array<unsigned char, num_groups> &bits)
{
return 0;
}

/**
* @copydoc shift(const std::array<char, num_groups>)
*
* @note this is the usual case of nested template
*/
template <int current_shift, int num_groups>
constexpr std::enable_if_t<(num_groups > current_shift + 1), int> shift(
const std::array<unsigned char, num_groups> &bits)
{
return bits[current_shift + 1] +
shift<(current_shift + 1), num_groups>(bits);
}


} // namespace detail


/**
* ConfigSet is a way to embed several information into one integer by given
* certain bits.
*
* The usage will be the following
* Set the method with bits Cfg = ConfigSet<b_0, b_1, ..., b_k>
* Encode the given infomation encoded = Cfg::encode(x_0, x_1, ..., x_k)
* Decode the specific position information x_t = Cfg::decode<t>(encoded)
* The encoded result will use 32 bits to record
* rrrrr0..01....1...k..k, which 1/2/.../k means the bits store the information
* for 1/2/.../k position and r is for rest of unused bits.
*
* Denote $B_t = \sum_{i = t+1}^k b_i$ and $F(X) = Cfg::encode(x_0, ..., x_k)$.
* Have $F(X) = \sum_{i = 0}^k (x_i << B_i) = \sum_{i = 0}^k (x_i * 2^{B_i})$.
* For all i, we have $0 <= x_i < 2^{b_i}$.
* $x_i$, $2^{B_i}$ are non-negative, so
* $F(X) = 0$ <=> $X = \{0\}$, $x_i = 0$ for all i.
* Assume $F(X) = F(Y)$, then
* $0 = |F(X) - F(Y)| = |F(X-Y)| = F(|X - Y|)$.
* $|x_i - y_i|$ is still in the same range $0 <= |x_i - y_i| < 2^{b_i}$.
* Thus, $F(|X - Y|) = 0$ -> $|X - Y| = \{0\}$, $x_i - y_i = 0$ -> $X = Y$.
* F is one-to-one function if $0 <= x_i < 2^{b_i}$ for all i.
* For any encoded result R, we can use the following to get the decoded series.
* for i = k to 0;
* $x_i = R % b_i$;
* $R = R / bi$;
* endfor;
* For any R in the range $[0, 2^{B_0})$, we have X such that $F(X) = R$.
* F is onto function.
* Thus, F is bijection.
*
* @tparam num_bits... the number of bits for each position.
*
* @note the num_bit is required at least $ceil(log_2(maxval) + 1)$
*/
template <unsigned char... num_bits>
class ConfigSet {
public:
static constexpr unsigned num_groups = sizeof...(num_bits);
static constexpr std::array<unsigned char, num_groups> bits{num_bits...};

/**
* Decodes the `position` information from encoded
*
* @tparam position the position of desired information
*
* @param encoded the encoded integer
*
* @return the decoded information at position
*/
template <int position>
static constexpr std::uint32_t decode(std::uint32_t encoded)
{
static_assert(position < num_groups,
"This position is over the bounds.");
constexpr int shift = detail::shift<position, num_groups>(bits);
constexpr auto mask = detail::mask<bits[position]>();
return (encoded >> shift) & mask;
}

/**
* Encodes the information with given bit set to encoded integer.
*
* @note the last case of nested template.
*/
template <unsigned current_iter>
static constexpr std::enable_if_t<(current_iter == num_groups),
std::uint32_t>
encode()
{
return 0;
}

/**
* Encodes the information with given bit set to encoded integer.
*
* @tparam current_iter the encoded place
* @tparam Rest... the rest type
*
* @param first the current encoded information
* @param rest... the rest of other information waiting for encoding
*
* @return the encoded integer
*/
template <unsigned current_iter = 0, typename... Rest>
static constexpr std::enable_if_t<(current_iter < num_groups),
std::uint32_t>
encode(std::uint32_t first, Rest &&... rest)
{
constexpr int shift = detail::shift<current_iter, num_groups>(bits);
if (current_iter == 0) {
static_assert(
bits[current_iter] + shift <= sizeof(std::uint32_t) * 8,
"the total bits usage is larger than std::uint32_t bits");
}
return (first << shift) |
encode<current_iter + 1>(std::forward<Rest>(rest)...);
}
};


} // namespace gko

#endif // GKO_CORE_BASE_TYPES_HPP_
61 changes: 30 additions & 31 deletions core/synthesizer/implementation_selection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,36 @@ namespace syn {
} \
}

#define GKO_ENABLE_IMPLEMENTATION_CONFIG_SELECTION(_name, _callable) \
template <typename Predicate, bool... BoolArgs, int... IntArgs, \
gko::size_type... SizeTArgs, typename... TArgs, \
typename... InferredArgs> \
inline void _name(::gko::syn::value_list<::gko::ConfigSetType>, Predicate, \
::gko::syn::value_list<bool, BoolArgs...>, \
::gko::syn::value_list<int, IntArgs...>, \
::gko::syn::value_list<gko::size_type, SizeTArgs...>, \
::gko::syn::type_list<TArgs...>, InferredArgs...) \
GKO_KERNEL_NOT_FOUND; \
\
template <::gko::ConfigSetType K, ::gko::ConfigSetType... Rest, \
typename Predicate, bool... BoolArgs, int... IntArgs, \
gko::size_type... SizeTArgs, typename... TArgs, \
typename... InferredArgs> \
inline void _name( \
::gko::syn::value_list<::gko::ConfigSetType, K, Rest...>, \
Predicate is_eligible, \
::gko::syn::value_list<bool, BoolArgs...> bool_args, \
::gko::syn::value_list<int, IntArgs...> int_args, \
::gko::syn::value_list<gko::size_type, SizeTArgs...> size_args, \
::gko::syn::type_list<TArgs...> type_args, InferredArgs... args) \
{ \
if (is_eligible(K)) { \
_callable<BoolArgs..., IntArgs..., SizeTArgs..., TArgs..., K>( \
std::forward<InferredArgs>(args)...); \
} else { \
_name(::gko::syn::value_list<::gko::ConfigSetType, Rest...>(), \
is_eligible, bool_args, int_args, size_args, type_args, \
std::forward<InferredArgs>(args)...); \
} \
#define GKO_ENABLE_IMPLEMENTATION_CONFIG_SELECTION(_name, _callable) \
template <typename Predicate, bool... BoolArgs, int... IntArgs, \
gko::size_type... SizeTArgs, typename... TArgs, \
typename... InferredArgs> \
inline void _name(::gko::syn::value_list<std::uint32_t>, Predicate, \
::gko::syn::value_list<bool, BoolArgs...>, \
::gko::syn::value_list<int, IntArgs...>, \
::gko::syn::value_list<gko::size_type, SizeTArgs...>, \
::gko::syn::type_list<TArgs...>, InferredArgs...) \
GKO_KERNEL_NOT_FOUND; \
\
template <std::uint32_t K, std::uint32_t... Rest, typename Predicate, \
bool... BoolArgs, int... IntArgs, gko::size_type... SizeTArgs, \
typename... TArgs, typename... InferredArgs> \
inline void _name( \
::gko::syn::value_list<std::uint32_t, K, Rest...>, \
Predicate is_eligible, \
::gko::syn::value_list<bool, BoolArgs...> bool_args, \
::gko::syn::value_list<int, IntArgs...> int_args, \
::gko::syn::value_list<gko::size_type, SizeTArgs...> size_args, \
::gko::syn::type_list<TArgs...> type_args, InferredArgs... args) \
{ \
if (is_eligible(K)) { \
_callable<BoolArgs..., IntArgs..., SizeTArgs..., TArgs..., K>( \
std::forward<InferredArgs>(args)...); \
} else { \
_name(::gko::syn::value_list<std::uint32_t, Rest...>(), \
is_eligible, bool_args, int_args, size_args, type_args, \
std::forward<InferredArgs>(args)...); \
} \
}


Expand Down
17 changes: 5 additions & 12 deletions core/test/base/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <gtest/gtest.h>


#include "core/base/types.hpp"


namespace {


Expand Down Expand Up @@ -115,9 +118,9 @@ TEST(ConfigSet, MaskCorrectly)

ASSERT_EQ(mask3_u, 7u);
ASSERT_EQ(fullmask_u, 0xffffffffu);
ASSERT_TRUE((std::is_same<decltype(mask3_u), const unsigned int>::value));
ASSERT_TRUE((std::is_same<decltype(mask3_u), const std::uint32_t>::value));
ASSERT_TRUE(
(std::is_same<decltype(fullmask_u), const unsigned int>::value));
(std::is_same<decltype(fullmask_u), const std::uint32_t>::value));
ASSERT_EQ(mask3_u64, 7ull);
ASSERT_EQ(fullmask_u64, 0xffffffffffffffffull);
ASSERT_TRUE(
Expand Down Expand Up @@ -210,14 +213,4 @@ TEST(ConfigSet, ConfigSetSomeFullCorrectly)
}


TEST(ConfigSet, ThrowOutOfBoundWhenExceedRepresentation)
{
using Cfg = gko::ConfigSet<3, 2, 1>;

ASSERT_THROW(auto a = Cfg::encode(0, 0, 2), std::out_of_range);
ASSERT_THROW(auto a = Cfg::encode(0, 4, 0), std::out_of_range);
ASSERT_THROW(auto a = Cfg::encode(8, 0, 0), std::out_of_range);
}


} // namespace
34 changes: 16 additions & 18 deletions dpcpp/base/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @param kernel_ the kernel name
*/
#define GKO_ENABLE_DEFAULT_HOST_CONFIG(name_, kernel_) \
template <::gko::ConfigSetType encoded, typename... InferredArgs> \
template <std::uint32_t encoded, typename... InferredArgs> \
inline void name_(dim3 grid, dim3 block, size_t dynamic_shared_memory, \
sycl::queue *queue, InferredArgs... args) \
{ \
Expand All @@ -79,21 +79,19 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* available to decode<0> for blocksize and decode<1> for
* subgroup_size by cfg_
*/
#define GKO_ENABLE_DEFAULT_CONFIG_CALL(name_, callable_, list_) \
template <typename... InferredArgs> \
void name_(::gko::ConfigSetType desired_cfg, dim3 grid, dim3 block, \
size_t dynamic_shared_memory, sycl::queue *queue, \
InferredArgs... args) \
{ \
callable_( \
list_, \
[&desired_cfg](::gko::ConfigSetType cfg) { \
return cfg == desired_cfg; \
}, \
::gko::syn::value_list<bool>(), ::gko::syn::value_list<int>(), \
::gko::syn::value_list<gko::size_type>(), \
::gko::syn::type_list<>(), grid, block, dynamic_shared_memory, \
queue, std::forward<InferredArgs>(args)...); \
#define GKO_ENABLE_DEFAULT_CONFIG_CALL(name_, callable_, list_) \
template <typename... InferredArgs> \
void name_(std::uint32_t desired_cfg, dim3 grid, dim3 block, \
size_t dynamic_shared_memory, sycl::queue *queue, \
InferredArgs... args) \
{ \
callable_( \
list_, \
[&desired_cfg](std::uint32_t cfg) { return cfg == desired_cfg; }, \
::gko::syn::value_list<bool>(), ::gko::syn::value_list<int>(), \
::gko::syn::value_list<gko::size_type>(), \
::gko::syn::type_list<>(), grid, block, dynamic_shared_memory, \
queue, std::forward<InferredArgs>(args)...); \
}

// __WG_BOUND__ gives the cuda-like launch bound in cuda ordering
Expand All @@ -116,8 +114,8 @@ namespace kernels {
namespace dpcpp {


bool validate(sycl::queue *queue, unsigned int workgroup_size,
unsigned int subgroup_size)
bool validate(sycl::queue *queue, unsigned workgroup_size,
unsigned subgroup_size)
{
auto device = queue->get_device();
auto subgroup_size_list =
Expand Down
Loading

0 comments on commit 7f72418

Please sign in to comment.