DASH  0.3.0
HaloMatrixWrapper.h
1 #ifndef DASH__HALO_HALOMATRIXWRAPPER_H
2 #define DASH__HALO_HALOMATRIXWRAPPER_H
3 
4 #include <dash/dart/if/dart.h>
5 
6 #include <dash/Matrix.h>
7 #include <dash/Pattern.h>
8 #include <dash/halo/StencilOperator.h>
9 #include <dash/halo/CoordinateAccess.h>
10 #include <dash/halo/Types.h>
11 
12 #include <type_traits>
13 #include <vector>
14 
15 
16 
17 namespace dash {
18 
19 namespace halo {
20 
52 template <typename MatrixT, SignalReady SigReady = SignalReady::OFF>
54 private:
55  using Pattern_t = typename MatrixT::pattern_type;
56  using pattern_index_t = typename Pattern_t::index_type;
57 
58  static constexpr auto NumDimensions = Pattern_t::ndim();
59  using GlobMem_t = typename MatrixT::GlobMem_t;
60 
61 public:
62  using Element_t = typename MatrixT::value_type;
63 
69  using ElementCoords_t = std::array<pattern_index_t, NumDimensions>;
70  using region_index_t = internal::region_index_t;
71  using stencil_dist_t = internal::spoint_value_t;
72 
73 private:
74  static constexpr auto MemoryArrange = Pattern_t::memory_order();
75 
76  using pattern_size_t = typename Pattern_t::size_type;
77  using signed_pattern_size_t = typename std::make_signed<pattern_size_t>::type;
80 
81 public:
86  template <size_t NumStencilPointsFirst, typename... StencilSpecRestT>
88  const StencilSpec<StencilPoint<NumDimensions>, NumStencilPointsFirst>& stencil_spec_first,
89  const StencilSpecRestT&... stencil_spec)
90  : _matrix(matrix), _glob_bnd_spec(glob_bnd_spec),
91  _halo_spec(stencil_spec_first, stencil_spec...),
92  _view_global(matrix.local.offsets(), matrix.local.extents()),
93  _haloblock(matrix.begin().globmem(), matrix.pattern(), _view_global,
94  _halo_spec, glob_bnd_spec),
95  _view_local(_haloblock.view_local()),
96  //_halomemory(_haloblock),
97  _halo_env(_haloblock, matrix.lbegin(), matrix.team(), matrix.pattern().teamspec()) {
98  }
99 
106  template <typename StencilPointT = StencilPoint<NumDimensions>>
108  stencil_dist_t dist, std::enable_if_t<std::is_integral<stencil_dist_t>::value, std::nullptr_t> = nullptr )
109  : HaloMatrixWrapper(matrix, glob_bnd_spec, StencilSpecFactory<StencilPointT>::full_stencil_spec(dist)) {
110  }
111 
118  template <typename StencilPointT = StencilPoint<NumDimensions>>
119  HaloMatrixWrapper(MatrixT& matrix, stencil_dist_t dist, std::enable_if_t<std::is_integral<stencil_dist_t>::value, std::nullptr_t> = nullptr )
120  : HaloMatrixWrapper(matrix, GlobBoundSpec_t(), StencilSpecFactory<StencilPointT>::full_stencil_spec(dist)) {
121  }
122 
128  template <typename StencilPointT, std::size_t NumStencilPoints>
130  : HaloMatrixWrapper(matrix, GlobBoundSpec_t(), stencil_spec) {}
131 
137  template <typename... StencilSpecT, typename std::enable_if_t<sizeof...(StencilSpecT) >= 2, bool>>
138  HaloMatrixWrapper(MatrixT& matrix, const StencilSpecT&... stencil_spec)
139  : HaloMatrixWrapper(matrix, GlobBoundSpec_t(), stencil_spec...) {}
140 
141  HaloMatrixWrapper() = delete;
142 
146  const HaloBlock_t& halo_block() { return _haloblock; }
147 
151  void update() {
152  _halo_env.update();
153  }
154 
159  void update_at(region_index_t index) {
160  _halo_env.update_at(index);
161  }
162 
166  void update_async() {
167  _halo_env.update_async();
168  }
169 
174  void update_async_at(region_index_t index) {
175  _halo_env.update_async_at(index);
176  }
177 
182  void wait() {
183  _halo_env.wait();
184  }
185 
190  void wait(region_index_t index) {
191  _halo_env.wait(index);
192  }
193 
198  const ViewSpec_t& view_local() const { return _view_local; }
199 
203  HaloUpdateEnv_t& halo_env() { return _halo_env; }
204 
208  const HaloUpdateEnv_t& halo_env() const { return _halo_env; }
209 
213  MatrixT& matrix() { return _matrix; }
214 
218  const MatrixT& matrix() const { return _matrix; }
219 
242  template <typename FunctionT>
243  void set_custom_halos(FunctionT f) {
244  using signed_extent_t = typename std::make_signed<pattern_size_t>::type;
245  for(const auto& region : _haloblock.boundary_regions()) {
246  if(!region.is_custom_region()) {
247  continue;
248  }
249 
250  const auto& spec = region.spec();
251  std::array<signed_extent_t, NumDimensions> coords_offset{};
252  const auto& reg_ext = region.view().extents();
253  for(auto d = 0; d < NumDimensions; ++d) {
254  if(spec[d] == 0) {
255  coords_offset[d] -= reg_ext[d];
256  continue;
257  }
258  if(spec[d] == 2)
259  coords_offset[d] = reg_ext[d];
260  }
261 
262  auto range_mem = _halo_env.halo_memory().range_at(region.index());
263  auto it_mem = range_mem.first;
264  auto it_reg_end = region.end();
265  DASH_ASSERT_MSG(
266  std::distance(range_mem.first, range_mem.second) == region.size(),
267  "Range distance of the HaloMemory is unequal region size");
268  const auto& pattern = _matrix.pattern();
269  for(auto it = region.begin(); it != it_reg_end; ++it, ++it_mem) {
270  auto coords = pattern.coords(it.rpos(), region.view());
271  for(auto d = 0; d < NumDimensions; ++d) {
272  coords[d] += coords_offset[d];
273  }
274 
275  *it_mem = f(coords);
276  }
277  }
278  }
279 
285  Element_t* halo_element_at_global(ElementCoords_t coords) {
286  const auto& offsets = _view_global.offsets();
287  for(auto d = 0; d < NumDimensions; ++d) {
288  coords[d] -= offsets[d];
289  }
290 
291  return halo_element_at(coords);
292  }
293 
298  Element_t* halo_element_at_local(ElementCoords_t coords) {
299  return halo_element_at(coords);
300  }
301 
306  template <typename StencilSpecT>
308  const StencilSpecT& stencil_spec) {
309  for(const auto& stencil : stencil_spec.specs()) {
310  DASH_ASSERT_MSG(
311  stencil.max()
312  <= _halo_spec.extent(RegionSpec<NumDimensions>::index(stencil)),
313  "Stencil point extent higher than halo region extent.");
314  }
315 
317  &_haloblock, _matrix.lbegin(), &_halo_env.halo_memory(), stencil_spec);
318  }
319 
320  CoordinateAccess<HaloBlock_t> coordinate_access() {
321  return CoordinateAccess<HaloBlock_t>(&_haloblock, _matrix.lbegin(),&_halo_env.halo_memory());
322  }
323 
324 private:
325 
326  Element_t* halo_element_at(ElementCoords_t& coords) {
327  auto index = _haloblock.index_at(_view_local, coords);
328  const auto& spec = _halo_spec.spec(index);
329  auto& halo_memory = _halo_env.halo_memory();
330  auto range_mem = halo_memory.range_at(index);
331  if(spec.level() == 0 || range_mem.first == range_mem.second)
332  return nullptr;
333 
334  if(!halo_memory.to_halo_mem_coords_check(index, coords))
335  return nullptr;
336 
337  return &*(range_mem.first + halo_memory.offset(index, coords));
338  }
339 
340 private:
341  MatrixT& _matrix;
342  const GlobBoundSpec_t _glob_bnd_spec;
343  const HaloSpec_t _halo_spec;
344  const ViewSpec_t _view_global;
345  const HaloBlock_t _haloblock;
346  const ViewSpec_t& _view_local;
347  HaloUpdateEnv_t _halo_env;
348 };
349 
350 } // namespace halo
351 
352 } // namespace dash
353 
354 #endif // DASH__HALO_HALOMATRIXWRAPPER_H
HaloUpdateEnv_t & halo_env()
Returns the halo environment management object HaloUpdateEnv.
void update()
Initiates a blocking halo region update for all halo elements.
const ViewSpec_t & view_local() const
Returns the local ViewSpec.
constexpr region_extent_t extent(const region_index_t index) const
Extent for a given region index.
Definition: Halo.h:129
constexpr std::enable_if< std::is_integral< IndexType >::value, IndexType >::type index(IndexType idx)
Definition: Iterator.h:60
HaloMatrixWrapper(MatrixT &matrix, stencil_dist_t dist, std::enable_if_t< std::is_integral< stencil_dist_t >::value, std::nullptr_t >=nullptr)
Constructor that takes Matrix and a stencil point distance to create a HaloMatrixWrapper with a full ...
void wait()
Waits until all halo updates are finished.
void update_at(region_index_t index)
Initiates a blocking halo region update for all halo elements within the the given region...
Definition: HaloMemory.h:591
constexpr auto local(ViewType &v) -> typename std::enable_if<(std::is_pointer< typename ViewType::iterator >::value||(dash::view_traits< ViewValueT >::is_local::value)), ViewType &>::type
Definition: Local.h:28
constexpr const Pattern_t & pattern() const
The pattern used to distribute matrix elements to units in its associated team.
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
MatrixT & matrix()
Returns the underlying NArray.
MemRange_t range_at(region_index_t index)
Returns the range of all halo elements for the given region index.
Definition: HaloMemory.h:73
RandomAccessIt::difference_type distance(const RandomAccessIt &first, const RandomAccessIt &last)
Resolve the number of elements between two iterators.
Definition: Iterator.h:90
const HaloBlock_t & halo_block()
Returns the underlying HaloBlock.
Takes the local part of the NArray and builds halo and boundary regions.
Definition: Halo.h:827
Specifies view parameters for implementing submat, rows and cols.
Definition: Dimensional.h:430
constexpr auto begin(RangeType &&range) -> decltype(std::forward< RangeType >(range).begin())
Definition: Range.h:89
void wait()
Waits until all halo updates are finished.
Definition: HaloMemory.h:629
constexpr const StencilArray_t & specs() const
Definition: Stencil.h:211
HaloMatrixWrapper(MatrixT &matrix, const StencilSpec< StencilPointT, NumStencilPoints > stencil_spec)
Constructor that takes Matrix and a user defined number of stencil specifications (StencilSpec)...
const MatrixT & matrix() const
Returns the underlying NArray.
Element_t * halo_element_at_local(ElementCoords_t coords)
Returns the halo value for a given local coordinate or nullptr if no halo element exists...
Stencil point with raletive coordinates for N dimensions e.g.
Definition: Stencil.h:19
HaloMatrixWrapper(MatrixT &matrix, const GlobBoundSpec_t &glob_bnd_spec, stencil_dist_t dist, std::enable_if_t< std::is_integral< stencil_dist_t >::value, std::nullptr_t >=nullptr)
Constructor that takes Matrix and a stencil point distance to create a HaloMatrixWrapper with a full ...
HaloMatrixWrapper(MatrixT &matrix, const StencilSpecT &... stencil_spec)
Constructor that takes Matrix and a user defined number of stencil specifications (StencilSpec)...
Element_t * halo_element_at_global(ElementCoords_t coords)
Returns the halo value for a given global coordinate or nullptr if no halo element exists...
void update_async_at(region_index_t index)
Initiates an asychronous halo region update for all halo elements within the given region...
Definition: HaloMemory.h:618
static constexpr dim_t ndim() noexcept
Number of dimensions of the cartesian space partitioned by the pattern.
void set_custom_halos(FunctionT f)
Sets all global border halo elements.
void update()
Initiates a blocking halo region update for all halo elements.
Definition: HaloMemory.h:577
region_index_t index_at(const ViewSpec_t &view, const ElementCoords_t &coords) const
Returns the region index belonging to the given coordinates and ViewSpec.
Definition: Halo.h:1038
Contains all specified Halo regions.
Definition: Halo.h:70
As known from classic stencil algorithms, boundaries are the outermost elements within a block that a...
void wait(region_index_t index)
Waits until the halo updates for the given halo region is finished.
PatternT pattern_type
The type of the pattern specifying linear iteration order and how elements are distribute to units...
Definition: Matrix.h:232
const RegionVector_t & boundary_regions() const
Returns all boundary regions.
Definition: Halo.h:982
Provides RegionIter and some region metadata like RegionSpec, size etc.
Definition: Region.h:411
void update_async_at(region_index_t index)
Initiates an asychronous halo region update for all halo elements within the given region...
HaloMemory_t & halo_memory()
Returns the halo memory management object HaloMemory.
Definition: HaloMemory.h:672
HaloMatrixWrapper(MatrixT &matrix, const GlobBoundSpec_t &glob_bnd_spec, const StencilSpec< StencilPoint< NumDimensions >, NumStencilPointsFirst > &stencil_spec_first, const StencilSpecRestT &... stencil_spec)
Constructor that takes Matrix, a GlobalBoundarySpec and a user defined number of stencil specificatio...
void update_async()
Initiates an asychronous halo region update for all halo elements.
Definition: HaloMemory.h:605
void update_async()
Initiates an asychronous halo region update for all halo elements.
constexpr RegionSpec_t spec(const region_index_t index) const
Matching RegionSpec for a given region index.
Definition: Halo.h:122
The StencilOperator provides stencil specific iterator and functions for a given HaloBlock and HaloMe...
void update_at(region_index_t index)
Initiates a blocking halo region update for all halo elements within the the given region...
A collection of stencil points (Stencil) e.g.
Definition: Stencil.h:167
Global boundary property specification for every dimension.
Definition: Halo.h:23
Region specification connecting RegionCoords with an extent.
Definition: Region.h:294
ElementT * lbegin() noexcept
Pointer to first element in local range.
const HaloUpdateEnv_t & halo_env() const
Returns the halo environment management object HaloUpdateEnv.
StencilOperator< HaloBlock_t, StencilSpecT > stencil_operator(const StencilSpecT &stencil_spec)
Crates StencilOperator for a given StencilSpec.
Forward-declaration.
static constexpr MemArrange memory_order() noexcept
Memory order followed by the pattern.