1 #ifndef DASH__BLOCK_PATTERN_1D_H_ 2 #define DASH__BLOCK_PATTERN_1D_H_ 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> 11 #include <dash/pattern/BlockPattern.h> 12 #include <dash/pattern/PatternProperties.h> 13 #include <dash/pattern/internal/PatternArguments.h> 15 #include <dash/internal/Math.h> 16 #include <dash/internal/Logging.h> 20 #include <type_traits> 32 MemArrange Arrangement,
38 static const dim_t NumDimensions = 1;
41 static constexpr
const char * PatternName =
"BlockPattern1D";
69 typedef typename std::make_unsigned<IndexType>::type
88 typedef internal::PatternArguments<NumDimensions, IndexType>
92 typedef IndexType index_type;
93 typedef SizeType size_type;
101 std::array<index_type, NumDimensions>
coords;
108 MemoryLayout_t _memory_layout;
111 DistributionSpec_t _distspec;
115 TeamSpec_t _teamspec;
117 SizeType _nunits = 0;
119 SizeType _blocksize = 0;
121 SizeType _nblocks = 0;
123 SizeType _local_size = 0;
125 LocalMemoryLayout_t _local_memory_layout;
127 SizeType _nlblocks = 0;
129 SizeType _local_capacity = 0;
131 std::array<IndexType, 2> _lbegin_lend = { };
133 IndexType _lend = -1;
157 template<
typename ... Args>
199 const DistributionSpec_t dist,
204 : _size(sizespec.
size()),
205 _memory_layout(
std::array<SizeType, 1> {{ _size }}),
212 _nunits(_team->
size()),
213 _blocksize(initialize_blocksize(
217 _nblocks(initialize_num_blocks(
222 initialize_local_extent(_team->myid())),
223 _local_memory_layout(std::array<SizeType, 1> {{ _local_size }}),
224 _nlblocks(initialize_num_local_blocks(
227 _local_capacity(initialize_local_capacity()),
228 _lbegin_lend(initialize_local_range(_local_size))
258 const SizeSpec_t sizespec,
261 const DistributionSpec_t dist = DistributionSpec_t(),
264 : _size(sizespec.
size()),
265 _memory_layout(
std::array<SizeType, 1> {{ _size }}),
268 _teamspec(_distspec, *_team),
269 _nunits(_team->
size()),
270 _blocksize(initialize_blocksize(
274 _nblocks(initialize_num_blocks(
279 initialize_local_extent(_team->myid())),
280 _local_memory_layout(std::array<SizeType, 1> {{ _local_size }}),
281 _nlblocks(initialize_num_local_blocks(
284 _local_capacity(initialize_local_capacity()),
285 _lbegin_lend(initialize_local_range(_local_size))
305 : BlockPattern(static_cast<const self_t &>(other)) {
311 self_t &
operator=(self_t && other) =
default;
316 self_t &
operator=(
const self_t & other) =
default;
329 || ( _size == other._size &&
330 _distspec == other._distspec &&
331 _teamspec == other._teamspec &&
332 _nblocks == other._nblocks &&
333 _nlblocks == other._nlblocks &&
334 _local_size == other._local_size &&
335 _blocksize == other._blocksize &&
336 _nunits == other._nunits &&
337 _lbegin_lend == other._lbegin_lend
349 return !(*
this == other);
358 return _lbegin_lend[0];
366 constexpr IndexType
lend()
const {
367 return _lbegin_lend[1];
381 const std::array<IndexType, NumDimensions> &
coords,
383 const ViewSpec_t & viewspec)
const {
384 return team_unit_t (((coords[0] + viewspec.offset(0)) / _blocksize)
394 const std::array<IndexType, NumDimensions> &
coords)
const {
395 return team_unit_t((coords[0] / _blocksize) % _nunits);
405 IndexType global_pos,
407 const ViewSpec_t & viewspec
409 return team_unit_t(((global_pos + viewspec.offset(0)) / _blocksize)
422 return team_unit_t((global_pos / _blocksize) % _nunits);
441 "Wrong dimension for Pattern::local_extent. " <<
442 "Expected dimension = 0, got " << dim);
455 template <dim_t dim = 0>
459 "Wrong dimension for Pattern::local_extent. " 460 "Expected dimension = 0");
478 "Wrong dimension for Pattern::local_extent. " <<
479 "Expected dimension = 0, got " << dim);
494 template <dim_t dim = 0>
498 "Wrong dimension for Pattern::local_extent. " 499 "Expected dimension = 0");
516 return std::array<SizeType, 1> {{
517 unit == _team->myid()
519 : initialize_local_extent(unit)
535 return std::array<SizeType, 1> {{ _local_size }};
550 const std::array<IndexType, NumDimensions> &
local_coords,
552 const ViewSpec_t & viewspec)
const {
553 return local_coords[0] + viewspec.offset(0);
563 const std::array<IndexType, NumDimensions> &
local_coords)
const {
564 return local_coords[0];
576 const std::array<IndexType, NumDimensions> & global_coords)
const {
577 return local_coords_t {
591 IndexType g_index)
const {
602 const std::array<IndexType, NumDimensions> & global_coords
604 return std::array<IndexType, 1> {{
605 static_cast<IndexType
>(
606 (((global_coords[0] / _blocksize) / _nunits) * _blocksize)
607 + (global_coords[0] % _blocksize)
618 const std::array<IndexType, NumDimensions> & g_coords)
const {
619 return local_index_t {
634 constexpr std::array<IndexType, NumDimensions>
global(
636 const std::array<IndexType, NumDimensions> &
local_coords)
const {
639 : std::array<IndexType, 1> {{
640 static_cast<IndexType
>(
641 (( _distspec[0].local_index_to_block_coord(
642 static_cast<IndexType>(unit),
646 + (local_coords[0] % _blocksize)
656 constexpr std::array<IndexType, NumDimensions>
global(
657 const std::array<IndexType, NumDimensions> & l_coords)
const 659 return global(_team->myid(), l_coords);
671 IndexType l_index)
const 673 return global(_team->myid(), std::array<IndexType, 1> {{ l_index }})[0];
686 const std::array<IndexType, NumDimensions> & l_coords)
const 688 return global(unit, l_coords)[0];
699 const std::array<IndexType, NumDimensions> & global_coords,
701 const ViewSpec_t & viewspec)
const {
702 return global_coords[0] + viewspec.offset(0);
712 const std::array<IndexType, NumDimensions> & global_coords)
const {
713 return global_coords[0];
729 constexpr IndexType
at(
730 const std::array<IndexType, NumDimensions> & g_coords)
const {
741 constexpr IndexType
at(
742 const std::array<IndexType, 1> & g_coords,
743 const ViewSpec_t & viewspec)
const {
745 std::array<IndexType, 1> {{
746 g_coords[0] + viewspec.offset(0)
758 template<
typename ... Values>
759 constexpr IndexType
at(IndexType value, Values ... values)
const {
761 sizeof...(values) == NumDimensions-1,
762 "Wrong parameter number");
763 return at(std::array<IndexType, NumDimensions> {
764 value, (IndexType)values...
782 IndexType dim_offset,
810 IndexType
index)
const {
822 return BlockSpec_t({ _nblocks });
830 return BlockSpec_t({ _nlblocks });
840 const std::array<index_type, NumDimensions> & g_coords)
const {
841 return g_coords[0] / _blocksize;
851 const std::array<index_type, NumDimensions> & g_coords)
const {
852 return local_index_t {
855 (g_coords[0] / _blocksize) % _teamspec.
size()),
857 static_cast<index_type>(
858 (g_coords[0] / _blocksize) / _teamspec.
size())
868 index_type g_block_index)
const 871 {{
static_cast<index_type
>(g_block_index * _blocksize) }},
872 {{
static_cast<size_type
>(
873 _blocksize - ( g_block_index < _nblocks - 1
886 index_type l_block_index)
const 890 {{
static_cast<index_type
>(
global(l_block_index * _blocksize) ) }},
891 {{
static_cast<size_type
>(
892 (l_block_index == (_nlblocks - 1)
893 ? (_local_size % _blocksize == 0
895 : _local_size % _blocksize )
906 index_type l_block_index)
const 908 DASH_LOG_DEBUG_VAR(
"BlockPattern<1>.local_block_local()", l_block_index);
909 index_type offset = l_block_index * _blocksize;
910 std::array<index_type, NumDimensions> offsets = {{ offset }};
911 std::array<size_type, NumDimensions>
extents = {{ _blocksize }};
912 if(l_block_index == (_nlblocks - 1))
914 size_type remaining = _local_size % extents[0];
915 extents[0] = (remaining == 0) ? extents[0] : remaining;
917 ViewSpec_t block_vs(offsets, extents);
918 DASH_LOG_DEBUG_VAR(
"BlockPattern<1>.local_block_local >", block_vs);
931 dim_t dimension)
const {
955 return _local_capacity;
970 return (unit == _team->myid().id)
972 : initialize_local_extent(unit);
1013 constexpr IndexType
size() const noexcept {
1028 constexpr
const DistributionSpec_t &
distspec() const noexcept {
1038 return SizeSpec_t(std::array<SizeType, 1> {{ _size }});
1046 constexpr
const std::array<SizeType, NumDimensions>
extents()
const {
1047 return std::array<SizeType, 1> {{ _size }};
1066 constexpr std::array<IndexType, NumDimensions>
coords(
1067 IndexType
index)
const {
1068 return std::array<IndexType, 1> {{ index }};
1077 constexpr std::array<IndexType, NumDimensions>
coords(
1079 const ViewSpec_t & viewspec)
const {
1080 return std::array<IndexType, 1> {{ index + viewspec.offset(0) }};
1103 dim_t dimension)
const {
1105 return ( _blocksize == 0 || _size % _blocksize == 0
1107 : _blocksize - (_size % _blocksize)
1114 : _size(arguments.sizespec().size()),
1115 _memory_layout(std::array<SizeType, 1> {{ _size }}),
1116 _distspec(arguments.distspec()),
1117 _team(&arguments.team()),
1118 _teamspec(arguments.teamspec()),
1119 _nunits(_team->
size()),
1120 _blocksize(initialize_blocksize(
1124 _nblocks(initialize_num_blocks(
1129 initialize_local_extent(_team->myid())),
1130 _local_memory_layout(std::array<SizeType, 1> {{ _local_size }}),
1131 _nlblocks(initialize_num_local_blocks(
1134 _local_capacity(initialize_local_capacity()),
1135 _lbegin_lend(initialize_local_range(_local_size))
1142 SizeType initialize_blocksize(
1144 const DistributionSpec_t &
distspec,
1145 SizeType nunits)
const {
1146 DASH_LOG_TRACE_VAR(
"BlockPattern<1>.init_blocksize", nunits);
1160 SizeType initialize_num_blocks(
1163 SizeType nunits)
const {
1164 if (blocksize == 0) {
1167 DASH_LOG_TRACE(
"BlockPattern<1>.init_num_blocks()",
1168 "size", size,
"blocksize", blocksize,
"nunits", nunits);
1169 SizeType n_blocks = dash::math::div_ceil(
1172 DASH_LOG_TRACE_VAR(
"BlockPattern<1>.init_blockspec", n_blocks);
1179 SizeType initialize_num_local_blocks(
1183 if (blocksize > 0) {
1184 num_l_blocks = dash::math::div_ceil(
1190 DASH_LOG_TRACE_VAR(
"BlockPattern<1>.init_num_local_blocks", num_l_blocks);
1191 return num_l_blocks;
1201 SizeType initialize_local_capacity()
const {
1202 SizeType l_capacity = 1;
1206 auto max_l_blocks = dash::math::div_ceil(
1209 DASH_LOG_TRACE_VAR(
"BlockPattern<1>.init_lcapacity.d", _nunits);
1210 DASH_LOG_TRACE_VAR(
"BlockPattern<1>.init_lcapacity.d", max_l_blocks);
1211 l_capacity = max_l_blocks * _blocksize;
1212 DASH_LOG_DEBUG_VAR(
"BlockPattern<1>.init_lcapacity >", l_capacity);
1220 constexpr std::array<IndexType, 2>
1221 initialize_local_range(SizeType l_size)
const {
1222 return (l_size == 0)
1223 ? std::array<IndexType, 2> {{ 0, 0 }}
1224 : std::array<IndexType, 2> {{
1233 constexpr SizeType initialize_local_extent(
1236 return (_nunits == 0
1238 : (_nblocks == 1 && _nunits == 1
1243 : ((_nblocks / _nunits) * _blocksize
1245 + (static_cast<SizeType>(unit) < (_nblocks % _nunits)
1250 - (static_cast<SizeType>(unit) ==
1252 (_nblocks % _nunits == 0
1254 : ((_nblocks % _nunits) - 1 ))
1266 #endif // DASH__BLOCK_PATTERN_1D_H_ All blocks have identical size.
static constexpr MemArrange memory_order()
Memory order followed by the pattern.
constexpr IndexType local_at(const std::array< IndexType, NumDimensions > &local_coords, const ViewSpec_t &viewspec) const
local
bool includes_index(IndexType index, dim_t dimension, IndexType dim_offset) const
Whether the given index lies in the cartesian sub-space specified by a dimension and offset in the di...
global_unit_t myid()
Shortcut to query the global unit ID of the calling unit.
constexpr SizeType blocksize(dim_t dimension) const
Maximum number of elements in a single block in the given dimension.
constexpr SizeType local_capacity() const
Maximum number of elements assigned to a single unit in total, equivalent to the local capacity of ev...
constexpr SizeType local_size(team_unit_t unit) const
The actual number of elements in this pattern that are local to the specified unit in total...
constexpr std::enable_if< std::is_integral< IndexType >::value, IndexType >::type index(IndexType idx)
Defines how a list of global indices is mapped to single units within a Team.
constexpr IndexType global_index(team_unit_t unit, const std::array< IndexType, NumDimensions > &l_coords) const
Resolve an element's linear global index from a given unit's local coordinates of that element...
constexpr SizeType blocksize(dim_t dimension) const noexcept
Maximum number of elements in a single block in the given dimension.
constexpr const DistributionSpec_t & distspec() const noexcept
Distribution specification of this pattern.
constexpr IndexType global_at(const std::array< IndexType, NumDimensions > &global_coords) const
Convert given global coordinates to linear global offset (index).
constexpr IndexType local_at(const std::array< IndexType, NumDimensions > &local_coords) const
Convert given local coordinates to linear local offset (index).
This class is a simple memory pool which holds allocates elements of size ValueType.
Local element order corresponds to a logical linearization within single blocks (if blocked) or withi...
constexpr IndexType size() const noexcept
The number of elements arranged in this pattern.
constexpr const DistributionSpec_t & distspec() const noexcept
Distribution specification of this pattern.
constexpr bool operator!=(const self_t &other) const
Inquality comparison operator.
BlockPattern(SizeType arg, Args &&... args)
Constructor, initializes a pattern from an argument list consisting of the pattern size (extent...
BlockPattern(SizeType arg, Args &&... args)
Constructor, initializes a pattern from an argument list consisting of the pattern size (extent...
Specifies cartesian extents in a specific number of dimensions.
constexpr index_type block_at(const std::array< index_type, NumDimensions > &g_coords) const
Gobal index of block at given global coordinates.
constexpr BlockSpec_t blockspec() const
block
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...
Specifies view parameters for implementing submat, rows and cols.
constexpr std::array< IndexType, NumDimensions > coords(IndexType index, const ViewSpec_t &viewspec) const
Convert given global linear offset (index) to global cartesian coordinates using viewspec.
constexpr IndexType at(const std::array< IndexType, NumDimensions > &g_coords) const
at
int dim_t
Scalar type for a dimension value, with 0 indicating the first dimension.
constexpr std::array< IndexType, NumDimensions > local_coords(const std::array< IndexType, NumDimensions > &global_coords) const noexcept
Converts global coordinates to their associated unit's respective local coordinates.
constexpr IndexType extent() const
The number of elements in this pattern in the given dimension.
constexpr const TeamSpec_t & teamspec() const noexcept
Cartesian arrangement of the Team containing the units to which this pattern's elements are mapped...
constexpr team_unit_t unit_at(const std::array< IndexType, NumDimensions > &coords) const
Convert given coordinate in pattern to its assigned unit id.
constexpr SizeType underfilled_blocksize(dim_t dimension) const
Number of elements missing in the overflow block of given dimension compared to the regular blocksize...
constexpr const TeamSpec_t & teamspec() const
Cartesian arrangement of the Team containing the units to which this pattern's elements are mapped...
constexpr bool has_local_elements(dim_t dim, IndexType dim_offset, team_unit_t unit) const
is_local
constexpr BlockPattern(self_t &other)
Copy constructor using non-const lvalue reference parameter.
All local indices are mapped to a single logical index domain and thus not indexed blockwise...
BlockPattern(const SizeSpec_t sizespec, const DistributionSpec_t dist, const TeamSpec_t teamspec, dash::Team &team=dash::Team::All())
Constructor, initializes a pattern from explicit instances of SizeSpec, DistributionSpec, TeamSpec and a Team.
constexpr local_index_t local_index(const std::array< IndexType, NumDimensions > &g_coords) const
Resolves the unit and the local index from global coordinates.
std::array< IndexType, NumDimensions > local_coords(const std::array< IndexType, NumDimensions > &global_coords) const
Converts global coordinates to their associated unit's respective local coordinates.
local_index_t local_index(const std::array< IndexType, NumDimensions > &global_coords) const
Resolves the unit and the local index from global coordinates.
The number of blocks assigned to units may differ.
constexpr std::array< IndexType, NumDimensions > global(const std::array< IndexType, NumDimensions > &l_coords) const
Converts local coordinates of active unit to global coordinates.
Block extents are constant for every dimension.
BlockPattern(const SizeSpec_t sizespec, const DistributionSpec_t dist=DistributionSpec_t(), Team &team=dash::Team::All())
Constructor, initializes a pattern from explicit instances of SizeSpec, DistributionSpec and a Team...
SizeType underfilled_blocksize(dim_t dimension) const
Number of elements missing in the overflow block of given dimension compared to the regular blocksize...
constexpr SizeSpec_t sizespec() const
Size specification of the index space mapped by this pattern.
constexpr bool is_local(IndexType index) const
Whether the given global index is local to the unit that created this pattern instance.
size_t size() const
The number of units in this team.
constexpr std::array< IndexType, NumDimensions > coords(IndexType index) const
Convert given global linear offset (index) to global cartesian coordinates.
std::array< IndexType, NumDimensions > global(team_unit_t unit, const std::array< IndexType, NumDimensions > &local_coords) const
global
Cartesian space defined by extents in n dimensions.
constexpr SizeType local_size(team_unit_t unit=UNDEFINED_TEAM_UNIT_ID) const noexcept
The actual number of elements in this pattern that are local to the calling unit in total...
bool is_local(IndexType index, team_unit_t unit) const
Whether the given global index is local to the specified unit.
constexpr std::array< SizeType, NumDimensions > local_extents(team_unit_t unit) const
The actual number of elements in this pattern that are local to the given unit, by dimension...
constexpr 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...
constexpr team_unit_t unit_at(IndexType global_pos, const ViewSpec_t &viewspec) const
Convert given global linear index to its assigned unit id.
A Team instance specifies a subset of all available units.
constexpr IndexType capacity() const noexcept
The maximum number of elements arranged in this pattern.
team_unit_t unit_at(const std::array< IndexType, NumDimensions > &coords, const ViewSpec_t &viewspec) const
unit_at
constexpr local_index_t local(IndexType g_index) const
Converts global index to its associated unit and respective local index.
constexpr SizeSpec_t sizespec() const noexcept
Size specification of the index space mapped by this pattern.
constexpr local_index_t local_block_at(const std::array< index_type, NumDimensions > &g_coords) const
Local index of block at given global coordinates.
Defines a cartesian, totally-ordered index space by mapping linear indices to cartesian coordinates d...
pattern_mapping_properties< pattern_mapping_tag::unbalanced > mapping_properties
Satisfiable properties in pattern property category Mapping:
constexpr bool operator==(const self_t &other) const
Equality comparison operator.
constexpr IndexType lbegin() const
Resolves the global index of the first local element in the pattern.
Specifies how a Pattern distributes elements to units in a specific dimension.
BlockPattern & operator=(const BlockPattern &other)
Assignment operator.
constexpr BlockSpec_t local_blockspec() const
Cartesian arrangement of local pattern blocks.
constexpr std::array< SizeType, NumDimensions > local_extents() const
The actual number of elements in this pattern that are local to the active unit, by dimension...
Size of blocks may differ.
constexpr std::array< IndexType, NumDimensions > global(team_unit_t unit, const std::array< IndexType, NumDimensions > &local_coords) const
global
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
constexpr IndexType local_extent() const
The actual number of elements in this pattern that are local to the calling unit in the given dimensi...
static constexpr dim_t ndim()
Number of dimensions of the cartesian space partitioned by the pattern.
constexpr IndexType at(const std::array< IndexType, 1 > &g_coords, const ViewSpec_t &viewspec) const
Global coordinates and viewspec to local index.
constexpr IndexType global(IndexType l_index) const
Resolve an element's linear global index from the calling unit's local index of that element...
constexpr team_unit_t unit_at(IndexType global_pos) const
Convert given global linear index to its assigned unit id.
IndexType max_blocksize_in_range(IndexType range, SizeType num_units) const
The maximum size of a single block within an extent for a given total number of units.
constexpr dash::Team & team() const noexcept
The Team containing the units to which this pattern's elements are mapped.
constexpr 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...
constexpr IndexType lend() const
Resolves the global index past the last local element in the pattern.
constexpr IndexType num_units() const noexcept
The number of units to which this pattern's elements are mapped.
pattern_layout_properties< pattern_layout_tag::canonical, pattern_layout_tag::linear > layout_properties
Satisfiable properties in pattern property category Layout:
constexpr IndexType at(IndexType value, Values ... values) const
Global coordinates to local index.
constexpr bool is_local(IndexType index, team_unit_t unit) const
Whether the given global index is local to the specified unit.
constexpr IndexType size() const noexcept
The number of elements arranged in this pattern.
constexpr SizeType size() const noexcept
The number of discrete elements within the space spanned by the coordinate.
constexpr IndexType global_at(const std::array< IndexType, NumDimensions > &global_coords, const ViewSpec_t &viewspec) const
Convert given global coordinates and viewspec to linear global offset (index).
constexpr team_unit_t unit_at(const std::array< IndexType, NumDimensions > &coords, const ViewSpec_t &viewspec) const
unit_at
constexpr SizeType max_blocksize() const
Maximum number of elements in a single block in all dimensions.
static Team & All()
The invariant Team instance containing all available units.
see https://en.cppreference.com/w/cpp/feature_test for recommended feature tests
IndexType at(const std::array< IndexType, NumDimensions > &global_coords) const
at
constexpr local_coords_t local(const std::array< IndexType, NumDimensions > &global_coords) const
Converts global coordinates to their associated unit and its respective local coordinates.
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...
constexpr const std::array< SizeType, NumDimensions > & extents() const noexcept
Size specification of the index space mapped by this pattern.
constexpr std::array< IndexType, NumDimensions > coords(IndexType index) const noexcept
Convert given global linear offset (index) to global cartesian coordinates.
constexpr const std::array< SizeType, NumDimensions > extents() const
Size specification of the index space mapped by this pattern.
IndexType extent(dim_t dim) const
extent
constexpr dash::Team & team() const noexcept
The Team containing the units to which this pattern's elements are mapped.
DistributionSpec describes distribution patterns of all dimensions,.
Generic type of mapping properties of a model satisfying the Pattern concept.
constexpr SizeType local_size() const noexcept
The actual number of elements in this pattern that are local to the calling unit in total...
pattern_partitioning_properties< pattern_partitioning_tag::rectangular, pattern_partitioning_tag::balanced, pattern_partitioning_tag::unbalanced > partitioning_properties
Satisfiable properties in pattern property category Partitioning: