Skip to main content
GameDev.net gamedev.net
🔒 Locked

Matrix Optimization

Started by Genjix Dec 23, 2004 at 7:29 AM 19 replies 2.2k views
Original Post
Genjix
Genjix
I dont know if this is possible... but assuming I have 4 matrices A B C D , and also Z = ABCD now if A and C stay constant and B and D vary , is there anything i can do that will enable me to pre-calculate a value in terms of A and C that I could use with B and D to find Z?
dave
dave
Well IMO since ABCD are all multiplied together there doesn't seem like theren is anything u can do. Butnto be honest it also wouldn't be worth any optimisation because it is so simple.

It would only save you 1 or 2 lines of code.

EDIT: Also don't forget that the hardware is a darn sight better at these things than u are so i would suggest optimising other routines :).

Ace
Genjix
Genjix
you might say so.. but the case is for 3D transformations where (A,B are translations and C,D are rotations) in the case where B and D are applied to many multiple objects. I dont really feel like applying the same 2 universal transformations (A and C) over...
grhodes_at_work
grhodes_at_work
In general you're going to have to recalc. Let us know exactly what transformations you are applying and why. Might be a more optimum way that is specific to your use.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
ajas95
ajas95
Well, if A and B are just translations, then ABCD is equivalent to BACD, so you calculate E = AC, and then you can do your multiplications BED.
Dmytry
Dmytry
i tried to somehow derive equation to somehow put A and C together and precalc something (before you have said that A and B is just translations) , and in general case it's probably not possible.

edit:
But, you can optimize it for your special case rather easily, as ajas95 explained.

Also, it is not efficient to combine translations and rotations using 4X4 matrix multiplication, and that is used usually when you absolutely don't care about performance. You can write much more efficient matrix multiplications for special cases.
snk_kid
snk_kid
if you *must* go down this route & your using C++, there is away to remove redundant temporary objects using a technique called expression templates
Dmytry
Dmytry
"We are mature, we don't optimize!" approach is quite funny [grin]

IMO,
1:math and algorithmic optimizations practically never do any harm, and when you're doing 'em you learn something really useful about math, usually some identity that can be used also to derive things, etc. In most cases, speed difference can be several times. Matrix multiply it's 16 multiplies and 12 additions. And you can concatenate translations using 3 additions. And you can concatenate rotations using 6 additions and 9 multiplies.
Unlike processor-specific optimizations where what you learn will be outdated in next year, and that anyway can be done by compiler.

2: AP's point is *absolutely* inapplicable there, and is applicable only if you precalculate something much bigger than data you're operating on, or if precalculations is expensive.
There, precalculated E takes LESS space than A and C, and you absolutely surely increase performance if you process several things.

Just like, if you have 2 matrices A and B, and want to transform many vectors by 'em, it is much faster to use M=A*B and then transform by M than to transform each vector by B then A.

3:most of other optimization questions asked on this forum absolutely sucks. For example, people ask for SSE-optimized 4x4 matrix multiplication, and use that multiplication just only to concatenate translations and rotations, damn.
Or these sine tables. Somebody writes extremely horrible things where 99% of all sines can be removed(using various identities), and then asks on this forum "is sine tables faster".

This thread is really best optimization-related thread in this forum in last months. First time there's question not about "how to do same amount of things faster", not to replace calculations by memory lookups, but to reduce total number of operations using math theorems. It's not like sucking lookup tables or not so sucking manual SSE assembly, it's more like gaussian elimination versus usual "stupid" inverse.


Quote:
Original post by snk_kid
if you *must* go down this route & your using C++, there is away to remove redundant temporary objects using a technique called expression templates

It's probably first time ever when OP were on correct route to 'optimize' math first, to reduce total number of operations using some mathematical theorem/identity. NOT "how to do same amount of operations faster on some CPU and some language".
So, do not post that "c++ optimization" things, please. Or this thread will degenerate into low-level tricks and "hacks for compilers that sucks", versus "if you'll optimize it will be slower".

[Edited by - Dmytry on December 23, 2004 3:05:12 PM]
ajas95
ajas95
Quote:
Original post by Anonymous Poster
You may make and extraordinary effort optimizing the product, and in fact you can
But later on find the net effect is a performance decrease doe to the extra overhead to keep track of precomputed values.
It may be faster numerically but slower in resource management.


I think that's a good point. In this case, it's not really applicable... but in general, people give no concern to optimization in their designs, and so they store their data in such a way that it will be impossible to ever optimize. And then later say "Ah, well it will be pointless to optimize this math code, the bottleneck is in the memory accesses."

However, if you think a little bit about bottlenecks first, or try to write some demo code beforehand, you'll know "ah, we'll store precomputed values here, and it's important to keep these values sequential and contiguous. And store this data separate, so we won't have to branch inside the loop." The actual math optimization will just be icing on the cake.
Dmytry
Dmytry
^ Above AP was me. (edit: and to clarify: ajas95 's solution is really faster. Unlike AP's one. ) would like to know who is other AP... for some reason, i'm pretty sure he is a member
Dmytry
Dmytry
your X depends to B . You need to recalculate X for every B. Write the loop that processes many Bs and Ds. It's you are truing to confuse with your nonsense.
Dmytry
Dmytry
And i'll write some loops for final clarification:

Genjix's loop is probably something like
matrix A,C,B[many_enough],D[many_enough]; // somehow compute A,B,C,Dfor(int i=0;i<many_enough;i++){// in this loop, A and C stay constant, B and D vary.matrix Z=A*B*C*D;// somehow use Z}

- he have to process many enough B D pairs i think, and want to somehow remove something common from loop.

ajas95's optimization:
matrix A,C,B[many_enough],D[many_enough];// somehow compute A,B,C,Dmatrix E=A*C;for(int i=0;i<many_enough;i++){// in this loop, A and C stay constant, B and D vary.matrix Z=B*E*D;// somehow use Z}


And let's AP will write his "optimization" himself.
Dmytry
Dmytry
Quote:
Original post by Anonymous Poster
Dmytry, I think you are right X depend on B.
So I took a closer look to the problem. Here is my version

Which I think is almost twice as fast as your, and much simpler.

// Pseudo code for multiplying 4 special matrices
multiply4 (matrix& A, matrix& B, matrix& C, matrix& D)
{
while (oparations left)
{
// since a and b are pure translation and c is a pure rotation, then the following apply

matrix tmp_ABC = C;
tmp_ ABC .translation = A.translation + B.translation;

// multiply the matrices (only one matrix multiply and one vector add faster than Dmytry code in all counts)
matrix Z = tmp_ABC * D;

// use Z
}
}

Dmytry you seem to be an extremely pedant and paranoid person in need of attention from an audience.
You get mad and piss off when things do not go your way, and result to name calling and challenging anyone who disagrees with you. You think you are really inventing or discovering these things, maybe you need to go to a therapist so he or she can explain to you, that Quaternions, Matrices, Euler integration, Newton Raphson and it goes on and on, is all history that it been around for hundred of years.
That stuff is popular now because of the video gamming industry, but apparently there is a group of people like you that feel the need to write papers and post in forum the same stuff over and over again.

Lighten up dude you are not inventing anything and getting mad and challenging does not look good.

It is ajas95's solution, man.
(And what i said about more efficient matrix multiplication in special cases.)
Dmytry
Dmytry
Quote:
Original post by Anonymous Poster
No it is not, you have Two multiply, I only have one

ajas95's* optimization will have one multiply if we will know more implementation details, and will be able to optimize out multiply by translation, as you(?) have said.

(*Note that i do not defend "my solution")

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.