Some places, some rules

In my last post, I talked about using In place computation to limit memory usage of your calculation. I actually did not had the time to mention some important rules and limitations on this mechanism.

You would be surprised but Matlab actually does not advertise the “In place system” much. Indeed, I couldn’t find these rules in the help, which is very unusual for Matlab. I don’t think “In place computation” is mentioned anywhere. I guess they don’t want you to write code that is too extensively tweaked to boost memory usage as memory management can change in new releases of Matlab. Still a semi-official documentation was done by Loren on her blog.

The most important rule is that the In-place calculation must be done within a function. If you actually use a subfunction for the In-place calculation, this one must be called from within a function. In other words, using the command line to do In place computation AND just a script in a M-file won’t work as well. These things MUST happen within a function. This is required so that Matlab can properly interpret your code and activate the In place calculation. Most Matlab functions Built-in should be able to work with In-place computation. but still some might not. It is impossible to tell as Mathworks does not publish an updated list of working functions.

Let’s look at the usage of In-place with subfunction :

function TestAll

x=rand(10000,10000);

x=testNoPlace(x);
y=testNoPlace(x);
x=testPlace(x);
z=testPlace(x);

function y=testNoPlace(x)
y=2*x;

function x=testPlace(x)
x=2*x;

This function, will compare 2 functions, TestNoPlace and TestPlace. TestPlace is meant to work in place, the output is sent to x, the same variable as the input. If you start TestAll while the profiler is on. You will see that x=testPlace(x) is about 2 times faster than all other calls. The line z=testPlace(x) is interesting. Matlab is smart enough not to modify x in this case, even if this function is written as “In place”. The time spent on this line is exactly the same as on all other calls of TestNoPlace. Please note that I wrote the In-Place calls from within a function (here TestAll).

Related Posts

  • Copy on WriteCopy on Write
    Since I was recently talking about Pointers in Matlab, I thought I should make this picture compl...
  • What happen at my place, stays at my placeWhat happen at my place, stays at my place
    I recently talked about the Copy on write mechanism. It ensures that, when you copy a variable, m...
  • Inline your linesInline your lines
    In essence, writing a Matlab program is aligning a succession of command that you send to the Mat...
  • Copy on Write in subfunctionsCopy on Write in subfunctions
    In my last post on Copy on Write, I didn't talk at all about one very important consequence of th...
  • Code is poetryCode is poetry
    Today, I want to give you some insight on how to make clean Matlab code that everyone can under...
This entry was posted in Advanced, Optimizing your code. Bookmark the permalink.

Leave a Reply