Thread Local Remember Tables
In Maple 17 remember tables are thread local by default. When a procedure with a remember table is called by multiple threads, the remembered results are only available on the thread that created them. This differs from previous versions of Maple where remember tables were shared between threads, thus changes to the table in one thread would effect other threads.
This change allows a procedure with a remember table that works when run on a single thread to continue to work when run in parallel, regardless of how the procedure uses its remember table. In previous versions of Maple a procedure could manipulate its remember table in such away as to break the behavior of a parallel execution of the procedure.
The following procedures implement a sequence similar to Fibonacci, where each term is the sum of the previous two terms, however the user can specify the initial values. A remember table is used to improve the performance of this procedure. The initial values are stored in FibsHelper's remember table where they act as the termination condition for the recursion.
FibsHelper := proc (n::nonnegint)
FibsHelper:=procn::nonnegintoptionremember;FibsHelper⁡n âˆ’ 1+FibsHelper⁡n âˆ’ 2end proc
Fibs := proc (n::nonnegint, t1 := 1, t2 := 1)
subsop(4 = NULL, op(FibsHelper));
FibsHelper(0) := t1;
FibsHelper(1) := t2;
seq(Fibs(i), i = 0 .. 10);
seq(Fibs(i, 2, 2), i = 0 .. 10);
seq(Fibs(i, 2, 38), i = 0 .. 10);
This works when run on a single thread, and in Maple 17, it works when run in parallel.
task := proc (n, t1, t2) local i; [seq(Fibs(i, t1, t2), i = 0 .. 10)] end proc;
Threads:-Task:-Start(passed, Tasks = [task, [10, 1, 1], [10, 2, 2], [10, 2, 38]]);
In previous versions of Maple, the parallel evaluation can fail.
A procedure with a shared remember table (Maple's previous behavior) can still be created in Maple 17 by specifying the shared option. The following procedure will work when used in a single thread, but not in parallel.
SharedFibsHelper := proc (n::nonnegint)
options remember, shared;
SharedFibsHelper:=procn::nonnegintoptionremember,shared;SharedFibsHelper⁡n âˆ’ 1+SharedFibsHelper⁡n âˆ’ 2end proc
SharedFibs := proc (n::nonnegint, t1 := 1, t2 := 1)
subsop(4 = NULL, op(SharedFibsHelper));
SharedFibsHelper(0) := t1;
SharedFibsHelper(1) := t2;
seq(SharedFibs(i), i = 0 .. 10);
seq(SharedFibs(i, 2, 2), i = 0 .. 10);
seq(SharedFibs(i, 2, 38), i = 0 .. 10);
When SharedFibs is called in parallel, incorrect results may be computed. Note that due to the nature of parallel execution, incorrect results may not occur on every run.
sharedTask := proc (n, t1, t2)
[seq(SharedFibs(i, t1, t2), i = 0 .. 10)]
Threads:-Task:-Start(passed, Tasks = [sharedTask, [10, 1, 1], [10, 2, 2], [10, 2, 38]]);
There are various things that can go wrong. Clearing the remember table in one thread can remove another thread's initial values, which will cause SharedFibsHelper to be called with invalid inputs (negative numbers). One threads initial values can overwrite another threads, which will cause that thread to compute incorrect results. One threads remembered values can be used by a different thread where those values are incorrect.
A procedure with a shared remember table can work correctly in parallel and computed results can be shared across threads. However there are may complexities that require procedures with shared remember tables to be written with care. Maple 17's thread local remember tables makes it easier for procedure written for single threaded execution to work in parallel.
Download Help Document
What kind of issue would you like to report? (Optional)