Skip to content

Commit

Permalink
fix shared allocation by using a unique ID
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminW3 committed Oct 29, 2015
1 parent 0d2a01c commit 78d286c
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 10 deletions.
8 changes: 6 additions & 2 deletions include/alpaka/block/shared/BlockSharedAllocCudaBuiltIn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ namespace alpaka
//!
//#############################################################################
template<
typename T>
typename T,
std::size_t TuniqueId>
struct AllocVar<
T,
TuniqueId,
BlockSharedAllocCudaBuiltIn>
{
// NOTE: CUDA requires the constructor and destructor of shared objects to be empty.
Expand Down Expand Up @@ -103,10 +105,12 @@ namespace alpaka
//#############################################################################
template<
typename T,
std::size_t TnumElements>
std::size_t TnumElements,
std::size_t TuniqueId>
struct AllocArr<
T,
TnumElements,
TuniqueId,
BlockSharedAllocCudaBuiltIn>
{
// NOTE: CUDA requires the constructor and destructor of shared objects to be empty.
Expand Down
8 changes: 6 additions & 2 deletions include/alpaka/block/shared/BlockSharedAllocMasterSync.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ namespace alpaka
//!
//#############################################################################
template<
typename T>
typename T,
std::size_t TuniqueId>
struct AllocVar<
T,
TuniqueId,
BlockSharedAllocMasterSync>
{
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -129,10 +131,12 @@ namespace alpaka
//#############################################################################
template<
typename T,
std::size_t TnumElements>
std::size_t TnumElements,
std::size_t TuniqueId>
struct AllocArr<
T,
TnumElements,
TuniqueId,
BlockSharedAllocMasterSync>
{
static_assert(
Expand Down
8 changes: 6 additions & 2 deletions include/alpaka/block/shared/BlockSharedAllocNoSync.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ namespace alpaka
//!
//#############################################################################
template<
typename T>
typename T,
std::size_t TuniqueId>
struct AllocVar<
T,
TuniqueId,
BlockSharedAllocNoSync>
{
//-----------------------------------------------------------------------------
Expand All @@ -111,10 +113,12 @@ namespace alpaka
//#############################################################################
template<
typename T,
std::size_t TnumElements>
std::size_t TnumElements,
std::size_t TuniqueId>
struct AllocArr<
T,
TnumElements,
TuniqueId,
BlockSharedAllocNoSync>
{
static_assert(
Expand Down
18 changes: 14 additions & 4 deletions include/alpaka/block/shared/Traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <alpaka/core/Common.hpp> // ALPAKA_FN_HOST_ACC

#include <alpaka/core/UniqueId.hpp> // core::detail::uniqueId

#include <type_traits> // std::enable_if, std::is_base_of, std::is_same, std::decay

namespace alpaka
Expand All @@ -47,6 +49,7 @@ namespace alpaka
//#############################################################################
template<
typename T,
std::size_t TuniqueId,
typename TBlockSharedAlloc,
typename TSfinae = void>
struct AllocVar;
Expand All @@ -56,6 +59,7 @@ namespace alpaka
template<
typename T,
std::size_t TnumElements,
std::size_t TuniqueId,
typename TBlockSharedAlloc,
typename TSfinae = void>
struct AllocArr;
Expand Down Expand Up @@ -86,6 +90,7 @@ namespace alpaka
return
traits::AllocVar<
T,
core::detail::uniqueId(),
TBlockSharedAlloc>
::allocVar(
blockSharedAlloc);
Expand Down Expand Up @@ -116,6 +121,7 @@ namespace alpaka
traits::AllocArr<
T,
TnumElements,
core::detail::uniqueId(),
TBlockSharedAlloc>
::allocArr(
blockSharedAlloc);
Expand Down Expand Up @@ -146,10 +152,12 @@ namespace alpaka
//! The AllocVar trait specialization for classes with BlockSharedAllocBase member type.
//#############################################################################
template<
typename TBlockSharedAlloc,
typename T>
typename T,
std::size_t TuniqueId,
typename TBlockSharedAlloc>
struct AllocVar<
T,
TuniqueId,
TBlockSharedAlloc,
typename std::enable_if<
std::is_base_of<typename TBlockSharedAlloc::BlockSharedAllocBase, typename std::decay<TBlockSharedAlloc>::type>::value
Expand All @@ -174,12 +182,14 @@ namespace alpaka
//! The AllocArr trait specialization for classes with BlockSharedAllocBase member type.
//#############################################################################
template<
typename TBlockSharedAlloc,
typename T,
std::size_t TnumElements>
std::size_t TnumElements,
std::size_t TuniqueId,
typename TBlockSharedAlloc>
struct AllocArr<
T,
TnumElements,
TuniqueId,
TBlockSharedAlloc,
typename std::enable_if<
std::is_base_of<typename TBlockSharedAlloc::BlockSharedAllocBase, typename std::decay<TBlockSharedAlloc>::type>::value
Expand Down
152 changes: 152 additions & 0 deletions include/alpaka/core/UniqueId.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/**
* \file
* Copyright 2015 Benjamin Worpitz
*
* This file is part of alpaka.
*
* alpaka is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alpaka is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with alpaka.
* If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <boost/predef.h> // workarounds

#include <cstddef> // std::size_t

namespace alpaka
{
namespace core
{
namespace detail
{
//#############################################################################
// Based on the code from Filip Roséen at http://b.atch.se/posts/constexpr-counter/
//#############################################################################
/*template<
std::size_t N>
struct flag;
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
template<
std::size_t N>
constexpr int adl_flag(flag<N>);*/

//#############################################################################
//
//#############################################################################
template<
std::size_t N>
struct flag
{
friend constexpr std::size_t adl_flag(flag<N>);
};

//#############################################################################
//
//#############################################################################
template<
std::size_t N>
struct writer
{
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
friend constexpr std::size_t adl_flag(flag<N>)
{
return N;
}

static constexpr std::size_t value = N;
};

#ifdef BOOST_COMP_MSVC
//-----------------------------------------------------------------------------
//! The matcher.
//-----------------------------------------------------------------------------
template<
std::size_t N,
class = char[noexcept(adl_flag(flag<N>{})) ? +1u : -1u]>
auto constexpr reader(std::size_t, flag<N>)
-> std::size_t
{
return N;
}
#else
//-----------------------------------------------------------------------------
//! The matcher.
//-----------------------------------------------------------------------------
template<
std::size_t N,
std::size_t = adl_flag(flag<N>{})>
auto constexpr reader(
std::size_t,
flag<N>)
-> std::size_t
{
return N;
}
#endif
//-----------------------------------------------------------------------------
//! The searcher.
//-----------------------------------------------------------------------------
template<
std::size_t N>
auto constexpr reader(
float,
flag<N>,
std::size_t R = reader(std::size_t{0u}, flag<N-1u>{}))
-> std::size_t
{
return R;
}
//-----------------------------------------------------------------------------
//! Reader base case.
//-----------------------------------------------------------------------------
std::size_t constexpr reader(float, flag<0u>)
{
return 0u;
}

#ifdef BOOST_COMP_MSVC
//-----------------------------------------------------------------------------
//! \return An unique compile time ID.
//-----------------------------------------------------------------------------
template<
std::size_t N = 1u,
std::size_t C = reader(std::size_t{0u}, flag<64u>{})>
auto constexpr uniqueId(
std::size_t R = writer<C + N>::value)
-> std::size_t
{
return R;
}
#else
//-----------------------------------------------------------------------------
//! \return An unique compile time ID.
//-----------------------------------------------------------------------------
template<
std::size_t N = 1u>
auto constexpr uniqueId(
std::size_t R = writer<reader(std::size_t{0u}, flag<64u>{}) + N>::value)
-> std::size_t
{
return R;
}
#endif
}
}
}

0 comments on commit 78d286c

Please sign in to comment.