TIPS(1p)
π TIPS(1p) β PDL::Tips π
okay bestie, so this is the User Contributed Perl Documentation page for PDL::Tips, and it's literally just a lil treasure chest of "Small tidbits of useful arcana. Programming tidbits and such." π Manual section 1p, and yes we're getting into it.
β¨ NAME β¨
PDL::Tips β small tidbits of useful arcana. Programming tidbits and such. π¦
β¨ SYNOPSIS β¨
use PDL;
# Whatever happens here.
like. do whatever. it's your world π
β¨ DESCRIPTION β¨
This page documents useful idioms, helpful hints and tips for using Perl Data Language v2.0. it's giving "cheat codes but for PDL" fr fr π
π«Ά Help
Use "help help" within perldl or pdl2, or use the pdldoc program from the command line, for access to the PerlDL documentation. π
HTML versions of the pages should also be present, in the HtmlDocs/PDL directory of the PDL distribution. To find this directory, try the following:
pdl> foreach ( map{"$_/PDL/HtmlDocs"}@INC ) { p "$_\n" if -d $_ }
look at her go, finding her own docs, main character energy π
π¦ Indexing idioms
The following code normalizes a bunch of vectors in $x. This works regardless of the dimensionality of $x. (no cap, dimensionality is irrelevant here, she ate)
$x /= $x->sumover->dummy(0);
π What is actually happening?
If you want to see what the code is actually doing, try the command
PDL::Core::set_debugging(1);
somewhere. This spews out a huge amount of debug info for PDL into STDOUT. π Plans for the future include making it possible to redirect the output, and also making it possible to select messages with more precision.
Many of the messages come from "Basic/Core/pdlapi.c" and you can look at the source to see what is going on.
If you have any extra time to work on these mechanisms, inform the pdl-porters mailing list. π«Ά (they would love that for them)
π Memory savings
If you are running recursively something that selects certain indices of a large ndarray, like
while(1) {
$inds = where($x>0);
$x = $x->index($inds);
$y = $y->index($inds);
func($y,$x);
}
If you are not writing to $y, it saves a lot of memory to change this to
$y = $y->index($inds)->sever;
The new method "sever" is a causes the write-back relation to be forgotten. It is like copy except it changes the original ndarray and returns it). π§
Of course, the probably best way to do the above is
$inds = xvals ($x->long);
while(1) {
$inds0 = where($x>0);
$inds1 = $inds->index($inds)->sever;
$x = $a0->index($inds1);
$y = $y->index($inds1)->sever;
func($y,$x);
}
which doesn't save all the temporary instances of $x in memory. πΈ See "mandel.pl" in the Demos subdirectory of the PerlDL distribution for an example.
π₯ PP speed
If you really want to write speedy PP code, the first thing you need to do is to make sure that your C compiler is allowed to do the necessary optimizations. (the vibe check happens at compile time bestie)
What this means is that you have to allow as many variables as possible to go into registers:
loop(a) %{
$a() += $COMP(foo_member) * $b()
%}
expands to
for(i=0; i<10000; i++) {
a[i] += __privtrans->foo_member * b[i];
}
is about the worst you can do, since your C compiler is not allowed to assume that "a" doesn't clobber "foo_member" which completely inhibits vectorization. π Instead, do
float foo = $COMP(foo_member);
loop(a) %{
$a() += foo * $b();
%}
This is not a restriction caused by PP but by ANSI C semantics. Of course, we could copy the struct into local variables and back but that could cause very strange things sometimes. π
There are many other issues on organizing loops.
We are currently planning to make PP able to do fixed-width things as well as physical ndarrays (where looping over the first dimensions would be cheaper as there are less distinct increments, which might make a difference on machines with a small number of registers). β
π AUTHOR π
Copyright (C) Tuomas J. Lukka 1997. All rights reserved. Duplication in the same form and printing a copy for yourself allowed. π«Ά
k that's the whole page bestie, go forth and normalize your vectors π
perl v5.38.2 Β· 2024-04-10 Β· TIPS(1p) β mwah, that's a wrap, no cap, periodt π β¨π
π¦ where this page comes from
- ubuntu noble: pdl 1:2.085-1ubuntu1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms