DASH  0.3.0
Matrix.h
1 #ifndef DASH__MATRIX_H_INCLUDED
2 #define DASH__MATRIX_H_INCLUDED
3 
4 #include <dash/dart/if/dart.h>
5 
6 #include <dash/Team.h>
7 #include <dash/Pattern.h>
8 #include <dash/GlobRef.h>
9 #include <dash/HView.h>
10 #include <dash/Meta.h>
11 
12 #include <dash/iterator/GlobIter.h>
13 
14 #include <dash/matrix/MatrixRefView.h>
15 #include <dash/matrix/MatrixRef.h>
16 #include <dash/matrix/LocalMatrixRef.h>
17 
18 #include <dash/memory/MemorySpace.h>
19 #include <dash/memory/UniquePtr.h>
20 
21 #include <type_traits>
22 
23 
92 namespace dash {
93 
95 template <
96  typename T,
97  dim_t NumDimensions,
98  typename IndexT,
99  class PatternT,
100  typename LocalMemSpaceT>
101 class Matrix;
103 template <
104  typename T,
105  dim_t NumDimensions,
106  dim_t CUR,
107  class PatternT,
108  typename LocalMemSpaceT>
109 class MatrixRef;
111 template <
112  typename T,
113  dim_t NumDimensions,
114  dim_t CUR,
115  class Pattern,
116  typename LocalMemSpaceT>
117 class LocalMatrixRef;
118 
135 template<
136  typename ElementT,
137  dim_t NumDimensions,
138  typename IndexT = dash::default_index_t,
139  class PatternT = TilePattern<NumDimensions, ROW_MAJOR, IndexT>,
140  typename LocalMemSpaceT = HostSpace>
141 class Matrix
142 {
143  static_assert(
145  "Type not supported for DASH containers");
146 
147  static_assert(std::is_same<IndexT, typename PatternT::index_type>::value,
148  "Index type IndexT must be the same for Matrix and specified pattern");
149 
150 private:
151  typedef Matrix<ElementT, NumDimensions, IndexT, PatternT, LocalMemSpaceT>
152  self_t;
153  typedef typename std::make_unsigned<IndexT>::type
154  SizeType;
155  typedef MatrixRefView<ElementT, NumDimensions, PatternT, LocalMemSpaceT>
156  MatrixRefView_t;
157  typedef LocalMatrixRef<ElementT, NumDimensions, NumDimensions, PatternT, LocalMemSpaceT>
158  LocalRef_t;
159  typedef LocalMatrixRef<
160  const ElementT, NumDimensions, NumDimensions, PatternT, LocalMemSpaceT>
161  LocalRef_const_t;
162  typedef PatternT
163  Pattern_t;
164 
165 public:
166  typedef GlobStaticMem<LocalMemSpaceT>
167  GlobMem_t;
168 
169  template<
170  typename T_,
171  dim_t NumDimensions1,
172  dim_t NumDimensions2,
173  class PatternT_,
174  typename MSpaceC>
175  friend class MatrixRef;
176  template<
177  typename T_,
178  dim_t NumDimensions1,
179  dim_t NumDimensions2,
180  class PatternT_,
181  typename MSpaceC>
182  friend class LocalMatrixRef;
183 
184 
185 public:
186  typedef ElementT value_type;
187  typedef typename PatternT::size_type size_type;
188  typedef typename PatternT::index_type difference_type;
189  typedef typename PatternT::index_type index_type;
190 
191  typedef GlobIter< value_type, Pattern_t, GlobMem_t> iterator;
192  typedef GlobIter<const value_type, Pattern_t, GlobMem_t> const_iterator;
193 
194  typedef std::reverse_iterator<iterator> reverse_iterator;
195  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
196 
197  typedef GlobRef<value_type> reference;
198  typedef typename GlobRef<value_type>::const_type const_reference;
199 
200  // TODO rko: use iterator traits
201  typedef typename iterator::pointer pointer;
202  typedef typename pointer::const_type const_pointer;
203 
204  typedef ElementT * local_pointer;
205  typedef const ElementT * const_local_pointer;
206 
207  typedef DistributionSpec<NumDimensions> distribution_spec;
208  typedef SizeSpec<NumDimensions, typename PatternT::size_type>
209  size_spec;
210  typedef TeamSpec<NumDimensions, typename PatternT::index_type>
211  team_spec;
212  typedef std::array<typename PatternT::size_type, NumDimensions>
213  extents_type;
214  typedef std::array<typename PatternT::index_type, NumDimensions>
215  offsets_type;
216 
217 
218 // Public types as required by dash container concept
219 public:
221  typedef LocalMatrixRef<
222  ElementT, NumDimensions, NumDimensions, PatternT, LocalMemSpaceT>
224 
226  typedef LocalMatrixRef<
227  const ElementT, NumDimensions, NumDimensions, PatternT, LocalMemSpaceT>
229 
232  typedef PatternT pattern_type;
233 
236  template <dim_t NumViewDim>
237  using view_type =
239 
242  template <dim_t NumViewDim>
243  using const_view_type =
245 
246 // public types exposed in Matrix interface
247 public:
248 
249 public:
252 
253 public:
254 
255  typedef std::integral_constant<dim_t, NumDimensions> rank;
256 
257  static constexpr dim_t ndim() {
258  return NumDimensions;
259  }
260 private:
262 
263  using unique_gptr_t = decltype(dash::allocate_unique<value_type>(
264  allocator_type{}, std::size_t{}));
265 
266 private:
269  dash::Team *_team = nullptr;
271  size_type _size;
273  size_type _lsize;
275  size_type _lcapacity;
277  iterator _begin;
279  Pattern_t _pattern;
281  GlobMem_t _glob_mem{};
283  allocator_type _allocator;
285  unique_gptr_t _data{};
287  ElementT *_lbegin{};
289  ElementT *_lend{};
292 
293 public:
300  explicit
301  Matrix(
302  Team & team = dash::Team::Null());
303 
307  explicit
308  Matrix(
309  const size_spec & ss,
310  const distribution_spec & ds = distribution_spec(),
311  Team & t = dash::Team::All(),
312  const team_spec & ts = team_spec());
313 
317  explicit
318  Matrix(
319  const PatternT & pat);
320 
325  template<typename... Args>
326  Matrix(SizeType arg, Args&&... args)
327  : Matrix(PatternT(arg, std::forward<Args>(args)...))
328  { }
329 
333  Matrix(const self_t &) = delete;
334 
338  Matrix(self_t && other);
339 
343  ~Matrix();
344 
348  self_t & operator=(const self_t & other) = delete;
349 
353  self_t & operator=(self_t && other);
354 
359  const std::array<index_type, NumDimensions> & block_gcoords);
360 
365  index_type block_gindex);
366 
373  bool allocate(
374  const size_spec & sizespec,
375  const distribution_spec & distribution,
376  const team_spec & teamspec,
377  dash::Team & team = dash::Team::All()
378  );
379 
384  bool allocate(
385  const PatternT & pattern
386  );
387 
388 
393  template<typename... Args>
394  bool
395  allocate(SizeType arg, Args... args)
396  {
397  return allocate(PatternT(arg, args... ));
398  }
399 
406  void deallocate();
407 
408  constexpr Team & team() const noexcept;
409 
410  constexpr size_type size() const noexcept;
411  constexpr size_type local_size() const noexcept;
412  constexpr size_type local_capacity() const noexcept;
413  constexpr size_type extent(dim_t dim) const noexcept;
414  constexpr extents_type extents() const noexcept;
415  constexpr index_type offset(dim_t dim) const noexcept;
416  constexpr offsets_type offsets() const noexcept;
417  constexpr bool empty() const noexcept;
418 
424  inline void barrier() const;
425 
432  inline void flush();
433 
440  inline void flush(dash::team_unit_t target);
441 
446  inline void flush_local();
447 
452  inline void flush_local(dash::team_unit_t target);
453 
460  constexpr const Pattern_t & pattern() const;
461 
467  iterator begin() noexcept;
468 
474  constexpr const_iterator begin() const noexcept;
475 
482  iterator end() noexcept;
483 
490  constexpr const_iterator end( ) const noexcept;
491 
497  ElementT * lbegin() noexcept;
498 
504  constexpr const ElementT * lbegin() const noexcept;
505 
511  ElementT * lend() noexcept;
512 
518  constexpr const ElementT * lend() const noexcept;
519 
524  template<dim_t __NumViewDim = NumDimensions-1>
525  typename std::enable_if<
526  (__NumViewDim != 0),
528  >::type
529  constexpr operator[](
530  size_type n
531  ) const;
532 
536  template<dim_t __NumViewDim = NumDimensions-1>
537  typename std::enable_if<
538  (__NumViewDim == 0),
539  const_reference
540  >::type
541  constexpr operator[](
542  size_type n
543  ) const;
544 
549  template<dim_t __NumViewDim = NumDimensions-1>
550  typename std::enable_if<(__NumViewDim != 0), view_type<__NumViewDim>>::type
551  operator[](
552  size_type n
553  );
554 
558  template<dim_t __NumViewDim = NumDimensions-1>
559  typename std::enable_if<(__NumViewDim == 0), reference>::type
560  operator[](
561  size_type n
562  );
563 
564  template<dim_t SubDimension>
566  size_type n,
567  size_type range
568  ) const;
569 
570  template<dim_t SubDimension>
572  size_type n,
573  size_type range
574  );
575 
582  template<dim_t SubDimension>
583  const_view_type<NumDimensions-1> sub(
584  size_type n
585  ) const;
586 
593  template<dim_t SubDimension>
594  view_type<NumDimensions-1> sub(
595  size_type n
596  );
597 
602  local_type sub_local() noexcept;
603 
613  const_view_type<NumDimensions-1> col(
614  size_type n
615  ) const;
616 
626  view_type<NumDimensions-1> col(
627  size_type n
628  );
629 
639  const_view_type<NumDimensions-1> row(
640  size_type n
641  ) const;
642 
652  view_type<NumDimensions-1> row(
653  size_type n
654  );
655 
666  size_type offset,
667  size_type range
668  );
669 
680  size_type n,
681  size_type range
682  );
683 
692  template<typename ... Args>
693  const_reference at(
694  Args... args
695  ) const;
696 
705  template<typename ... Args>
706  reference at(
707  Args... args
708  );
709 
719  template<typename... Args>
720  const_reference operator()(
721  Args... args
722  ) const;
723 
733  template<typename... Args>
734  reference operator()(
735  Args... args
736  );
737 
744  constexpr bool is_local(
745  size_type g_pos
746  ) const;
747 
752  template<dim_t Dimension>
753  constexpr bool is_local(
754  size_type g_pos
755  ) const;
756 
757  template <int level>
759 
763  operator
765 
766 private:
767  void destruct_at_end(value_type *new_last);
768 };
769 
776 template <
777  typename T,
778  dim_t NumDimensions,
779  typename IndexT = dash::default_index_t,
781  typename LocalMemSpaceT = HostSpace>
783 
784 } // namespace dash
785 
786 #include <dash/matrix/internal/Matrix-inl.h>
787 
788 #endif // DASH__MATRIX_H_INCLUDED
void flush()
Complete all outstanding non-blocking operations to all units on the container&#39;s underlying global me...
constexpr dim_t rank(const DimensionalType &d)
Definition: Dimensional.h:64
view_type< NumDimensions > cols(size_type offset, size_type range)
Create a view representing the matrix slice within a column range.
Defines how a list of global indices is mapped to single units within a Team.
Definition: BlockPattern.h:42
iterator end() noexcept
Iterator referencing past the last matrix element in global index space.
Matrix(SizeType arg, Args &&... args)
Constructor, creates a new instance of Matrix of given extents.
Definition: Matrix.h:326
Forward-declaration.
size_t size()
Return the number of units in the global team.
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
const_view_type< NumDimensions > sub(size_type n, size_type range) const
self_t & operator=(const self_t &other)=delete
Copy-assignment operator, deleted.
~Matrix()
Destructor, frees underlying memory.
dash::BlockPattern< NumDimensions, Arrangement, IndexType > Pattern
Template alias for dash::Pattern with the same default template arguments.
Definition: Pattern.h:644
int dim_t
Scalar type for a dimension value, with 0 indicating the first dimension.
Definition: Types.h:39
const_view_type< NumDimensions-1 > row(size_type n) const
Projection to given offset in second sub-dimension (rows), same as sub<1>(n).
constexpr bool is_local(size_type g_pos) const
Whether the element at a global, canonical offset in the matrix is local to the active unit...
bool allocate(const size_spec &sizespec, const distribution_spec &distribution, const team_spec &teamspec, dash::Team &team=dash::Team::All())
Explicit allocation of matrix elements, used for delayed allocation of default-constructed Matrix ins...
bool allocate(SizeType arg, Args... args)
Allocation and distribution of matrix elements as specified by given extents.
Definition: Matrix.h:395
LocalMatrixRef< const ElementT, NumDimensions, NumDimensions, PatternT, LocalMemSpaceT > const_local_type
Type specifying the view on const local matrix elements.
Definition: Matrix.h:228
local_type sub_local() noexcept
Local proxy object representing a view consisting of matrix elements that are located in the active u...
void barrier() const
Synchronize units associated with the matrix.
internal::default_signed_index default_index_t
Signed integer type used as default for index values.
Definition: Types.h:59
Matrix(Team &team=dash::Team::Null())
Default constructor, for delayed allocation.
A Team instance specifies a subset of all available units.
Definition: Team.h:41
void deallocate()
Explicit deallocation of matrix elements, called implicitly in destructor and team deallocation...
constexpr dim_t ndim(const DimensionalType &d)
Definition: Dimensional.h:56
view_type< NumDimensions > rows(size_type n, size_type range)
Create a view representing the matrix slice within a row range.
std::enable_if<(__NumViewDim !=0), const_view_type< __NumViewDim > >::type constexpr operator[](size_type n) const
Subscript operator, returns a submatrix reference at given offset in global element range...
iterator begin() noexcept
Iterator referencing first matrix element in global index space.
PatternT pattern_type
The type of the pattern specifying linear iteration order and how elements are distribute to units...
Definition: Matrix.h:232
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
Definition: Types.h:319
view_type< NumDimensions > block(const std::array< index_type, NumDimensions > &block_gcoords)
View at block at given global block coordinates.
const_view_type< NumDimensions-1 > col(size_type n) const
Projection to given offset in first sub-dimension (column), same as sub<0>(n).
const_reference operator()(Args... args) const
Fortran-style subscript operator, alias for at().
Type trait indicating whether the specified type is eligible for elements of DASH containers...
Definition: Types.h:236
LocalMatrixRef< ElementT, NumDimensions, NumDimensions, PatternT, LocalMemSpaceT > local_type
Type specifying the view on local matrix elements.
Definition: Matrix.h:223
void flush_local()
Locally complete all outstanding non-blocking operations to all units on the container&#39;s underlying g...
static Team & Null()
The invariant Team instance representing an undefined team.
Definition: Team.h:229
const_reference at(Args... args) const
Fortran-style subscript operator.
constexpr DimensionalType::extent_type extent(const DimensionalType &d)
Definition: Dimensional.h:73
Forward-declaration.
local_type local
Local view proxy object.
Definition: Matrix.h:251
static Team & All()
The invariant Team instance containing all available units.
Definition: Team.h:213
see https://en.cppreference.com/w/cpp/feature_test for recommended feature tests
Definition: cstddef.h:8
ElementT * lbegin() noexcept
Pointer to first element in local range.
Forward-declaration.
ElementT * lend() noexcept
Pointer past final element in local range.