Skip to content
13 changes: 12 additions & 1 deletion cmake/modules/FindEigen3.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ if(NOT Eigen3_FIND_VERSION)
endif(NOT Eigen3_FIND_VERSION)

macro(_eigen3_check_version)
file(READ "${EIGEN3_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _eigen3_version_header)
# Try the Eigen 5.x location first
if(EXISTS "${EIGEN3_INCLUDE_DIR}/Eigen/Version")
file(READ "${EIGEN3_INCLUDE_DIR}/Eigen/Version" _eigen3_version_header)
# Fall back to Eigen 3.x location
elseif(EXISTS "${EIGEN3_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h")
file(READ "${EIGEN3_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _eigen3_version_header)
else()
# Could not locate any known Eigen version header; mark version as not OK and return.
set(EIGEN3_VERSION_OK FALSE)
message(STATUS "Could not find Eigen version header under ${EIGEN3_INCLUDE_DIR}")
return()
endif()

string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen3_world_version_match "${_eigen3_version_header}")
set(EIGEN3_WORLD_VERSION "${CMAKE_MATCH_1}")
Expand Down
4 changes: 4 additions & 0 deletions src/TiledArray/external/eigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ TILEDARRAY_PRAGMA_GCC(diagnostic pop)
#endif
#endif // EIGEN_USE_LAPACKE || EIGEN_USE_LAPACKE_STRICT

#if EIGEN_VERSION_AT_LEAST(3, 5, 0)
#define EIGEN_HAS_MAJOR_VERSION_5 1
#endif

TILEDARRAY_PRAGMA_GCC(diagnostic pop)

namespace madness {
Expand Down
20 changes: 17 additions & 3 deletions src/TiledArray/tensor/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ class Tensor {
: range_(range), nbatch_(nbatch) {
size_t size = range_.volume() * nbatch;
allocator_type allocator;
#if EIGEN_HAS_MAJOR_VERSION_5
// Eigen >=3.5.0 returns nullptr if size=0
auto* ptr = (!size && range_.rank())
? static_cast<pointer>(std::malloc(size))
: allocator.allocate(size);
#else
auto* ptr = allocator.allocate(size);
#endif
// default construct elements of data only if can have any effect ...
if constexpr (!std::is_trivially_default_constructible_v<T>) {
// .. and requested
Expand Down Expand Up @@ -226,7 +233,14 @@ class Tensor {
: range_(std::move(range)), nbatch_(nbatch) {
size_t size = range_.volume() * nbatch;
allocator_type allocator;
#if EIGEN_HAS_MAJOR_VERSION_5
// Eigen >=3.5.0 returns nullptr if size=0
auto* ptr = (!size && range_.rank())
? static_cast<pointer>(std::malloc(size))
: allocator.allocate(size);
#else
auto* ptr = allocator.allocate(size);
#endif
// default construct elements of data only if can have any effect ...
if constexpr (!std::is_trivially_default_constructible_v<T>) {
// .. and requested
Expand Down Expand Up @@ -1186,10 +1200,10 @@ class Tensor {
bool empty = this->empty();
auto range = this->range_;
auto nbatch = this->nbatch_;
ar & empty;
ar& empty;
if (!empty) {
ar & range;
ar & nbatch;
ar& range;
ar& nbatch;
if constexpr (madness::is_input_archive_v<Archive>) {
*this = Tensor(std::move(range), nbatch, default_construct{true});
}
Expand Down
4 changes: 2 additions & 2 deletions src/TiledArray/tensor/tensor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class TensorInterface {
/// \param data The data pointer for this tensor
TensorInterface(const range_type& range, pointer data)
: range_(range), data_(data) {
TA_ASSERT(data);
TA_ASSERT(data_);
}

/// Construct a new view of \c tensor
Expand All @@ -188,7 +188,7 @@ class TensorInterface {
/// \param data The data pointer for this tensor
TensorInterface(range_type&& range, pointer data)
: range_(std::move(range)), data_(data) {
TA_ASSERT(data);
TA_ASSERT(data_);
}

template <typename T1, typename std::enable_if<detail::is_nested_tensor<
Expand Down
35 changes: 19 additions & 16 deletions tests/sparse_tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,31 @@ class EigenSparseTile {

/// makes an uninitialized matrix
explicit EigenSparseTile(const range_type& r)
: impl_(std::make_shared<impl_type>(
std::make_tuple(matrix_type(r.extent()[0], r.extent()[1]), r))) {
TA_ASSERT(r.extent()[0] > 0);
TA_ASSERT(r.extent()[1] > 0);
}
: impl_([&]() {
TA_ASSERT(r.extent()[0] > 0);
TA_ASSERT(r.extent()[1] > 0);
return std::make_shared<impl_type>(
std::make_tuple(matrix_type(r.extent()[0], r.extent()[1]), r));
}()) {}

/// ctor using sparse matrix
EigenSparseTile(matrix_type&& mat, const range_type& range)
: impl_(std::make_shared<impl_type>(
std::make_tuple(std::move(mat), range))) {
using extent_type = typename range_type::extent_type::value_type;
TA_ASSERT(static_cast<extent_type>(mat.rows()) == range.extent()[0]);
TA_ASSERT(static_cast<extent_type>(mat.cols()) == range.extent()[1]);
}
: impl_([&]() {
using extent_type = typename range_type::extent_type::value_type;
TA_ASSERT(static_cast<extent_type>(mat.rows()) == range.extent()[0]);
TA_ASSERT(static_cast<extent_type>(mat.cols()) == range.extent()[1]);
return std::make_shared<impl_type>(
std::make_tuple(std::move(mat), range));
}()) {}

/// ctor using sparse matrix
EigenSparseTile(const matrix_type& mat, const range_type& range)
: impl_(std::make_shared<impl_type>(std::make_tuple(mat, range))) {
using extent_type = typename range_type::extent_type::value_type;
TA_ASSERT(static_cast<extent_type>(mat.rows()) == range.extent()[0]);
TA_ASSERT(static_cast<extent_type>(mat.cols()) == range.extent()[1]);
}
: impl_([&]() {
using extent_type = typename range_type::extent_type::value_type;
TA_ASSERT(static_cast<extent_type>(mat.rows()) == range.extent()[0]);
TA_ASSERT(static_cast<extent_type>(mat.cols()) == range.extent()[1]);
return std::make_shared<impl_type>(std::make_tuple(mat, range));
}()) {}

// Deep copy
EigenSparseTile clone() const {
Expand Down
Loading