[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: vectorization help
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Re: vectorization help |
Date: |
Mon, 3 Oct 2011 23:48:15 -0500 |
On 3 October 2011 18:26, Liam Groener <address@hidden> wrote:
> On Oct 3, 2011, at 12:10 PM, William Krekeler wrote:
>
> Thanks for the useful and very informative response. That definitely works.
> I'll have to read more on broadcasting to see if I can apply it elsewhere.
>
> My code has to be cross-compatible with ML. As an FYI the bsxfun version
> will work in matlab 7.8.0.347:
> Cube = bsxfun(@rdivide, permute(RC, [1, 3, 2]), Cube);
>
> I'm curious; does this actually save any significant time over your for loop
> version?
Actually, it doesn't. It's in fact slower! But I don't want you to
believe this means "broadcasting is slower". In fact, we have a
problem with the implementation of bsxfun right now, which I want to
fix. It turns out the order of the dimensions affects the speed:
rc = rand( 1, 1, 448 );
cube = rand( 160, 512, 448 );
p = perms([1,2,3]);
for i = 1:rows(p)
tic
x = bsxfun(@rdivide, permute(rc,p(i,:)), permute(cube, p(i,:)));
toc
endfor
I have investigated the problem, and I'm convinced that the problem is
the number of function calls to divide changes depending on the
dimensions of the inputs. I will work on fixing this over the next few
days. Looks like a fun problem.
But this doesn't mean "don't use bsxfun". This slowness is a minor
problem that we can fix in the implementation, which is another reason
to vectorise code: when the code is vectorised, it's much easier to
optimise in Octave's C++ implementation, so you can gain future
optimisation if you vectorise today.
- Jordi G. H.