DASH  0.3.0
CSRPattern.h
1 #ifndef DASH__CSR_PATTERN_1D_H_
2 #define DASH__CSR_PATTERN_1D_H_
3 
4 #include <dash/Types.h>
5 #include <dash/Distribution.h>
6 #include <dash/Exception.h>
7 #include <dash/Dimensional.h>
8 #include <dash/Cartesian.h>
9 #include <dash/Team.h>
10 
11 #include <dash/pattern/PatternProperties.h>
12 #include <dash/pattern/internal/PatternArguments.h>
13 
14 #include <dash/internal/Math.h>
15 #include <dash/internal/Logging.h>
16 
17 #include <functional>
18 #include <array>
19 #include <utility>
20 #include <vector>
21 #include <type_traits>
22 
23 
24 namespace dash {
25 
26 #ifndef DOXYGEN
27 
33 template <
34  dim_t NumDimensions,
35  MemArrange Arrangement = dash::ROW_MAJOR,
36  typename IndexType = dash::default_index_t
37 >
38 class CSRPattern;
39 
40 #endif // DOXYGEN
41 
48 template<
49  MemArrange Arrangement,
50  typename IndexType
51 >
52 class CSRPattern<1, Arrangement, IndexType>
53 {
54 private:
55  static const dim_t NumDimensions = 1;
56 
57 public:
58  static constexpr char const * PatternName = "CSRPattern1D";
59 
60 public:
63  // Minimal number of blocks in every dimension, i.e. one block
64  // per unit.
66  // Block extents are constant for every dimension.
68  // Identical number of elements in every block.
70  // Size of blocks may differ.
75  // Number of blocks assigned to a unit may differ.
80  // Elements are contiguous in local memory within single block.
82  // Local element order corresponds to a logical linearization
83  // within single blocks.
86 
87 private:
89  typedef CSRPattern<NumDimensions, Arrangement, IndexType>
90  self_t;
92  typedef typename std::make_unsigned<IndexType>::type
93  SizeType;
103  TeamSpec_t;
105  SizeSpec_t;
107  ViewSpec_t;
108  typedef internal::PatternArguments<NumDimensions, IndexType>
109  PatternArguments_t;
110 
111 public:
112  typedef IndexType index_type;
113  typedef SizeType size_type;
114  typedef ViewSpec_t viewspec_type;
115  typedef struct {
116  team_unit_t unit;
117  IndexType index{};
118  } local_index_t;
119  typedef struct {
120  team_unit_t unit;
121  std::array<index_type, NumDimensions> coords;
122  } local_coords_t;
123 
124 private:
126  SizeType _size = 0;
128  std::vector<size_type> _local_sizes;
130  std::vector<size_type> _block_offsets;
132  MemoryLayout_t _memory_layout;
134  BlockSpec_t _blockspec;
137  DistributionSpec_t _distspec;
139  dash::Team * _team = nullptr;
141  TeamSpec_t _teamspec;
143  SizeType _nunits = 0;
145  SizeType _local_size = 0;
147  LocalMemoryLayout_t _local_memory_layout;
149  SizeType _local_capacity = 0;
151  IndexType _lbegin = 0;
153  IndexType _lend = 0;
154 
155 public:
162  template<typename ... Args>
167  SizeType arg,
171  Args && ... args)
172  : CSRPattern(PatternArguments_t(arg, args...))
173  {
174  DASH_LOG_TRACE("CSRPattern()", "Constructor with argument list");
175  DASH_ASSERT_EQ(
176  _local_sizes.size(), _nunits,
177  "Number of given local sizes " << _local_sizes.size() << " " <<
178  "does not match number of units" << _nunits);
179  initialize_local_range();
180  DASH_LOG_TRACE("CSRPattern()", "CSRPattern initialized");
181  }
182 
190  const SizeSpec_t & sizespec,
192  const DistributionSpec_t & distspec,
194  const TeamSpec_t & teamspec,
196  Team & team = dash::Team::All())
197  : _size(sizespec.size()),
198  _local_sizes(initialize_local_sizes(
199  _size,
200  distspec,
201  team)),
202  _block_offsets(initialize_block_offsets(
203  _local_sizes)),
204  _memory_layout(std::array<SizeType, 1> {{ _size }}),
205  _blockspec(initialize_blockspec(
206  _local_sizes)),
207  _distspec(DistributionSpec_t()),
208  _team(&team),
209  _teamspec(
210  teamspec,
211  _distspec,
212  *_team),
213  _nunits(_team->size()),
214  _local_size(
215  initialize_local_extent(
216  _team->myid(),
217  _local_sizes)),
218  _local_memory_layout(std::array<SizeType, 1> {{ _local_size }}),
219  _local_capacity(initialize_local_capacity(_local_sizes))
220  {
221  DASH_LOG_TRACE("CSRPattern()", "(sizespec, dist, team)");
222  DASH_ASSERT_EQ(
223  _local_sizes.size(), _nunits,
224  "Number of given local sizes " << _local_sizes.size() << " " <<
225  "does not match number of units" << _nunits);
226  initialize_local_range();
227  DASH_LOG_TRACE("CSRPattern()", "CSRPattern initialized");
228  }
229 
237  const SizeSpec_t & sizespec,
239  const DistributionSpec_t & distspec,
241  Team & team = dash::Team::All())
242  : _size(sizespec.size()),
243  _local_sizes(initialize_local_sizes(
244  _size,
245  distspec,
246  team)),
247  _block_offsets(initialize_block_offsets(
248  _local_sizes)),
249  _memory_layout(std::array<SizeType, 1> {{ _size }}),
250  _blockspec(initialize_blockspec(
251  _local_sizes)),
252  _distspec(DistributionSpec_t()),
253  _team(&team),
254  _teamspec(_distspec, *_team),
255  _nunits(_team->size()),
256  _local_size(
257  initialize_local_extent(
258  _team->myid(),
259  _local_sizes)),
260  _local_memory_layout(std::array<SizeType, 1> {{ _local_size }}),
261  _local_capacity(initialize_local_capacity(_local_sizes))
262  {
263  DASH_LOG_TRACE("CSRPattern()", "(sizespec, dist, team)");
264  DASH_ASSERT_EQ(
265  _local_sizes.size(), _nunits,
266  "Number of given local sizes " << _local_sizes.size() << " " <<
267  "does not match number of units" << _nunits);
268  initialize_local_range();
269  DASH_LOG_TRACE("CSRPattern()", "CSRPattern initialized");
270  }
271 
278  template<typename ... Args>
281  const std::vector<size_type> & local_sizes,
285  SizeType arg,
289  Args && ... args)
290  : CSRPattern(local_sizes, PatternArguments_t(arg, args...))
291  {
292  DASH_LOG_TRACE("CSRPattern()", "Constructor with argument list");
293  DASH_ASSERT_EQ(
294  _local_sizes.size(), _nunits,
295  "Number of given local sizes " << _local_sizes.size() << " " <<
296  "does not match number of units" << _nunits);
297  initialize_local_range();
298  DASH_LOG_TRACE("CSRPattern()", "CSRPattern initialized");
299  }
300 
308  const std::vector<size_type> & local_sizes,
310  const TeamSpec_t & teamspec,
312  dash::Team & team = dash::Team::All())
313  : _size(initialize_size(
314  local_sizes)),
315  _local_sizes(local_sizes),
316  _block_offsets(initialize_block_offsets(
317  _local_sizes)),
318  _memory_layout(std::array<SizeType, 1> {{ _size }}),
319  _blockspec(initialize_blockspec(
320  _local_sizes)),
321  _distspec(DistributionSpec_t()),
322  _team(&team),
323  _teamspec(
324  teamspec,
325  _distspec,
326  *_team),
327  _nunits(_team->size()),
328  _local_size(
329  initialize_local_extent(
330  _team->myid(),
331  _local_sizes)),
332  _local_memory_layout(std::array<SizeType, 1> {{ _local_size }}),
333  _local_capacity(initialize_local_capacity(_local_sizes))
334  {
335  DASH_LOG_TRACE("CSRPattern()", "(sizespec, dist, teamspec, team)");
336  DASH_ASSERT_EQ(
337  _local_sizes.size(), _nunits,
338  "Number of given local sizes " << _local_sizes.size() << " " <<
339  "does not match number of units" << _nunits);
340  initialize_local_range();
341  DASH_LOG_TRACE("CSRPattern()", "CSRPattern initialized");
342  }
343 
351  const std::vector<size_type> & local_sizes,
353  Team & team = dash::Team::All())
354  : _size(
355  initialize_size(
356  local_sizes)),
357  _local_sizes(local_sizes),
358  _block_offsets(
359  initialize_block_offsets(
360  _local_sizes)),
361  _memory_layout(std::array<SizeType, 1> {{ _size }}),
362  _blockspec(
363  initialize_blockspec(
364  _local_sizes)),
365  _distspec(DistributionSpec_t()),
366  _team(&team),
367  _teamspec(_distspec, *_team),
368  _nunits(_team->size()),
369  _local_size(
370  initialize_local_extent(
371  _team->myid(),
372  _local_sizes)),
373  _local_memory_layout(
374  std::array<SizeType, 1> {{ _local_size }}),
375  _local_capacity(
376  initialize_local_capacity(_local_sizes))
377  {
378  DASH_LOG_TRACE("CSRPattern()", "(sizespec, dist, team)");
379  DASH_ASSERT_EQ(
380  _local_sizes.size(), _nunits,
381  "Number of given local sizes " << _local_sizes.size() << " " <<
382  "does not match number of units" << _nunits);
383  initialize_local_range();
384  DASH_LOG_TRACE("CSRPattern()", "CSRPattern initialized");
385  }
386 
390  CSRPattern(const self_t & other) = default;
391 
395  CSRPattern(self_t && other) = default;
396 
403  CSRPattern(self_t & other)
404  : CSRPattern(static_cast<const self_t &>(other))
405  { }
406 
410  self_t & operator=(const self_t & other) = default;
411 
415  self_t & operator=(self_t && other) = default;
416 
420  constexpr bool operator==(
422  const self_t & other) const noexcept
423  {
424  return (this == &other) ||
425  ( // no need to compare all members as most are derived from
426  // constructor arguments.
427  _size == other._size &&
428  _local_sizes == other._local_sizes &&
429  _distspec == other._distspec &&
430  _teamspec == other._teamspec &&
431  _nunits == other._nunits
432  );
433  }
434 
438  constexpr bool operator!=(
440  const self_t & other) const noexcept
441  {
442  return !(*this == other);
443  }
444 
450  constexpr IndexType lbegin() const noexcept
451  {
452  return _lbegin;
453  }
454 
460  constexpr IndexType lend() const noexcept
461  {
462  return _lend;
463  }
464 
468 
474  constexpr team_unit_t unit_at(
476  const std::array<IndexType, NumDimensions> & coords,
478  const ViewSpec_t & viewspec) const
479  {
480  return unit_at(coords[0] + viewspec.offset(0));
481  }
482 
488  constexpr team_unit_t unit_at(
489  const std::array<IndexType, NumDimensions> & g_coords) const
490  {
491  return unit_at(g_coords[0]);
492  }
493 
499  constexpr team_unit_t unit_at(
501  IndexType global_pos,
503  const ViewSpec_t & viewspec) const
504  {
505  return unit_at(global_pos + viewspec.offset(0));
506  }
507 
515  IndexType g_index) const
516  {
517  DASH_LOG_TRACE_VAR("CSRPattern.unit_at()", g_index);
518 
519  for (team_unit_t unit_idx{0}; unit_idx < _nunits; ++unit_idx) {
520  if (g_index < _local_sizes[unit_idx]) {
521  DASH_LOG_TRACE_VAR("CSRPattern.unit_at >", unit_idx);
522  return unit_idx;
523  }
524  g_index -= _local_sizes[unit_idx];
525  }
526  DASH_THROW(
528  "CSRPattern.unit_at: " <<
529  "global index " << g_index << " is out of bounds");
530  }
531 
535 
545  inline IndexType extent(dim_t dim) const
546  {
547  DASH_ASSERT_EQ(
548  0, dim,
549  "Wrong dimension for Pattern::local_extent. " <<
550  "Expected dimension = 0, got " << dim);
551  return _size;
552  }
553 
565  inline IndexType local_extent(dim_t dim) const
566  {
567  DASH_ASSERT_EQ(
568  0, dim,
569  "Wrong dimension for Pattern::local_extent. " <<
570  "Expected dimension = 0, got " << dim);
571  return _local_size;
572  }
573 
585  constexpr std::array<SizeType, NumDimensions>
586  local_extents() const noexcept
587  {
588  return std::array<SizeType, 1> {{ _local_sizes[_team->myid()] }};
589  }
601  constexpr std::array<SizeType, NumDimensions> local_extents(
602  team_unit_t unit) const noexcept
603  {
604  return std::array<SizeType, 1> {{ _local_sizes[unit] }};
605  }
606 
610 
617  constexpr IndexType local_at(
619  const std::array<IndexType, NumDimensions> & local_coords,
621  const ViewSpec_t & viewspec) const noexcept
622  {
623  return local_coords[0] + viewspec.offset(0);
624  }
625 
631  constexpr IndexType local_at(
633  const std::array<IndexType, NumDimensions> & local_coords) const
634  {
635  return local_coords[0];
636  }
637 
646  inline local_coords_t local(
647  const std::array<IndexType, NumDimensions> & g_coords) const
648  {
649  local_index_t l_index = local(g_coords[0]);
650  local_coords_t l_coords;
651  l_coords.unit = l_index.unit;
652  l_coords.coords[0] = l_index.index;
653  return l_coords;
654  }
655 
663  inline local_index_t local(
664  IndexType g_index) const
665  {
666  DASH_LOG_TRACE_VAR("CSRPattern.local()", g_index);
667  local_index_t l_index;
668 
669  for (team_unit_t unit_idx{0}; unit_idx < _nunits; ++unit_idx) {
670  if (g_index < _local_sizes[unit_idx]) {
671  l_index.unit = unit_idx;
672  l_index.index = g_index;
673  DASH_LOG_TRACE("CSRPattern.local >",
674  "unit:", l_index.unit,
675  "index:", l_index.index);
676  return l_index;
677  }
678  g_index -= _local_sizes[unit_idx];
679  }
680  DASH_THROW(
682  "CSRPattern.local: " <<
683  "global index " << g_index << " is out of bounds");
684  }
685 
692  constexpr std::array<IndexType, NumDimensions> local_coords(
693  const std::array<IndexType, NumDimensions> & g_coords) const
694  {
695  return std::array<IndexType, 1> {{ local(g_coords[0]).index }};
696  }
697 
704  constexpr local_index_t local_index(
705  const std::array<IndexType, NumDimensions> & g_coords) const
706  {
707  return local(g_coords[0]);
708  }
709 
713 
719  inline std::array<IndexType, NumDimensions> global(
720  team_unit_t unit,
721  const std::array<IndexType, NumDimensions> & local_coords) const
722  {
723  DASH_LOG_DEBUG_VAR("CSRPattern.global()", unit);
724  DASH_LOG_DEBUG_VAR("CSRPattern.global()", local_coords);
725  DASH_LOG_TRACE_VAR("CSRPattern.global", _nunits);
726  if (_nunits < 2) {
727  return local_coords;
728  }
729  // Initialize global index with element phase (= local coords):
730  index_type glob_index = _block_offsets[unit] + local_coords[0];
731  DASH_LOG_TRACE_VAR("CSRPattern.global >", glob_index);
732  return std::array<IndexType, 1> {{ glob_index }};
733  }
734 
740  constexpr std::array<IndexType, NumDimensions> global(
741  const std::array<IndexType, NumDimensions> & l_coords) const
742  {
743  return global(_team->myid(), l_coords);
744  }
745 
754  constexpr IndexType global(
755  team_unit_t unit,
756  IndexType l_index) const
757  {
758  return global(unit, std::array<IndexType, 1> {{ l_index }})[0];
759  }
760 
769  constexpr IndexType global(
770  IndexType l_index) const
771  {
772  return global(_team->myid(), std::array<IndexType, 1> {{ l_index }})[0];
773  }
774 
783  constexpr IndexType global_index(
784  team_unit_t unit,
785  const std::array<IndexType, NumDimensions> & l_coords) const
786  {
787  return global(unit, l_coords[0]);
788  }
789 
793 
802  constexpr IndexType at(
803  const std::array<IndexType, NumDimensions> & g_coords) const
804  {
805  return local_coords(g_coords)[0];
806  }
807 
815  inline IndexType at(
816  const std::array<IndexType, NumDimensions> & g_coords,
817  const ViewSpec_t & viewspec) const
818  {
819  auto vs_coords = g_coords;
820  vs_coords[0] += viewspec.offset(0);
821  return local_coords(vs_coords)[0];
822  }
823 
831  template<typename ... Values>
832  constexpr IndexType at(IndexType value, Values ... values) const
833  {
834  static_assert(
835  sizeof...(values) == NumDimensions-1,
836  "Wrong parameter number");
837  return at(std::array<IndexType, NumDimensions> {{
838  value, (IndexType)values...
839  }});
840  }
841 
845 
851  constexpr bool is_local(
852  IndexType index,
853  team_unit_t unit) const noexcept
854  {
855  return (index >= _block_offsets[unit])
856  && ( unit == _nunits-1
857  || index < _block_offsets[unit+1]
858  );
859  }
860 
867  inline bool is_local(
868  IndexType index) const
869  {
870  auto unit = team().myid();
871  bool is_loc = index >= _block_offsets[unit] &&
872  (unit == _nunits-1 ||
873  index < _block_offsets[unit+1]);
874  return is_loc;
875  }
876 
880 
884  constexpr const BlockSpec_t & blockspec() const noexcept
885  {
886  return _blockspec;
887  }
888 
894  constexpr index_type block_at(
896  const std::array<index_type, NumDimensions> & g_coords) const
897  {
898  return static_cast<index_type>(unit_at(g_coords[0]));
899  }
900 
905  ViewSpec_t block(
906  index_type g_block_index) const
907  {
908  DASH_LOG_DEBUG_VAR("CSRPattern<1>.block >", g_block_index);
909  index_type offset = _block_offsets[g_block_index];
910  auto block_size = _local_sizes[g_block_index];
911  std::array<index_type, NumDimensions> offsets = {{ offset }};
912  std::array<size_type, NumDimensions> extents = {{ block_size }};
913  ViewSpec_t block_vs(offsets, extents);
914  DASH_LOG_DEBUG_VAR("CSRPattern<1>.block >", block_vs);
915  return block_vs;
916  }
917 
922  ViewSpec_t local_block(
923  index_type l_block_index) const
924  {
925  DASH_LOG_DEBUG_VAR("CSRPattern<1>.local_block()", l_block_index);
926  DASH_ASSERT_EQ(
927  0, l_block_index,
928  "CSRPattern always assigns exactly 1 block to a single unit");
929  index_type block_offset = _block_offsets[_team->myid()];
930  size_type block_size = _local_sizes[_team->myid()];
931  std::array<index_type, NumDimensions> offsets = {{ block_offset }};
932  std::array<size_type, NumDimensions> extents = {{ block_size }};
933  ViewSpec_t block_vs(offsets, extents);
934  DASH_LOG_DEBUG_VAR("CSRPattern<1>.local_block >", block_vs);
935  return block_vs;
936  }
937 
942  inline ViewSpec_t local_block_local(
943  index_type l_block_index) const
944  {
945  DASH_LOG_DEBUG_VAR("CSRPattern<1>.local_block_local >", l_block_index);
946  size_type block_size = _local_sizes[_team->myid()];
947  std::array<index_type, NumDimensions> offsets = {{ 0 }};
948  std::array<size_type, NumDimensions> extents = {{ block_size }};
949  ViewSpec_t block_vs(offsets, extents);
950  DASH_LOG_DEBUG_VAR("CSRPattern<1>.local_block_local >", block_vs);
951  return block_vs;
952  }
953 
961  constexpr SizeType blocksize(
963  dim_t dimension) const noexcept
964  {
965  return _local_capacity;
966  }
967 
976  constexpr SizeType max_blocksize() const noexcept
977  {
978  return _local_capacity;
979  }
980 
987  constexpr SizeType local_capacity() const noexcept
988  {
989  return _local_capacity;
990  }
991 
1003  constexpr SizeType local_size() const noexcept
1004  {
1005  return _local_size;
1006  }
1007 
1008  constexpr SizeType local_size(team_unit_t unit) const noexcept
1009  {
1010  return _local_sizes[unit.id];
1011  }
1012 
1018  constexpr IndexType num_units() const noexcept
1019  {
1020  return _nunits;
1021  }
1022 
1028  constexpr IndexType capacity() const noexcept
1029  {
1030  return _size;
1031  }
1032 
1038  constexpr IndexType size() const noexcept
1039  {
1040  return _size;
1041  }
1042 
1047  constexpr dash::Team & team() const noexcept
1048  {
1049  return *_team;
1050  }
1051 
1055  constexpr const DistributionSpec_t & distspec() const noexcept
1056  {
1057  return _distspec;
1058  }
1059 
1065  constexpr SizeSpec_t sizespec() const noexcept
1066  {
1067  return SizeSpec_t(std::array<SizeType, 1> {{ _size }});
1068  }
1069 
1075  constexpr const std::array<SizeType, NumDimensions>
1076  extents() const noexcept
1077  {
1078  return std::array<SizeType, 1> {{ _size }};
1079  }
1080 
1087  constexpr const TeamSpec_t & teamspec() const noexcept
1088  {
1089  return _teamspec;
1090  }
1091 
1098  constexpr std::array<IndexType, NumDimensions> coords(
1099  IndexType index) const noexcept
1100  {
1101  return std::array<IndexType, 1> {{ index }};
1102  }
1103 
1110  std::array<IndexType, NumDimensions> coords(
1111  IndexType index,
1112  const ViewSpec_t & viewspec) const {
1113  return std::array<IndexType, 1> {{ index + viewspec.offset(0) }};
1114  }
1115 
1116 
1120  constexpr static MemArrange memory_order()
1121  {
1122  return Arrangement;
1123  }
1124 
1129  constexpr static dim_t ndim()
1130  {
1131  return 1;
1132  }
1133 
1134 private:
1135 
1136  CSRPattern(
1137  const PatternArguments_t & arguments)
1138  : CSRPattern(
1139  initialize_local_sizes(
1140  arguments.sizespec().size(),
1141  arguments.distspec(),
1142  arguments.team()),
1143  arguments)
1144  {}
1145 
1146  CSRPattern(
1147  std::vector<size_type> local_sizes, const PatternArguments_t &arguments)
1148  : _size(arguments.sizespec().size())
1149  , _local_sizes(std::move(local_sizes))
1150  , _block_offsets(initialize_block_offsets(_local_sizes))
1151  , _memory_layout(std::array<SizeType, 1>{{_size}})
1152  , _blockspec(initialize_blockspec(_local_sizes))
1153  , _distspec(arguments.distspec())
1154  , _team(&arguments.team())
1155  , _teamspec(arguments.teamspec())
1156  , _nunits(_team->size())
1157  , _local_size(initialize_local_extent(_team->myid(), _local_sizes))
1158  , _local_memory_layout(std::array<SizeType, 1>{{_local_size}})
1159  , _local_capacity(initialize_local_capacity(_local_sizes))
1160  {}
1161 
1165  inline SizeType initialize_size(
1166  const std::vector<size_type> & local_sizes) const
1167  {
1168  DASH_LOG_TRACE_VAR("CSRPattern.init_size()", local_sizes);
1169  size_type size = 0;
1170  for (size_type unit_idx = 0; unit_idx < local_sizes.size(); ++
1171  unit_idx) {
1172  size += local_sizes[unit_idx];
1173  }
1174  DASH_LOG_TRACE_VAR("CSRPattern.init_size >", size);
1175  return size;
1176  }
1177 
1182  std::vector<size_type> initialize_local_sizes(
1183  size_type total_size,
1184  const DistributionSpec_t & distspec,
1185  const dash::Team & team) const
1186  {
1187  DASH_LOG_TRACE_VAR("CSRPattern.init_local_sizes()", total_size);
1188  std::vector<size_type> l_sizes;
1189  auto nunits = team.size();
1190  DASH_LOG_TRACE_VAR("CSRPattern.init_local_sizes()", nunits);
1191  if (nunits == 1) {
1192  l_sizes.push_back(total_size);
1193  }
1194  if (nunits <= 1) {
1195  return l_sizes;
1196  }
1197  auto dist_type = distspec[0].type;
1198  DASH_LOG_TRACE_VAR("CSRPattern.init_local_sizes()", dist_type);
1199  // Tiled and blocked distribution:
1200  if (dist_type == dash::internal::DIST_BLOCKED ||
1201  dist_type == dash::internal::DIST_TILE) {
1202  auto blocksize = dash::math::div_ceil(total_size, nunits);
1203  for (size_type u = 0; u < nunits; ++u) {
1204  l_sizes.push_back(blocksize);
1205  }
1206  // Unspecified distribution (default-constructed pattern instance),
1207  // set all local sizes to 0:
1208  } else if (dist_type == dash::internal::DIST_UNDEFINED) {
1209  for (size_type u = 0; u < nunits; ++u) {
1210  l_sizes.push_back(0);
1211  }
1212  // No distribution, assign all indices to unit 0:
1213  } else if (dist_type == dash::internal::DIST_NONE) {
1214  l_sizes.push_back(total_size);
1215  for (size_type u = 0; u < nunits-1; ++u) {
1216  l_sizes.push_back(0);
1217  }
1218  // Incompatible distribution type:
1219  } else {
1220  DASH_THROW(
1222  "CSRPattern expects TILE (" << dash::internal::DIST_TILE << ") " <<
1223  "or BLOCKED (" << dash::internal::DIST_BLOCKED << ") " <<
1224  "distribution, got " << dist_type);
1225  }
1226  DASH_LOG_TRACE_VAR("CSRPattern.init_local_sizes >", l_sizes);
1227  return l_sizes;
1228  }
1229 
1230  BlockSpec_t initialize_blockspec(
1231  const std::vector<size_type> & local_sizes) const
1232  {
1233  DASH_LOG_TRACE_VAR("CSRPattern.init_blockspec", local_sizes);
1234  BlockSpec_t blockspec({
1235  static_cast<size_type>(local_sizes.size())
1236  });
1237  DASH_LOG_TRACE_VAR("CSRPattern.init_blockspec >", blockspec);
1238  return blockspec;
1239  }
1240 
1245  std::vector<size_type> initialize_block_offsets(
1246  const std::vector<size_type> & local_sizes) const
1247  {
1248  DASH_LOG_TRACE_VAR("CSRPattern.init_block_offsets", local_sizes);
1249  std::vector<size_type> block_offsets;
1250  if (local_sizes.size() > 0) {
1251  // NOTE: Assuming 1 block for every unit.
1252  block_offsets.push_back(0);
1253  for (size_type unit_idx = 0;
1254  unit_idx < local_sizes.size()-1;
1255  ++unit_idx)
1256  {
1257  auto block_offset = block_offsets[unit_idx] +
1258  local_sizes[unit_idx];
1259  block_offsets.push_back(block_offset);
1260  }
1261  }
1262  DASH_LOG_TRACE_VAR("CSRPattern.init_block_offsets >", block_offsets);
1263  return block_offsets;
1264  }
1265 
1269  SizeType initialize_local_capacity(
1270  const std::vector<size_type> & local_sizes) const
1271  {
1272  SizeType l_capacity = 0;
1273  // Local capacity is maximum number of elements assigned to a single unit,
1274  // i.e. the maximum local size:
1275  auto max_local_size = std::max_element(local_sizes.begin(),
1276  local_sizes.end());
1277  if (max_local_size != local_sizes.end()) {
1278  l_capacity = *max_local_size;
1279  }
1280  DASH_LOG_DEBUG_VAR("CSRPattern.init_lcapacity >", l_capacity);
1281  return l_capacity;
1282  }
1283 
1288  void initialize_local_range()
1289  {
1290  auto l_size = _local_size;
1291  DASH_LOG_DEBUG_VAR("CSRPattern.init_local_range()", l_size);
1292  if (l_size == 0) {
1293  _lbegin = 0;
1294  _lend = 0;
1295  } else {
1296  // First local index transformed to global index
1297  _lbegin = global(0);
1298  // Index past last local index transformed to global index.
1299  // global(l_size) would be out of range, so we use the global index
1300  // to the last element and increment by 1:
1301  _lend = global(l_size - 1) + 1;
1302  }
1303  DASH_LOG_DEBUG_VAR("CSRPattern.init_local_range >", _lbegin);
1304  DASH_LOG_DEBUG_VAR("CSRPattern.init_local_range >", _lend);
1305  }
1306 
1310  SizeType initialize_local_extent(
1311  team_unit_t unit,
1312  const std::vector<size_type> & local_sizes) const
1313  {
1314  DASH_LOG_DEBUG_VAR("CSRPattern.init_local_extent()", unit);
1315  if (local_sizes.size() == 0) {
1316  return 0;
1317  }
1318  // Local size of given unit:
1319  SizeType l_extent = local_sizes[static_cast<int>(unit)];
1320  DASH_LOG_DEBUG_VAR("CSRPattern.init_local_extent >", l_extent);
1321  return l_extent;
1322  }
1323 
1324 }; // class CSRPattern<1>
1325 
1326 } // namespace dash
1327 
1328 #endif // DASH__CSR_PATTERN_1D_H_
constexpr const std::array< SizeType, NumDimensions > extents() const noexcept
Size specification of the index space mapped by this pattern.
Definition: CSRPattern.h:1076
All blocks have identical size.
pattern_layout_properties< pattern_layout_tag::blocked, pattern_layout_tag::linear > layout_properties
Satisfiable properties in pattern property category Layout:
Definition: CSRPattern.h:85
ViewSpec_t local_block(index_type l_block_index) const
View spec (offset and extents) of block at local linear block index in global cartesian element space...
Definition: CSRPattern.h:922
constexpr const TeamSpec_t & teamspec() const noexcept
Cartesian arrangement of the Team containing the units to which this pattern&#39;s elements are mapped...
Definition: CSRPattern.h:1087
constexpr SizeType local_size() const noexcept
The actual number of elements in this pattern that are local to the calling unit in total...
Definition: CSRPattern.h:1003
constexpr std::enable_if< std::is_integral< IndexType >::value, IndexType >::type index(IndexType idx)
Definition: Iterator.h:60
size_t size()
Return the number of units in the global team.
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
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
Local element order corresponds to a logical linearization within single blocks (if blocked) or withi...
constexpr std::array< SizeType, NumDimensions > local_extents(team_unit_t unit) const noexcept
The actual number of elements in this pattern that are local to the given unit, by dimension...
Definition: CSRPattern.h:601
local_index_t local(IndexType g_index) const
Converts global index to its associated unit and respective local index.
Definition: CSRPattern.h:663
constexpr team_unit_t unit_at(IndexType global_pos, const ViewSpec_t &viewspec) const
Convert given global linear index to its assigned unit id.
Definition: CSRPattern.h:499
Specifies cartesian extents in a specific number of dimensions.
Definition: Cartesian.h:197
constexpr IndexType at(const std::array< IndexType, NumDimensions > &g_coords) const
at
Definition: CSRPattern.h:802
pattern_partitioning_properties< pattern_partitioning_tag::minimal, pattern_partitioning_tag::rectangular, pattern_partitioning_tag::balanced, pattern_partitioning_tag::unbalanced > partitioning_properties
Satisfiable properties in pattern property category Partitioning:
Definition: CSRPattern.h:72
constexpr IndexType global(team_unit_t unit, IndexType l_index) const
Resolve an element&#39;s linear global index from the given unit&#39;s local index of that element...
Definition: CSRPattern.h:754
Specifies view parameters for implementing submat, rows and cols.
Definition: Dimensional.h:430
constexpr bool operator==(const self_t &other) const noexcept
Equality comparison operator.
Definition: CSRPattern.h:420
constexpr std::array< SizeType, NumDimensions > local_extents() const noexcept
The actual number of elements in this pattern that are local to the calling unit, by dimension...
Definition: CSRPattern.h:586
int dim_t
Scalar type for a dimension value, with 0 indicating the first dimension.
Definition: Types.h:39
constexpr SizeSpec_t sizespec() const noexcept
Size specification of the index space mapped by this pattern.
Definition: CSRPattern.h:1065
constexpr SizeType blocksize(dim_t dimension) const noexcept
Maximum number of elements in a single block in the given dimension.
Definition: CSRPattern.h:961
CSRPattern(const std::vector< size_type > &local_sizes, Team &team=dash::Team::All())
Constructor, initializes a pattern from explicit instances of SizeSpec, DistributionSpec, TeamSpec and a Team.
Definition: CSRPattern.h:349
constexpr index_type block_at(const std::array< index_type, NumDimensions > &g_coords) const
Index of block at given global coordinates.
Definition: CSRPattern.h:894
IndexType local_extent(dim_t dim) const
The actual number of elements in this pattern that are local to the calling unit in the given dimensi...
Definition: CSRPattern.h:565
static constexpr MemArrange memory_order()
Memory order followed by the pattern.
Definition: CSRPattern.h:1120
CSRPattern(const std::vector< size_type > &local_sizes, const TeamSpec_t &teamspec, dash::Team &team=dash::Team::All())
Constructor, initializes a pattern from explicit instances of SizeSpec, DistributionSpec, TeamSpec and a Team.
Definition: CSRPattern.h:306
The number of blocks assigned to units may differ.
constexpr team_unit_t unit_at(const std::array< IndexType, NumDimensions > &coords, const ViewSpec_t &viewspec) const
unit_at
Definition: CSRPattern.h:474
ViewSpec_t local_block_local(index_type l_block_index) const
View spec (offset and extents) of block at local linear block index in local cartesian element space...
Definition: CSRPattern.h:942
Minimal number of blocks in every dimension, typically at most one block per unit.
Block extents are constant for every dimension.
constexpr bool is_local(IndexType index, team_unit_t unit) const noexcept
is_local
Definition: CSRPattern.h:851
constexpr std::enable_if< dash::view_traits< ViewType >::is_view::value &&dash::view_traits< ViewType >::is_local::value, const typename ViewType::global_type & >::type global(const ViewType &v)
Definition: Global.h:19
size_t size() const
The number of units in this team.
Definition: Team.h:498
constexpr dash::Team & team() const noexcept
The Team containing the units to which this pattern&#39;s elements are mapped.
Definition: CSRPattern.h:1047
constexpr std::array< IndexType, NumDimensions > coords(IndexType index) const noexcept
Convert given global linear offset (index) to global cartesian coordinates.
Definition: CSRPattern.h:1098
Cartesian space defined by extents in n dimensions.
Definition: Cartesian.h:26
std::array< IndexType, NumDimensions > global(team_unit_t unit, const std::array< IndexType, NumDimensions > &local_coords) const
global
Definition: CSRPattern.h:719
internal::default_signed_index default_index_t
Signed integer type used as default for index values.
Definition: Types.h:59
constexpr local_index_t local_index(const std::array< IndexType, NumDimensions > &g_coords) const
Converts global coordinates to their associated unit and their respective local index.
Definition: CSRPattern.h:704
constexpr std::array< IndexType, NumDimensions > global(const std::array< IndexType, NumDimensions > &l_coords) const
Converts local coordinates of active unit to global coordinates.
Definition: CSRPattern.h:740
static constexpr dim_t ndim()
Number of dimensions of the cartesian space partitioned by the pattern.
Definition: CSRPattern.h:1129
A Team instance specifies a subset of all available units.
Definition: Team.h:41
constexpr IndexType local_at(const std::array< IndexType, NumDimensions > &local_coords, const ViewSpec_t &viewspec) const noexcept
local
Definition: CSRPattern.h:617
Elements are contiguous in local memory within a single block and thus indexed blockwise.
constexpr IndexType global(IndexType l_index) const
Resolve an element&#39;s linear global index from the calling unit&#39;s local index of that element...
Definition: CSRPattern.h:769
CSRPattern(const SizeSpec_t &sizespec, const DistributionSpec_t &distspec, const TeamSpec_t &teamspec, Team &team=dash::Team::All())
Constructor, initializes a pattern from explicit instances of SizeSpec, DistributionSpec, TeamSpec and Team.
Definition: CSRPattern.h:188
Defines a cartesian, totally-ordered index space by mapping linear indices to cartesian coordinates d...
Definition: Cartesian.h:239
constexpr SizeType local_capacity() const noexcept
Maximum number of elements assigned to a single unit in total, equivalent to the local capacity of ev...
Definition: CSRPattern.h:987
constexpr std::array< IndexType, NumDimensions > local_coords(const std::array< IndexType, NumDimensions > &g_coords) const
Converts global coordinates to their associated unit&#39;s respective local coordinates.
Definition: CSRPattern.h:692
constexpr team_unit_t unit_at(const std::array< IndexType, NumDimensions > &g_coords) const
Convert given coordinate in pattern to its assigned unit id.
Definition: CSRPattern.h:488
IndexType at(const std::array< IndexType, NumDimensions > &g_coords, const ViewSpec_t &viewspec) const
Global coordinates and viewspec to local index.
Definition: CSRPattern.h:815
constexpr const BlockSpec_t & blockspec() const noexcept
block
Definition: CSRPattern.h:884
CSRPattern(self_t &other)
Copy constructor using non-const lvalue reference parameter.
Definition: CSRPattern.h:403
constexpr IndexType capacity() const noexcept
The maximum number of elements arranged in this pattern.
Definition: CSRPattern.h:1028
constexpr const DistributionSpec_t & distspec() const noexcept
Distribution specification of this pattern.
Definition: CSRPattern.h:1055
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
constexpr SizeType max_blocksize() const noexcept
Maximum number of elements in a single block in all dimensions.
Definition: CSRPattern.h:976
GlobIter max_element(const GlobIter &first, const GlobIter &last, Compare compare=Compare())
Finds an iterator pointing to the element with the greatest value in the range [first,last).
Definition: MinMax.h:332
constexpr IndexType size() const noexcept
The number of elements arranged in this pattern.
Definition: CSRPattern.h:1038
ViewSpec_t block(index_type g_block_index) const
View spec (offset and extents) of block at global linear block index in cartesian element space...
Definition: CSRPattern.h:905
CSRPattern(SizeType arg, Args &&... args)
Constructor, initializes a pattern from an argument list consisting of the pattern size (extent...
Definition: CSRPattern.h:163
CSRPattern(const SizeSpec_t &sizespec, const DistributionSpec_t &distspec, Team &team=dash::Team::All())
Constructor, initializes a pattern from explicit instances of SizeSpec, DistributionSpec and Team...
Definition: CSRPattern.h:235
constexpr IndexType at(IndexType value, Values ... values) const
Global coordinates to local index.
Definition: CSRPattern.h:832
local_coords_t local(const std::array< IndexType, NumDimensions > &g_coords) const
Converts global coordinates to their associated unit and its respective local coordinates.
Definition: CSRPattern.h:646
team_unit_t unit_at(IndexType g_index) const
Convert given global linear index to its assigned unit id.
Definition: CSRPattern.h:513
constexpr IndexType lbegin() const noexcept
Resolves the global index of the first local element in the pattern.
Definition: CSRPattern.h:450
constexpr IndexType local_at(const std::array< IndexType, NumDimensions > &local_coords) const
Convert given local coordinates to linear local offset (index).
Definition: CSRPattern.h:631
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
constexpr IndexType lend() const noexcept
Resolves the global index past the last local element in the pattern.
Definition: CSRPattern.h:460
std::array< IndexType, NumDimensions > coords(IndexType index, const ViewSpec_t &viewspec) const
Convert given global linear offset (index) to global cartesian coordinates using viewspec.
Definition: CSRPattern.h:1110
bool is_local(IndexType index) const
Whether the given global index is local to the unit that created this pattern instance.
Definition: CSRPattern.h:867
CSRPattern(const std::vector< size_type > &local_sizes, SizeType arg, Args &&... args)
Constructor, initializes a pattern from an argument list consisting of the pattern size (extent...
Definition: CSRPattern.h:279
IndexType extent(dim_t dim) const
extent
Definition: CSRPattern.h:545
constexpr IndexType global_index(team_unit_t unit, const std::array< IndexType, NumDimensions > &l_coords) const
Resolve an element&#39;s linear global index from a given unit&#39;s local coordinates of that element...
Definition: CSRPattern.h:783
constexpr IndexType num_units() const noexcept
The number of units to which this pattern&#39;s elements are mapped.
Definition: CSRPattern.h:1018
DistributionSpec describes distribution patterns of all dimensions,.
Definition: Dimensional.h:222
pattern_mapping_properties< pattern_mapping_tag::unbalanced > mapping_properties
Satisfiable properties in pattern property category Mapping:
Definition: CSRPattern.h:77
Generic type of mapping properties of a model satisfying the Pattern concept.
constexpr bool operator!=(const self_t &other) const noexcept
Inquality comparison operator.
Definition: CSRPattern.h:438