Reducing memory by emulating IDL's temporary() function
From:
Judd Storrs
Subject:
Reducing memory by emulating IDL's temporary() function
Date:
Thu, 24 Apr 2008 15:51:08 -0400
Hello,
I previously posted to octave-help about IDL's temporary() function which can be used in IDL to reduce memory duplications in some situations. The documentation for IDL's function can be found at http://idlastro.gsfc.nasa.gov/idl_html_help/TEMPORARY.html
From the summary:
"The TEMPORARY function returns a temporary copy of a variable, and sets
the original variable to "undefined". This function can be used to
conserve memory when performing operations on large arrays, as it
avoids making a new copy of results that are only temporary. In
general, the TEMPORARY routine can be used to advantage whenever a
variable containing an array on the left hand side of an assignment
statement is also referenced on the right hand side."
I've cobbled together a DLM that tries to replicate this behavior in octave (code at bottom of message). Here's the code I'm using for testing it:
% Baseline A = zeros(4096,4096); B = zeros(4096,4096); A = A + B + B;
% Using temporary() A = zeros(4096,4096);
B = zeros(4096,4096);
A = temporary("A") + B + B;
Right now, temporary() takes a string, because I haven't figured out how to find the symbol_record of the parameter.
I think it may be working -- I used the gnome-system-monitor graphs and increased the matrix sizes until I could see the bumps in the graphs. Using the baseline code I think I see the memory use step up three times. Using temporary it only steps up twice. Is there a better way to monitor octave's memory use?
DEFUN_DLD(temporary, args, , "Try to remove a variable from the caller scope and also return it.")
{ std::string name = args(0).string_value(); symbol_record * ptr; ptr = curr_caller_sym_tab->lookup(name); octave_value retval = ptr->def(); ptr->clear(); return retval; }