Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

matlab - BSXFUN on memory efficiency with relational operations

There are mainly two things I would like to research on about here -

  • There are six built-in relational operations for use with bsxfun : @eq (equal), @ne (not-equal), @lt (less-than), @le (less-than or equal), @gt (greater-than) and @ge (greater-than or equal). Lots of times we use them on floating point numbers and being relational operations, they output logical arrays. So, it got me curious, if the inherent expansion with bsxfun when using these relational operations on floating point numbers involve actual replication of input elements and that is precisely my first question.

  • I would also like to know how this memory efficiency issue translates to the anonymous functions when used with bsxfun, again with the case of relational operations.

This is inspired by the runtime/speedup tests performed for Comparing BSXFUN and REPMAT.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Introduction & Test Setup

To perform memory tests to inquire about the points raised in the question, let's define the inputs A and B:

A = rand(M,N)
B = rand(1,N)

Here, M and N are the size parameters and are kept as really large numbers.

I would be using repmat for comparisons as that seems like the closest alternative to bsxfun. So, the idea here to run the bsxfun and repmat equivalent codes and watch out for the bumps in memory usages from the Task Manager (on Windows).

This solution that compared bsxfun and repmat for runtime efficiency led to the conclusions that using relational operations with bsxfun is hugely runtime efficient, so it would be interesting to extend the basis of memory efficiency to the comparisons.

Thus, the bsxfun and repmat equivalents would look something like these -

REPMAT version:  A == repmat(B,size(A,1),1)
BSXFUN version: bsxfun(@eq,A,B))

Results

On running the repmat and then bsxfun codes, the Windows Task Manager showed something like this with the first bump denoting the run for repmat and the next one is for the bsxfun one -

enter image description here

The repmat bump has the same height as that when an actual copy of A is created. This basically shows that repmat makes an actual replication of B and then does the equality check. Since, B is to be replicated to a bigger floating point array, the memory requirements are huge as again shown in the memory graph earlier. On the other hand with bsxfun, from its bump height it seems is not replicating the actual floating point values and that leads to an efficient memory usage.

Now, after converting both A and B to logical arrays, the memory usage bumps changed to this -

enter image description here

Thus, it suggests that repmat was then able to optimize memory, as this time the replication was of logical datatype.

Using anonymous functions with bsxfun: One can experiment a bit with the anonymous functions usage with bsxfun and see if MATLAB shows the same smartness with it in optimizing memory requirements as with the built-in.

So, bsxfun(@eq,A,B) could be replaced by bsxfun(@(k1,k2) k1==k2,A,B). The resultant memory usage with this built-in and anonymous function implementation when operated on floating point input arrays, resulted in a memory graph as shown below -

enter image description here

The plot indicates that the use of anonymous function keeps the memory efficiency as with the built-in, even though the runtime is hampered quite a bit. The test results were similar when other relational operations were used instead.

Conclusions

When working with relational operations on floating-point arrays, it's definitely preferable to use bsxfun over repmat for both runtime and memory efficiency. So, this just proves that there are more reasons to go with bsxfun!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...