mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Remove slow index check in Tensor::resize from release mode.
This commit is contained in:
@@ -303,12 +303,16 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
|
||||
|
||||
/** Normal Dimension */
|
||||
EIGEN_DEVICE_FUNC void resize(const array<Index, NumIndices>& dimensions) {
|
||||
int i;
|
||||
#ifndef EIGEN_NO_DEBUG
|
||||
Index size = Index(1);
|
||||
for (i = 0; i < NumIndices; i++) {
|
||||
for (int i = 0; i < NumIndices; i++) {
|
||||
internal::check_rows_cols_for_overflow<Dynamic, Dynamic, Dynamic>::run(size, dimensions[i]);
|
||||
size *= dimensions[i];
|
||||
}
|
||||
#else
|
||||
Index size = internal::array_prod(dimensions);
|
||||
#endif
|
||||
|
||||
#ifdef EIGEN_INITIALIZE_COEFFS
|
||||
bool size_changed = size != this->size();
|
||||
m_storage.resize(size, dimensions);
|
||||
@@ -318,15 +322,6 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
|
||||
#endif
|
||||
}
|
||||
|
||||
// Why this overload, DSizes is derived from array ??? //
|
||||
EIGEN_DEVICE_FUNC void resize(const DSizes<Index, NumIndices>& dimensions) {
|
||||
array<Index, NumIndices> dims;
|
||||
for (int i = 0; i < NumIndices; ++i) {
|
||||
dims[i] = dimensions[i];
|
||||
}
|
||||
resize(dims);
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC void resize() {
|
||||
EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE);
|
||||
// Nothing to do: rank 0 tensors have fixed size
|
||||
@@ -347,7 +342,6 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
|
||||
resize(internal::customIndices2Array<Index, NumIndices>(dimensions));
|
||||
}
|
||||
|
||||
#ifndef EIGEN_EMULATE_CXX11_META_H
|
||||
template <typename std::ptrdiff_t... Indices>
|
||||
EIGEN_DEVICE_FUNC void resize(const Sizes<Indices...>& dimensions) {
|
||||
array<Index, NumIndices> dims;
|
||||
@@ -356,16 +350,6 @@ class Tensor : public TensorBase<Tensor<Scalar_, NumIndices_, Options_, IndexTyp
|
||||
}
|
||||
resize(dims);
|
||||
}
|
||||
#else
|
||||
template <std::size_t V1, std::size_t V2, std::size_t V3, std::size_t V4, std::size_t V5>
|
||||
EIGEN_DEVICE_FUNC void resize(const Sizes<V1, V2, V3, V4, V5>& dimensions) {
|
||||
array<Index, NumIndices> dims;
|
||||
for (int i = 0; i < NumIndices; ++i) {
|
||||
dims[i] = static_cast<Index>(dimensions[i]);
|
||||
}
|
||||
resize(dims);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef EIGEN_TENSOR_PLUGIN
|
||||
#include EIGEN_TENSOR_PLUGIN
|
||||
|
||||
@@ -55,12 +55,10 @@ template <>
|
||||
struct is_input_scalar<Sizes<>> {
|
||||
static const bool value = true;
|
||||
};
|
||||
#ifndef EIGEN_EMULATE_CXX11_META_H
|
||||
template <typename std::ptrdiff_t... Indices>
|
||||
struct is_input_scalar<Sizes<Indices...>> {
|
||||
static const bool value = (Sizes<Indices...>::total_size == 1);
|
||||
static constexpr bool value = (Sizes<Indices...>::total_size == 1);
|
||||
};
|
||||
#endif
|
||||
|
||||
} // end namespace internal
|
||||
|
||||
|
||||
@@ -81,7 +81,6 @@ struct fixed_size_tensor_index_extraction_helper<Index, 0> {
|
||||
} // end namespace internal
|
||||
|
||||
// Fixed size
|
||||
#ifndef EIGEN_EMULATE_CXX11_META_H
|
||||
template <typename std::ptrdiff_t... Indices>
|
||||
struct Sizes {
|
||||
typedef internal::numeric_list<std::ptrdiff_t, Indices...> Base;
|
||||
@@ -133,87 +132,6 @@ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes<Indi
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
#else
|
||||
|
||||
template <std::ptrdiff_t n>
|
||||
struct non_zero_size {
|
||||
typedef internal::type2val<std::ptrdiff_t, n> type;
|
||||
};
|
||||
template <>
|
||||
struct non_zero_size<0> {
|
||||
typedef internal::null_type type;
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t V1 = 0, std::ptrdiff_t V2 = 0, std::ptrdiff_t V3 = 0, std::ptrdiff_t V4 = 0,
|
||||
std::ptrdiff_t V5 = 0>
|
||||
struct Sizes {
|
||||
typedef typename internal::make_type_list<typename non_zero_size<V1>::type, typename non_zero_size<V2>::type,
|
||||
typename non_zero_size<V3>::type, typename non_zero_size<V4>::type,
|
||||
typename non_zero_size<V5>::type>::type Base;
|
||||
static const std::ptrdiff_t count = Base::count;
|
||||
static const std::ptrdiff_t total_size = internal::arg_prod<Base>::value;
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t rank() const { return count; }
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t TotalSize() { return internal::arg_prod<Base>::value; }
|
||||
|
||||
Sizes() {}
|
||||
template <typename DenseIndex>
|
||||
explicit Sizes(const array<DenseIndex, Base::count>& /*indices*/) {
|
||||
// todo: add assertion
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
Sizes& operator=(const T& /*other*/) {
|
||||
// add assertion failure if the size of other is different
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename... DenseIndex>
|
||||
Sizes(DenseIndex... /*indices*/) {}
|
||||
explicit Sizes(std::initializer_list<std::ptrdiff_t>) {
|
||||
// todo: add assertion
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index operator[](const Index index) const {
|
||||
switch (index) {
|
||||
case 0:
|
||||
return internal::get<0, Base>::value;
|
||||
case 1:
|
||||
return internal::get<1, Base>::value;
|
||||
case 2:
|
||||
return internal::get<2, Base>::value;
|
||||
case 3:
|
||||
return internal::get<3, Base>::value;
|
||||
case 4:
|
||||
return internal::get<4, Base>::value;
|
||||
default:
|
||||
eigen_assert(false && "index overflow");
|
||||
return static_cast<Index>(-1);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename DenseIndex>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t IndexOfColMajor(const array<DenseIndex, Base::count>& indices) const {
|
||||
return internal::fixed_size_tensor_index_linearization_helper<DenseIndex, Base::count, Base::count, false>::run(
|
||||
indices, *reinterpret_cast<const Base*>(this));
|
||||
}
|
||||
template <typename DenseIndex>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ptrdiff_t IndexOfRowMajor(const array<DenseIndex, Base::count>& indices) const {
|
||||
return internal::fixed_size_tensor_index_linearization_helper<DenseIndex, Base::count, Base::count, true>::run(
|
||||
indices, *reinterpret_cast<const Base*>(this));
|
||||
}
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
template <std::ptrdiff_t V1, std::ptrdiff_t V2, std::ptrdiff_t V3, std::ptrdiff_t V4, std::ptrdiff_t V5>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_prod(const Sizes<V1, V2, V3, V4, V5>&) {
|
||||
return Sizes<V1, V2, V3, V4, V5>::total_size;
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
#endif
|
||||
|
||||
// Boilerplate
|
||||
namespace internal {
|
||||
template <typename Index, std::ptrdiff_t NumIndices, std::ptrdiff_t n, bool RowMajor>
|
||||
@@ -289,21 +207,12 @@ struct DSizes : array<DenseIndex, NumDims> {
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef EIGEN_EMULATE_CXX11_META_H
|
||||
template <typename std::ptrdiff_t... Indices>
|
||||
EIGEN_DEVICE_FUNC DSizes(const Sizes<Indices...>& a) {
|
||||
for (int i = 0; i < NumDims; ++i) {
|
||||
(*this)[i] = a[i];
|
||||
}
|
||||
}
|
||||
#else
|
||||
template <std::ptrdiff_t V1, std::ptrdiff_t V2, std::ptrdiff_t V3, std::ptrdiff_t V4, std::ptrdiff_t V5>
|
||||
EIGEN_DEVICE_FUNC DSizes(const Sizes<V1, V2, V3, V4, V5>& a) {
|
||||
for (int i = 0; i < NumDims; ++i) {
|
||||
(*this)[i] = a[i];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename... IndexTypes>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE explicit DSizes(DenseIndex firstDimension, DenseIndex secondDimension,
|
||||
@@ -374,7 +283,6 @@ template <typename DenseIndex, int NumDims>
|
||||
struct array_size<DSizes<DenseIndex, NumDims> > {
|
||||
static const ptrdiff_t value = NumDims;
|
||||
};
|
||||
#ifndef EIGEN_EMULATE_CXX11_META_H
|
||||
template <typename std::ptrdiff_t... Indices>
|
||||
struct array_size<const Sizes<Indices...> > {
|
||||
static const std::ptrdiff_t value = Sizes<Indices...>::count;
|
||||
@@ -392,22 +300,6 @@ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_get(const Sizes<>&) {
|
||||
eigen_assert(false && "should never be called");
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
template <std::ptrdiff_t V1, std::ptrdiff_t V2, std::ptrdiff_t V3, std::ptrdiff_t V4, std::ptrdiff_t V5>
|
||||
struct array_size<const Sizes<V1, V2, V3, V4, V5> > {
|
||||
static const ptrdiff_t value = Sizes<V1, V2, V3, V4, V5>::count;
|
||||
};
|
||||
template <std::ptrdiff_t V1, std::ptrdiff_t V2, std::ptrdiff_t V3, std::ptrdiff_t V4, std::ptrdiff_t V5>
|
||||
struct array_size<Sizes<V1, V2, V3, V4, V5> > {
|
||||
static const ptrdiff_t value = Sizes<V1, V2, V3, V4, V5>::count;
|
||||
};
|
||||
template <std::ptrdiff_t n, std::ptrdiff_t V1, std::ptrdiff_t V2, std::ptrdiff_t V3, std::ptrdiff_t V4,
|
||||
std::ptrdiff_t V5>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::ptrdiff_t array_get(const Sizes<V1, V2, V3, V4, V5>&) {
|
||||
return get<n, typename Sizes<V1, V2, V3, V4, V5>::Base>::value;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
template <typename Dims1, typename Dims2, ptrdiff_t n, ptrdiff_t m>
|
||||
struct sizes_match_below_dim {
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace Eigen {
|
||||
|
||||
template <Index n>
|
||||
struct type2index {
|
||||
static const Index value = n;
|
||||
static constexpr Index value = n;
|
||||
EIGEN_DEVICE_FUNC constexpr operator Index() const { return n; }
|
||||
EIGEN_DEVICE_FUNC void set(Index val) { eigen_assert(val == n); }
|
||||
};
|
||||
@@ -46,8 +46,8 @@ struct type2index {
|
||||
// such as IndexPairList<type2indexpair<1,2>, type2indexpair<3,4>>().
|
||||
template <Index f, Index s>
|
||||
struct type2indexpair {
|
||||
static const Index first = f;
|
||||
static const Index second = s;
|
||||
static constexpr Index first = f;
|
||||
static constexpr Index second = s;
|
||||
|
||||
constexpr EIGEN_DEVICE_FUNC operator IndexPair<Index>() const { return IndexPair<Index>(f, s); }
|
||||
|
||||
@@ -134,7 +134,7 @@ struct IndexTuple<T, O...> {
|
||||
EIGEN_DEVICE_FUNC constexpr IndexTuple() : head(), others() {}
|
||||
EIGEN_DEVICE_FUNC constexpr IndexTuple(const T& v, const O... o) : head(v), others(o...) {}
|
||||
|
||||
constexpr static int count = 1 + sizeof...(O);
|
||||
static constexpr int count = 1 + sizeof...(O);
|
||||
T head;
|
||||
IndexTuple<O...> others;
|
||||
typedef T Head;
|
||||
@@ -194,11 +194,11 @@ EIGEN_DEVICE_FUNC constexpr const typename IndexTupleExtractor<N, T, O...>::ValT
|
||||
}
|
||||
template <typename T, typename... O>
|
||||
struct array_size<IndexTuple<T, O...>> {
|
||||
static const size_t value = IndexTuple<T, O...>::count;
|
||||
static constexpr size_t value = IndexTuple<T, O...>::count;
|
||||
};
|
||||
template <typename T, typename... O>
|
||||
struct array_size<const IndexTuple<T, O...>> {
|
||||
static const size_t value = IndexTuple<T, O...>::count;
|
||||
static constexpr size_t value = IndexTuple<T, O...>::count;
|
||||
};
|
||||
|
||||
template <Index Idx, typename ValueT>
|
||||
|
||||
Reference in New Issue
Block a user