DASH  0.3.0
AllocationPolicy.h
1 #ifndef DASH__ALLOCATOR__POLICY_H__INCLUDED
2 #define DASH__ALLOCATOR__POLICY_H__INCLUDED
3 
4 #include <dash/dart/if/dart.h>
5 
6 #include <dash/Types.h>
7 #include <dash/Team.h>
8 
9 #include <dash/allocator/internal/Types.h>
10 #include <dash/internal/Logging.h>
11 #include <dash/memory/MemorySpaceBase.h>
12 
13 std::ostream& operator<<(std::ostream& os, const dart_gptr_t& dartptr);
14 
15 namespace dash {
16 
17 enum class global_allocation_policy : uint8_t {
19  collective,
20 
23 
27 };
28 
29 namespace allocator {
30 
32  inline dart_gptr_t do_global_attach(
33  dart_team_t teamid, void* ptr, std::size_t nbytes)
34  {
35  dart_gptr_t gptr;
36 
37  if (dart_team_memregister(teamid, nbytes, DART_TYPE_BYTE, ptr, &gptr) !=
38  DART_OK) {
39  gptr = DART_GPTR_NULL;
40  DASH_LOG_ERROR(
41  "AttachDetachPolicy.global_attach", "cannot attach pointer", ptr);
42  }
43  return gptr;
44  }
45 
46  inline bool do_global_detach(dart_gptr_t gptr)
47  {
48  if (dart_team_memderegister(gptr) != DART_OK) {
49  DASH_LOG_ERROR(
50  "AttachDetachPolicy.global_detach",
51  "cannot detach global pointer",
52  gptr);
53  return false;
54  }
55  return true;
56  }
57 };
58 
67 template <
68  class AllocationPolicy,
69  class SynchronizationPolicy,
70  class LMemSpaceTag>
72 
73 template <class LMemSpaceTag>
77  LMemSpaceTag> : AttachDetachPolicy {
78  using memory_space_tag = LMemSpaceTag;
79 
80 public:
85  dart_team_t teamid,
87  std::size_t nbytes,
88  std::size_t alignment)
89  {
90  DASH_LOG_DEBUG(
91  "GlobalAllocationPolicy.do_global_allocate(nlocal)",
92  "number of local values:",
93  nbytes);
94 
95  auto lp = res->allocate(nbytes, alignment);
96 
97  if (!lp && nbytes > 0) {
98  throw std::bad_alloc{};
99  }
100 
101  DASH_LOG_DEBUG_VAR(
102  "GlobalAllocationPolicy.do_global_allocate(nlocal)", lp);
103 
104  // Here, we attach the allocated global pointer. DART internally stores
105  // this pointer in such a way that we can retrieve it by looking up the
106  // local address (teamid <- my_id, addr_or_offset.offset <- 0)
107  auto gptr = AttachDetachPolicy::do_global_attach(
108  teamid, lp, nbytes);
109 
110  if (DART_GPTR_ISNULL(gptr)) {
111  res->deallocate(lp, nbytes, alignment);
112  throw std::bad_alloc{};
113  }
114 
115  DASH_LOG_DEBUG_VAR(
116  "GlobalAllocationPolicy.do_global_allocate(nlocal)", gptr);
117 
118  DASH_LOG_DEBUG("GlobalAllocationPolicy.do_global_allocate(nlocal) >");
119 
120  return gptr;
121  }
122 
126  dart_gptr_t gptr,
128  void* lptr,
129  size_t nbytes,
130  size_t alignment)
131  {
132  DASH_LOG_DEBUG("< GlobalAllocationPolicy.do_global_deallocate");
133  DASH_LOG_DEBUG_VAR("GlobalAllocationPolicy.do_global_deallocate", gptr);
134  DASH_LOG_DEBUG_VAR("GlobalAllocationPolicy.do_global_deallocate", lptr);
135  DASH_LOG_DEBUG_VAR("GlobalAllocationPolicy.do_global_deallocate", nbytes);
136 
137  DASH_ASSERT_RETURNS(AttachDetachPolicy::do_global_detach(gptr), true);
138 
139  res->deallocate(lptr, nbytes, alignment);
140 
141  DASH_LOG_DEBUG("GlobalAllocationPolicy.do_global_deallocate >");
142  return true;
143  }
144 };
145 
146 template <>
152 
153 public:
158  dart_team_t teamid,
160  std::size_t nbytes,
161  std::size_t /*alignment*/)
162  {
163  DASH_LOG_DEBUG(
164  "GlobalAllocationPolicy.do_global_allocate(nlocal)",
165  "number of local values:",
166  nbytes);
167 
168  dart_gptr_t gptr;
169 
170  dash::dart_storage<uint8_t> ds(nbytes);
171  if (dart_team_memalloc_aligned(teamid, ds.nelem, ds.dtype, &gptr) !=
172  DART_OK) {
173  DASH_LOG_ERROR(
174  "GlobalAllocationPolicy.do_global_allocate(nlocal)",
175  "cannot allocate global memory segment",
176  nbytes);
177  gptr = DART_GPTR_NULL;
178  }
179 
180  if (DART_GPTR_ISNULL(gptr)) {
181  throw std::bad_alloc{};
182  }
183 
184  return gptr;
185  }
186 
190  dart_gptr_t gptr,
192  void* /* unused */,
193  size_t /* unused */,
194  size_t /*unused*/)
195  {
196  DASH_LOG_TRACE(
197  "GlobalAllocationPolicy.do_global_deallocate",
198  "deallocating memory segment",
199  gptr);
200 
201  if (DART_GPTR_ISNULL(gptr)) {
202  return true;
203  }
204 
205  auto ret = dart_team_memfree(gptr) == DART_OK;
206 
207  return ret;
208  }
209 };
210 
223 template <>
229 
230 public:
238  std::size_t nbytes,
240  std::size_t /*alignment*/)
241  {
243 
244  if (nbytes > 0) {
245  dash::dart_storage<uint8_t> ds(nbytes);
246  auto const ret = dart_memalloc(ds.nelem, ds.dtype, &gptr);
247  if (ret != DART_OK) {
248  DASH_LOG_ERROR(
249  "LocalAllocationPolicy.do_global_allocate",
250  "cannot allocate local memory",
251  ret);
252  throw std::bad_alloc{};
253  }
254  DASH_LOG_DEBUG_VAR("LocalAllocator.allocate >", gptr);
255  }
256  return gptr;
257  }
258 
262  dart_gptr_t gptr,
264  void* /* unused */,
265  size_t /* unused */,
266  size_t /*unused*/)
267  {
268  auto const ret = dart_memfree(gptr) == DART_OK;
269  return ret;
270  }
271 };
272 
273 } // namespace allocator
274 } // namespace dash
275 #endif
bool deallocate_segment(dart_gptr_t gptr, LocalMemorySpaceBase< memory_space_tag > *, void *, size_t, size_t)
Similar to the allocation case above global memory is deallocated symmetrically in DART...
dart_ret_t dart_team_memregister(dart_team_t teamid, size_t nlelem, dart_datatype_t dtype, void *addr, dart_gptr_t *gptr)
Collective function, attaches external memory previously allocated by the user.
only one unit allocates in global memory
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
Signals success.
Definition: dart_types.h:33
bool deallocate_segment(dart_gptr_t gptr, LocalMemorySpaceBase< memory_space_tag > *res, void *lptr, size_t nbytes, size_t alignment)
Similar to the allocation case above global memory is deallocated symmetrically in DART...
dart_ret_t dart_memfree(dart_gptr_t gptr)
Frees memory in the global address space allocated by a previous call of dart_memalloc.
dart_ret_t dart_memalloc(size_t nelem, dart_datatype_t dtype, dart_gptr_t *gptr)
Allocates memory for nelem elements of type dtype in the global address space of the calling unit and...
dart_gptr_t allocate_segment(dart_team_t teamid, LocalMemorySpaceBase< memory_space_tag > *res, std::size_t nbytes, std::size_t alignment)
Variant to allocate symmetrically in global memory space if we allocate in the default Host Space...
dart_gptr_t allocate_segment(LocalMemorySpaceBase< memory_space_tag > *, std::size_t nbytes, std::size_t)
Variant to allocate only locally in global memory space if we allocate in the default Host Space...
global_allocation_policy
Allocation Policy.
#define DART_TYPE_BYTE
integral data types
Definition: dart_types.h:125
#define DART_GPTR_NULL
A NULL global pointer.
Definition: dart_globmem.h:105
all units allocate invdividually in local memory and synchronize in epochs
Collective AllocationPolicy: Implements the mechanisms to allocate symmetrically from the global memo...
dart_ret_t dart_team_memfree(dart_gptr_t gptr)
Collective function to free global memory previously allocated using dart_team_memalloc_aligned.
DART Global pointer type.
Definition: dart_globmem.h:77
dart_gptr_t allocate_segment(dart_team_t teamid, LocalMemorySpaceBase< memory_space_tag > *, std::size_t nbytes, std::size_t)
Variant to allocate symmetrically in global memory space if we allocate in the default Host Space...
#define DART_GPTR_ISNULL(gptr_)
Test for NULL global pointer.
Definition: dart_globmem.h:118
int16_t dart_team_t
Data type for storing a team ID.
Definition: dart_types.h:252
All units collectively allocate global memory.
bool deallocate_segment(dart_gptr_t gptr, LocalMemorySpaceBase< memory_space_tag > *, void *, size_t, size_t)
Similar to the allocation case above global memory is deallocated symmetrically in DART...
Synchronization Policy.
Convencience wrapper to determine the DART type and number of elements required for the given templat...
Definition: Types.h:295
dart_ret_t dart_team_memderegister(dart_gptr_t gptr)
Collective function similar to dart_team_memfree() but on previously externally allocated memory...
dart_ret_t dart_team_memalloc_aligned(dart_team_t teamid, size_t nelem, dart_datatype_t dtype, dart_gptr_t *gptr)
Collective function on the specified team to allocate nelem elements of type dtype of memory in each ...