TiledArray  0.7.0
dense_to_sparse.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef TILEDARRAY_DENSETOSPARSE_H__INCLUDED
3 #define TILEDARRAY_DENSETOSPARSE_H__INCLUDED
4 
5 #include "../dist_array.h"
6 
7 namespace TiledArray {
8 
10 
14  template <typename Tile>
15  DistArray<Tile, SparsePolicy>
17  typedef DistArray<Tile, SparsePolicy> ArrayType; // return type
18 
19  // Constructing a tensor to hold the norm of each tile in the Dense Array
20  TiledArray::Tensor<float> tile_norms(dense_array.trange().tiles_range(), 0.0);
21 
22  const auto end = dense_array.end();
23  const auto begin = dense_array.begin();
24  for (auto it = begin; it != end; ++it) {
25  // write the norm of each local tile to the tensor
26  tile_norms[it.ordinal()] = it->get().norm();
27  }
28 
29  // Construct a sparse shape the constructor will handle communicating the
30  // norms of the local tiles to the other nodes
31  TiledArray::SparseShape<float> shape(dense_array.world(), tile_norms,
32  dense_array.trange());
33 
34  ArrayType sparse_array(dense_array.world(), dense_array.trange(),
35  shape);
36 
37  // Loop over the local dense tiles and if that tile is in the
38  // sparse_array set the sparse array tile with a clone so as not to hold
39  // a pointer to the original tile.
40  for (auto it = begin; it != end; ++it) {
41  const auto ord = it.ordinal();
42  if (!sparse_array.is_zero(ord)) {
43  sparse_array.set(ord, it->get().clone());
44  }
45  }
46 
47  return sparse_array;
48  }
49 
51  template <typename Tile>
52  DistArray<Tile, SparsePolicy>
54  return sparse_array;
55  }
56 
57 } // namespace TiledArray
58 
59 #endif /* end of include guard: TILEDARRAY_DENSETOSPARSE_H__INCLUDED */
An N-dimensional tensor object.
Definition: foreach.h:40
const trange_type & trange() const
Tiled range accessor.
Definition: dist_array.h:547
Arbitrary sparse shape.
Definition: sparse_shape.h:55
DistArray< Tile, SparsePolicy > to_sparse(DistArray< Tile, DensePolicy > const &dense_array)
Function to convert a dense array into a block sparse array.
Forward declarations.
Definition: clone.h:32
scalar_type norm() const
Vector 2-norm.
Definition: tensor.h:1391
World & world() const
World accessor.
Definition: dist_array.h:633
iterator begin()
Begin iterator factory function.
Definition: dist_array.h:288
iterator end()
End iterator factory function.
Definition: dist_array.h:304