Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/TiledArray/conversions/make_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "TiledArray/array_impl.h"
#include "TiledArray/external/madness.h"
#include "TiledArray/pmap/replicated_pmap.h"
#include "TiledArray/shape.h"
#include "TiledArray/type_traits.h"

Expand Down Expand Up @@ -73,7 +74,7 @@ template <typename Array, typename Op,
typename std::enable_if<is_dense<Array>::value>::type* = nullptr>
inline Array make_array(
World& world, const detail::trange_t<Array>& trange,
const std::shared_ptr<const detail::pmap_t<Array> >& pmap, Op&& op) {
const std::shared_ptr<const detail::pmap_t<Array>>& pmap, Op&& op) {
typedef typename Array::value_type value_type;
typedef typename value_type::range_type range_type;

Expand Down Expand Up @@ -150,10 +151,10 @@ template <typename Array, typename Op,
typename std::enable_if<!is_dense<Array>::value>::type* = nullptr>
inline Array make_array(
World& world, const detail::trange_t<Array>& trange,
const std::shared_ptr<const detail::pmap_t<Array> >& pmap, Op&& op) {
const std::shared_ptr<const detail::pmap_t<Array>>& pmap, Op&& op) {
typedef typename Array::value_type value_type;
typedef typename Array::ordinal_type ordinal_type;
typedef std::pair<ordinal_type, Future<value_type> > datum_type;
typedef std::pair<ordinal_type, Future<value_type>> datum_type;

// Create a vector to hold local tiles
std::vector<datum_type> tiles;
Expand Down Expand Up @@ -241,6 +242,41 @@ inline Array make_array(World& world, const detail::trange_t<Array>& trange,
op);
}

/// a make_array variant that uses a sequence of tiles
/// to construct a DistArray with default pmap
template <typename Array, typename Tiles>
Array make_array(World& world, const detail::trange_t<Array>& tiled_range,
Tiles begin, Tiles end, bool replicated) {
Array array;
using Tuple = std::remove_reference_t<decltype(*begin)>;
using Index = std::tuple_element_t<0, Tuple>;
using shape_type = typename Array::shape_type;

std::shared_ptr<typename Array::pmap_interface> pmap;
if (replicated) {
size_t ntiles = tiled_range.tiles_range().volume();
pmap = std::make_shared<detail::ReplicatedPmap>(world, ntiles);
}

if constexpr (shape_type::is_dense()) {
array = Array(world, tiled_range, pmap);
} else {
std::vector<std::pair<Index, float>> tile_norms;
for (Tiles it = begin; it != end; ++it) {
auto [index, tile] = *it;
tile_norms.push_back({index, tile.norm()});
}
shape_type shape(world, tile_norms, tiled_range);
array = Array(world, tiled_range, shape, pmap);
}
for (Tiles it = begin; it != end; ++it) {
auto [index, tile] = *it;
if (array.is_zero(index)) continue;
array.set(index, tile);
}
return array;
}

} // namespace TiledArray

#endif // TILEDARRAY_CONVERSIONS_MAKE_ARRAY_H__INCLUDED
33 changes: 0 additions & 33 deletions src/TiledArray/dist_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -1878,39 +1878,6 @@ auto norm2(const DistArray<Tile, Policy>& a) {
return std::sqrt(squared_norm(a));
}

template <typename Array, typename Tiles>
Array make_array(World& world, const detail::trange_t<Array>& tiled_range,
Tiles begin, Tiles end, bool replicated) {
Array array;
using Tuple = std::remove_reference_t<decltype(*begin)>;
using Index = std::tuple_element_t<0, Tuple>;
using shape_type = typename Array::shape_type;

std::shared_ptr<typename Array::pmap_interface> pmap;
if (replicated) {
size_t ntiles = tiled_range.tiles_range().volume();
pmap = std::make_shared<detail::ReplicatedPmap>(world, ntiles);
}

if constexpr (shape_type::is_dense()) {
array = Array(world, tiled_range, pmap);
} else {
std::vector<std::pair<Index, float>> tile_norms;
for (Tiles it = begin; it != end; ++it) {
auto [index, tile] = *it;
tile_norms.push_back({index, tile.norm()});
}
shape_type shape(world, tile_norms, tiled_range);
array = Array(world, tiled_range, shape, pmap);
}
for (Tiles it = begin; it != end; ++it) {
auto [index, tile] = *it;
if (array.is_zero(index)) continue;
array.set(index, tile);
}
return array;
}

template <typename T, typename P>
DistArray<T, P> replicated(const DistArray<T, P>& a) {
auto& world = a.world();
Expand Down
1 change: 1 addition & 0 deletions src/TiledArray/einsum/tiledarray.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TILEDARRAY_EINSUM_TILEDARRAY_H__INCLUDED
#define TILEDARRAY_EINSUM_TILEDARRAY_H__INCLUDED

#include "TiledArray/conversions/make_array.h"
#include "TiledArray/dist_array.h"
#include "TiledArray/einsum/index.h"
#include "TiledArray/einsum/range.h"
Expand Down