Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ohmms matrix assignment operator should be able to do a widening assigment #5145

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/Containers/OhmmsPETE/OhmmsMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2021 QMCPACK developers.
// Copyright (c) 2024 QMCPACK developers.
//
// File developed by: Ken Esler, [email protected], University of Illinois at Urbana-Champaign
// Miguel Morales, [email protected], Lawrence Livermore National Laboratory
Expand Down Expand Up @@ -35,6 +35,7 @@ class Matrix
using size_type = typename Container_t::size_type;
using iterator = typename Container_t::iterator;
using This_t = Matrix<T, Alloc>;
using Alloc_t = Alloc;

Matrix() : D1(0), D2(0), TotSize(0) {} // Default Constructor initializes to zero.

Expand Down Expand Up @@ -154,7 +155,7 @@ class Matrix
template<class T_FROM, typename ALLOC_FROM>
void assignUpperLeft(const Matrix<T_FROM, ALLOC_FROM>& from)
{
auto& this_ref = *this;
auto& this_ref = *this;
const size_type cols = std::min(this_ref.cols(), from.cols());
const size_type rows = std::min(this_ref.rows(), from.rows());
for (int i = 0; i < rows; ++i)
Expand All @@ -163,14 +164,36 @@ class Matrix
}

// Assignment Operators
/// From another Matrix with matching type.
inline This_t& operator=(const This_t& rhs)
{
resize(rhs.D1, rhs.D2);
// I don't understand why it would be desirable to have this compile
// but just do the resize and not the assigment, just seems like a surprising foot gun.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good catch. Users can abuse it for making matching matrices without the need of copying over values. Such use cases should not call operator = .
Needs a revisit when we scrutinize the operator = overloading.

if (qmc_allocator_traits<Alloc>::is_host_accessible)
assign(*this, rhs);
return *this;
}

/** From a Matrix with a narrower value_type
* so this is a widening assignment and there is no loss of precision.
* Giving it the same semantics as the matching type matrix is desirable.
*/
template<typename OtherT,
typename = std::enable_if_t<sizeof(This_t::value_type) >= sizeof(typename Matrix<OtherT>::value_type), void>>
This_t& operator=(const Matrix<OtherT>& rhs)
{
static_assert(qmc_allocator_traits<Alloc_t>::is_host_accessible &&
qmc_allocator_traits<typename Matrix<OtherT>::Alloc_t>::is_host_accessible);
resize(rhs.size1(), rhs.size2());
assign(*this, rhs);
return *this;
}

/** From any type except the above two cases that assign can resolve.
* Historically this allowed narrowing assigments from one Matrix type to another.
* I am not sure this was intentional.
*/
template<class RHS, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
This_t& operator=(const RHS& rhs)
{
Expand Down
17 changes: 16 additions & 1 deletion src/Containers/OhmmsPETE/tests/test_Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
// Copyright (c) 2024 QMCPACK developers.
//
// File developed by: Mark Dewing, [email protected], University of Illinois at Urbana-Champaign
// Peter Doak, [email protected], Oak Ridge National Lab
//
// File created by: Mark Dewing, [email protected], University of Illinois at Urbana-Champaign
//////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -109,6 +110,20 @@ TEST_CASE("matrix converting assignment", "[OhmmsPETE]")
mat_A.assignUpperLeft(mat_D);
CHECK(mat_A(1,0) == Approx(2.3));
CHECK(mat_A(1,2) == Approx(6.9));

Matrix<float> mat_too_small(2,2);
CHECK_THROWS(mat_too_small = mat_A);

Matrix<double> mat_too_small_but_larger_value_type(2,2);
mat_too_small_but_larger_value_type = mat_B;
CHECK(mat_too_small_but_larger_value_type.rows() == 3);
CHECK(mat_too_small_but_larger_value_type.cols() == 3);
CHECK(mat_too_small_but_larger_value_type(1,1) == Approx(mat_B(1,1)));
Matrix<double> too_small_but_same(2,2);
too_small_but_same = mat_A;
CHECK(too_small_but_same.rows() == 3);
CHECK(too_small_but_same.cols() == 3);
CHECK(too_small_but_same(1,1) == Approx(mat_A(1,1)));
}

} // namespace qmcplusplus
Loading