Matrix mul

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Nov 23 05:36:15 PST 2008


Tom S wrote:
> Don wrote:
>> bearophile wrote:
>>> While writing code that works on matrices I have found something 
>>> curious...
>>
>> Here's what I think is going on. AFAIK, D hasn't got any special code 
>> for initializing jagged arrays. So
>>         auto A = new double[][](N, N);
>> involves N+1 memory allocations.
>> As well as being slow, this is going to give horrible locality of 
>> reference in the memory allocation. You'll suffer cache misses on 
>> almost every array access!
>>
>> I don't think this has anything to do with bounds checking.
> 
> Nah, it's about NaNs :)
> 
> Version #1 initializes C to NaN, Version #2 initializes it to 0. The 
> 'init mats randomly' loop doesn't touch C at all, thus all the latter 
> additions leave C at NaN, causing lots of FP exceptions.
> 
> We actually had something like this happen in Deadlock a few times in 
> the animation subsystem. Brought the framerate down to a single digit :D

I swear I wrote my previous message before reading this :o). Damn you're 
right. I hadn't seen the bug in initialization. But since version #2 
uses malloc, isn't that memory garbage (possibly, but not necessarily, 
zero)?

Andrei



More information about the Digitalmars-d mailing list