[Ktechlab-devel] Codecheck. =P

Alan Grimes agrimes at speakeasy.net
Tue Sep 29 18:56:02 UTC 2009


I followed my own advice and loaded the latest fixmes... Here's an
interesting one in a function in code that I designed, wrote, and
contributed.

Zoltan thinks I have a bug here so let's see if we can explicate the
code....

// ####################################
// behaves oddly but doesn't crash if m_a == m_b
bool QuickMatrix::partialScaleAndAdd(CUI m_a, CUI m_b, const double
scalor) {

	if(m_a >= m || m_b >= m) return false;

Here we have a basic sanity check. I tried to make the interface to my
class as "safe" as possible so it will not raise too much of a stink if
it is given silly parameters. Notice that the parameters are specified
as unsigned so that we only need to check the upper end of the range.

	const double *arow = values[m_a];
	double *brow = values[m_b];

When you want to run a loop fast, it is important to avoid repeating as
many computations as possible, so therefore we do our row
de-refferencing once before we enter our loop.


// iterate over n - m_a columns.
	for(unsigned int j = m_a; j < n; j++) // FIXME BUG: j is on X coord. ,
m_a is on Y, but they are compared. WTF?
		brow[j] += arow[j] * scalor;

So, really, what the fuck is going on here? Lets look at this function's
slightly more general cousin:

### FROM OTHER FUNCTION:
	for(unsigned int j = from; j < n; j++)
		brow[j] += arow[j] * scalor;
#####

This function is almost exactly the same except that there's an
additional parameter, "from". So the code that upset Zoltan so greatly,
was simply starting from the diagonal. This is a common requirement for
things such as Gausian elimination where the lower triangular part of
the matrix is zero (or being set to zero). So therefore we don't want to
do any more computations on that part.

So why, then, did I bother to make a separate function? Because you
could call "partialSAF(a, a, b);" and it'll do the same thing. Well the
idea was to have it suitable for use in an inner loop where every cycle
is precious, so here we spare the stack, registers, and cycles for
processing that second parameter because, in a very common case, its
redundant.


	return true;
}
// ########################################





-- 
New president: Here we go again...
Chemistry.com: A total rip-off.
Powers are not rights.





More information about the Ktechlab-devel mailing list