1 #ifndef DASH__SHARED_H_ 2 #define DASH__SHARED_H_ 8 #include <dash/GlobRef.h> 9 #include <dash/memory/MemorySpace.h> 10 #include <dash/memory/UniquePtr.h> 11 #include <dash/allocator/GlobalAllocator.h> 13 #include <dash/iterator/GlobIter.h> 22 template <
typename ElementType>
26 typedef ElementType element_t;
29 typedef size_t size_type;
30 typedef size_t difference_type;
39 typedef typename reference::value_type value_type;
44 using unique_gptr_t = decltype(
45 dash::allocate_unique<element_t>(
allocator_type{}, std::size_t{}));
46 using pointer_t =
typename unique_gptr_t::pointer;
48 template <
typename T_>
51 static constexpr
size_t nels = 1;
52 static constexpr
size_t nbytes =
sizeof(element_t);
59 unique_gptr_t m_data{};
60 pointer_t m_glob_pointer;
74 "Shared.Shared(team,owner) >",
"finished delegating constructor");
83 const value_type& val,
90 , m_memory_resource(nbytes,
team)
91 , m_allocator(&m_memory_resource)
93 DASH_LOG_DEBUG_VAR(
"Shared.Shared(value,team,owner)()",
owner);
95 DASH_ASSERT_RETURNS(
init(val),
true);
107 Shared(
const self_t& other) =
delete;
113 : m_team(other.m_team)
114 , m_owner(other.m_owner)
115 , m_memory_resource(std::move(other.m_memory_resource))
116 , m_allocator(&m_memory_resource)
118 auto gptr = other.m_data.release();
119 m_data = unique_gptr_t{
120 gptr,
typename unique_gptr_t::deleter{m_allocator, nels}};
121 DASH_LOG_DEBUG(
"Shared.Shared(Shared&&) >");
127 self_t&
operator=(
const self_t& other) =
delete;
134 if (
this == &other) {
138 m_team = other.m_team;
139 m_owner = other.m_owner;
144 m_memory_resource = std::move(other.m_memory_resource);
147 m_allocator.resource(),
149 "invalid state in move assignment");
152 m_data.reset(other.m_data.release());
154 m_glob_pointer = other.m_glob_pointer;
156 if (m_team->myid() == m_owner) {
158 m_glob_pointer, m_data.get(),
"invalid state in move assignment");
161 DASH_LOG_DEBUG(
"Shared.Shared(Shared&&) >");
175 bool init(value_type val = value_type{})
184 if (m_glob_pointer) {
185 DASH_LOG_ERROR(
"dash::Shared::init",
"Shared scalar is already initialized");
190 if (!m_data && m_team->myid() == m_owner) {
192 "Shared.init(value,team,owner)",
193 "allocating shared value in local memory");
195 m_data = std::move(dash::allocate_unique<element_t>(m_allocator, nels));
197 DASH_ASSERT_MSG(m_data,
"null pointer after allocation");
199 auto* laddr =
static_cast<element_t*
>(m_data.get().local());
202 auto local_alloc = m_memory_resource.get_allocator();
206 allocator_traits::construct(local_alloc, laddr, element_t{val});
207 m_glob_pointer = m_data.get();
214 &m_glob_pointer, ds.nelem, ds.dtype, m_owner, m_team->
dart_id()),
217 DASH_LOG_DEBUG_VAR(
"Shared.init(value,team,owner) >", m_glob_pointer);
218 DASH_LOG_DEBUG(
"Shared.init(value,team,owner) >");
220 return static_cast<bool>(m_glob_pointer);
226 void set(
const value_type& val)
228 DASH_LOG_DEBUG(
"Shared.set()");
229 DASH_LOG_DEBUG_VAR(
"Shared.set", m_owner);
230 DASH_LOG_DEBUG_VAR(
"Shared.set", m_glob_pointer);
231 DASH_ASSERT(m_glob_pointer);
232 this->
get().
set(val);
233 DASH_LOG_DEBUG(
"Shared.set >");
241 DASH_LOG_DEBUG(
"Shared.cget()");
242 DASH_LOG_DEBUG_VAR(
"Shared.cget", m_owner);
243 DASH_LOG_DEBUG_VAR(
"Shared.get", m_glob_pointer);
244 DASH_ASSERT(static_cast<bool>(m_glob_pointer));
245 return reference(m_glob_pointer.dart_gptr());
251 const_reference
get()
const 253 DASH_LOG_DEBUG(
"Shared.get()");
254 DASH_LOG_DEBUG_VAR(
"Shared.get", m_owner);
255 DASH_LOG_DEBUG_VAR(
"Shared.get", m_glob_pointer);
256 DASH_ASSERT(m_glob_pointer);
257 return const_reference(m_glob_pointer.dart_gptr());
264 constexpr value_type
const*
local() const noexcept
266 return (m_team->myid() == m_owner) ? m_glob_pointer.local() :
nullptr;
275 return (m_team->myid() == m_owner) ? m_glob_pointer.local() :
nullptr;
299 DASH_ASSERT(static_cast<bool>(m_glob_pointer));
311 DASH_ASSERT(m_team !=
nullptr);
320 return m_glob_pointer;
324 template <
typename T>
329 swap(a.m_team, b.m_team);
330 swap(a.m_owner, b.m_owner);
331 swap(a.m_memory_resource, b.m_memory_resource);
337 auto ptrA = a.m_data.release();
338 a.m_data.reset(b.m_data.release());
339 b.m_data.reset(ptrA);
341 swap(a.m_glob_pointer, b.m_glob_pointer);
346 #endif // DASH__SHARED_H_ dash::Team & team() const noexcept
The dash::Team that created this shared object.
This class is a simple memory pool which holds allocates elements of size ValueType.
void barrier()
Flush global memory of shared value and synchronize its associated units.
dart_ret_t dart_bcast(void *buf, size_t nelem, dart_datatype_t dtype, dart_team_unit_t root, dart_team_t team)
DART Equivalent to MPI broadcast.
self_t & operator=(self_t &&other) DASH_NOEXCEPT
Move-assignment operator.
Shared(team_unit_t owner=team_unit_t(0), Team &team=dash::Team::All())
Constructor, allocates shared value at single unit in specified team.
bool is_initialized()
Check whether DASH has been initialized already.
A Team instance specifies a subset of all available units.
~Shared()=default
Destructor, frees shared memory.
Shared(self_t &&other) noexcept
Move-constructor.
self_t & operator=(const self_t &other)=delete
Assignment operator: DELETED.
value_type * local() noexcept
Native pointer to the starting address of the local memory of the unit that initialized this dash::Sh...
DART Global pointer type.
dash::team_unit_t owner() const noexcept
The unit owning the memory in the global address space.
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
Shared(const value_type &val, team_unit_t owner=team_unit_t(0), Team &team=dash::Team::All())
Constructor, allocates shared value at single unit in specified team.
void flush()
Flush global memory of shared value.
dart_gptr_t dart_gptr() const noexcept
Get underlying DART global pointer of the shared variable.
#define DART_UNDEFINED_UNIT_ID
Undefined unit ID.
Shared access to a value in global memory across a team.
constexpr value_type const * local() const noexcept
Native pointer to the starting address of the local memory of the unit that initialized this dash::Sh...
Convencience wrapper to determine the DART type and number of elements required for the given templat...
static Team & All()
The invariant Team instance containing all available units.
bool init(value_type val=value_type{})
Collective allocation of a shared variable with an initial value.
dart_team_t dart_id() const
Index of this team relative to global team dash::Team::All().
dart_ret_t dart_flush(dart_gptr_t gptr)
Guarantee completion of all outstanding operations involving a segment on a certain unit...