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

Copy device_matrix_data into arrays #1476

Merged
merged 5 commits into from
Dec 8, 2023
Merged

Conversation

MarcelKoch
Copy link
Member

@MarcelKoch MarcelKoch commented Nov 28, 2023

This PR changes the behavior for the read(const device_matrix_data&) overload for Coo and Csr.

Currently, the const & read overload copies the input device_matrix_data and moves it into the && overload. The && overload will cause the internal arrays of the matrix to use the arrays of the device_matrix_data. This means that if the matrix was created from views, the matrix after the read will have owning arrays.

For the && overload this is justified, since here the matrix takes ownership of the input data. But for the const & it's unexpected, since no ownership is transferred. So, this PR makes the const & copy the input device matrix data directly into the matrix's arrays. The ownership property of the internal arrays will thus be preserved.

Note: The behavior of the && overload is a bit inconsistent across our matrix types. For Coo and Csr the && overload will result in the matrix arrays having ownership. But for all other matrix types, the ownership property is not changed. Maybe we should consolidate that. I was under the impression that device_matrix_data always owns its arrays. But that is not the case. The device_matrix_data can also be constructed from array, which might be views.

@MarcelKoch MarcelKoch added 1:ST:do-not-merge Please do not merge PR this yet. 1:ST:ready-for-review This PR is ready for review labels Nov 28, 2023
@MarcelKoch MarcelKoch self-assigned this Nov 28, 2023
@ginkgo-bot ginkgo-bot added reg:testing This is related to testing. mod:core This is related to the core module. type:matrix-format This is related to the Matrix formats labels Nov 28, 2023
@MarcelKoch MarcelKoch changed the title Copy into device_matrix_data into arrays Copy device_matrix_data into arrays Nov 28, 2023
@MarcelKoch
Copy link
Member Author

This fixes an issue in #1450

core/matrix/csr.cpp Outdated Show resolved Hide resolved
core/matrix/coo.cpp Outdated Show resolved Hide resolved
core/matrix/coo.cpp Outdated Show resolved Hide resolved
core/matrix/csr.cpp Outdated Show resolved Hide resolved
include/ginkgo/core/base/array.hpp Outdated Show resolved Hide resolved
include/ginkgo/core/base/array.hpp Outdated Show resolved Hide resolved
Copy link
Member

@upsj upsj left a comment

Choose a reason for hiding this comment

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

LGTM!

include/ginkgo/core/base/exception_helpers.hpp Outdated Show resolved Hide resolved
@MarcelKoch MarcelKoch force-pushed the correctly-copy-matrix-read branch 2 times, most recently from 6e43034 to 60c2269 Compare November 28, 2023 15:59
@MarcelKoch
Copy link
Member Author

format!

@MarcelKoch
Copy link
Member Author

Another question: how should const matrix_data& be handled. Currently it calls device_matrix_data&& internally, so it moves into the stored arrays. Should that stay, or also call const device_matrix_data&?

@upsj
Copy link
Member

upsj commented Nov 30, 2023

This only applies to Csr and Coo only, right? In that case, I would suggest calling aos_to_soa directly onto the resized arrays inside the matrix, i.e. combining the copy with the transformation

Copy link
Member

@upsj upsj left a comment

Choose a reason for hiding this comment

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

LGTM for the most part, only some test assertions are throwing instead of failing the test directly.

core/matrix/coo.cpp Outdated Show resolved Hide resolved
core/matrix/csr.cpp Show resolved Hide resolved
core/matrix/csr.cpp Outdated Show resolved Hide resolved
core/test/matrix/coo.cpp Outdated Show resolved Hide resolved
core/test/matrix/coo.cpp Outdated Show resolved Hide resolved
core/test/matrix/coo.cpp Show resolved Hide resolved
core/test/matrix/csr.cpp Outdated Show resolved Hide resolved
core/test/matrix/csr.cpp Outdated Show resolved Hide resolved
core/test/matrix/csr.cpp Show resolved Hide resolved
include/ginkgo/core/base/array.hpp Outdated Show resolved Hide resolved
@MarcelKoch MarcelKoch force-pushed the correctly-copy-matrix-read branch 4 times, most recently from b86c726 to 5ae2e93 Compare December 5, 2023 13:22
Copy link
Member

@pratikvn pratikvn left a comment

Choose a reason for hiding this comment

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

Macro change and usage of this->

core/matrix/coo.cpp Outdated Show resolved Hide resolved
core/matrix/csr.cpp Outdated Show resolved Hide resolved
core/matrix/csr.cpp Outdated Show resolved Hide resolved
core/matrix/csr.cpp Outdated Show resolved Hide resolved
include/ginkgo/core/base/exception_helpers.hpp Outdated Show resolved Hide resolved
include/ginkgo/core/base/array.hpp Show resolved Hide resolved
@MarcelKoch MarcelKoch requested a review from upsj December 6, 2023 08:25
core/test/base/array.cpp Outdated Show resolved Hide resolved
@pratikvn
Copy link
Member

pratikvn commented Dec 6, 2023

Also a general question: Is a macro part of the public interface and if so does removing a macro mean breaking interface ?

Copy link
Member

@pratikvn pratikvn left a comment

Choose a reason for hiding this comment

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

Thanks for updating this! LGTM!

Copy link
Member

@upsj upsj left a comment

Choose a reason for hiding this comment

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

LGTM! The macro is part of our public headers, but to me, it's only intended for internal usage, so I'm fine with removing it. There is no such thing as a detail macro after all.

include/ginkgo/core/base/array.hpp Outdated Show resolved Hide resolved
@MarcelKoch MarcelKoch added 1:ST:ready-to-merge This PR is ready to merge. and removed 1:ST:do-not-merge Please do not merge PR this yet. 1:ST:ready-for-review This PR is ready for review labels Dec 7, 2023
Copy link
Member

@yhmtsai yhmtsai left a comment

Choose a reason for hiding this comment

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

I do not think the device_matrix_data view means the matrix also be a view on it.
If user want the Coo view on the device_matrix_data view, they can use the Coo constructor rather than the read

include/ginkgo/core/base/array.hpp Outdated Show resolved Hide resolved
core/matrix/coo.cpp Outdated Show resolved Hide resolved
core/test/base/array.cpp Show resolved Hide resolved
core/test/base/array.cpp Show resolved Hide resolved
Comment on lines +205 to +209
auto row_idxs = gko::array<index_type>(this->exec, 4);
auto col_idxs = gko::array<index_type>(this->exec, 4);
auto values = gko::array<value_type>(this->exec, 4);
auto m = Mtx::create(this->exec, gko::dim<2>{2, 3}, values.as_view(),
col_idxs.as_view(), row_idxs.as_view());
Copy link
Member

Choose a reason for hiding this comment

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

if the view is larger than read size?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will add tests to check that is throws.

core/test/matrix/csr.cpp Outdated Show resolved Hide resolved
core/test/matrix/csr.cpp Show resolved Hide resolved
include/ginkgo/core/base/array.hpp Show resolved Hide resolved
include/ginkgo/core/base/exception_helpers.hpp Outdated Show resolved Hide resolved
Comment on lines +433 to +435
// the device matrix data contains views on the column indices
// and values array of this matrix, and an owning array for the
// row indices (which doesn't exist in this matrix)
Copy link
Member

Choose a reason for hiding this comment

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

It introduces the inconsistence.

Copy link
Member

Choose a reason for hiding this comment

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

we had the same view + owning split previously when moving from a device_matrix_data, because row pointers need to be allocated separately in any case.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, discussed something related to this with @MarcelKoch. My question is not introduced in this pr.
I will create another issue and we can discuss the reason there.

@MarcelKoch
Copy link
Member Author

I do not think the device_matrix_data view means the matrix also be a view on it.
If user want the Coo view on the device_matrix_data view, they can use the Coo constructor rather than the read

I'm not understanding this. Do you want to see something changed?

Comment on lines +341 to +380
ASSERT_EQ(orig_row_idxs, m->get_row_idxs());
ASSERT_EQ(orig_col_idxs, m->get_col_idxs());
ASSERT_EQ(orig_values, m->get_values());
Copy link
Member

Choose a reason for hiding this comment

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

matrix array view is now to use the device_data view not the original one

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, but device_data is empty after the move, so we need to store the pointers beforehand.

@MarcelKoch MarcelKoch force-pushed the correctly-copy-matrix-read branch 2 times, most recently from 24272c3 to a773e16 Compare December 7, 2023 11:29
Comment on lines +433 to +435
// the device matrix data contains views on the column indices
// and values array of this matrix, and an owning array for the
// row indices (which doesn't exist in this matrix)
Copy link
Member

Choose a reason for hiding this comment

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

Yes, discussed something related to this with @MarcelKoch. My question is not introduced in this pr.
I will create another issue and we can discuss the reason there.

include/ginkgo/core/base/array.hpp Show resolved Hide resolved
include/ginkgo/core/base/array.hpp Outdated Show resolved Hide resolved
include/ginkgo/core/base/array.hpp Outdated Show resolved Hide resolved
EXPECT_EQ(view.get_data()[2], TypeParam{3});
EXPECT_EQ(view.get_size(), 3);
EXPECT_EQ(const_view3.get_size(), 3);
ASSERT_THROW(view = const_view2, gko::ValueMismatch);
Copy link
Member

Choose a reason for hiding this comment

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

if you use GKO_ENSURE_COMPATIBLE_BOUNDS, then it is accepted.

MarcelKoch and others added 5 commits December 8, 2023 07:48
Co-authored-by: Marcel Koch <[email protected]>
- ensure equal bounds
- don't make row_ptrs owning
- disable conversion assignment
- add in-code comment
- fix tests
- use this->
- add tests for incompatible reads

Co-authored-by: Tobias Ribizel <[email protected]>
Co-authored-by: Pratik Nayak <[email protected]>
Co-authored-by: Yu-Hsiang M. Tsai <[email protected]>
@MarcelKoch MarcelKoch merged commit a206ab6 into develop Dec 8, 2023
13 of 15 checks passed
@MarcelKoch MarcelKoch deleted the correctly-copy-matrix-read branch December 8, 2023 15:29
Copy link

sonarcloud bot commented Dec 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

95.2% 95.2% Coverage
0.0% 0.0% Duplication

warning The version of Java (11.0.3) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
Read more here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1:ST:ready-to-merge This PR is ready to merge. mod:core This is related to the core module. reg:testing This is related to testing. type:matrix-format This is related to the Matrix formats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants