1TIPS(1) User Contributed Perl Documentation TIPS(1)
2
3
4
6 PDL::Tips - Small tidbits of useful arcana. Programming tidbits and
7 such.
8
10 use PDL;
11
12 # Whatever happens here.
13
15 This page documents useful idioms, helpful hints and tips for using
16 Perl Data Language v2.0.
17
18 Help
19 Use "help help" within perldl or the "pdldoc" program from the command
20 line for access to the PerlDL documentation. HTML versions of the
21 pages should also be present, in the HtmlDocs/PDL directory of the PDL
22 distribution. To find this directory, try the following
23
24 perldl> foreach ( map{"$_/PDL/HtmlDocs"}@INC ) { p "$_\n" if -d $_ }
25
26 Indexing idioms
27 The following code normalizes a bunch of vectors in $a. This works
28 regardless of the dimensionality of $a.
29
30 $a /= $a->sumover->dummy(0);
31
32 What is actually happening?
33 If you want to see what the code is actually doing, try the command
34
35 PDL::Core::set_debugging(1);
36
37 somewhere. This spews out a huge amount of debug info for PDL into
38 STDOUT. It is planned to eventually make this redirectable and the
39 messages selectable more accurately.
40
41 Many of the messages come from "Basic/Core/pdlapi.c" and you can look
42 at the source to see what is going on.
43
44 If you have any extra time to work on these mechanisms, infrom the pdl-
45 porters mailing list.
46
47 Memory savings
48 If you are running recursively something that selects certain indices
49 of a large piddle, like
50
51 while(1) {
52 $inds = where($a>0);
53 $a = $a->index($inds);
54 $b = $b->index($inds);
55 func($b,$a);
56 }
57
58 If you are not writing to $b, it saves a lot of memory to change this
59 to
60
61 $b = $b->index($inds)->sever;
62
63 The new method "sever" is a causes the write-back relation to be
64 forgotten. It is like copy except it changes the original piddle and
65 returns it).
66
67 Of course, the probably best way to do the above is
68
69 $inds = xvals ($a->long);
70 while(1) {
71 $inds0 = where($a>0);
72 $inds1 = $inds->index($inds)->sever;
73 $a = $a0->index($inds1);
74 $b = $b->index($inds1)->sever;
75 func($b,$a);
76 }
77
78 which doesn't save all the temporary instances of $a in memory. See
79 "mandel.pl" in the Demos subdirectory of the PerlDL distribution for an
80 example.
81
82 PP speed
83 If you really want to write speedy PP code, the first thing you need to
84 do is to make sure that your C compiler is allowed to do the necessary
85 optimizations.
86
87 What this means is that you have to allow as many variables as possible
88 to go into registers:
89
90 loop(a) %{
91 $a() += $COMP(foo_member) * $b()
92 %}
93
94 expands to
95
96 for(i=0; i<10000; i++) {
97 a[i] += __privtrans->foo_member * b[i];
98 }
99
100 is about the worst you can do, since your C compiler is not allowed to
101 assume that "a" doesn't clobber "foo_member" which completely inhibits
102 vectorization. Instead, do
103
104 float foo = $COMP(foo_member);
105 loop(a) %{
106 $a() += foo * $b();
107 %}
108
109 This is not a restriction caused by PP but by ANSI C semantics. Of
110 course, we could copy the struct into local varibles and back but that
111 could cause very strange things sometimes.
112
113 There are many other issues on organizing loops.
114
115 We are currently planning to make PP able to do fixed-width things as
116 well as physical piddles (where looping over the first dimensions would
117 be cheaper as there are less distinct increments, which might make a
118 difference on machines with a small number of registers).
119
121 Copyright (C) Tuomas J. Lukka 1997. All rights reserved. Duplication
122 in the same form and printing a copy for yourself allowed.
123
124
125
126perl v5.12.3 2009-10-17 TIPS(1)