This file is indexed.

/usr/lib/perl5/PDL/Internals.pod is in pdl 1:2.4.7+dfsg-2ubuntu5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
=head1 NAME

PDL::Internals - description of some aspects of the current internals

=head1 DESCRIPTION

=head2 Intro

This document explains various aspects of the current implementation
of PDL. If you just want to use PDL for something, you definitely
do not need to read this. Even if you want to interface your C routines
to PDL or create new L<PDL::PP|PDL::PP> functions, you do not need to read this
man page (though it may be informative). This document is primarily
intended for people interested in debugging or changing the internals
of PDL. To read this, a good understanding of the C language
and programming and data structures in general is required, as well
as some Perl understanding. If you read through this document
and understand all of it and are able to point what any part of
this document refers to in the PDL core sources and additionally
struggle to understand L<PDL::PP|PDL::PP>, you will be awarded the title
"PDL Guru" (of course, the current version of this document
is so incomplete that this is next to impossible from just these notes).

B<Warning:> If it seems that this document has gotten out of date,
please inform the PDL porters email list (pdl-porters@jach.hawaii.edu).
This may well happen.

=head2 Piddles

The pdl data object is generally an opaque scalar reference into a 
pdl structure in memory. Alternatively, it may be a hash reference with
the C<PDL> field containing the scalar reference (this makes overloading
piddles easy, see L<PDL::Objects|PDL::Objects>). You can easily find out
at the Perl level which type of piddle you are dealing with. The example
code below demonstrates how to do it:

   # check if this a piddle
   die "not a piddle" unless UNIVERSAL::isa($pdl, 'PDL');
   # is it a scalar ref or a hash ref?
   if (UNIVERSAL::isa($pdl, "HASH")) {
     die "not a valid PDL" unless exists $pdl->{PDL} &&
	UNIVERSAL::isa($pdl->{PDL},'PDL');
     print "This is a hash reference,",
	" the PDL field contains the scalar ref\n";
   } else {
        print "This is a scalar ref that points to address $$pdl in memory\n";
   }

The scalar reference points to the numeric address of a C structure of
type C<pdl> which is defined in F<pdl.h>. The mapping between the
object at the Perl level and the C structure containing the actual
data and structural that makes up a piddle is done by the PDL typemap.
The functions used in the PDL typemap are defined pretty much at the top
of the file F<pdlcore.h>. So what does the structure look like:

	struct pdl {
	   unsigned long magicno; /* Always stores PDL_MAGICNO as a sanity check */
	     /* This is first so most pointer accesses to wrong type are caught */
	   int state;        /* What's in this pdl */

	   pdl_trans *trans; /* Opaque pointer to internals of transformation from
				parent */

	   pdl_vaffine *vafftrans;

	   void*    sv;      /* (optional) pointer back to original sv.
				  ALWAYS check for non-null before use.
				  We cannot inc refcnt on this one or we'd
				  never get destroyed */

	   void *datasv;        /* Pointer to SV containing data. Refcnt inced */
	   void *data;            /* Null: no data alloced for this one */
	   int nvals;           /* How many values allocated */
	   int datatype;
	   PDL_Long   *dims;      /* Array of data dimensions */
	   PDL_Long   *dimincs;   /* Array of data default increments */
	   short    ndims;     /* Number of data dimensions */

	   unsigned char *threadids;  /* Starting index of the thread index set n */
	   unsigned char nthreadids;

	   pdl *progenitor; /* I'm in a mutated family. make_physical_now must
			       copy me to the new generation. */
	   pdl *future_me;  /* I'm the "then" pdl and this is my "now" (or more modern
			       version, anyway */

	   pdl_children children;

	   short living_for; /* Perl side not referenced; delete me when */

	   PDL_Long   def_dims[PDL_NDIMS];   /* Preallocated space for efficiency */
	   PDL_Long   def_dimincs[PDL_NDIMS];   /* Preallocated space for efficiency */
	   unsigned char def_threadids[PDL_NTHREADIDS];

	   struct pdl_magic *magic;

	   void *hdrsv; /* "header", settable from outside */
	};

This is quite a structure for just storing some data in - what is going on?

=over 5

=item Data storage

We are going to start with some of the simpler members: first of all,
there is the member

	void *datasv;

which is really a pointer to a Perl SV structure (C<SV *>). The SV is
expected to be representing a string, in which the data of the piddle
is stored in a tightly packed form. This pointer counts as a reference
to the SV so the reference count has been incremented when the C<SV *>
was placed here (this reference count business has to do with Perl's
garbage collection mechanism -- don't worry if this doesn't mean much
to you). This pointer is allowed to have the value C<NULL> which 
means that there is no actual Perl SV for this data - for instance, the data
might be allocated by a C<mmap> operation. Note the use of an SV*
was purely for convenience, it allows easy transformation of
packed data from files into piddles. Other implementations are not
excluded.

The actual pointer to data is stored in the member

	void *data;

which contains a pointer to a memory area with space for

	int nvals;

data items of the data type of this piddle.

The data type of the data is stored in the variable

	int datatype;

the values for this member are given in the enum C<pdl_datatypes> (see
F<pdl.h>). Currently we have byte, short, unsigned short, long, float and
double types, see also L<PDL::Types>.

=item Dimensions

The number of dimensions in the piddle is given by the member

	int ndims;

which shows how many entries there are in the arrays

	PDL_Long   *dims;      
	PDL_Long   *dimincs;

These arrays are intimately related: C<dims> gives the sizes of the dimensions
and C<dimincs> is always calculated by the code

	int inc = 1;
        for(i=0; i<it->ndims; i++) {
		it->dimincs[i] = inc; inc *= it->dims[i];
	}

in the routine C<pdl_resize_defaultincs> in C<pdlapi.c>.
What this means is that the dimincs can be used to calculate the offset
by code like

	int offs = 0;
	for(i=0; i<it->ndims; i++) {
		offs += it->dimincs[i] * index[i];
	}

but this is not always the right thing to do,
at least without checking for certain things first.

=item Default storage

Since the vast majority of piddles don't have more than 6 dimensions,
it is more efficient to have default storage for the dimensions and dimincs
inside the PDL struct.

   	PDL_Long   def_dims[PDL_NDIMS];   
   	PDL_Long   def_dimincs[PDL_NDIMS]; 

The C<dims> and C<dimincs> may be set to point to the beginning of these
arrays if C<ndims> is smaller than or equal to the compile-time constant
C<PDL_NDIMS>. This is important to note when freeing a piddle struct.
The same applies for the threadids:

   	unsigned char def_threadids[PDL_NTHREADIDS];

=item Magic

It is possible to attach magic to piddles, much like Perl's own magic
mechanism. If the member pointer

	   struct pdl_magic *magic;

is nonzero, the PDL has some magic attached to it. The implementation
of magic can be gleaned from the file F<pdlmagic.c> in the distribution.

=item State

One of the first members of the structure is 

	int state;

The possible flags and their meanings are given in C<pdl.h>.
These are mainly used to implement the lazy evaluation mechanism
and keep track of piddles in these operations.

=item Transformations and virtual affine transformations

As you should already know, piddles often carry information about
where they come from. For example, the code

	$b = $a->slice("2:5");
	$b .= 1;

will alter $a. So C<$b> and C<$a> I<know> that they are connected
via a C<slice>-transformation. This information is stored in the members

   	pdl_trans *trans; 
   	pdl_vaffine *vafftrans;

Both C<$a> (the I<parent>) and C<$b> (the child) store this information
about the transformation in appropriate slots of the C<pdl> structure.

C<pdl_trans> and C<pdl_vaffine> are structures that we will look at in
more detail below.

=item The Perl SVs

When piddles are referred to through Perl SVs, we store an additional
reference to it in the member

	void*    sv;

in order to be able to return a reference to the user when he wants to 
inspect the transformation structure on the Perl side.

Also, we store an opaque

	void *hdrsv; 

which is just for use by the user to hook up arbitrary data with this sv.
This one is generally manipulated through L<sethdr|PDL::Core/sethdr> and
L<gethdr|PDL::Core/gethdr> calls.

=back

=head2 Smart references and transformations: slicing and dicing

Smart references and most other fundamental functions
operating on piddles are implemented via I<transformations>
(Aas mentioned above) which are represented by the type C<pdl_trans> in PDL.

A transformation links input and output piddles and contains
all the infrastructure that defines how

=over 4

=item *

output piddles are obtained from input piddles

=item *

changes in smartly linked output piddles (e.g. the I<child>
of a sliced I<parent> piddle) are flown back to the input
piddle in transformations where this is supported (the most
often used example being C<slice> here).

=item *

datatype and size of output piddles that need to be created
are obtained

=back

In general, executing a PDL function on a group of piddles
results in creation of a transformation of the requested
type that links all input and output arguments (at least
those that are piddles). In PDL functions that support
data flow between input and output args (e.g. C<slice>,
C<index>) this transformation links I<parent> (input) and
I<child> (output) piddles permanently until either the link is
explicitly broken by user request (C<sever> at the Perl level)
or all parents and children have been destroyed. In those
cases the transformation is lazy-evaluated, e.g. only executed
when piddle values are actually accessed.

In I<non-flowing> functions, for example addition (C<+>) and inner
products (C<inner>), the transformation is installed just as
in flowing functions but then the transformation is immediately
executed and destroyed (breaking the link between input and output args)
before the function returns.

It should be noted that the close link between input and output args
of a flowing function (like L<slice|PDL::Slices/slice>) requires
that piddle objects that are linked in
such a way be kept alive beyond the point where they have gone
out of scope from the point of view of Perl:

  $a = zeroes(20);
  $b = $a->slice('2:4');
  undef $a;    # last reference to $a is now destroyed

Although $a should now be destroyed according to Perl's rules
the underlying C<pdl> structure must actually only be freed when C<$b>
also goes out of scope (since it still references
internally some of C<$a>'s data). This example demonstrates that such
a dataflow paradigm between PDL objects necessitates a special
destruction algorithm that takes the links between piddles
into account and couples the lifespan of those objects. The
non-trivial algorithm is implemented in the function
C<pdl_destroy> in F<pdlapi.c>. In fact, most of the code
in F<pdlapi.c> and F<pdlfamily.c> is concerned with
making sure that piddles (C<pdl *>s) are created, updated
and freed at the right times depending on interactions
with other piddles via PDL transformations (remember, C<pdl_trans>). 

=head2 Accessing children and parents of a piddle

When piddles are dynamically linked via transformations as
suggested above input and output piddles are referred to as parents
and children, respectively.

An example of processing the children of a piddle is provided
by the C<baddata> method of PDL::Bad (only available if you
have compiled PDL with the C<WITH_BADVAL> option set to 1,
but still useful as an example!). 

Consider the following situation:

 pdl> $a = rvals(7,7,Centre=>[3,4]);
 pdl> $b = $a->slice('2:4,3:5');
 pdl> ? vars
 PDL variables in package main::

 Name         Type   Dimension       Flow  State          Mem
 ----------------------------------------------------------------
 $a           Double D [7,7]                P            0.38Kb 
 $b           Double D [3,3]                VC           0.00Kb 

Now, if I suddenly decide that C<$a> should be flagged as possibly
containing bad values, using

 pdl> $a->baddata(1)

then I want the state of C<$b> - it's I<child> - to be changed as
well (since it will either share or inherit some of C<$a>'s data and
so be also I<bad>), so that I get a 'B' in the I<State> field:

 pdl> ? vars                    
 PDL variables in package main::

 Name         Type   Dimension       Flow  State          Mem
 ----------------------------------------------------------------
 $a           Double D [7,7]                PB           0.38Kb 
 $b           Double D [3,3]                VCB          0.00Kb 

This bit of magic is performed by the C<propogate_badflag> function,
which is listed below:

 /* newval = 1 means set flag, 0 means clear it */
 /* thanks to Christian Soeller for this */

 void propogate_badflag( pdl *it, int newval ) {
    PDL_DECL_CHILDLOOP(it)
    PDL_START_CHILDLOOP(it)
    {
	pdl_trans *trans = PDL_CHILDLOOP_THISCHILD(it);
	int i;
	for( i = trans->vtable->nparents;
	     i < trans->vtable->npdls;
	     i++ ) {
	    pdl *child = trans->pdls[i];

	    if ( newval ) child->state |=  PDL_BADVAL;
            else          child->state &= ~PDL_BADVAL;

	    /* make sure we propogate to grandchildren, etc */
	    propogate_badflag( child, newval );

        } /* for: i */
    }
    PDL_END_CHILDLOOP(it)
 } /* propogate_badflag */

Given a piddle (C<pdl *it>), the routine loops through each 
C<pdl_trans> structure, where access to this structure is provided by the 
C<PDL_CHILDLOOP_THISCHILD> macro.
The I<children> of the piddle are stored in the C<pdls> array, after the
I<parents>, hence the loop from C<i = ...nparents> to 
C<i = ...nparents - 1>.
Once we have the pointer to the child piddle, we can do what we want to 
it; here we change the value of the C<state> variable, but the details
are unimportant).
What B<is> important is that we call C<propogate_badflag> on this
piddle, to ensure we loop through its children. This recursion
ensures we get to all the I<offspring> of a particular piddle.

Access to I<parents> is similar, with the C<for> loop replaced by:

	for( i = 0;
	     i < trans->vtable->nparents;
	     i++ ) {
           /* do stuff with parent #i: trans->pdls[i] */
        }

=head2 What's in a transformation (C<pdl_trans>)

All transformations are implemented as structures

  struct XXX_trans {
	int magicno; /* to detect memory overwrites */
	short flags; /* state of the trans */
	pdl_transvtable *vtable;   /* the all important vtable */
	void (*freeproc)(struct pdl_trans *);  /* Call to free this trans
		(in case we had to malloc some stuff dor this trans) */
        pdl *pdls[NP]; /* The pdls involved in the transformation */
	int __datatype; /* the type of the transformation */
        /* in general more members
        /* depending on the actual transformation (slice, add, etc)
	 */
  };

The transformation identifies all C<pdl>s involved in the trans

  pdl *pdls[NP];

with C<NP> depending on the number of piddle args of the particular
trans. It records a state

  short flags;

and the datatype

  int __datatype;

of the trans (to which all piddles must be converted unless
they are explicitly typed, PDL functions created with L<PDL::PP|PDL::PP>
make sure that these conversions are done as necessary). Most important is
the pointer to the vtable (virtual table) that contains the actual
functionality

 pdl_transvtable *vtable;

The vtable structure in turn looks something like (slightly
simplified from F<pdl.h> for clarity)

  typedef struct pdl_transvtable {
	pdl_transtype transtype;
	int flags;
	int nparents;   /* number of parent pdls (input) */
	int npdls;      /* number of child pdls (output) */
	char *per_pdl_flags;  /* optimization flags */
	void (*redodims)(pdl_trans *tr);  /* figure out dims of children */
	void (*readdata)(pdl_trans *tr);  /* flow parents to children  */
	void (*writebackdata)(pdl_trans *tr); /* flow backwards */
	void (*freetrans)(pdl_trans *tr); /* Free both the contents and it of
					the trans member */
	pdl_trans *(*copy)(pdl_trans *tr); /* Full copy */
  	int structsize;
	char *name; /* For debuggers, mostly */
  } pdl_transvtable;

We focus on the callback functions:

  	void (*redodims)(pdl_trans *tr);

C<redodims> will work out the dimensions of piddles that need
to be created and is called from within the API function that
should be called to ensure that the dimensions of a piddle are
accessible (F<pdlapi.c>):

   void pdl_make_physdims(pdl *it)

C<readdata> and C<writebackdata> are responsible for the actual
computations of the child data from the parents or parent data
from those of the children, respectively (the dataflow aspect).
The PDL core makes sure that these are called as needed when
piddle data is accessed (lazy-evaluation). The general API
function to ensure that a piddle is up-to-date is

  void pdl_make_physvaffine(pdl *it)

which should be called before accessing piddle data from
XS/C (see F<Core.xs> for some examples).

C<freetrans> frees dynamically allocated memory associated
with the trans as needed and C<copy> can copy the transformation.
Again, functions built with L<PDL::PP|PDL::PP> make sure that copying
and freeing via these callbacks happens at the right times. (If they
fail to do that we have got a memory leak -- this has happened in
the past ;).

The transformation and vtable code is hardly ever written by
hand but rather generated by L<PDL::PP|PDL::PP> from concise descriptions.

Certain types of transformations can be optimized very
efficiently obviating the need for explicit C<readdata>
and C<writebackdata> methods. Those transformations are
called I<pdl_vaffine>. Most dimension manipulating
functions (e.g., C<slice>, C<xchg>) belong to this class.

The basic trick is that parent and child of such a transformation work
on the same (shared) block of data which they just choose
to interpret differently (by using different C<dims>, C<dimincs> and
C<offs> on the same data, compare the C<pdl> structure above).
Each operation on a piddle sharing
data with another one in this way is therefore automatically flown
from child to parent and back -- after all they are reading and writing
the same block of memory. This is currently not Perl thread safe --
no big loss since the whole PDL core is not reentrant
(Perl threading C<!=> PDL threading!).

=head2 Signatures: threading over elementary operations

Most of that functionality of PDL threading (automatic iteration
of elementary operations over multi-dim piddles) is implemented in the
file F<pdlthread.c>.

The L<PDL::PP|PDL::PP> generated functions (in particular the
C<readdata> and C<writebackdata> callbacks) use this infrastructure to 
make sure that the fundamental operation implemented by the
trans is performed in agreement with PDL's threading semantics.

=head2 Defining new PDL functions -- Glue code generation

Please, see L<PDL::PP> and examples in the PDL distribution. Implementation
and syntax are currently far from perfect but it does a good job!

=head2 The Core struct

As discussed in L<PDL::API|PDL::API>, PDL uses a pointer to a structure
to allow PDL modules access to its core routines. The definition of this
structure (the C<Core> struct) is in F<pdlcore.h> (created by 
F<pdlcore.h.PL> in F<Basic/Core>) and looks something like

 /* Structure to hold pointers core PDL routines so as to be used by 
  * many modules
  */
 struct Core {
    I32    Version;
    pdl*   (*SvPDLV)      ( SV*  );
    void   (*SetSV_PDL)   ( SV *sv, pdl *it );
 #if defined(PDL_clean_namespace) || defined(PDL_OLD_API)
    pdl*   (*new)      ( );     /* make it work with gimp-perl */
 #else
    pdl*   (*pdlnew)      ( );  /* renamed because of C++ clash */
 #endif
    pdl*   (*tmp)         ( );
    pdl*   (*create)      (int type);
    void   (*destroy)     (pdl *it);
    ...
 }
 typedef struct Core Core;

The first field of the structure (C<Version>) is used to ensure 
consistency between modules at run time; the following code
is placed in the BOOT section of the generated xs code:

 if (PDL->Version != PDL_CORE_VERSION)
   Perl_croak(aTHX_ "Foo needs to be recompiled against the newly installed PDL");

If you add a new field to the F<Core> struct you should:

=over 5

=item *

discuss it on the pdl porters email list (pdl-porters@jach.hawaii.edu)
[with the possibility of making your changes to a separate
branch of the CVS tree if it's a change that will take time to complete]

=item *

increase by 1 the value of the C<$pdl_core_version> variable in 
F<pdlcore.h.PL>. This sets the value of the
C<PDL_CORE_VERSION> C macro used to populate the Version field

=item *

add documentation (e.g. to L<PDL::API|PDL::API>) if it's a
"useful" function for external module writers (as well as
ensuring the code is as well documented as the rest of PDL
;)

=back

=head1 BUGS

This description is far from perfect. If you need more details
or something is still unclear please ask on the pdl-porters
mailing list (pdl-porters@jach.hawaii.edu).

=head1 AUTHOR

Copyright(C) 1997 Tuomas J. Lukka (lukka@fas.harvard.edu),
2000 Doug Burke (djburke@cpan.org), 2002 Christian Soeller & Doug Burke.

Redistribution in the same form is allowed but reprinting requires
a permission from the author.