Skip to content

Commit 1925853

Browse files
authored
Merge pull request #1149 from STEllAR-GROUP/add_retiling
Adding Retile for distributed arrays
2 parents f156576 + fb30001 commit 1925853

13 files changed

Lines changed: 2879 additions & 114 deletions

File tree

phylanx/plugins/dist_matrixops/dist_matrixops.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <phylanx/plugins/dist_matrixops/dist_identity.hpp>
1313
#include <phylanx/plugins/dist_matrixops/dist_random.hpp>
1414
#include <phylanx/plugins/dist_matrixops/dist_transpose_operation.hpp>
15+
#include <phylanx/plugins/dist_matrixops/retile_annotations.hpp>
1516

1617

1718
#endif
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2020 Bita Hasheminezhad
2+
// Copyright (c) 2020 Hartmut Kaiser
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#if !defined(PHYLANX_EXECUTION_TREE_PRIMITIVES_RETILE_ANNOTATIONS)
8+
#define PHYLANX_EXECUTION_TREE_PRIMITIVES_RETILE_ANNOTATIONS
9+
10+
#include <phylanx/config.hpp>
11+
#include <phylanx/execution_tree/primitives/base_primitive.hpp>
12+
#include <phylanx/execution_tree/primitives/node_data_helpers.hpp>
13+
#include <phylanx/execution_tree/primitives/primitive_component_base.hpp>
14+
15+
#include <hpx/lcos/future.hpp>
16+
17+
#include <array>
18+
#include <cstddef>
19+
#include <cstdint>
20+
#include <memory>
21+
#include <string>
22+
#include <utility>
23+
#include <vector>
24+
25+
namespace phylanx { namespace dist_matrixops { namespace primitives
26+
{
27+
class retile_annotations
28+
: public execution_tree::primitives::primitive_component_base
29+
, public std::enable_shared_from_this<retile_annotations>
30+
{
31+
public:
32+
static execution_tree::match_pattern_type const match_data;
33+
34+
retile_annotations() = default;
35+
36+
retile_annotations(execution_tree::primitive_arguments_type&& operands,
37+
std::string const& name, std::string const& codename);
38+
39+
protected:
40+
hpx::future<execution_tree::primitive_argument_type> eval(
41+
execution_tree::primitive_arguments_type const& operands,
42+
execution_tree::primitive_arguments_type const& args,
43+
execution_tree::eval_context ctx) const override;
44+
45+
private:
46+
execution_tree::primitive_argument_type retile1d(
47+
execution_tree::primitive_argument_type&& arr,
48+
std::string const& tiling_type, std::size_t intersection,
49+
std::uint32_t numtiles, ir::range&& new_tiling) const;
50+
template <typename T>
51+
execution_tree::primitive_argument_type retile1d(ir::node_data<T>&& arr,
52+
std::string const& tiling_type, std::size_t intersection,
53+
std::uint32_t numtiles, ir::range&& new_tiling,
54+
execution_tree::localities_information&& arr_localities) const;
55+
56+
execution_tree::primitive_argument_type retile2d(
57+
execution_tree::primitive_argument_type&& arr,
58+
std::string const& tiling_type,
59+
std::array<std::size_t, PHYLANX_MAX_DIMENSIONS> const& intersection,
60+
std::uint32_t numtiles, ir::range&& new_tiling) const;
61+
template <typename T>
62+
execution_tree::primitive_argument_type retile2d(ir::node_data<T>&& arr,
63+
std::string const& tiling_type,
64+
std::array<std::size_t, PHYLANX_MAX_DIMENSIONS> const& intersection,
65+
std::uint32_t numtiles, ir::range&& new_tiling,
66+
execution_tree::localities_information&& arr_localities) const;
67+
};
68+
69+
inline execution_tree::primitive create_retile_annotations(
70+
hpx::id_type const& locality,
71+
execution_tree::primitive_arguments_type&& operands,
72+
std::string const& name = "", std::string const& codename = "")
73+
{
74+
return create_primitive_component(
75+
locality, "retile_d", std::move(operands), name, codename);
76+
}
77+
}}}
78+
79+
#endif

phylanx/plugins/dist_matrixops/tile_calculation_helper.hpp

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <hpx/errors/throw_exception.hpp>
1313

14-
#include <array>
1514
#include <cstddef>
1615
#include <cstdint>
1716
#include <string>
@@ -55,6 +54,39 @@ namespace tile_calculation
5554
return std::make_tuple(start, size);
5655
}
5756

57+
inline std::tuple<std::int64_t, std::size_t> tile_calculation_overlap_1d(
58+
std::int64_t start, std::size_t size, std::size_t dim,
59+
std::size_t intersection)
60+
{
61+
if (start + size == dim)
62+
{
63+
start -= intersection;
64+
}
65+
else if (start != 0)
66+
{
67+
start -= static_cast<std::size_t>(intersection / 2);
68+
}
69+
size += intersection;
70+
71+
if (start < 0)
72+
{
73+
HPX_THROW_EXCEPTION(hpx::bad_parameter,
74+
"tile_calculation::tile_calculation_overlap_1d",
75+
phylanx::util::generate_error_message(
76+
"the given intersection produces negative start for the "
77+
"array"));
78+
}
79+
if (start + size > dim)
80+
{
81+
HPX_THROW_EXCEPTION(hpx::bad_parameter,
82+
"tile_calculation::tile_calculation_overlap_1d",
83+
phylanx::util::generate_error_message(
84+
"the given intersection need an end point larger than the "
85+
"array size"));
86+
}
87+
return std::make_tuple(start, size);
88+
}
89+
5890
///////////////////////////////////////////////////////////////////////////
5991
inline std::tuple<std::size_t, std::size_t> middle_divisors(std::size_t num)
6092
{
@@ -139,56 +171,5 @@ namespace tile_calculation
139171
}
140172
return std::make_tuple(row_start, column_start, row_size, column_size);
141173
}
142-
143-
///////////////////////////////////////////////////////////////////////////
144-
inline std::size_t extract_num_dimensions(phylanx::ir::range const& shape)
145-
{
146-
return shape.size();
147-
}
148-
149-
inline std::array<std::size_t, PHYLANX_MAX_DIMENSIONS> extract_dimensions(
150-
phylanx::ir::range const& shape)
151-
{
152-
std::array<std::size_t, PHYLANX_MAX_DIMENSIONS> result = {0};
153-
if (!shape.empty())
154-
{
155-
if (shape.size() == 1)
156-
{
157-
result[0] = extract_scalar_positive_integer_value_strict(
158-
*shape.begin());
159-
}
160-
else if (shape.size() == 2)
161-
{
162-
auto elem_1 = shape.begin();
163-
result[0] =
164-
extract_scalar_positive_integer_value_strict(*elem_1);
165-
result[1] =
166-
extract_scalar_positive_integer_value_strict(*++elem_1);
167-
}
168-
else if (shape.size() == 3)
169-
{
170-
auto elem_1 = shape.begin();
171-
result[0] =
172-
extract_scalar_positive_integer_value_strict(*elem_1);
173-
result[1] =
174-
extract_scalar_positive_integer_value_strict(*++elem_1);
175-
result[2] =
176-
extract_scalar_positive_integer_value_strict(*++elem_1);
177-
}
178-
else if (shape.size() == 4)
179-
{
180-
auto elem_1 = shape.begin();
181-
result[0] =
182-
extract_scalar_positive_integer_value_strict(*elem_1);
183-
result[1] =
184-
extract_scalar_positive_integer_value_strict(*++elem_1);
185-
result[2] =
186-
extract_scalar_positive_integer_value_strict(*++elem_1);
187-
result[3] =
188-
extract_scalar_positive_integer_value_strict(*++elem_1);
189-
}
190-
}
191-
return result;
192-
}
193174
}
194175
#endif
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) 2020 Bita Hasheminezhad
2+
// Copyright (c) 2020 Hartmut Kaiser
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#if !defined(PHYLANX_UTIL_DETAIL_RANGE_DIMENSION)
8+
#define PHYLANX_UTIL_DETAIL_RANGE_DIMENSION
9+
10+
#include <array>
11+
#include <cstddef>
12+
#include <string>
13+
14+
namespace phylanx { namespace util { namespace detail {
15+
16+
inline std::size_t extract_range_num_dimensions(
17+
phylanx::ir::range const& shape)
18+
{
19+
return shape.size();
20+
}
21+
22+
inline std::array<std::size_t, PHYLANX_MAX_DIMENSIONS>
23+
extract_positive_range_dimensions(phylanx::ir::range const& shape,
24+
std::string const& name, std::string const& codename)
25+
{
26+
std::array<std::size_t, PHYLANX_MAX_DIMENSIONS> result = {0};
27+
if (!shape.empty())
28+
{
29+
if (shape.size() == 1)
30+
{
31+
result[0] = extract_scalar_positive_integer_value_strict(
32+
*shape.begin(), name, codename);
33+
}
34+
else if (shape.size() == 2)
35+
{
36+
auto elem_1 = shape.begin();
37+
result[0] = extract_scalar_positive_integer_value_strict(
38+
*elem_1, name, codename);
39+
result[1] = extract_scalar_positive_integer_value_strict(
40+
*++elem_1, name, codename);
41+
}
42+
else if (shape.size() == 3)
43+
{
44+
auto elem_1 = shape.begin();
45+
result[0] = extract_scalar_positive_integer_value_strict(
46+
*elem_1, name, codename);
47+
result[1] = extract_scalar_positive_integer_value_strict(
48+
*++elem_1, name, codename);
49+
result[2] = extract_scalar_positive_integer_value_strict(
50+
*++elem_1, name, codename);
51+
}
52+
else if (shape.size() == 4)
53+
{
54+
auto elem_1 = shape.begin();
55+
result[0] = extract_scalar_positive_integer_value_strict(
56+
*elem_1, name, codename);
57+
result[1] = extract_scalar_positive_integer_value_strict(
58+
*++elem_1, name, codename);
59+
result[2] = extract_scalar_positive_integer_value_strict(
60+
*++elem_1, name, codename);
61+
result[3] = extract_scalar_positive_integer_value_strict(
62+
*++elem_1, name, codename);
63+
}
64+
}
65+
return result;
66+
}
67+
68+
inline std::array<std::size_t, PHYLANX_MAX_DIMENSIONS>
69+
extract_nonneg_range_dimensions(phylanx::ir::range const& shape,
70+
std::string const& name, std::string const& codename)
71+
{
72+
std::array<std::size_t, PHYLANX_MAX_DIMENSIONS> result = {0};
73+
if (!shape.empty())
74+
{
75+
if (shape.size() == 1)
76+
{
77+
result[0] = extract_scalar_nonneg_integer_value_strict(
78+
*shape.begin(), name, codename);
79+
}
80+
else if (shape.size() == 2)
81+
{
82+
auto elem_1 = shape.begin();
83+
result[0] = extract_scalar_nonneg_integer_value_strict(
84+
*elem_1, name, codename);
85+
result[1] = extract_scalar_nonneg_integer_value_strict(
86+
*++elem_1, name, codename);
87+
}
88+
else if (shape.size() == 3)
89+
{
90+
auto elem_1 = shape.begin();
91+
result[0] = extract_scalar_nonneg_integer_value_strict(
92+
*elem_1, name, codename);
93+
result[1] = extract_scalar_nonneg_integer_value_strict(
94+
*++elem_1, name, codename);
95+
result[2] = extract_scalar_nonneg_integer_value_strict(
96+
*++elem_1, name, codename);
97+
}
98+
else if (shape.size() == 4)
99+
{
100+
auto elem_1 = shape.begin();
101+
result[0] = extract_scalar_nonneg_integer_value_strict(
102+
*elem_1, name, codename);
103+
result[1] = extract_scalar_nonneg_integer_value_strict(
104+
*++elem_1, name, codename);
105+
result[2] = extract_scalar_nonneg_integer_value_strict(
106+
*++elem_1, name, codename);
107+
result[3] = extract_scalar_nonneg_integer_value_strict(
108+
*++elem_1, name, codename);
109+
}
110+
}
111+
return result;
112+
}
113+
}}}
114+
#endif

src/plugins/dist_matrixops/dist_constant.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <phylanx/plugins/dist_matrixops/dist_constant.hpp>
1515
#include <phylanx/plugins/dist_matrixops/tile_calculation_helper.hpp>
1616
#include <phylanx/execution_tree/localities_annotation.hpp>
17+
#include <phylanx/util/detail/range_dimension.hpp>
1718

1819
#include <hpx/include/lcos.hpp>
1920
#include <hpx/include/naming.hpp>
@@ -312,9 +313,9 @@ namespace phylanx { namespace dist_matrixops { namespace primitives
312313
"dimensions that is not supported"));
313314
}
314315

315-
dims =
316-
tile_calculation::extract_dimensions(overall_shape);
317-
numdims = tile_calculation::extract_num_dimensions(
316+
dims = util::detail::extract_positive_range_dimensions(
317+
overall_shape, this_->name_, this_->codename_);
318+
numdims = util::detail::extract_range_num_dimensions(
318319
overall_shape);
319320
}
320321
else if (is_numeric_operand(args[1]))
@@ -366,8 +367,9 @@ namespace phylanx { namespace dist_matrixops { namespace primitives
366367
HPX_THROW_EXCEPTION(hpx::bad_parameter,
367368
"dist_constant::eval",
368369
this_->generate_error_message(
369-
"invalid tling_type. the tiling_type can be "
370-
"one of these: `sym`, `row` or `column`"));
370+
"invalid tiling_type. The tiling_type can "
371+
"be one of these: `sym`, `row` or "
372+
"`column`"));
371373
}
372374
}
373375

src/plugins/dist_matrixops/dist_matrixops.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ PHYLANX_REGISTER_PLUGIN_FACTORY(dist_random_plugin,
2323
phylanx::dist_matrixops::primitives::dist_random::match_data)
2424
PHYLANX_REGISTER_PLUGIN_FACTORY(dist_transpose_operation_plugin,
2525
phylanx::dist_matrixops::primitives::dist_transpose_operation::match_data);
26-
26+
PHYLANX_REGISTER_PLUGIN_FACTORY(retile_annotations_plugin,
27+
phylanx::dist_matrixops::primitives::retile_annotations::match_data);

src/plugins/dist_matrixops/dist_random.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <phylanx/ir/node_data.hpp>
1515
#include <phylanx/plugins/dist_matrixops/dist_random.hpp>
1616
#include <phylanx/plugins/dist_matrixops/tile_calculation_helper.hpp>
17+
#include <phylanx/util/detail/range_dimension.hpp>
1718
#include <phylanx/util/random.hpp>
1819

1920
#include <hpx/include/lcos.hpp>
@@ -238,9 +239,10 @@ namespace phylanx { namespace dist_matrixops { namespace primitives
238239
"dimensions that is not supported"));
239240
}
240241

241-
dims = tile_calculation::extract_dimensions(shape);
242+
dims = util::detail::extract_positive_range_dimensions(
243+
shape, this_->name_, this_->codename_);
242244
numdims =
243-
tile_calculation::extract_num_dimensions(shape);
245+
util::detail::extract_range_num_dimensions(shape);
244246
}
245247
else if (is_numeric_operand(args[0]))
246248
{

0 commit comments

Comments
 (0)