PDL::Dataflow -- description of the dataflow implementation and philosophy
pdl> $x = zeroes(10);
pdl> $y = $x->slice("2:4:2");
pdl> $y ++;
pdl> print $x;
[0 0 1 0 1 0 0 0 0 0]
As of 2.079, this is now a description of the current implementation, together
with some design thoughts from its original author, Tuomas Lukka.
Two-directional dataflow (which implements "->slice()" etc.) is
fully functional, as shown in the SYNOPSIS. One-way is implemented, but with
restrictions.
Just about any function which returns some subset of the values in some ndarray
will make a binding. $y has become effectively a window to some sub-elements
of $x. You can also define your own routines that do different types of
subsets. If you don't want $y to be a window to $x, you must do
$y = $x->slice("some parts")->sever;
The "sever" destroys the "slice" transform, thereby turning
off all dataflow between the two ndarrays.
This works, thanks to a two-way flowing transform that implements
type-conversions, particularly for supplied outputs of the "wrong"
type for the given transform:
pdl> $a_bad = pdl double, '[1 BAD 3]';
pdl> $b_float = zeroes float, 3;
pdl> $a_bad->assgn($b_float); # could be written as $b_float .= $a_bad
pdl> p $b_float->badflag;
1
pdl> p $b_float;
[1 BAD 3]
You need to explicitly turn on one-way dataflow on an ndarray to activate it for
non-flowing operations, so
pdl> $x = pdl 2,3,4;
pdl> $x->doflow;
pdl> $y = $x * 2;
pdl> print $y;
[4 6 8]
pdl> $x->set(0,5);
pdl> print $y;
[10 6 8]
It is not possible to turn on backwards dataflow (such as is used by
"slice"-type operations), because there is no general way for PDL
(or maths, in fact) to know how to reverse most operations - consider "$z
= $x * $y", then adding one to $z.
Consider the following code:
$u = sequence(3,3); $u->doflow;
$v = ones(3,3); $v->doflow;
$w = $u + $v; $w->doflow; # must turn on for each
$y = $w + 1; $y->doflow;
$x = $w->diagonal(0,1);
$x += 50;
$z = $w + 2;
What do $y and $z contain now?
pdl> p $y
[
[52 3 4]
[ 5 56 7]
[ 8 9 60]
]
pdl> p $z
[
[53 4 5]
[ 6 57 8]
[ 9 10 61]
]
What about when $u is changed and a recalculation is triggered? A problem
arises, in that PDL currently (as of 2.079) disallows (see
pdlapi.c),
for normal transforms, output ndarrays with flow, or output ndarrays with any
parent with dataflow. So "$u++" throws an exception. But it is
currently possible to use "set", which is a sort of micro-transform
that calls (in the C API) "PDL.set" to mutate the data, then
"PDL.changed" to trigger flow updates:
pdl> $u->set(1,1,90)
pdl> p $y
[
[ 2 3 4]
[ 5 92 7]
[ 8 9 10]
]
You'll notice that while the setting of "1,1" (the middle) of $u
updated $y, the changes to $y that resulted from adding 50 to the diagonal
(via $x, and two-way flow) got lost. This is one-way flow.
In one-way flow context like the above, with:
pdl> $y = $x * 2;
nothing will have been calculated at this point. Even the memory for the
contents of $y has not been allocated. Only the command
pdl> print $y
will actually cause $y to be calculated. This is important to bear in mind when
doing performance measurements and benchmarks as well as when tracking errors.
There is an explanation for this behaviour: it may save cycles but more
importantly, imagine the following:
pdl> $x = pdl 2,3,4; $x->doflow;
pdl> $y = pdl 5,6,7; $y->doflow;
pdl> $c = $x + $y;
pdl> $x->setdims([4]);
pdl> $y->setdims([4]);
pdl> print $c;
Now, if $c were evaluated between the two resizes, an error condition of
incompatible sizes would occur.
What happens in the current version is that resizing $x raises a flag in $c:
"PDL_PARENTDIMSCHANGED" and $y just raises the same flag again. When
$c is next evaluated, the flags are checked and it is found that a
recalculation is needed.
Of course, lazy evaluation can sometimes make debugging more painful because
errors may occur somewhere where you'd not expect them.
This is one of the more intricate concepts of dataflow. In order to make
dataflow work like you'd expect, a rather strange concept must be introduced:
families. Let us make a diagram of the one-way flow example - it uses a
hypergraph because the transforms (with "+") are connectors between
ndarrays (with "*"):
u* *v
\ /
+(plus)
|
1* *w
\ /|\
\ / | \
(plus)+ | +(diagonal)
| | |
y* | *x
|
| *1
|/
+(plus)
|
z*
This is what PDL actually has in memory after the first three lines. When $x is
changed, $w changes due to "diagonal" being a two-way operation.
If you want flow from $w, you opt in using "$w->doflow" (as shown
in this scenario). If you didn't, then don't enable it. If you have it but
want to stop it, call "$ndarray->sever". That will destroy the
ndarray's "trans_parent" (here, a node marked with "+"),
and as you can visually tell, will stop changes flowing thereafter. If you
want to leave the flow operating, but get a copy of the ndarray at that point,
use "$ndarray->copy" - it will have the same data at that moment,
but have no flow relationships.
There is the start of a mechanism to bind events onto changed data, intended to
allow this to work:
pdl> $x = pdl 2,3,4
pdl> $y = $x + 1;
pdl> $c = $y * 2;
pdl> $c->bind( sub { print "A now: $x, C now: $c\n" } )
pdl> PDL::dowhenidle();
A now: [2,3,4], C now: [6 8 10]
pdl> $x->set(0,1);
pdl> $x->set(1,1);
pdl> PDL::dowhenidle();
A now: [1,1,4], C now: [4 4 10]
This hooks into PDL's "magic" which resembles Perl's, but does not
currently operate.
There would be many kinds of uses for this feature: self-updating charts, for
instance. It is not yet fully clear whether it would be most useful to queue
up changes (useful for doing asynchronously, e.g. when idle), or to activate
things immediately.
In the 2022 era of both GPUs and multiple cores, it is a pity that Perl's
dominant model remains single-threaded on CPU, but PDL can use multi-cores for
CPU processing (albeit controlled in a single-threaded style) - see
PDL::ParallelCPU. It is planned that PDL will gain the ability to use GPUs,
and there might be a way to hook that up albeit probably with an event loop to
"subscribe" to GPU events.
PDL implements nearly everything (except for XS oddities like "set")
using transforms which connect ndarrays. This includes data transformations
like addition, "slicing" to access/operate on subsets, and data-type
conversions (which have two-way dataflow, see "Type conversions").
This does not currently include a resizing transformation, and
"setdims" mutates its input. This is intended to change.
Copyright(C) 1997 Tuomas J. Lukka (
[email protected]). Same terms as the
rest of PDL.