27 #ifndef LBANN_UTILS_TENSOR_HPP 28 #define LBANN_UTILS_TENSOR_HPP 35 #include <El/core/DistMatrix/AbstractDistMatrix.hpp> 37 #include <type_traits> 45 template <
typename TDT>
51 template <
typename TDT>
53 El::AbstractDistMatrix<TDT>& tgt,
54 bool locked_view =
true);
64 template <
typename MatrixT>
67 return {El::To<size_t>(A.Width()), El::To<size_t>(A.Height())};
79 std::vector<size_t>
const& global_dims)
81 if (A.ColDist() == El::Dist::STAR) {
82 std::vector<size_t> out = global_dims;
83 out.front() = A.LocalWidth();
86 else if (global_dims.size() == 2UL) {
90 std::ostringstream oss;
92 for (
auto const& d : global_dims)
95 LBANN_WARNING(
"Dimension localization is ill-posed. Dims=", oss.str());
101 template <
typename MatrixOutT,
typename MatrixInT>
104 static MatrixOutT&
cast(MatrixInT& in) {
return in; }
105 static MatrixOutT
const&
cast(MatrixInT
const& in) {
return in; }
108 template <
typename OutDataType, El::Device D,
typename InDataType>
110 El::AbstractMatrix<InDataType>>
113 using InType = El::AbstractMatrix<InDataType>;
117 return static_cast<OutType&
>(in);
122 return static_cast<OutType const&
>(in);
126 template <
typename MatrixOutT,
typename MatrixInT>
129 using OutType = std::decay_t<MatrixOutT>;
130 using InType = std::decay_t<MatrixInT>;
135 template <
typename MatrixT>
142 template <
typename MatT>
144 : m_data{SafeMatrixCast<matrix_type&>(std::forward<MatT>(x))}
150 std::reference_wrapper<matrix_type>
m_data;
154 template <
typename MatrixT>
158 template <
typename MatT>
163 std::vector<size_t>
const& dims()
const noexcept {
return m_dims; }
164 size_t rank()
const noexcept {
return m_dims.size(); };
171 template <
typename TDT>
173 El::AbstractDistMatrix<TDT>& tgt);
176 template <
typename TDT,
183 El::DistMatrix<TDT, ColDist, RowDist, Wrap, Device>& tgt);
187 template <
typename T, El::Device D>
193 template <
typename MatT>
197 template <
typename MatT>
199 :
base_type{std::forward<MatT>(mat), dims}
203 template <
typename T, El::Device D>
210 template <
typename MatT>
214 template <
typename MatT>
216 :
base_type{std::forward<MatT>(mat), dims}
232 template <
typename T, El::Device D>
239 template <
typename MatT>
243 template <
typename MatT>
245 :
base_type{std::forward<MatT>(mat), dims},
257 template <
typename T, El::Device D>
265 template <
typename MatT>
270 template <
typename MatT>
272 :
base_type{std::forward<MatT>(mat), dims},
273 m_local_data{mat.LockedMatrix(),
290 #endif // LBANN_UTILS_TENSOR_HPP MatrixReferenceWrapper(MatT &&x)
ConstDistTensorView(MatT &&mat)
std::vector< size_t > m_dims
static MatrixOutT const & cast(MatrixInT const &in)
ConstTensorView< T, D > const & local_data() const noexcept
TensorView(MatT &&mat, std::vector< size_t > const &dims)
std::vector< size_t > localize_dims(El::AbstractDistMatrix< T > const &A, std::vector< size_t > const &global_dims)
Attempt to compute the tensor dimensions of the local portion of the matrix, given the global tensor ...
ConstTensorView(MatT &&mat, std::vector< size_t > const &dims)
ConstDistTensorView(MatT &&mat, std::vector< size_t > const &dims)
std::reference_wrapper< matrix_type > m_data
El::Matrix< OutDataType, D > OutType
constexpr El::Device Device
#define LBANN_ASSERT(cond)
Interpret a matrix as a tensor.
DistTensorView(MatT &&mat, std::vector< size_t > const &dims)
void do_tensor_copy_between_grids(const BaseDistMat &src, El::AbstractDistMatrix< TDT > &tgt)
Copy between two tensors on different process grids.
Manage a reference to a (possibly const) matrix.
ConstTensorView< T, D > m_local_data
static OutType const & cast(InType const &in)
MatrixOutT & SafeMatrixCast(MatrixInT &in)
void view_or_copy_tensor(const BaseDistMat &src, El::AbstractDistMatrix< TDT > &tgt, bool locked_view=true)
If distributed tensors have the same distribution setup the target to use a view to the source tensor...
TensorView< T, D > const & local_data() const noexcept
Access the local tensor data.
matrix_type & data() const noexcept
El::BaseDistMatrix BaseDistMat
void do_tensor_copy(const BaseDistMat &src, El::AbstractDistMatrix< TDT > &tgt)
Function to efficiently select the best method for copying between two distributed tensors...
ConstTensorView(MatT &&mat)
DistTensorView(MatT &&mat)
#define LBANN_WARNING(...)
static OutType & cast(InType &in)
TensorView< T, D > m_local_data
std::vector< size_t > get_tensor_dims(MatrixT const &A)
Interpret the matrix as a tensor and return the tensor-ized dimensions.
El::AbstractMatrix< InDataType > InType
MatrixAsTensorView(MatT &&mat, std::vector< size_t > const &dims)
::distconv::tensor::Distribution Dist
El::AbstractDistMatrix< T > matrix_type
static MatrixOutT & cast(MatrixInT &in)