-
Notifications
You must be signed in to change notification settings - Fork 137
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
ye-luo
merged 2 commits into
QMCPACK:develop
from
PDoakORNL:OhmmsMatrix_assignment_operator
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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. | ||
|
||
|
@@ -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) | ||
|
@@ -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. | ||
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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
////////////////////////////////////////////////////////////////////////////////////// | ||
|
@@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.