26 #ifndef TILEDARRAY_CONVERSIONS_EIGEN_H__INCLUDED 27 #define TILEDARRAY_CONVERSIONS_EIGEN_H__INCLUDED 64 template <
typename T,
typename A>
70 Eigen::RowMajor>, Eigen::AutoAlign>(tensor.
data(), m, n);
82 template <
typename T,
typename A>
88 Eigen::RowMajor>, Eigen::AutoAlign>(tensor.
data(), m, n);
99 template <
typename T,
typename A>
105 Eigen::AutoAlign>(tensor.
data(), n);
116 template <
typename T,
typename A>
122 Eigen::AutoAlign>(tensor.
data(), n);
133 template <
typename T,
typename A>
137 const auto* MADNESS_RESTRICT
const tensor_extent = tensor.
range().extent_data();
138 return eigen_map(tensor, tensor_extent[0],
139 (tensor.
range().rank() == 2u ? tensor_extent[1] : 1ul));
149 template <
typename T,
typename A>
153 const auto* MADNESS_RESTRICT
const tensor_extent = tensor.
range().extent_data();
154 return eigen_map(tensor, tensor_extent[0],
155 (tensor.
range().rank() == 2u ? tensor_extent[1] : 1ul));
171 template <
typename T,
typename A,
typename Derived>
177 const auto* MADNESS_RESTRICT
const tensor_lower = tensor.
range().lobound_data();
178 const auto* MADNESS_RESTRICT
const tensor_upper = tensor.
range().upbound_data();
179 const auto* MADNESS_RESTRICT
const tensor_extent = tensor.
range().extent_data();
181 if(tensor.
range().rank() == 2u) {
183 const std::size_t tensor_lower_0 = tensor_lower[0];
184 const std::size_t tensor_lower_1 = tensor_lower[1];
185 const std::size_t tensor_upper_0 = tensor_upper[0];
186 const std::size_t tensor_upper_1 = tensor_upper[1];
187 const std::size_t tensor_extent_0 = tensor_extent[0];
188 const std::size_t tensor_extent_1 = tensor_extent[1];
190 TA_ASSERT(tensor_upper_0 <= size_type(matrix.rows()));
191 TA_ASSERT(tensor_upper_1 <= size_type(matrix.cols()));
194 eigen_map(tensor, tensor_extent_0, tensor_extent_1) =
195 matrix.block(tensor_lower_0, tensor_lower_1,
196 tensor_extent_0, tensor_extent_1);
199 const std::size_t tensor_lower_0 = tensor_lower[0];
200 const std::size_t tensor_upper_0 = tensor_upper[0];
201 const std::size_t tensor_extent_0 = tensor_extent[0];
204 TA_ASSERT((matrix.rows() == 1) || (matrix.cols() == 1));
206 if(matrix.rows() == 1) {
207 TA_ASSERT(tensor_upper_0 <= size_type(matrix.cols()));
211 matrix.block(0, tensor_lower_0, 1, tensor_extent_0);
213 TA_ASSERT(tensor_upper_0 <= size_type(matrix.rows()));
217 matrix.block(tensor_lower_0, 0, tensor_extent_0, 1);
235 template <
typename T,
typename A,
typename Derived>
241 const auto* MADNESS_RESTRICT
const tensor_lower = tensor.
range().lobound_data();
242 const auto* MADNESS_RESTRICT
const tensor_upper = tensor.
range().upbound_data();
243 const auto* MADNESS_RESTRICT
const tensor_extent = tensor.
range().extent_data();
245 if(tensor.
range().rank() == 2) {
247 const std::size_t tensor_lower_0 = tensor_lower[0];
248 const std::size_t tensor_lower_1 = tensor_lower[1];
249 const std::size_t tensor_upper_0 = tensor_upper[0];
250 const std::size_t tensor_upper_1 = tensor_upper[1];
251 const std::size_t tensor_extent_0 = tensor_extent[0];
252 const std::size_t tensor_extent_1 = tensor_extent[1];
254 TA_ASSERT(tensor_upper_0 <= size_type(matrix.rows()));
255 TA_ASSERT(tensor_upper_1 <= size_type(matrix.cols()));
258 matrix.block(tensor_lower_0, tensor_lower_1,
259 tensor_extent_0, tensor_extent_1) =
260 eigen_map(tensor, tensor_extent_0, tensor_extent_1);
263 const std::size_t tensor_lower_0 = tensor_lower[0];
264 const std::size_t tensor_upper_0 = tensor_upper[0];
265 const std::size_t tensor_extent_0 = tensor_extent[0];
267 TA_ASSERT((matrix.rows() == 1) || (matrix.cols() == 1));
269 if(matrix.rows() == 1) {
270 TA_ASSERT(tensor_upper_0 <= size_type(matrix.cols()));
273 matrix.block(0, tensor_lower_0, 1, tensor_extent_0) =
276 TA_ASSERT(tensor_upper_0 <= size_type(matrix.rows()));
279 matrix.block(tensor_lower_0, 0, tensor_extent_0, 1) =
295 template <
typename A,
typename Derived>
297 A* array,
const typename A::size_type i, madness::AtomicInt* counter)
299 typename A::value_type tensor(array->trange().make_tile_range(i));
301 array->set(i, tensor);
312 template <
typename Derived,
typename T>
314 Eigen::MatrixBase<Derived>* matrix, madness::AtomicInt* counter)
360 template <
typename A,
typename Derived>
362 const Eigen::MatrixBase<Derived>& matrix,
bool replicated =
false)
364 typedef typename A::size_type size_type;
366 if((matrix.cols() > 1) && (matrix.rows() > 1)) {
368 "TiledArray::eigen_to_array(): The number of dimensions in trange is not equal to that of the Eigen matrix.");
369 TA_USER_ASSERT(trange.elements_range().extent(0) == size_type(matrix.rows()),
370 "TiledArray::eigen_to_array(): The number of rows in trange is not equal to the number of rows in the Eigen matrix.");
371 TA_USER_ASSERT(trange.elements_range().extent(1) == size_type(matrix.cols()),
372 "TiledArray::eigen_to_array(): The number of columns in trange is not equal to the number of columns in the Eigen matrix.");
375 "TiledArray::eigen_to_array(): The number of dimensions in trange must match that of the Eigen matrix.");
376 TA_USER_ASSERT(trange.elements_range().extent(0) == size_type(matrix.size()),
377 "TiledArray::eigen_to_array(): The size of trange must be equal to the matrix size.");
383 "An array cannot be assigned with an Eigen::Matrix when the number of World ranks is greater than 1.");
386 A array = (replicated && (world.size() > 1)
388 std::static_pointer_cast<typename A::pmap_interface>(
389 std::make_shared<detail::ReplicatedPmap>(
390 world, trange.tiles_range().volume())))
394 madness::AtomicInt counter;
397 for(std::size_t i = 0; i < array.size(); ++i) {
398 world.taskq.add(& detail::counted_eigen_submatrix_to_tensor<A, Derived>,
399 &matrix, &array, i, &counter);
404 array.world().await([&counter,n] () {
return counter == n; });
432 template <
typename Tile,
typename Policy,
433 unsigned int EigenStorageOrder = Eigen::ColMajor>
434 Eigen::Matrix<
typename Tile::value_type, Eigen::Dynamic, Eigen::Dynamic,
437 typedef Eigen::Matrix<
typename Tile::value_type, Eigen::Dynamic,
438 Eigen::Dynamic, EigenStorageOrder>
441 const auto rank = array.
trange().tiles_range().rank();
445 "TiledArray::array_to_eigen(): The array dimensions must be equal to 1 or 2.");
449 if(! array.
pmap()->is_replicated())
451 "TiledArray::array_to_eigen(): Array cannot be assigned with an Eigen::Matrix when the number of World ranks is greater than 1.");
454 const auto* MADNESS_RESTRICT
const array_extent = array.
trange().elements_range().extent_data();
456 EigenMatrix matrix = EigenMatrix::Zero(array_extent[0], (rank == 2 ? array_extent[1] : 1));
459 madness::AtomicInt counter;
462 for(std::size_t i = 0; i < array.
size(); ++i) {
464 array.
world().taskq.add(
467 array.
find(i), &matrix, &counter);
474 array.
world().await([&counter,n] () {
return counter == n; });
519 template <
typename A>
521 const typename A::value_type::value_type* buffer,
const std::size_t m,
522 const std::size_t n,
const bool replicated =
false)
525 "TiledArray::eigen_to_array(): The number of rows in trange is not equal to m.");
527 "TiledArray::eigen_to_array(): The number of columns in trange is not equal to n.");
529 typedef Eigen::Matrix<
typename A::value_type::value_type, Eigen::Dynamic,
530 Eigen::Dynamic, Eigen::RowMajor> matrix_type;
532 Eigen::AutoAlign>(buffer, m, n), replicated);
575 template <
typename A>
577 const typename A::value_type::value_type* buffer,
const std::size_t m,
578 const std::size_t n,
const bool replicated =
false)
581 "TiledArray::eigen_to_array(): The number of rows in trange is not equal to m.");
583 "TiledArray::eigen_to_array(): The number of columns in trange is not equal to n.");
585 typedef Eigen::Matrix<
typename A::value_type::value_type, Eigen::Dynamic,
586 Eigen::Dynamic, Eigen::ColMajor> matrix_type;
588 Eigen::AutoAlign>(buffer, m, n), replicated);
593 #endif // TILEDARRAY_CONVERSIONS_EIGEN_H__INCLUDED void counted_tensor_to_eigen_submatrix(const T &tensor, Eigen::MatrixBase< Derived > *matrix, madness::AtomicInt *counter)
Task function for assigning a tensor to an Eigen submatrix.
A row_major_buffer_to_array(World &world, const typename A::trange_type &trange, const typename A::value_type::value_type *buffer, const std::size_t m, const std::size_t n, const bool replicated=false)
Convert a row-major matrix buffer into an Array object.
Future< value_type > find(const Index &i) const
Find local or remote tile.
An N-dimensional tensor object.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenMatrixXd
const range_type & range() const
Tensor range object accessor.
Eigen::Matrix< long, Eigen::Dynamic, 1 > EigenVectorXl
const trange_type & trange() const
Tiled range accessor.
Eigen::Matrix< std::complex< float >, 1, Eigen::Dynamic > EigenVectorXcf
Eigen::Matrix< std::complex< double >, 1, Eigen::Dynamic > EigenVectorXcd
void tensor_to_eigen_submatrix(const Tensor< T, A > &tensor, Eigen::MatrixBase< Derived > &matrix)
Copy the content of a tensor into an Eigen matrix block.
const_pointer data() const
Data direct access.
bool is_zero(const Index &i) const
Check for zero tiles.
Eigen::Map< const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >, Eigen::AutoAlign > eigen_map(const Tensor< T, A > &tensor, const std::size_t m, const std::size_t n)
Construct a const Eigen::Map object for a given Tensor object.
Eigen::Matrix< typename Tile::value_type, Eigen::Dynamic, Eigen::Dynamic, EigenStorageOrder > array_to_eigen(const DistArray< Tile, Policy > &array)
Convert an Array object into an Eigen matrix object.
World & world() const
World accessor.
A column_major_buffer_to_array(World &world, const typename A::trange_type &trange, const typename A::value_type::value_type *buffer, const std::size_t m, const std::size_t n, const bool replicated=false)
Convert a column-major matrix buffer into an Array object.
A eigen_to_array(World &world, const typename A::trange_type &trange, const Eigen::MatrixBase< Derived > &matrix, bool replicated=false)
Convert an Eigen matrix into an Array object.
Eigen::Matrix< float, Eigen::Dynamic, 1 > EigenVectorXf
void counted_eigen_submatrix_to_tensor(const Eigen::MatrixBase< Derived > *matrix, A *array, const typename A::size_type i, madness::AtomicInt *counter)
Task function for converting Eigen submatrix to a tensor.
range_type::size_type size_type
size type
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenMatrixXf
size_type size() const
Tensor dimension size accessor.
Eigen::Matrix< int, Eigen::Dynamic, 1 > EigenVectorXi
Eigen::Matrix< double, Eigen::Dynamic, 1 > EigenVectorXd
#define TA_USER_ASSERT(a, m)
void eigen_submatrix_to_tensor(const Eigen::MatrixBase< Derived > &matrix, Tensor< T, A > &tensor)
Copy a block of an Eigen matrix into a tensor.
Eigen::Matrix< std::complex< float >, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenMatrixXcf
Eigen::Matrix< std::complex< double >, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenMatrixXcd
An N-dimensional shallow copy wrapper for tile objects.
Eigen::Matrix< long, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenMatrixXl
Eigen::Matrix< int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EigenMatrixXi
const std::shared_ptr< pmap_interface > & pmap() const
Process map accessor.