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

Enable copy to be used with MatrixRef #1021

Merged
merged 13 commits into from
Nov 16, 2023
Merged

Conversation

albestro
Copy link
Collaborator

@albestro albestro commented Oct 27, 2023

Basic implementation (mainly copy-paste) for a copy method involving MatrixRefs.

Currently Matrix and MatrixRef are still two independent objects, and this implies that we would have to duplicate all the code for the Matrix (more or less, e.g. MatrixRef might have an offset which creates some new corner cases) the same for MatrixRef.

This represents a problem

  • from the code point of view: with duplicated code, exponential number of combinations (if A,B,C are 3 matrix-like objects there are 8 combinations of Matrix vs MatrixRef calls), trivial template solution might costs a lot in terms of compile times and binaries sizes.
  • but also for tests: should I take almost all Matrix tests and replicate them for MatrixRef?!

Nothing new, but since it does not sound useful to spend time replicating things like that, in this PR I started with just the basic implementation of the copy and as soon as we take a decision on how to proceed, I will try to apply here too. 😉

Waiting for your ideas and suggestions!

EDIT:
After a discussion with others, we opted for using copy as a "testbed" also for the sub-matrix/pipeline mechanisms, as a "real" use-case. This means that test for copy will be more "extensive" wrt other algorithms (e.g. gemm) and we will have:

  • tests for Matrix: just full matrix
  • tests for MatrixRef and Pipeline (i.e. Matrix)
    • sub-matrix
    • full-matrix

For the rest of the algorithms (so gemm included), we will just test the with MatrixRef both full and sub-matrices cases (where applicable).

@albestro albestro self-assigned this Oct 27, 2023
@albestro albestro added this to the Optimizations milestone Oct 27, 2023
@albestro albestro added the Type:New Feature New feature or request label Oct 27, 2023
@msimberg
Copy link
Collaborator

Just for the record, I will benchmark these changes: master...msimberg:DLA-Future:matrix-ref-algorithm-input-shortcut-trivial-ref. Hopefully by early next week we'll know more about if we can possibly only instantiate algorithms with MatrixRef and then you could update this PR to do the same.

@albestro albestro force-pushed the alby/matrix-ref-copy-local branch from 6e866b2 to 0fff1a9 Compare November 6, 2023 16:56
@albestro albestro marked this pull request as ready for review November 10, 2023 16:09
@albestro albestro force-pushed the alby/matrix-ref-copy-local branch from 53b00a7 to 7d46120 Compare November 13, 2023 08:47
namespace ex = pika::execution::experimental;

if constexpr (Source == Destination) {
if (&src == &dst)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know if this needs to be resolved in this PR, but since this compares MatrixRef addresses and not Matrix addresses this could in the worst case lead to deadlocks if the MatrixRefs refer to the same Matrix (read and readwrite into the same task). I think one step in the right direction would be to have operator== on MatrixRef (which compares the addresses of the contained Matrix and sub distributions), but I'm not sure if it covers all possible issues.

I'd be fine with dealing with this outside of this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a good point. I did a no-brainer copy-paste, but actually it hides a dangerous thing. How should we address this? Immediately or just open an issue? @rasolca @msimberg

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that Submatrix pipeline should be considered as well, so it is better to work on it separately.
However it should be taken care as the tridiagonal solver might be impacted by it.
(device <-> host mirror copies in the case of multicore are skipped as they are the same matrix, however after the cleanup they will be two equal ref but different objects)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks @msimberg for opening the issue!

I'll not resolve this comment hoping it will be easier in the future to spot it in case of need of reference.

Copy link
Collaborator

@rasolca rasolca left a comment

Choose a reason for hiding this comment

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

Please start to use snake_case for functions.

test/unit/matrix/test_copy.cpp Outdated Show resolved Hide resolved
test/unit/matrix/test_copy.cpp Outdated Show resolved Hide resolved
test/unit/matrix/test_copy.cpp Outdated Show resolved Hide resolved
@albestro
Copy link
Collaborator Author

cscs-ci run

@codecov-commenter
Copy link

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (3097ab4) 94.10% compared to head (b1a41bf) 94.09%.
Report is 10 commits behind head on master.

Files Patch % Lines
include/dlaf/matrix/copy.h 81.81% 1 Missing and 1 partial ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1021      +/-   ##
==========================================
- Coverage   94.10%   94.09%   -0.02%     
==========================================
  Files         146      146              
  Lines        8843     8856      +13     
  Branches     1121     1125       +4     
==========================================
+ Hits         8322     8333      +11     
- Misses        326      327       +1     
- Partials      195      196       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@albestro albestro requested a review from rasolca November 16, 2023 14:18
Comment on lines +171 to +187
template <class ElementGetter>
auto subValues(ElementGetter&& fullValues, const GlobalElementIndex& offset) {
return [fullValues, offset = sizeFromOrigin(offset)](const GlobalElementIndex& ij) {
return fullValues(ij + offset);
};
}

template <class OutsideElementGetter, class InsideElementGetter>
auto mixValues(const GlobalElementIndex& offset, const GlobalElementSize& sub_size,
InsideElementGetter&& insideValues, OutsideElementGetter&& outsideValues) {
return [outsideValues, insideValues, offset, sub_size](const GlobalElementIndex& ij) {
if (ij.isInSub(offset, sub_size))
return insideValues(ij - common::sizeFromOrigin(offset));
else
return outsideValues(ij);
};
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These helpers might be useful elsewhere, and I will probably move them in some "common" place so that they can be used in other tests too. (Probably this will happen in #969)

@albestro albestro force-pushed the alby/matrix-ref-copy-local branch from b1a41bf to 651f2f6 Compare November 16, 2023 16:11
@albestro
Copy link
Collaborator Author

cscs-ci run

rebased on new master (for getting matrix base snake_case) and addressed snake_case in implementation too.

after this it is ready to be merged 👍🏻

@rasolca rasolca merged commit 1b9b469 into master Nov 16, 2023
@rasolca rasolca deleted the alby/matrix-ref-copy-local branch November 16, 2023 17:30
github-actions bot pushed a commit that referenced this pull request Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type:New Feature New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants