External Memoization Using Modules
This worksheet demonstrates a technique for building robust memoized procedures using modules and other techniques. This worksheet is referenced in the help topic on efficiency hints, and goes into further detail about the caching and memoization techniques mentioned there.
Memoization of a procedure is a process by which previously computed values are stored somewhere and, whenever a computation is to be repeated, the stored valued is looked up and returned instead of repeating the entire (typically expensive) computation.
Maple already provides automatic memoization by means of the "remember" option that may be specified for procedures. A procedure with option remember has, as part of its internal data structure, a table in which previously computed values are stored. Whenever the procedure is called, Maple examines the remember table (if one exists) to determine whether the procedure has been called before with the same arguments and therefore has a stored value. If a stored value is found, then it is returned; otherwise, the computation is performed normally, and the computed value is stored in the remember table of the procedure.
|
Caching
|
|
Caching is an optimization technique that can be used in situations in which you do not want to incur the storage overhead of option remember, but would like to store the last (or last few) computed results. This technique is advantageous when you expect a procedure to be called repeatedly with the same inputs
Instead of storing all results in a table when they are computed, save only the most recent argument sequence and computed result. The simplest way to do this is to store these values in a pair of global variables. The following procedure, which caches the most recently computed determinant, illustrates this straightforward mechanism.
>
|
global cdet_args, cdet_result;
|
>
|
local last_args, last_result;
|
>
|
last_args, last_result := cdet_args, cdet_result;
|
>
|
if [ last_args ] = [ args ] then
|
>
|
last_result := LinearAlgebra:-Determinant( args )
|
>
|
cdet_args := last_args;
|
>
|
cdet_result := last_result
|
>
|
M := LinearAlgebra:-RandomMatrix( 50, 50, 'outputoptions' = [ 'datatype' = 'integer' ] ):
|
>
|
time( CachedDet( M ) );
|
| (1.1) |
>
|
time( seq( CachedDet( M ), i = 1 .. 100 ) );
|
| (1.2) |
The principal difficulty with this technique is that is relies on global variables, which should be avoided whenever possible. Essentially the same mechanism can be used if the cache variables are instead stored as local variables of a module that exports the caching wrapper routine. The cache variables (detargs and detresult below) must be local to the module to prevent "tampering," and to avoid colliding with other globals.
>
|
local detargs, detresult;
|
>
|
cached_det := proc( m )
|
>
|
local last_args, last_result;
|
>
|
last_args, last_result := detargs, detresult;
|
>
|
if [ last_args ] = [ args ] then
|
>
|
last_result := LinearAlgebra:-Determinant( args )
|
>
|
detresult := last_result
|
>
|
CachedDet := DetCache:-cached_det;
|
| (1.3) |
>
|
M := LinearAlgebra:-RandomMatrix( 50, 50, 'outputoptions' = [ 'datatype' = 'integer' ] ):
|
>
|
time( CachedDet( M ) );
|
| (1.4) |
>
|
time( seq( CachedDet( M ), i = 1 .. 10 ) );
|
| (1.5) |
You can use Maple's dynamic nature to create a procedure that automates the creation of cached versions of procedures. This allows you to apply the technique to already coded routines, without having to recode them. Here, we develop a Maple procedure called CachedProc that, when applied to a Maple procedure, returns a new procedure that employs the caching optimization.
>
|
CachedProc := proc( p )
|
>
|
# Build the caching module
|
>
|
local theArgs, theResult;
|
>
|
# Create the optimised procedure
|
>
|
local lastArgs, lastResult;
|
>
|
lastArgs := theArgs; lastResult := theResult;
|
>
|
if [ lastArgs ] = [ args ] then
|
>
|
lastResult := p( args )
|
>
|
theResult := lastResult
|
>
|
cint := CachedProc( radnormal@int );
|
| (1.6) |
>
|
time( cint( sin(x^4), x ) );
|
| (1.7) |
>
|
time( cint( sin(x^4), x ) );
|
| (1.8) |
The sort of computation for which this kind of optimization is suitable is illustrated by the following example. Here, a list of 1000 entries is sorted, and a procedure is mapped onto the list. When the list contains many duplicated elements, caching can reduce the time required.
>
|
L := sort( [seq(expand(ChebyshevT(r(),x)),i=1..1000)] ):
|
>
|
nops( L ), nops( convert( L, 'set' ) );
|
| (1.9) |
>
|
time( map( CachedProc( radnormal@int ), L, x ) );
|
| (1.10) |
>
|
time( map( radnormal@int, L, x ) );
|
| (1.11) |
| (1.12) |
|
|
Remembering Extra Information
|
|
The remember option cannot always be used on a procedure. The result of a computation may depend on the values of global or environment variables. A procedure may also return a result in a parameter passed as a name. (For details, see Procedure parameter definition and type checking.) The built-in procedure evalf has a specially managed remember table that, in addition to storing the arguments to a call, stores the current value of the environment variable Digits, upon which the results of evalf depend. This kind of facility is not provided by the remember option.
You can implement your own procedures that store extra information, such as the values of global or environment variables, by using modules.
Here, the procedure f depends on the global variable t. The results of calling it with the same argument may differ if the value of t at the time of the call is different.
| (2.1) |
| (2.2) |
| (2.3) |
For procedures such as this, the remember option is not valid.
>
|
f := proc( x ) global t; option remember; x + t end proc:
|
| (2.4) |
| (2.5) |
To take advantage of memoizing optimizations for procedures that have "external" dependencies, you can use a module to provide a "safe" environment in which to store previously computed results, and which stores--in addition to the argument sequence--all required external information. An implementation of a memoized variant of the procedure f above follows. This technique uses a local variable of the module memo_f to hold a table in which the values that have been computed are stored. The values are keyed on both the argument x and the global variable t.
>
|
local memory; # table in which to store results
|
>
|
if assigned( memory[ t, x ] ) then
|
>
|
memory[ t, x ] := x + t
|
| (2.6) |
| (2.7) |
| (2.8) |
| (2.9) |
| (2.10) |
| (2.11) |
| (2.12) |
An alternative technique is to use a helper procedure that takes extra arguments (in this case, just one) that does not depend on any external state and to which the remember option may validly be applied. The procedure of interest can then call the helper procedure, passing any required external values as extra arguments.
Here, for example, the procedure f is written to call the helper procedure remf, which takes two arguments and to which the remember option may validly be applied. It passes the value of the global variable t as a second parameter to remf.
This technique also works well, but suffers the disadvantage that the helper procedure remf, and thus its remember table, are globally accessible and subject to "tampering."
|
|
Performing Selective Memoization
|
|
Another case in which special-purpose memoization code may be useful is for selective memoization, in which only certain among the previously computed values are to be stored. For example, there may be two cases in a computation, one of which is expensive. Results that were obtained by expensive computation can be stored, while those that are able to be computed cheaply need not be.
>
|
pgcd := proc( a, b, p )
|
>
|
if type( [ a, b ], '[ zppoly, zppoly ]' ) then
|
>
|
modp1( Gcd( a, b ), p )
|
| (3.1) |
>
|
a := modp1( ConvertIn( 1 - (p-1)*T + T^2, T ), p ):
|
>
|
b := modp1( ConvertIn( 1 - (p-1)*T - T^2, T ), p ):
|
| (3.2) |
>
|
#store the results of polynomial gcd's but not integer gcd's
pgcd_memo := module()
|
>
|
pgcd := proc( a, b, p )
|
>
|
if type( [ a, b ], '[ zppoly, zppoly ]' ) then
|
>
|
memory[ a, b, p ] := modp1( Gcd( a, b ), p )
|
>
|
pgcd := pgcd_memo:-pgcd;
|
| (3.3) |
| (3.4) |
| (3.5) |
|
Return to Index for Example Worksheets
|