11#ifndef EIGEN_TENSOR_TENSOR_CONCATENATION_H
12#define EIGEN_TENSOR_TENSOR_CONCATENATION_H
15#include "./InternalHeaderCheck.h"
20template <
typename Axis,
typename LhsXprType,
typename RhsXprType>
21struct traits<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>> {
23 using Scalar =
typename promote_storage_type<typename LhsXprType::Scalar, typename RhsXprType::Scalar>::ret;
24 using StorageKind =
typename promote_storage_type<typename traits<LhsXprType>::StorageKind,
25 typename traits<RhsXprType>::StorageKind>::ret;
27 typename promote_index_type<typename traits<LhsXprType>::Index,
typename traits<RhsXprType>::Index>::type;
28 static constexpr int NumDimensions = traits<LhsXprType>::NumDimensions;
29 static constexpr int Layout = traits<LhsXprType>::Layout;
32 std::conditional_t<Pointer_type_promotion<typename LhsXprType::Scalar, Scalar>::val,
33 typename traits<LhsXprType>::PointerType,
typename traits<RhsXprType>::PointerType>;
36template <
typename Axis,
typename LhsXprType,
typename RhsXprType>
37struct eval<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, Eigen::Dense> {
38 using type =
const TensorConcatenationOp<Axis, LhsXprType, RhsXprType>&;
48template <
typename Axis,
typename LhsXprType,
typename RhsXprType>
49class TensorConcatenationOp :
public TensorBase<TensorConcatenationOp<Axis, LhsXprType, RhsXprType>, WriteAccessors> {
52 using Scalar =
typename internal::traits<TensorConcatenationOp>::Scalar;
53 using StorageKind =
typename internal::traits<TensorConcatenationOp>::StorageKind;
54 using Index =
typename internal::traits<TensorConcatenationOp>::Index;
55 using Nested =
typename internal::ref_selector<TensorConcatenationOp>::type;
56 using CoeffReturnType =
typename internal::promote_storage_type<
typename LhsXprType::CoeffReturnType,
57 typename RhsXprType::CoeffReturnType>::ret;
60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorConcatenationOp(
const LhsXprType& lhs,
const RhsXprType& rhs, Axis axis)
61 : m_lhs_xpr(lhs), m_rhs_xpr(rhs), m_axis(axis) {}
63 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename LhsXprType::Nested>& lhsExpression()
const {
67 EIGEN_DEVICE_FUNC
const internal::remove_all_t<typename RhsXprType::Nested>& rhsExpression()
const {
71 EIGEN_DEVICE_FUNC
const Axis& axis()
const {
return m_axis; }
73 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(TensorConcatenationOp)
75 typename LhsXprType::Nested m_lhs_xpr;
76 typename RhsXprType::Nested m_rhs_xpr;
81template <
typename Axis,
typename LeftArgType,
typename RightArgType,
typename Device>
84 using Index =
typename XprType::Index;
85 static constexpr int NumDims = internal::array_size<typename TensorEvaluator<LeftArgType, Device>::Dimensions>::value;
86 static constexpr int RightNumDims =
87 internal::array_size<typename TensorEvaluator<RightArgType, Device>::Dimensions>::value;
88 using Dimensions = DSizes<Index, NumDims>;
89 using Scalar =
typename XprType::Scalar;
90 using CoeffReturnType =
typename XprType::CoeffReturnType;
91 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
92 using Storage = StorageMemory<CoeffReturnType, Device>;
93 using EvaluatorPointerType =
typename Storage::Type;
94 static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
98 TensorEvaluator<LeftArgType, Device>::PacketAccess && TensorEvaluator<RightArgType, Device>::PacketAccess,
108 PreferBlockAccess =
true,
112 using ScalarNoConst = std::remove_const_t<Scalar>;
115 using TensorBlockDesc = internal::TensorBlockDescriptor<NumDims, Index>;
116 using TensorBlockScratch = internal::TensorBlockScratchAllocator<Device>;
118 using TensorBlock =
typename internal::TensorMaterializedBlock<ScalarNoConst, NumDims, Layout, Index>;
121 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device)
122 : m_leftImpl(op.lhsExpression(), device),
123 m_rightImpl(op.rhsExpression(), device),
126 EIGEN_STATIC_ASSERT((
static_cast<int>(TensorEvaluator<LeftArgType, Device>::Layout) ==
127 static_cast<int>(TensorEvaluator<RightArgType, Device>::Layout) ||
129 YOU_MADE_A_PROGRAMMING_MISTAKE);
133 EIGEN_STATIC_ASSERT((NumDims == RightNumDims), YOU_MADE_A_PROGRAMMING_MISTAKE);
134 EIGEN_STATIC_ASSERT((NumDims > 0), YOU_MADE_A_PROGRAMMING_MISTAKE);
136 eigen_assert(0 <= m_axis && m_axis < NumDims);
137 m_leftAxisSize = m_leftImpl.dimensions()[m_axis];
138 const Dimensions& lhs_dims = m_leftImpl.dimensions();
139 const Dimensions& rhs_dims = m_rightImpl.dimensions();
142 for (; i < m_axis; ++i) {
143 eigen_assert(lhs_dims[i] >= 0);
144 eigen_assert(lhs_dims[i] == rhs_dims[i]);
145 m_dimensions[i] = lhs_dims[i];
147 eigen_assert(lhs_dims[i] >= 0);
148 eigen_assert(rhs_dims[i] >= 0);
149 m_dimensions[i] = lhs_dims[i] + rhs_dims[i];
150 for (++i; i < NumDims; ++i) {
151 eigen_assert(lhs_dims[i] >= 0);
152 eigen_assert(lhs_dims[i] == rhs_dims[i]);
153 m_dimensions[i] = lhs_dims[i];
157 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
158 m_leftStrides[0] = 1;
159 m_rightStrides[0] = 1;
160 m_outputStrides[0] = 1;
162 for (
int j = 1; j < NumDims; ++j) {
163 m_leftStrides[j] = m_leftStrides[j - 1] * lhs_dims[j - 1];
164 m_rightStrides[j] = m_rightStrides[j - 1] * rhs_dims[j - 1];
165 m_outputStrides[j] = m_outputStrides[j - 1] * m_dimensions[j - 1];
168 m_leftStrides[NumDims - 1] = 1;
169 m_rightStrides[NumDims - 1] = 1;
170 m_outputStrides[NumDims - 1] = 1;
172 for (
int j = NumDims - 2; j >= 0; --j) {
173 m_leftStrides[j] = m_leftStrides[j + 1] * lhs_dims[j + 1];
174 m_rightStrides[j] = m_rightStrides[j + 1] * rhs_dims[j + 1];
175 m_outputStrides[j] = m_outputStrides[j + 1] * m_dimensions[j + 1];
180 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Dimensions& dimensions()
const {
return m_dimensions; }
183#ifdef EIGEN_USE_THREADS
184 template <
typename EvalSubExprsCallback>
185 EIGEN_STRONG_INLINE
void evalSubExprsIfNeededAsync(EvaluatorPointerType, EvalSubExprsCallback done) {
186 m_leftImpl.evalSubExprsIfNeededAsync(
187 nullptr, [
this, done](
bool) { m_rightImpl.evalSubExprsIfNeededAsync(
nullptr, [done](
bool) { done(
true); }); });
191 EIGEN_STRONG_INLINE
bool evalSubExprsIfNeeded(EvaluatorPointerType) {
192 m_leftImpl.evalSubExprsIfNeeded(
nullptr);
194 EIGEN_TRY { m_rightImpl.evalSubExprsIfNeeded(
nullptr); }
196 m_leftImpl.cleanup();
202 EIGEN_STRONG_INLINE
void cleanup() {
203 m_leftImpl.cleanup();
204 m_rightImpl.cleanup();
207 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE internal::TensorBlockResourceRequirements getResourceRequirements()
const {
215 const size_t target_size = m_device.firstLevelCacheSize();
216 return internal::TensorBlockResourceRequirements::merge(
217 internal::TensorBlockResourceRequirements::skewed<Scalar>(target_size),
218 internal::TensorBlockResourceRequirements::merge(m_leftImpl.getResourceRequirements(),
219 m_rightImpl.getResourceRequirements()));
226 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
bool isContiguousOperandSlab(
const Dimensions& operand_dims,
227 const Dimensions& block_dims)
const {
228 static constexpr bool IsColMajor = Layout ==
static_cast<int>(
ColMajor);
229 int matching_inner_dims = 0;
230 for (
int i = 0; i < NumDims; ++i) {
231 const int dim = IsColMajor ? i : NumDims - i - 1;
232 if (operand_dims[dim] != block_dims[dim])
break;
233 ++matching_inner_dims;
236 for (
int i = matching_inner_dims + 1; i < NumDims; ++i) {
237 const int dim = IsColMajor ? i : NumDims - i - 1;
238 if (block_dims[dim] != 1)
return false;
246 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorBlock block(TensorBlockDesc& desc, TensorBlockScratch& scratch,
247 bool root_of_expr_ast =
false)
const {
248 static constexpr bool IsColMajor = Layout ==
static_cast<int>(
ColMajor);
250 if (desc.size() == 0) {
251 return TensorBlock(internal::TensorBlockKind::kView,
nullptr, desc.dimensions());
254 Index remaining = desc.offset();
255 DSizes<Index, NumDims> out_coords;
256 EIGEN_IF_CONSTEXPR (IsColMajor) {
257 for (
int i = NumDims - 1; i > 0; --i) {
258 out_coords[i] = remaining / m_outputStrides[i];
259 remaining -= out_coords[i] * m_outputStrides[i];
261 out_coords[0] = remaining;
263 for (
int i = 0; i < NumDims - 1; ++i) {
264 out_coords[i] = remaining / m_outputStrides[i];
265 remaining -= out_coords[i] * m_outputStrides[i];
267 out_coords[NumDims - 1] = remaining;
270 const Index axis_start = out_coords[m_axis];
271 const Index axis_size = desc.dimension(
static_cast<int>(m_axis));
272 const Index axis_end = axis_start + axis_size;
283 if (axis_end <= m_leftAxisSize) {
284 if (isContiguousOperandSlab(m_leftImpl.dimensions(), desc.dimensions())) {
285 Index left_src_offset = 0;
286 for (
int i = 0; i < NumDims; ++i) {
287 left_src_offset += out_coords[i] * m_leftStrides[i];
289 return TensorBlock(internal::TensorBlockKind::kView, m_leftImpl.data() + left_src_offset, desc.dimensions());
291 }
else if (axis_start >= m_leftAxisSize) {
292 if (isContiguousOperandSlab(m_rightImpl.dimensions(), desc.dimensions())) {
293 Index right_src_offset = (axis_start - m_leftAxisSize) * m_rightStrides[m_axis];
294 for (
int i = 0; i < NumDims; ++i) {
296 right_src_offset += out_coords[i] * m_rightStrides[i];
299 return TensorBlock(internal::TensorBlockKind::kView, m_rightImpl.data() + right_src_offset, desc.dimensions());
303 using TensorBlockIO = internal::TensorBlockIO<ScalarNoConst, Index, NumDims, Layout>;
304 using TensorBlockIODst =
typename TensorBlockIO::Dst;
305 using TensorBlockIOSrc =
typename TensorBlockIO::Src;
310 typename TensorBlock::Storage block_storage =
311 TensorBlock::prepareStorage(desc, scratch, root_of_expr_ast);
313 if (axis_start < m_leftAxisSize) {
314 const Index left_rows_in_block = numext::mini(m_leftAxisSize, axis_end) - axis_start;
315 DSizes<Index, NumDims> left_sub_dims = desc.dimensions();
316 left_sub_dims[m_axis] = left_rows_in_block;
318 Index left_src_offset = 0;
319 for (
int i = 0; i < NumDims; ++i) {
320 left_src_offset += out_coords[i] * m_leftStrides[i];
323 typename TensorBlockIO::Dimensions left_strides(m_leftStrides);
324 TensorBlockIOSrc src(left_strides, m_leftImpl.data(), left_src_offset);
325 TensorBlockIODst dst(left_sub_dims, block_storage.strides(), block_storage.data(),
327 TensorBlockIO::Copy(dst, src);
330 if (axis_end > m_leftAxisSize) {
331 const Index right_rows_in_block = axis_end - numext::maxi(m_leftAxisSize, axis_start);
332 DSizes<Index, NumDims> right_sub_dims = desc.dimensions();
333 right_sub_dims[m_axis] = right_rows_in_block;
337 const Index right_axis_start = numext::maxi(Index(0), axis_start - m_leftAxisSize);
338 Index right_src_offset = right_axis_start * m_rightStrides[m_axis];
339 for (
int i = 0; i < NumDims; ++i) {
341 right_src_offset += out_coords[i] * m_rightStrides[i];
346 const Index dst_axis_offset = numext::maxi(Index(0), m_leftAxisSize - axis_start);
347 const Index dst_offset = dst_axis_offset * block_storage.strides()[m_axis];
349 typename TensorBlockIO::Dimensions right_strides(m_rightStrides);
350 TensorBlockIOSrc src(right_strides, m_rightImpl.data(), right_src_offset);
351 TensorBlockIODst dst(right_sub_dims, block_storage.strides(), block_storage.data(), dst_offset);
352 TensorBlockIO::Copy(dst, src);
355 return block_storage.AsTensorMaterializedBlock();
363 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index)
const {
365 array<Index, NumDims> subs;
366 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
367 for (
int i = NumDims - 1; i > 0; --i) {
368 subs[i] = index / m_outputStrides[i];
369 index -= subs[i] * m_outputStrides[i];
373 for (
int i = 0; i < NumDims - 1; ++i) {
374 subs[i] = index / m_outputStrides[i];
375 index -= subs[i] * m_outputStrides[i];
377 subs[NumDims - 1] = index;
380 const Dimensions& left_dims = m_leftImpl.dimensions();
381 if (subs[m_axis] < left_dims[m_axis]) {
383 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
384 left_index = subs[0];
386 for (
int i = 1; i < NumDims; ++i) {
387 left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
390 left_index = subs[NumDims - 1];
392 for (
int i = NumDims - 2; i >= 0; --i) {
393 left_index += (subs[i] % left_dims[i]) * m_leftStrides[i];
396 return m_leftImpl.coeff(left_index);
398 subs[m_axis] -= left_dims[m_axis];
399 const Dimensions& right_dims = m_rightImpl.dimensions();
401 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
402 right_index = subs[0];
404 for (
int i = 1; i < NumDims; ++i) {
405 right_index += (subs[i] % right_dims[i]) * m_rightStrides[i];
408 right_index = subs[NumDims - 1];
410 for (
int i = NumDims - 2; i >= 0; --i) {
411 right_index += (subs[i] % right_dims[i]) * m_rightStrides[i];
414 return m_rightImpl.coeff(right_index);
428 template <
int LoadMode>
429 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index)
const {
430 const int packetSize = PacketType<CoeffReturnType, Device>::size;
431 EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
432 eigen_assert(index + packetSize - 1 < dimensions().TotalSize());
434 array<Index, NumDims> subs;
435 array<Index, NumDims> subs_end;
436 Index remaining = index;
437 Index remaining_end = index + packetSize - 1;
438 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
439 for (
int i = NumDims - 1; i > 0; --i) {
440 subs[i] = remaining / m_outputStrides[i];
441 remaining -= subs[i] * m_outputStrides[i];
442 subs_end[i] = remaining_end / m_outputStrides[i];
443 remaining_end -= subs_end[i] * m_outputStrides[i];
446 subs_end[0] = remaining_end;
448 for (
int i = 0; i < NumDims - 1; ++i) {
449 subs[i] = remaining / m_outputStrides[i];
450 remaining -= subs[i] * m_outputStrides[i];
451 subs_end[i] = remaining_end / m_outputStrides[i];
452 remaining_end -= subs_end[i] * m_outputStrides[i];
454 subs[NumDims - 1] = remaining;
455 subs_end[NumDims - 1] = remaining_end;
458 const Dimensions& left_dims = m_leftImpl.dimensions();
459 const Index left_axis_size = left_dims[m_axis];
461 constexpr int innermost = (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) ? 0 : NumDims - 1;
462 bool packet_in_single_inner_row =
true;
464 for (
int i = 0; i < NumDims; ++i) {
465 if (i != innermost && subs[i] != subs_end[i]) {
466 packet_in_single_inner_row =
false;
471 packet_in_single_inner_row && subs[m_axis] < left_axis_size && subs_end[m_axis] < left_axis_size;
472 const bool on_right =
473 packet_in_single_inner_row && subs[m_axis] >= left_axis_size && subs_end[m_axis] >= left_axis_size;
477 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
478 left_index = subs[0];
480 for (
int i = 1; i < NumDims; ++i) {
481 left_index += subs[i] * m_leftStrides[i];
484 left_index = subs[NumDims - 1];
486 for (
int i = NumDims - 2; i >= 0; --i) {
487 left_index += subs[i] * m_leftStrides[i];
490 return m_leftImpl.template packet<LoadMode>(left_index);
493 subs[m_axis] -= left_axis_size;
495 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
496 right_index = subs[0];
498 for (
int i = 1; i < NumDims; ++i) {
499 right_index += subs[i] * m_rightStrides[i];
502 right_index = subs[NumDims - 1];
504 for (
int i = NumDims - 2; i >= 0; --i) {
505 right_index += subs[i] * m_rightStrides[i];
508 return m_rightImpl.template packet<LoadMode>(right_index);
513 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment) CoeffReturnType values[packetSize];
515 for (
int i = 0; i < packetSize; ++i) {
516 values[i] = coeff(index + i);
518 PacketReturnType rslt = internal::pload<PacketReturnType>(values);
522 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(
bool vectorized)
const {
523 const double compute_cost = NumDims * (2 * TensorOpCost::AddCost<Index>() + 2 * TensorOpCost::MulCost<Index>() +
524 TensorOpCost::DivCost<Index>() + TensorOpCost::ModCost<Index>());
525 const double lhs_size = m_leftImpl.dimensions().TotalSize();
526 const double rhs_size = m_rightImpl.dimensions().TotalSize();
527 return (lhs_size / (lhs_size + rhs_size)) * m_leftImpl.costPerCoeff(vectorized) +
528 (rhs_size / (lhs_size + rhs_size)) * m_rightImpl.costPerCoeff(vectorized) + TensorOpCost(0, 0, compute_cost);
531 EIGEN_DEVICE_FUNC EvaluatorPointerType data()
const {
return nullptr; }
534 Dimensions m_dimensions;
535 array<Index, NumDims> m_outputStrides;
536 array<Index, NumDims> m_leftStrides;
537 array<Index, NumDims> m_rightStrides;
538 TensorEvaluator<LeftArgType, Device> m_leftImpl;
539 TensorEvaluator<RightArgType, Device> m_rightImpl;
540 const Device EIGEN_DEVICE_REF m_device;
542 Index m_leftAxisSize;
546template <
typename Axis,
typename LeftArgType,
typename RightArgType,
typename Device>
548 :
public TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device> {
549 using Base = TensorEvaluator<const TensorConcatenationOp<Axis, LeftArgType, RightArgType>, Device>;
550 using XprType = TensorConcatenationOp<Axis, LeftArgType, RightArgType>;
551 using Dimensions =
typename Base::Dimensions;
552 static constexpr int NumDims = Base::NumDims;
553 static constexpr int Layout = TensorEvaluator<LeftArgType, Device>::Layout;
557 TensorEvaluator<LeftArgType, Device>::PacketAccess && TensorEvaluator<RightArgType, Device>::PacketAccess,
560 BlockAccess = TensorEvaluator<LeftArgType, Device>::RawAccess && TensorEvaluator<RightArgType, Device>::RawAccess,
564 PreferBlockAccess =
true,
568 using ScalarNoConst = std::remove_const_t<typename XprType::Scalar>;
571 using TensorBlockDesc = internal::TensorBlockDescriptor<NumDims, typename XprType::Index>;
579 EIGEN_STRONG_INLINE TensorEvaluator(
const XprType& op,
const Device& device) : Base(op, device) {}
581 using Index =
typename XprType::Index;
582 using Scalar =
typename XprType::Scalar;
583 using CoeffReturnType =
typename XprType::CoeffReturnType;
584 using PacketReturnType =
typename PacketType<CoeffReturnType, Device>::type;
586 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType& coeffRef(Index index)
const {
587 EIGEN_STATIC_ASSERT((
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)), YOU_MADE_A_PROGRAMMING_MISTAKE);
589 array<Index, Base::NumDims> subs;
590 for (
int i = Base::NumDims - 1; i > 0; --i) {
591 subs[i] = index / this->m_outputStrides[i];
592 index -= subs[i] * this->m_outputStrides[i];
596 const Dimensions& left_dims = this->m_leftImpl.dimensions();
597 if (subs[this->m_axis] < left_dims[this->m_axis]) {
598 Index left_index = subs[0];
599 for (
int i = 1; i < Base::NumDims; ++i) {
600 left_index += (subs[i] % left_dims[i]) * this->m_leftStrides[i];
602 return this->m_leftImpl.coeffRef(left_index);
604 subs[this->m_axis] -= left_dims[this->m_axis];
605 const Dimensions& right_dims = this->m_rightImpl.dimensions();
606 Index right_index = subs[0];
607 for (
int i = 1; i < Base::NumDims; ++i) {
608 right_index += (subs[i] % right_dims[i]) * this->m_rightStrides[i];
610 return this->m_rightImpl.coeffRef(right_index);
614 template <
int StoreMode>
615 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void writePacket(Index index,
const PacketReturnType& x)
const {
616 EIGEN_STATIC_ASSERT((
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)), YOU_MADE_A_PROGRAMMING_MISTAKE);
617 const int packetSize = PacketType<CoeffReturnType, Device>::size;
618 EIGEN_STATIC_ASSERT((packetSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
619 eigen_assert(index + packetSize - 1 < this->dimensions().TotalSize());
621 EIGEN_ALIGN_TO_BOUNDARY(internal::unpacket_traits<PacketReturnType>::alignment) CoeffReturnType values[packetSize];
622 internal::pstore<CoeffReturnType, PacketReturnType>(values, x);
623 for (
int i = 0; i < packetSize; ++i) {
624 coeffRef(index + i) = values[i];
630 template <
typename TensorBlock>
631 EIGEN_STRONG_INLINE
void writeBlock(
const TensorBlockDesc& desc,
const TensorBlock& block) {
632 if (desc.size() == 0)
return;
633 eigen_assert(this->m_leftImpl.data() !=
nullptr && this->m_rightImpl.data() !=
nullptr);
635 const DSizes<Index, NumDims> block_strides = internal::strides<Layout>(desc.dimensions());
638 const ScalarNoConst* block_buffer = block.data();
640 if (block_buffer ==
nullptr) {
641 mem = this->m_device.allocate(desc.size() *
sizeof(Scalar));
642 ScalarNoConst* buf =
static_cast<ScalarNoConst*
>(mem);
644 using TensorBlockAssignment =
645 internal::TensorBlockAssignment<ScalarNoConst, NumDims, typename TensorBlock::XprType, Index>;
646 TensorBlockAssignment::Run(TensorBlockAssignment::target(desc.dimensions(), block_strides, buf), block.expr());
652 Index remaining = desc.offset();
653 DSizes<Index, NumDims> out_coords;
654 EIGEN_IF_CONSTEXPR (
static_cast<int>(Layout) ==
static_cast<int>(
ColMajor)) {
655 for (
int i = NumDims - 1; i > 0; --i) {
656 out_coords[i] = remaining / this->m_outputStrides[i];
657 remaining -= out_coords[i] * this->m_outputStrides[i];
659 out_coords[0] = remaining;
661 for (
int i = 0; i < NumDims - 1; ++i) {
662 out_coords[i] = remaining / this->m_outputStrides[i];
663 remaining -= out_coords[i] * this->m_outputStrides[i];
665 out_coords[NumDims - 1] = remaining;
668 const Index axis_start = out_coords[this->m_axis];
669 const Index axis_size = desc.dimension(
static_cast<int>(this->m_axis));
670 const Index axis_end = axis_start + axis_size;
671 const Index left_axis_size = this->m_leftAxisSize;
673 using TensorBlockIO = internal::TensorBlockIO<ScalarNoConst, Index, NumDims, Layout>;
674 using TensorBlockIODst =
typename TensorBlockIO::Dst;
675 using TensorBlockIOSrc =
typename TensorBlockIO::Src;
677 if (axis_start < left_axis_size) {
678 DSizes<Index, NumDims> left_sub_dims = desc.dimensions();
679 left_sub_dims[this->m_axis] = numext::mini(left_axis_size, axis_end) - axis_start;
681 Index left_dst_offset = 0;
682 for (
int i = 0; i < NumDims; ++i) {
683 left_dst_offset += out_coords[i] * this->m_leftStrides[i];
686 TensorBlockIOSrc src(block_strides, block_buffer, 0);
687 TensorBlockIODst dst(left_sub_dims,
typename TensorBlockIO::Dimensions(this->m_leftStrides),
688 this->m_leftImpl.data(), left_dst_offset);
689 TensorBlockIO::Copy(dst, src);
692 if (axis_end > left_axis_size) {
693 DSizes<Index, NumDims> right_sub_dims = desc.dimensions();
694 right_sub_dims[this->m_axis] = axis_end - numext::maxi(left_axis_size, axis_start);
696 const Index right_axis_start = numext::maxi(Index(0), axis_start - left_axis_size);
697 Index right_dst_offset = right_axis_start * this->m_rightStrides[this->m_axis];
698 for (
int i = 0; i < NumDims; ++i) {
699 if (i != this->m_axis) {
700 right_dst_offset += out_coords[i] * this->m_rightStrides[i];
705 const Index src_offset = numext::maxi(Index(0), left_axis_size - axis_start) * block_strides[this->m_axis];
707 TensorBlockIOSrc src(block_strides, block_buffer, src_offset);
708 TensorBlockIODst dst(right_sub_dims,
typename TensorBlockIO::Dimensions(this->m_rightStrides),
709 this->m_rightImpl.data(), right_dst_offset);
710 TensorBlockIO::Copy(dst, src);
714 if (mem !=
nullptr) this->m_device.deallocate(mem);
The tensor base class.
Definition TensorForwardDeclarations.h:69
Tensor concatenation class.
Definition TensorConcatenation.h:49
Namespace containing all symbols from the Eigen library.
The tensor evaluator class.
Definition TensorEvaluator.h:47