|
28 | 28 |
|
29 | 29 | #include "TiledArray/array_impl.h" |
30 | 30 | #include "TiledArray/external/madness.h" |
| 31 | +#include "TiledArray/pmap/replicated_pmap.h" |
31 | 32 | #include "TiledArray/shape.h" |
32 | 33 | #include "TiledArray/type_traits.h" |
33 | 34 |
|
@@ -73,7 +74,7 @@ template <typename Array, typename Op, |
73 | 74 | typename std::enable_if<is_dense<Array>::value>::type* = nullptr> |
74 | 75 | inline Array make_array( |
75 | 76 | World& world, const detail::trange_t<Array>& trange, |
76 | | - const std::shared_ptr<const detail::pmap_t<Array> >& pmap, Op&& op) { |
| 77 | + const std::shared_ptr<const detail::pmap_t<Array>>& pmap, Op&& op) { |
77 | 78 | typedef typename Array::value_type value_type; |
78 | 79 | typedef typename value_type::range_type range_type; |
79 | 80 |
|
@@ -150,10 +151,10 @@ template <typename Array, typename Op, |
150 | 151 | typename std::enable_if<!is_dense<Array>::value>::type* = nullptr> |
151 | 152 | inline Array make_array( |
152 | 153 | World& world, const detail::trange_t<Array>& trange, |
153 | | - const std::shared_ptr<const detail::pmap_t<Array> >& pmap, Op&& op) { |
| 154 | + const std::shared_ptr<const detail::pmap_t<Array>>& pmap, Op&& op) { |
154 | 155 | typedef typename Array::value_type value_type; |
155 | 156 | typedef typename Array::ordinal_type ordinal_type; |
156 | | - typedef std::pair<ordinal_type, Future<value_type> > datum_type; |
| 157 | + typedef std::pair<ordinal_type, Future<value_type>> datum_type; |
157 | 158 |
|
158 | 159 | // Create a vector to hold local tiles |
159 | 160 | std::vector<datum_type> tiles; |
@@ -241,6 +242,41 @@ inline Array make_array(World& world, const detail::trange_t<Array>& trange, |
241 | 242 | op); |
242 | 243 | } |
243 | 244 |
|
| 245 | +/// a make_array variant that uses a sequence of tiles |
| 246 | +/// to construct a DistArray with default pmap |
| 247 | +template <typename Array, typename Tiles> |
| 248 | +Array make_array(World& world, const detail::trange_t<Array>& tiled_range, |
| 249 | + Tiles begin, Tiles end, bool replicated) { |
| 250 | + Array array; |
| 251 | + using Tuple = std::remove_reference_t<decltype(*begin)>; |
| 252 | + using Index = std::tuple_element_t<0, Tuple>; |
| 253 | + using shape_type = typename Array::shape_type; |
| 254 | + |
| 255 | + std::shared_ptr<typename Array::pmap_interface> pmap; |
| 256 | + if (replicated) { |
| 257 | + size_t ntiles = tiled_range.tiles_range().volume(); |
| 258 | + pmap = std::make_shared<detail::ReplicatedPmap>(world, ntiles); |
| 259 | + } |
| 260 | + |
| 261 | + if constexpr (shape_type::is_dense()) { |
| 262 | + array = Array(world, tiled_range, pmap); |
| 263 | + } else { |
| 264 | + std::vector<std::pair<Index, float>> tile_norms; |
| 265 | + for (Tiles it = begin; it != end; ++it) { |
| 266 | + auto [index, tile] = *it; |
| 267 | + tile_norms.push_back({index, tile.norm()}); |
| 268 | + } |
| 269 | + shape_type shape(world, tile_norms, tiled_range); |
| 270 | + array = Array(world, tiled_range, shape, pmap); |
| 271 | + } |
| 272 | + for (Tiles it = begin; it != end; ++it) { |
| 273 | + auto [index, tile] = *it; |
| 274 | + if (array.is_zero(index)) continue; |
| 275 | + array.set(index, tile); |
| 276 | + } |
| 277 | + return array; |
| 278 | +} |
| 279 | + |
244 | 280 | } // namespace TiledArray |
245 | 281 |
|
246 | 282 | #endif // TILEDARRAY_CONVERSIONS_MAKE_ARRAY_H__INCLUDED |
0 commit comments