Matrix mul
Tom S
h3r3tic at remove.mat.uni.torun.pl
Sun Nov 23 11:44:54 PST 2008
Andrei Alexandrescu wrote:
> 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)?
Damn, you're right too ;) I always forget whether malloc or calloc
initializes to zero. According to my tests, DMD/Tango's malloc returns
mostly zeroes in bearophile's test anyway.
--
Tomasz Stachowiak
http://h3.team0xf.com/
h3/h3r3tic on #D freenode
More information about the Digitalmars-d
mailing list