octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

more indexing optimizations


From: Jaroslav Hajek
Subject: more indexing optimizations
Date: Fri, 13 Mar 2009 14:27:30 +0100

hi all,

I noticed today that repmat is quite slow when used to replicate a row
vector to a matrix, the reason being that it results in an indexing
expression of type: a(:, ones (1, n), :, ones (1, 1)) with size(a) =
[1, 1, n, 1].
Rather than adding workarounds to repmat, I made the index reductions
rules a bit smarter in that they will now ignore singleton dims
indexed by colon or 1.
Also, call of the form `ones(1, n)' will now produce the result in
packed form (a range with zero increment). When subsequently used in
indexing, it is recognized and handled optimally, possibly being
translated to std::fill_n (after reductions).
a*ones(1, n) will also stay in packed form, as long as a is finite,
though I'm not sure whether it has any use.

Here's a short benchmark for repmat:
n = 8e3;
a = rand (1, n);
disp ("repmat row vector")
tic; repmat (a, n, 1); toc
disp ("...kron equivalent");
tic; kron (ones (n, 1), a); toc
disp ("..rank-1 product equivalent")
tic; ones (n, 1) * a; toc
disp ("repmat array with leading singletons")
tic; repmat (a, [1, 1, 1, n]); toc
disp ("repmat array with middle singletons")
tic; repmat (reshape (a, 100, 1, n/100), [1, n, 1]); toc

using a recent tip, I get:
repmat row vector
Elapsed time is 1.87884 seconds.
...kron equivalent
Elapsed time is 1.46305 seconds.
..rank-1 product equivalent
Elapsed time is 0.727102 seconds.
repmat array with leading singletons
Elapsed time is 1.82271 seconds.
repmat array with middle singletons
Elapsed time is 0.331135 seconds.

using the current tip, I get:
repmat row vector
Elapsed time is 0.258762 seconds.
...kron equivalent
Elapsed time is 1.46054 seconds.
..rank-1 product equivalent
Elapsed time is 0.725892 seconds.
repmat array with leading singletons
Elapsed time is 0.30604 seconds.
repmat array with middle singletons
Elapsed time is 0.305458 seconds.


so we get ~6x speed-up in certain cases.
Also, it seems that repmat is now really the generally fastest way to
replicate arrays.
No more need for those rank-1 or kronecker product tricks, I hope.

cheers


-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz


reply via email to

[Prev in Thread] Current Thread [Next in Thread]