blib/lib/PDL/Parallel/threads.pm

filter: Statementstmt 84.1% 90 / 107 Branchbran 59.3% 19 / 32 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 11 / 11 Podpod 60.0% 3 / 5 totaltotal 76.2% 125 / 164 CC 21 SCAR 21.5
CC
21
Cov
77%
retrieve_pdls
13.7
share_pdls
12.2
free_pdls
3.0
CRAP 8.6
« blib/lib/PDL/ImageND.pm ↑ Summary blib/lib/PDL/IO/FastRaw.pm »
1 package PDL::Parallel::threads;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use PDL::LiteF;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 our $VERSION = '0.07';
9  
10 3 our @ISA = ( 'PDL::Exporter' );
11 3 our @EXPORT_OK = qw(share_pdls retrieve_pdls free_pdls);
12 3 our %EXPORT_TAGS = (Func=>\@EXPORT_OK);
13  
14 3 my $can_use_threads;
15 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=2.6
16 3 $can_use_threads = eval {
17 3 require threads;
18 0 threads->import();
19 0 require threads::shared;
20 0 threads::shared->import();
21 0 1;
22 };
23 }
24  
25 # These are the means by which we share data across Perl threads. Note that
26 # we cannot share ndarrays directly across threads, but we can share arrays
27 # of scalars, scalars whose integer values are the pointers to ndarray data,
28 # etc.
29 3 my %datasv_pointers :shared;
30 3 my %dataref_svs;
31 3 my %dim_arrays :shared;
32 3 my %types :shared;
33 3 my %badflag :shared;
34 3 my %badvalue :shared;
35 3 my %nbytes :shared;
36 3 my %originating_tid :shared;
37  
38 # PDL data should not be naively copied by Perl. Tell PDL we know about this
39 3 $PDL::no_clone_skip_warning = 1;
40  
41 18 ▶sub auto_package_name {
Subroutine auto_package_name: called CC=2 SCAR=6.9
Pod undocumented
42 18 my $name = shift;
43 18 my ($package_name) = caller(1);
44 18 ▶ $name = "$package_name/$name" if $name =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\w+$/ 6 12 18
45 18 return $name;
46 }
47  
48 4 ▶sub share_pdls {
Subroutine share_pdls: called CC=9 SCAR=25.0
Pod documented
49 4 ▶ croak("share_pdls: expected key/value pairs")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 4 4
50 unless @_ % 2 == 0;
51 4 my %to_store = @_;
52  
53 4 ▶ while (my ($name, $to_store) = each %to_store) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %to_store) 4 4 8
54 4 $name = auto_package_name($name);
55  
56 # Make sure we're not overwriting already shared data
57 4 ▶ if (exists $datasv_pointers{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $datasv_pointers{$name}) 0 4 4
58 0 croak("share_pdls: you already have data associated with '$name'");
59 }
60  
61 4 ▶ if ( eval{$to_store->isa("PDL")} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $to_store->isa('PDL') } }) { } 4 0 4
62 # Integers (which can be cast to and from
63 # pointers) are easily shared using threads::shared
64 # in a shared hash. This method provides a
65 # way to obtain the pointer to the datasv for
66 # the incoming ndarray, and it increments the
67 # SV's refcount.
68 4 $dataref_svs{$name} = eval{
69 4 ▶ croak("the ndarray does not have any allocated memory\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $to_store->allocated 0 4 4
70 if !$to_store->allocated;
71 4 $to_store->get_dataref;
72 };
73 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
74 0 my $error = $@;
75 0 chomp $error;
76 0 delete $datasv_pointers{$name};
77 0 croak('share_pdls: Could not share an ndarray under '
78 . "name '$name' because $error");
79 }
80 4 $datasv_pointers{$name} = 0+$dataref_svs{$name};
81 4 $to_store->set_donttouchdata($nbytes{$name} = $to_store->nbytes); # protect its memory
82 4 ▶ if ($can_use_threads) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_use_threads) { } 0 4 4
83 0 $dim_arrays{$name} = shared_clone([$to_store->dims]);
84 0 $originating_tid{$name} = threads->tid;
85 }
86 else {
87 4 $dim_arrays{$name} = [$to_store->dims];
88 }
89 4 $types{$name} = $to_store->get_datatype;
90 4 $badflag{$name} = $to_store->badflag;
91 4 my $badval = $to_store->badvalue->sclr;
92 4 ▶ $badval = shared_clone([$badval->Re,$badval->Im]) if ref $badval ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $badval 0 4 4
93 4 $badvalue{$name} = $badval;
94 }
95 else {
96 0 croak("share_pdls passed data under '$name' that it doesn't "
97 . "know how to store");
98 }
99 }
100 }
101  
102  
103  
104 # Frees the memory associated with the given names.
105 2 ▶sub free_pdls {
Subroutine free_pdls: called CC=3 SCAR=11.0
Pod documented
106 # Keep track of each name that is successfully freed
107 2 my @removed;
108  
109 2 for my $short_name (@_) {
110 2 my $name = auto_package_name($short_name);
111  
112 # If it's a regular ndarray, decrement the memory's refcount
113 2 ▶ if (exists $datasv_pointers{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $datasv_pointers{$name}) { } 2 0 2
114 2 delete $dataref_svs{$name};
115 2 delete $datasv_pointers{$name};
116 2 delete $dim_arrays{$name};
117 2 delete $types{$name};
118 2 delete $badflag{$name};
119 2 delete $badvalue{$name};
120 2 delete $nbytes{$name};
121 2 delete $originating_tid{$name};
122 2 push @removed, $name;
123 }
124 # If its none of the above, indicate that we didn't free anything
125 else {
126 0 push @removed, '';
127 }
128 }
129  
130 2 return @removed;
131 }
132  
133 # PDL method to share an individual ndarray
134 4 ▶sub PDL::share_as {
Subroutine share_as: called CC=1 SCAR=0.0
Pod undocumented
135 4 my ($self, $name) = @_;
136 4 share_pdls(auto_package_name($name) => $self);
137 4 return $self;
138 }
139  
140 # Method to get an ndarray that points to the shared data associated with the
141 # given name(s).
142 8 ▶sub retrieve_pdls {
Subroutine retrieve_pdls: called CC=10 SCAR=26.2
Pod documented
143 8 ▶ return if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 8 8
144  
145 8 my @to_return;
146 8 for my $short_name (@_) {
147 8 my $name = auto_package_name($short_name);
148  
149 8 ▶ if (exists $datasv_pointers{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $datasv_pointers{$name}) { } 7 1 8
150 # Make sure that the originating thread still exists, or the
151 # data will be gone.
152 7 ▶ if ($can_use_threads and $originating_tid{$name} > 0
Branch: 1 branch
BranchTrueFalseTotal
if ($can_use_threads and $originating_tid{$name} > 0 and not defined "threads"->object($originating_tid{$name})) 0 7 7
Condition33%2/6
$can_use_threads and $originating_tid{$name} > 0 and not defined "threads"->object($originating_tid{$name})
!ll&&!rl&&r
700
$can_use_threads and $originating_tid{$name} > 0
!ll&&!rl&&r
700
MC/DC0%0/3
$can_use_threads and $originating_tid{$name} > 0 and not defined "threads"->object($originating_tid{$name})
$can_use_threads $originating_tid{$name} > 0 not defined "threads"->object($originating_tid{$name})
Truth table25%1/4
A and B and not(C)
A= $can_use_threadsB= $originating_tid{$name} > 0C= not defined "threads"->object($originating_tid{$name})
ABCresult
0XX0
10X0
1100
1111
153 and not defined (threads->object($originating_tid{$name}))
154 ) {
155 0 croak("retrieve_pdls: '$name' was created in a thread that "
156 . "has ended or is detached");
157 }
158  
159 # Create the new thinly wrapped ndarray
160 7 my $new_ndarray = PDL->new_around_datasv($datasv_pointers{$name});
161 7 $new_ndarray->set_datatype($types{$name});
162 7 $new_ndarray->badflag($badflag{$name});
163 $new_ndarray->badvalue(ref $badvalue{$name}
164 0 ? Math::Complex->make(@{$badvalue{$name}})
165 7 ▶ : $badvalue{$name});
Branch: 1 branch
BranchTrueFalseTotal
ref $badvalue{$name} ? : 0 7 7
166 7 $new_ndarray->setdims(\@{$dim_arrays{$name}});
167 7 $new_ndarray->set_donttouchdata($nbytes{$name}); # protect its memory
168 7 push @to_return, $new_ndarray;
169 }
170 else {
171 1 croak("retrieve_pdls could not find data associated with '$name'");
172 }
173 }
174  
175 # In list context, return all the ndarrays
176 7 ▶ return @to_return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 7 7
177  
178 # Scalar context only makes sense if they asked for a single name
179 7 ▶ return $to_return[0] if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 7 0 7
180  
181 # We're here if they asked for multiple names but assigned the result
182 # to a single scalar, which is probably not what they meant:
183 0 carp("retrieve_pdls: requested many ndarrays... in scalar context?");
184 0 return $to_return[0];
185 }
186  
187 3 1;
188  
189 __END__
« blib/lib/PDL/ImageND.pm ↑ Summary blib/lib/PDL/IO/FastRaw.pm »