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 pdl2 or use the "pdldoc" program from
20 the command line for access to the PerlDL documentation. HTML versions
21 of the pages should also be present, in the HtmlDocs/PDL directory of
22 the PDL distribution. To find this directory, try the following
23
24 pdl> foreach ( map{"$_/PDL/HtmlDocs"}@INC ) { p "$_\n" if -d $_ }
25
26 Indexing idioms
27 The following code normalizes a bunch of vectors in $x. This works
28 regardless of the dimensionality of $x.
29
30 $x /= $x->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. Plans for the future include making it possible to redirect the
39 output, and also making it possible to select messages with more
40 precision.
41
42 Many of the messages come from "Basic/Core/pdlapi.c" and you can look
43 at the source to see what is going on.
44
45 If you have any extra time to work on these mechanisms, inform the pdl-
46 porters mailing list.
47
48 Memory savings
49 If you are running recursively something that selects certain indices
50 of a large ndarray, like
51
52 while(1) {
53 $inds = where($x>0);
54 $x = $x->index($inds);
55 $y = $y->index($inds);
56 func($y,$x);
57 }
58
59 If you are not writing to $y, it saves a lot of memory to change this
60 to
61
62 $y = $y->index($inds)->sever;
63
64 The new method "sever" is a causes the write-back relation to be
65 forgotten. It is like copy except it changes the original ndarray and
66 returns it).
67
68 Of course, the probably best way to do the above is
69
70 $inds = xvals ($x->long);
71 while(1) {
72 $inds0 = where($x>0);
73 $inds1 = $inds->index($inds)->sever;
74 $x = $a0->index($inds1);
75 $y = $y->index($inds1)->sever;
76 func($y,$x);
77 }
78
79 which doesn't save all the temporary instances of $x in memory. See
80 "mandel.pl" in the Demos subdirectory of the PerlDL distribution for an
81 example.
82
83 PP speed
84 If you really want to write speedy PP code, the first thing you need to
85 do is to make sure that your C compiler is allowed to do the necessary
86 optimizations.
87
88 What this means is that you have to allow as many variables as possible
89 to go into registers:
90
91 loop(a) %{
92 $a() += $COMP(foo_member) * $b()
93 %}
94
95 expands to
96
97 for(i=0; i<10000; i++) {
98 a[i] += __privtrans->foo_member * b[i];
99 }
100
101 is about the worst you can do, since your C compiler is not allowed to
102 assume that "a" doesn't clobber "foo_member" which completely inhibits
103 vectorization. Instead, do
104
105 float foo = $COMP(foo_member);
106 loop(a) %{
107 $a() += foo * $b();
108 %}
109
110 This is not a restriction caused by PP but by ANSI C semantics. Of
111 course, we could copy the struct into local variables and back but that
112 could cause very strange things sometimes.
113
114 There are many other issues on organizing loops.
115
116 We are currently planning to make PP able to do fixed-width things as
117 well as physical ndarrays (where looping over the first dimensions
118 would be cheaper as there are less distinct increments, which might
119 make a difference on machines with a small number of registers).
120
122 Copyright (C) Tuomas J. Lukka 1997. All rights reserved. Duplication
123 in the same form and printing a copy for yourself allowed.
124
125
126
127perl v5.36.0 2023-01-20 TIPS(1)