DASH  0.3.0
dash::Shared< ElementType > Class Template Reference

Shared access to a value in global memory across a team. More...

#include <Shared.h>

Public Types

typedef size_t size_type
 
typedef size_t difference_type
 
typedef GlobRef< element_t > reference
 
typedef GlobRef< const element_t > const_reference
 
typedef reference::value_type value_type
 

Public Member Functions

 Shared (team_unit_t owner=team_unit_t(0), Team &team=dash::Team::All())
 Constructor, allocates shared value at single unit in specified team. More...
 
 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. More...
 
 ~Shared ()=default
 Destructor, frees shared memory. More...
 
 Shared (const self_t &other)=delete
 Copy-constructor: DELETED. More...
 
 Shared (self_t &&other) noexcept
 Move-constructor. More...
 
self_toperator= (const self_t &other)=delete
 Assignment operator: DELETED. More...
 
self_toperator= (self_t &&other) DASH_NOEXCEPT
 Move-assignment operator. More...
 
bool init (value_type val=value_type{})
 Collective allocation of a shared variable with an initial value. More...
 
void set (const value_type &val)
 Set the value of the shared element. More...
 
reference get ()
 Get a reference on the shared value. More...
 
const_reference get () const
 Get a const reference on the shared value. More...
 
constexpr value_type const * local () const noexcept
 Native pointer to the starting address of the local memory of the unit that initialized this dash::Shared instance. More...
 
value_type * local () noexcept
 Native pointer to the starting address of the local memory of the unit that initialized this dash::Shared instance. More...
 
dash::team_unit_t owner () const noexcept
 The unit owning the memory in the global address space. More...
 
dash::Teamteam () const noexcept
 The dash::Team that created this shared object. More...
 
void flush ()
 Flush global memory of shared value. More...
 
void barrier ()
 Flush global memory of shared value and synchronize its associated units. More...
 
dart_gptr_t dart_gptr () const noexcept
 Get underlying DART global pointer of the shared variable. More...
 

Friends

template<typename T_ >
void swap (Shared< T_ > &a, Shared< T_ > &b)
 

Detailed Description

template<typename ElementType>
class dash::Shared< ElementType >

Shared access to a value in global memory across a team.

Template Parameters
ElementTypeThe type of the shared value.

Definition at line 23 of file Shared.h.

Constructor & Destructor Documentation

◆ Shared() [1/4]

template<typename ElementType>
dash::Shared< ElementType >::Shared ( team_unit_t  owner = team_unit_t(0),
Team team = dash::Team::All() 
)
inline

Constructor, allocates shared value at single unit in specified team.

Parameters
ownerUnit id of the shared value's owner.
teamTeam containing all units accessing the element in shared memory

Definition at line 66 of file Shared.h.

References dash::Shared< ElementType >::owner(), and dash::Shared< ElementType >::team().

Referenced by dash::Shared< ElementType >::Shared().

71  : Shared(value_type{}, owner, team)
72  {
73  DASH_LOG_TRACE(
74  "Shared.Shared(team,owner) >", "finished delegating constructor");
75  }
dash::Team & team() const noexcept
The dash::Team that created this shared object.
Definition: Shared.h:289
Shared(team_unit_t owner=team_unit_t(0), Team &team=dash::Team::All())
Constructor, allocates shared value at single unit in specified team.
Definition: Shared.h:66
dash::team_unit_t owner() const noexcept
The unit owning the memory in the global address space.
Definition: Shared.h:281

◆ Shared() [2/4]

template<typename ElementType>
dash::Shared< ElementType >::Shared ( const value_type &  val,
team_unit_t  owner = team_unit_t(0),
Team team = dash::Team::All() 
)
inline

Constructor, allocates shared value at single unit in specified team.

The element is initialized with the given value.

Parameters
valThe value to initialize the element with
ownerUnit id of the shared value's owner.
teamTeam containing all units accessing the element in shared memory

Definition at line 81 of file Shared.h.

References dash::Shared< ElementType >::init(), dash::is_initialized(), dash::Shared< ElementType >::owner(), dash::Shared< ElementType >::Shared(), and dash::Shared< ElementType >::~Shared().

88  : m_team(&team)
89  , m_owner(owner)
90  , m_memory_resource(nbytes, team)
91  , m_allocator(&m_memory_resource)
92  {
93  DASH_LOG_DEBUG_VAR("Shared.Shared(value,team,owner)()", owner);
94  if (dash::is_initialized()) {
95  DASH_ASSERT_RETURNS(init(val), true);
96  }
97  }
dash::Team & team() const noexcept
The dash::Team that created this shared object.
Definition: Shared.h:289
bool is_initialized()
Check whether DASH has been initialized already.
dash::team_unit_t owner() const noexcept
The unit owning the memory in the global address space.
Definition: Shared.h:281
bool init(value_type val=value_type{})
Collective allocation of a shared variable with an initial value.
Definition: Shared.h:175

◆ ~Shared()

template<typename ElementType>
dash::Shared< ElementType >::~Shared ( )
default

Destructor, frees shared memory.

Referenced by dash::Shared< ElementType >::Shared().

◆ Shared() [3/4]

template<typename ElementType>
dash::Shared< ElementType >::Shared ( const self_t other)
delete

Copy-constructor: DELETED.

◆ Shared() [4/4]

template<typename ElementType>
dash::Shared< ElementType >::Shared ( self_t &&  other)
inlinenoexcept

Move-constructor.

Transfers ownership from other instance.

Definition at line 112 of file Shared.h.

References dash::Shared< ElementType >::operator=().

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)
117  {
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&&) >");
122  }

Member Function Documentation

◆ barrier()

template<typename ElementType>
void dash::Shared< ElementType >::barrier ( )
inline

Flush global memory of shared value and synchronize its associated units.

Definition at line 308 of file Shared.h.

References dash::Shared< ElementType >::flush().

309  {
310  flush();
311  DASH_ASSERT(m_team != nullptr);
312  m_team->barrier();
313  }
void flush()
Flush global memory of shared value.
Definition: Shared.h:297

◆ dart_gptr()

template<typename ElementType>
dart_gptr_t dash::Shared< ElementType >::dart_gptr ( ) const
inlinenoexcept

Get underlying DART global pointer of the shared variable.

Definition at line 318 of file Shared.h.

319  {
320  return m_glob_pointer;
321  }

◆ flush()

template<typename ElementType>
void dash::Shared< ElementType >::flush ( )
inline

Flush global memory of shared value.

Definition at line 297 of file Shared.h.

References dart_flush(), and DART_OK.

Referenced by dash::Shared< ElementType >::barrier().

298  {
299  DASH_ASSERT(static_cast<bool>(m_glob_pointer));
300  DASH_ASSERT_RETURNS(
301  dart_flush(static_cast<dart_gptr_t>(m_glob_pointer)), DART_OK);
302  }
Signals success.
Definition: dart_types.h:33
dart_ret_t dart_flush(dart_gptr_t gptr)
Guarantee completion of all outstanding operations involving a segment on a certain unit...

◆ get() [1/2]

template<typename ElementType>
reference dash::Shared< ElementType >::get ( )
inline

Get a reference on the shared value.

Definition at line 239 of file Shared.h.

Referenced by dash::io::hdf5::StoreHDF::write().

240  {
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());
246  }

◆ get() [2/2]

template<typename ElementType>
const_reference dash::Shared< ElementType >::get ( ) const
inline

Get a const reference on the shared value.

Definition at line 251 of file Shared.h.

252  {
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());
258  }

◆ init()

template<typename ElementType>
bool dash::Shared< ElementType >::init ( value_type  val = value_type{})
inline

Collective allocation of a shared variable with an initial value.

NOTE: This call succeeds only once during the lifetime of a single object.

Parameters
theinitial value of the globally shared variable.
Returns
true, if allocation and initialization succeeds. false otherwise.

Definition at line 175 of file Shared.h.

References dart_bcast(), dash::Team::dart_id(), DART_OK, and dash::is_initialized().

Referenced by dash::Shared< ElementType >::Shared().

175  {})
176  {
177  if (!dash::is_initialized()) {
178  DASH_THROW(
179  dash::exception::RuntimeError, "runtime not properly initialized");
180  }
181 
182  // If our Shared has already a non-null pointer let us return it. The user
183  // has first to deallocate it.
184  if (m_glob_pointer) {
185  DASH_LOG_ERROR("dash::Shared::init", "Shared scalar is already initialized");
186  return false;
187  }
188 
189  // Shared value is only allocated at unit 0:
190  if (!m_data && m_team->myid() == m_owner) {
191  DASH_LOG_DEBUG(
192  "Shared.init(value,team,owner)",
193  "allocating shared value in local memory");
194 
195  m_data = std::move(dash::allocate_unique<element_t>(m_allocator, nels));
196 
197  DASH_ASSERT_MSG(m_data, "null pointer after allocation");
198 
199  auto* laddr = static_cast<element_t*>(m_data.get().local());
200 
201  // get the underlying allocator for local memory space
202  auto local_alloc = m_memory_resource.get_allocator();
203  using allocator_traits = std::allocator_traits<decltype(local_alloc)>;
204 
205  // copy construct based on val
206  allocator_traits::construct(local_alloc, laddr, element_t{val});
207  m_glob_pointer = m_data.get();
208  }
209  // Broadcast global pointer of shared value at unit 0 to all units:
211 
212  DASH_ASSERT_RETURNS(
213  dart_bcast(
214  &m_glob_pointer, ds.nelem, ds.dtype, m_owner, m_team->dart_id()),
215  DART_OK);
216 
217  DASH_LOG_DEBUG_VAR("Shared.init(value,team,owner) >", m_glob_pointer);
218  DASH_LOG_DEBUG("Shared.init(value,team,owner) >");
219 
220  return static_cast<bool>(m_glob_pointer);
221  }
Signals success.
Definition: dart_types.h:33
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.
bool is_initialized()
Check whether DASH has been initialized already.
Convencience wrapper to determine the DART type and number of elements required for the given templat...
Definition: Types.h:295
dart_team_t dart_id() const
Index of this team relative to global team dash::Team::All().
Definition: Team.h:522

◆ local() [1/2]

template<typename ElementType>
constexpr value_type const* dash::Shared< ElementType >::local ( ) const
inlinenoexcept

Native pointer to the starting address of the local memory of the unit that initialized this dash::Shared instance.

Definition at line 264 of file Shared.h.

Referenced by dash::broadcast().

265  {
266  return (m_team->myid() == m_owner) ? m_glob_pointer.local() : nullptr;
267  }

◆ local() [2/2]

template<typename ElementType>
value_type* dash::Shared< ElementType >::local ( )
inlinenoexcept

Native pointer to the starting address of the local memory of the unit that initialized this dash::Shared instance.

Definition at line 273 of file Shared.h.

274  {
275  return (m_team->myid() == m_owner) ? m_glob_pointer.local() : nullptr;
276  }

◆ operator=() [1/2]

template<typename ElementType>
self_t& dash::Shared< ElementType >::operator= ( const self_t other)
delete

Assignment operator: DELETED.

Referenced by dash::Shared< ElementType >::Shared().

◆ operator=() [2/2]

template<typename ElementType>
self_t& dash::Shared< ElementType >::operator= ( self_t &&  other)
inline

Move-assignment operator.

Definition at line 132 of file Shared.h.

133  {
134  if (this == &other) {
135  return *this;
136  }
137 
138  m_team = other.m_team;
139  m_owner = other.m_owner;
140  // a) release old resources with current memory resource
141  m_data.reset();
142 
143  // b) move the memory resource
144  m_memory_resource = std::move(other.m_memory_resource);
145  // do no move the allocator because we already have one...
146  DASH_ASSERT_EQ(
147  m_allocator.resource(),
148  &m_memory_resource,
149  "invalid state in move assignment");
150 
151  // c) move the data, set pointer
152  m_data.reset(other.m_data.release());
153 
154  m_glob_pointer = other.m_glob_pointer;
155 
156  if (m_team->myid() == m_owner) {
157  DASH_ASSERT_EQ(
158  m_glob_pointer, m_data.get(), "invalid state in move assignment");
159  }
160 
161  DASH_LOG_DEBUG("Shared.Shared(Shared&&) >");
162  return *this;
163  }

◆ owner()

template<typename ElementType>
dash::team_unit_t dash::Shared< ElementType >::owner ( ) const
inlinenoexcept

The unit owning the memory in the global address space.

Definition at line 281 of file Shared.h.

Referenced by dash::broadcast(), and dash::Shared< ElementType >::Shared().

282  {
283  return m_owner;
284  }

◆ set()

template<typename ElementType>
void dash::Shared< ElementType >::set ( const value_type &  val)
inline

Set the value of the shared element.

Definition at line 226 of file Shared.h.

Referenced by dash::io::hdf5::StoreHDF::write().

227  {
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 >");
234  }

◆ team()

template<typename ElementType>
dash::Team& dash::Shared< ElementType >::team ( ) const
inlinenoexcept

The dash::Team that created this shared object.

Definition at line 289 of file Shared.h.

Referenced by dash::broadcast(), and dash::Shared< ElementType >::Shared().

290  {
291  return *m_team;
292  }

The documentation for this class was generated from the following file: