Skip to content

Inefficient MatrixVectorMult_DDRM.innerProduct #201

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

Open
audetto opened this issue Sep 23, 2024 · 2 comments
Open

Inefficient MatrixVectorMult_DDRM.innerProduct #201

audetto opened this issue Sep 23, 2024 · 2 comments

Comments

@audetto
Copy link

audetto commented Sep 23, 2024

I have noticed that innerProduct

for (int k = 0; k < B.numCols; k++) {
double sum = 0;
for (int i = 0; i < B.numRows; i++) {
sum += a[offsetA + i]*B.data[k + i*cols];
}
output += sum*c[offsetC + k];
}

performs a lot worse (2x minimum, but varies with size, 1000s) than my original naive implementation.

I think I figured out the reason.

The access of the matrix data likely trashes the CPU cache, because it keeps jumping column: B.data[k + i*cols], where i is incremented in the inner loop.

If I swap the loops, I get a back the lost speed.

Before I provide a PR, is there any reason this is does this way?

@lessthanoptimal
Copy link
Owner

There should be two implementations here. One for small matrices where everything is contained inside the CPU cache (where this would be faster) and another for larger matrices that's designed to reduce cache misses.

Now another question is, why does this function exist. It looks like it was not designed to be part of a public API but to perform operations inside of a block-wise algorithm. Are you using this function? I don't see it getting referenced any place so maybe it's best to delete it.

@audetto
Copy link
Author

audetto commented Mar 14, 2025

Hi, I used it because it was available in the autocompletion and it did exactly what I needed.

What is not clear from your answer is in which circumstance this implementation is optimal?
If everything stays in the cache, why is this version better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants