dense_to_sparse.h
Go to the documentation of this file.
1 #ifndef TILEDARRAY_CONVERSIONS_DENSETOSPARSE_H__INCLUDED
2 #define TILEDARRAY_CONVERSIONS_DENSETOSPARSE_H__INCLUDED
3 
4 #include "../dist_array.h"
5 
6 namespace TiledArray {
7 
9 
13 template <typename Tile, typename ResultPolicy = SparsePolicy,
14  typename ArgPolicy>
15 std::enable_if_t<!is_dense_v<ResultPolicy> && is_dense_v<ArgPolicy>,
16  DistArray<Tile, ResultPolicy>>
18  typedef DistArray<Tile, ResultPolicy> ArrayType; // return type
19  using ShapeType = typename ResultPolicy::shape_type;
20  using ShapeValueType = typename ShapeType::value_type;
21 
22  // Constructing a tensor to hold the norm of each tile in the Dense Array
24  dense_array.trange().tiles_range(), 0.0);
25 
26  const auto end = dense_array.end();
27  const auto begin = dense_array.begin();
28  for (auto it = begin; it != end; ++it) {
29  // write the norm of each local tile to the tensor
30  norm(it->get(), tile_norms[it.ordinal()]);
31  }
32 
33  // Construct a sparse shape the constructor will handle communicating the
34  // norms of the local tiles to the other nodes
35  ShapeType shape(dense_array.world(), tile_norms, dense_array.trange());
36 
37  ArrayType sparse_array(dense_array.world(), dense_array.trange(), shape);
38 
39  // Loop over the local dense tiles and if that tile is in the
40  // sparse_array set the sparse array tile with a clone so as not to hold
41  // a pointer to the original tile.
42  for (auto it = begin; it != end; ++it) {
43  const auto ord = it.ordinal();
44  if (!sparse_array.is_zero(ord)) {
45  sparse_array.set(ord, it->get().clone());
46  }
47  }
48 
49  return sparse_array;
50 }
51 
53 template <typename Tile, typename Policy>
54 std::enable_if_t<!is_dense_v<Policy>, DistArray<Tile, Policy>> to_sparse(
55  DistArray<Tile, Policy> const &sparse_array) {
56  return sparse_array;
57 }
58 
59 } // namespace TiledArray
60 
61 #endif /* end of include guard: \
62  TILEDARRAY_CONVERSIONS_DENSETOSPARSE_H__INCLUDED */
std::enable_if_t<!is_dense_v< ResultPolicy > &&is_dense_v< ArgPolicy >, DistArray< Tile, ResultPolicy > > to_sparse(DistArray< Tile, ArgPolicy > const &dense_array)
Function to convert a dense array into a block sparse array.
decltype(auto) norm(const Tile< Arg > &arg)
Vector 2-norm of a tile.
Definition: tile.h:1527
iterator begin()
Begin iterator factory function.
Definition: dist_array.h:484
const trange_type & trange() const
Tiled range accessor.
Definition: dist_array.h:917
Forward declarations.
Definition: dist_array.h:57
iterator end()
End iterator factory function.
Definition: dist_array.h:498
World & world() const
World accessor.
Definition: dist_array.h:1007
An N-dimensional tensor object.
Definition: tensor.h:50