blib/lib/Data/NDArray/Shared.pm

filter: Statementstmt 96.9% 63 / 65 Branchbran 75.0% 15 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 100% 4 / 4 totaltotal 91.9% 91 / 99 CC 11 SCAR 11.2
CC
11
Cov
92%
from_pdl
5.0
update_from_pdl
4.0
_pdl_ctor
3.1
CRAP 3.1
↑ Summary ndarray.h »
1 package Data::NDArray::Shared;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 our $VERSION = '0.07';
6 7 require XSLoader;
7 7 XSLoader::load('Data::NDArray::Shared', $VERSION);
8  
9 0 ▶sub CLONE_SKIP { 1 } # blessed C-pointer handle: never clone into ithreads (double-free)
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
10  
11 7 *numel = \&size;
12 7 *flat = \&to_list;
13  
14 # ---------------------------------------------------------------------------
15 # PDL interop. PDL is an optional, load-on-demand dependency (no build or
16 # runtime prereq). Each dtype maps to a PDL type of the SAME byte width, so the
17 # data copies/aliases with no element conversion. NOTE the axis order: this
18 # array is row-major C-order while PDL's dim(0) is the fastest-varying axis, so
19 # shapes are reversed across the boundary -- an (r, c) array <-> PDL dims (c, r).
20 # ---------------------------------------------------------------------------
21 7 my %PDL_TYPE = ( # dtype => PDL type-constructor name
22 f64 => 'double', f32 => 'float',
23 i64 => 'longlong', i32 => 'long', i16 => 'short', i8 => 'sbyte',
24 u64 => 'ulonglong', u32 => 'ulong', u16 => 'ushort', u8 => 'byte',
25 );
26 7 my %DTYPE_OF = reverse %PDL_TYPE; # PDL type name => dtype
27  
28 36 ▶sub _require_pdl {
Subroutine _require_pdl: called CC=2 SCAR=7.1
29 # Load core, copying and arithmetic methods without importing into this package.
30 36 ▶ eval { require PDL::Lite; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require PDL::Lite; 1 } } or Carp::croak("Data::NDArray::Shared: PDL interop needs PDL installed (cpanm PDL)") 36 0 36
31 or Carp::croak("Data::NDArray::Shared: PDL interop needs PDL installed (cpanm PDL)");
32 }
33 16 ▶sub _pdl_ctor {
Subroutine _pdl_ctor: called CC=3 SCAR=11.2
34 16 my ($dtype) = @_;
35 16 ▶ my $name = $PDL_TYPE{$dtype} or Carp::croak("no PDL type for dtype '$dtype'");
Branch: 1 branch
BranchTrueFalseTotal
my $name = $PDL_TYPE{$dtype} or Carp::croak("no PDL type for dtype '${dtype}'") 16 0 16
36 16 ▶ exists &{"PDL::$name"}
Branch: 1 branch
BranchTrueFalseTotal
exists &{"PDL::$name";} or Carp::croak("this PDL has no '${name}' type (needed for dtype '${dtype}'); upgrade PDL") 16 0 16
37 or Carp::croak("this PDL has no '$name' type (needed for dtype '$dtype'); upgrade PDL");
38 16 \&{"PDL::$name"};
39 }
40  
41 # NDArray -> a NEW (copied) PDL piddle; dims = reverse(shape).
42 13 ▶sub to_pdl {
Subroutine to_pdl: called CC=1 SCAR=0.0
Pod documented
43 13 my ($self) = @_;
44 13 _require_pdl();
45 13 my $p = PDL->new_from_specification(_pdl_ctor($self->dtype)->(), reverse $self->shape);
46 13 ${ $p->get_dataref } = $self->buffer; # read-locked snapshot
47 13 $p->upd_data;
48 13 return $p;
49 }
50  
51 # A NEW shared NDArray copied from a piddle; $path undef => anonymous mapping.
52 16 ▶sub from_pdl {
Subroutine from_pdl: called CC=5 SCAR=16.1
Pod documented
53 16 my ($class, $p, $path) = @_;
54 16 _require_pdl();
55 16 my $tname = "" . $p->type;
56 16 ▶ my $dt = $DTYPE_OF{$tname}
Branch: 1 branch
BranchTrueFalseTotal
my $dt = $DTYPE_OF{$tname} or Carp::croak("Data::NDArray::Shared->from_pdl: unsupported PDL type '${tname}'") 16 0 16
57 or Carp::croak("Data::NDArray::Shared->from_pdl: unsupported PDL type '$tname'");
58 16 $p = $p->copy;
59 16 my $self = $class->new($path, $dt, reverse $p->dims);
60 16 ▶ if (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 4 12 16
61 4 ▶ $self->dtype eq $dt or Carp::croak("from_pdl: dtype mismatch");
Branch: 1 branch
BranchTrueFalseTotal
$self->dtype eq $dt or Carp::croak("from_pdl: dtype mismatch") 3 1 4
62 3 my @shape = $self->shape;
63 3 ▶ "@shape" eq join(' ', reverse $p->dims) or Carp::croak("from_pdl: shape mismatch");
Branch: 1 branch
BranchTrueFalseTotal
join($", @shape) eq join(" ", reverse($p->dims)) or Carp::croak("from_pdl: shape mismatch") 2 1 3
64 }
65 14 $self->update_from_bytes(${ $p->get_dataref });
66 14 return $self;
67 }
68  
69 # Copy a piddle into THIS array in place (same dtype + shape); returns self.
70 4 ▶sub update_from_pdl {
Subroutine update_from_pdl: called CC=4 SCAR=13.9
Pod documented
71 4 my ($self, $p) = @_;
72 4 _require_pdl();
73 4 my $tname = "" . $p->type;
74 4 ▶ my $dt = $DTYPE_OF{$tname}
Branch: 1 branch
BranchTrueFalseTotal
my $dt = $DTYPE_OF{$tname} or Carp::croak("Data::NDArray::Shared->update_from_pdl: unsupported PDL type '${tname}'") 4 0 4
75 or Carp::croak("Data::NDArray::Shared->update_from_pdl: unsupported PDL type '$tname'");
76 4 ▶ $dt eq $self->dtype
Branch: 1 branch
BranchTrueFalseTotal
$dt eq $self->dtype or Carp::croak("update_from_pdl: dtype mismatch (piddle $dt vs array " . $self->dtype . ')') 3 1 4
77 or Carp::croak("update_from_pdl: dtype mismatch (piddle $dt vs array " . $self->dtype . ")");
78 3 my @want = reverse $self->shape;
79 3 my @got = $p->dims;
80 3 ▶ "@want" eq "@got"
Branch: 1 branch
BranchTrueFalseTotal
join($", @want) eq join($", @got) or Carp::croak("update_from_pdl: shape mismatch (array (@{[$self->shape];}) vs piddle dims (@got))") 2 1 3
81 1 or Carp::croak("update_from_pdl: shape mismatch (array (@{[ $self->shape ]}) vs piddle dims (@got))");
82 2 $p = $p->copy;
83 2 $self->update_from_bytes(${ $p->get_dataref });
84 2 return $self;
85 }
86  
87 # Zero-copy: a PDL ndarray ALIASING this array's shared mmap, built via PDL's C
88 # API (PDL_DONTTOUCHDATA, so PDL never frees/reallocates our mapping). In-place
89 # PDL ops write straight through (visible to every sharing process); reads see
90 # live data. NO locking -- coordinate access yourself. The array is kept alive
91 # while the piddle lives. Needs PDL at BUILD time (the C path); croaks otherwise.
92 3 ▶sub as_pdl_alias {
Subroutine as_pdl_alias: called CC=1 SCAR=0.1
Pod documented
93 3 my ($self) = @_;
94 3 _require_pdl();
95 3 my $typenum = _pdl_ctor($self->dtype)->()->enum; # PDL type number for our dtype
96 # _alias_pdl_create croaks if the module was built without PDL (no C path).
97 3 my $p = $self->_alias_pdl_create($typenum, [ reverse $self->shape ]); # dims in PDL order
98 0 return $p;
99 }
100  
101 7 1;
102 __END__
↑ Summary ndarray.h »