[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
BSX enabled for assignment operators
From: |
Jordi Gutiérrez Hermoso |
Subject: |
BSX enabled for assignment operators |
Date: |
Thu, 15 Sep 2011 05:41:07 -0500 |
One of the problems with BSX is that sometimes you can incur in large
intermediate matrices. For example, the following is a bad way to
implement ordinary matrix multiplication with BSX:
x = rand(3,4);
y = rand(4,5);
assert(sum( permute(x, [1,3,2]) .* permute(y, [3,2,1]),3), x*y, eps)
which works, but incurs a large intermediate 3x5x4 NDArray. In the
following changeset,
http://hg.savannah.gnu.org/hgweb/octave/rev/aa4a23337a0f
I have therefore enabled BSX for assignment operators, which can
reduce memory consumption. For example, you can multiply-in place
either the columns or rows of a matrix like so:
x = reshape([1:9], 3, 3);
y = reshape([1:9], 3, 3);
z = [3 5 7];
x .*= z ## Multiply in place the columns of x by entries in z
y .*= z' ## Multiply in place the rows of y by entries in z.
This is how assignment operators also work in Numpy, which inspired me
to implement BSX with a cleaner syntax in Octave.
I have a question. While I was reading this, I noticed that internally
only for a few operators (the basic 4 arithmetic functions plus
logical AND and OR) are the operations actually done in place.
Operators like .\= and .^= do incur copying in memory the whole array.
If I'm correct, can this be helped? I don't know if, say, C++'s op*=
actually gets translated into a single hardware instruction.
Thanks and HTH,
- Jordi G. H.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- BSX enabled for assignment operators,
Jordi Gutiérrez Hermoso <=