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

Optimise matrices copy when multiplying in vtkMath #3015

Open
bruyeret opened this issue Feb 8, 2024 · 2 comments
Open

Optimise matrices copy when multiplying in vtkMath #3015

bruyeret opened this issue Feb 8, 2024 · 2 comments

Comments

@bruyeret
Copy link
Contributor

bruyeret commented Feb 8, 2024

Originally posted by @finetjul in #3009 (comment)

@finetjul
Copy link
Member

image

@bruyeret
Copy link
Contributor Author

bruyeret commented Feb 12, 2024

The test with byteOffset is not enough, you also have to use byteLength
For example, if you want to multiply a matrix with a vector extracted from the matrix
The full test could be something like this:

const a = new Float32Array(25); // 5x5 matrix
const b = new Float32Array(a.buffer, 10 * 4, 5); // offset by 10 elements (4 bytes per element), take 5 elements

// Translation of the conditions:
// [ A and B are the same array ]
// OR
// [
//     A uses a buffer AND
//     B uses the same buffer AND
//     NOT ( A is completely before B OR B is completely before A )
// ]
const shouldCopy =
  a === b ||
  (a.buffer &&
    a.buffer === b.buffer &&
    !(a.byteOffset + a.byteLength <= b.byteOffset ||
      b.byteOffset + b.byteLength <= a.byteOffset));

// An equivalent, if you convert the `!(P || Q)` into a `!P && !Q`
const shouldCopyBis =
  a === b ||
  (a.buffer &&
    a.buffer === b.buffer &&
    a.byteOffset + a.byteLength > b.byteOffset &&
    b.byteOffset + b.byteLength > a.byteOffset);

This is to check if the two arrays overlap but in this particular case of matrix multiplication, you can for example use the first 16 elements of an array of size 100 as a matrix. So you could replace the x.byteLength with numberOfElements * x.BYTES_PER_ELEMENT.

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