blib/lib/PDL/Parallel/threads.pm

filter: Statementstmt 73.9% 91 / 123 Branchbran 42.8% 18 / 42 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 87.5% 14 / 16 Podpod 60.0% 3 / 5 totaltotal 61.9% 130 / 210 CC 29 SCAR 34.3
CC
29
Cov
63%
share_pdls
73.9
retrieve_pdls
14.3
free_pdls
4.6
CRAP 31.0
« _build/lib/Module/Build/Custom.pm ↑ Summary blib/lib/PDL/Parallel/threads/SIMD.pm »
1 package PDL::Parallel::threads;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use PDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use PDL::IO::FastRaw;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 my $can_use_threads;
10 BEGIN {
11 4 ▶ $can_use_threads = eval {
Subroutine BEGIN: called CC=1 SCAR=0.5
12 4 require threads;
13 0 threads->import();
14 0 require threads::shared;
15 0 threads::shared->import();
16 0 1;
17 };
18
19 4 our $VERSION = '0.04';
20 4 ▶ use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 XSLoader::load 'PDL::Parallel::threads', $VERSION;
22 }
23  
24 # These are the means by which we share data across Perl threads. Note that
25 # we cannot share piddles directly accross threads, but we can share arrays
26 # of scalars, scalars whose integer values are the pointers to piddle data,
27 # etc.
28 4 my %datasv_pointers :shared;
29 4 my %dim_arrays :shared;
30 4 my %types :shared;
31 4 my %originating_tid :shared;
32 4 my %file_names :shared;
33  
34 4 require Exporter;
35 4 our @ISA = qw(Exporter);
36 4 our @EXPORT_OK = qw(share_pdls retrieve_pdls free_pdls);
37  
38 # PDL data should not be naively copied by Perl. This is only necessary
39 # for older versions of PDL. Newer versions contain the CLONE_SKIP
40 # method... which is nice except that it issues warning. So wrap this
41 # in a no-warnings block.
42 {
43 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 0 ▶ sub PDL::CLONE_SKIP { 1 }
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
45 # Also suppress the warnings that PDL now apparently issues
46 4 $PDL::no_clone_skip_warning = 1;
47 }
48  
49 sub auto_package_name {
50 18 ▶ my $name = shift;
Subroutine auto_package_name: called CC=2 SCAR=6.9
Pod undocumented
51 18 my ($package_name) = caller(1);
52 18 ▶ $name = "$package_name/$name" if $name =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\w+$/ 6 12 18
53 18 return $name;
54 }
55  
56 sub share_pdls {
57 4 ▶ croak("share_pdls: expected key/value pairs")
Subroutine share_pdls: called CC=16 SCAR=43.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 4 4
58 unless @_ % 2 == 0;
59 4 my %to_store = @_;
60
61 4 ▶ while (my ($name, $to_store) = each %to_store) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %to_store) 4 4 8
62 4 $name = auto_package_name($name);
63
64 # Make sure we're not overwriting already shared data
65 4 ▶ if (exists $datasv_pointers{$name} or exists $file_names{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $datasv_pointers{$name} or exists $file_names{$name}) 0 4 4
Condition33%1/3
exists $datasv_pointers{$name} or exists $file_names{$name}
l!l&&r!l&&!r
004
MC/DC0%0/2
exists $datasv_pointers{$name} or exists $file_names{$name}
exists $datasv_pointers{$name} exists $file_names{$name}
Truth table33%1/3
A or B
A= exists $datasv_pointers{$name}B= exists $file_names{$name}
ABresult
1X1
011
000
66 0 croak("share_pdls: you already have data associated with '$name'");
67 }
68
69 # Handle the special case where a memory mapped piddle was sent, and
70 # for which the memory mapped piddle knows its file name
71 4 ▶ if ( eval{$to_store->isa("PDL")}
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $to_store->isa("PDL") } } and exists $to_store->hdr->{"mmapped_filename"}) 0 4 4
Condition33%1/3
eval { do { $to_store->isa("PDL") } } and exists $to_store->hdr->{"mmapped_filename"}
!ll&&!rl&&r
040
MC/DC0%0/2
eval { do { $to_store->isa("PDL") } } and exists $to_store->hdr->{"mmapped_filename"}
eval { do { $to_store->isa("PDL") } } exists $to_store->hdr->{"mmapped_filename"}
Truth table33%1/3
A and B
A= eval { do { $to_store->isa("PDL") } }B= exists $to_store->hdr->{"mmapped_filename"}
ABresult
0X0
100
111
72 and exists $to_store->hdr->{mmapped_filename}
73 ) {
74 0 $to_store = $to_store->hdr->{mmapped_filename};
75 }
76
77 4 ▶ if ( eval{$to_store->isa("PDL")} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { $to_store->isa('PDL') } }) { } 4 0 4
elsif (ref $to_store eq '') { } 0 0 0
78 # Share piddle memory directly
79 4 $datasv_pointers{$name} = eval{_get_and_mark_datasv_pointer($to_store)};
80 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
81 0 my $error = $@;
82 0 chomp $error;
83 0 delete $datasv_pointers{$name};
84 0 croak('share_pdls: Could not share a piddle under '
85 . "name '$name' because $error");
86 }
87 4 ▶ if ($can_use_threads) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_use_threads) { } 0 4 4
88 0 $dim_arrays{$name} = shared_clone([$to_store->dims]);
89 0 $originating_tid{$name} = threads->tid;
90 }
91 else {
92 4 $dim_arrays{$name} = [$to_store->dims];
93 }
94 4 $types{$name} = $to_store->get_datatype;
95 }
96 elsif (ref($to_store) eq '') {
97 # A file name, presumably; share via memory mapping
98 0 ▶ if (-w $to_store and -r "$to_store.hdr") {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $to_store and -r "$to_store.hdr") { } 0 0 0
Condition0%0/3
-w $to_store and -r "$to_store.hdr"
!ll&&!rl&&r
000
MC/DC0%0/2
-w $to_store and -r "$to_store.hdr"
-w $to_store -r "$to_store.hdr"
Truth table0%0/3
A and B
A= -w $to_storeB= -r "$to_store.hdr"
ABresult
0X0
100
111
99 0 $file_names{$name} = $to_store;
100 }
101 else {
102 0 my $to_croak = join('', 'When share_pdls gets a scalar, it '
103 , 'expects that to be a file to share as '
104 , "memory mapped data.\nFor key '$name', "
105 , "'$to_store' was given, but");
106 0 ▶ croak("$to_croak there is no associated header file")
Branch: 1 branch
BranchTrueFalseTotal
unless -f "$to_store.hdr" 0 0 0
107 unless -f "$to_store.hdr";
108 0 ▶ croak("$to_croak you do not have permissions to read the "
Branch: 1 branch
BranchTrueFalseTotal
unless -r "$to_store.hdr" 0 0 0
109 . "associated header file") unless -r "$to_store.hdr";
110 0 ▶ croak("$to_croak you do not have write permissions for that "
Branch: 1 branch
BranchTrueFalseTotal
if -w $to_store 0 0 0
111 . "file") if -w $to_store;
112 # Default: the file does not exist
113 0 croak("$to_croak the file does not exist");
114 }
115 }
116 else {
117 0 croak("share_pdls passed data under '$name' that it doesn't "
118 . "know how to store");
119 }
120 }
121 }
122  
123  
124  
125 # Frees the memory associated with the given names.
126 sub free_pdls {
127 # Keep track of each name that is successfully freed
128 2 ▶ my @removed;
Subroutine free_pdls: called CC=4 SCAR=15.2
Pod documented
129
130 2 for my $short_name (@_) {
131 2 my $name = auto_package_name($short_name);
132
133 # If it's a regular piddle, decrement the memory's refcount
134 2 ▶ if (exists $datasv_pointers{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $datasv_pointers{$name}) { } 2 0 2
elsif (exists $file_names{$name}) { } 0 0 0
135 2 _dec_datasv_refcount($datasv_pointers{$name});
136 2 delete $datasv_pointers{$name};
137 2 delete $dim_arrays{$name};
138 2 delete $types{$name};
139 2 delete $originating_tid{$name};
140 2 push @removed, $name;
141 }
142 # If it's mmapped, remove the file name
143 elsif (exists $file_names{$name}) {
144 0 delete $file_names{$name};
145 0 push @removed, $name;
146 }
147 # If its none of the above, indicate that we didn't free anything
148 else {
149 0 push @removed, '';
150 }
151 }
152
153 2 return @removed;
154 }
155  
156 # PDL method to share an individual piddle
157 sub PDL::share_as {
158 4 ▶ my ($self, $name) = @_;
Subroutine share_as: called CC=1 SCAR=0.0
Pod undocumented
159 4 share_pdls(auto_package_name($name) => $self);
160 4 return $self;
161 }
162  
163 # Method to get a piddle that points to the shared data assocaited with the
164 # given name(s).
165 sub retrieve_pdls {
166 8 ▶ return if @_ == 0;
Subroutine retrieve_pdls: called CC=10 SCAR=26.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 8 8
167
168 8 my @to_return;
169 8 for my $short_name (@_) {
170 8 my $name = auto_package_name($short_name);
171
172 8 ▶ if (exists $datasv_pointers{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $datasv_pointers{$name}) { } 7 1 8
elsif (exists $file_names{$name}) { } 0 1 1
173 # Make sure that the originating thread still exists, or the
174 # data will be gone.
175 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
176 and not defined (threads->object($originating_tid{$name}))
177 ) {
178 0 croak("retrieve_pdls: '$name' was created in a thread that "
179 . "has ended or is detached");
180 }
181
182 # Create the new thinly wrapped piddle
183 my $new_piddle = _new_piddle_around($datasv_pointers{$name},
184 7 $types{$name});
185
186 # Set the dimensions
187 7 my @dims = @{$dim_arrays{$name}};
188 7 $new_piddle->setdims(\@dims);
189
190 # Set flags to protect the piddle's memory:
191 7 _update_piddle_data_state_flags($new_piddle);
192
193 7 push @to_return, $new_piddle;
194 }
195 elsif (exists $file_names{$name}) {
196 0 push @to_return, mapfraw($file_names{$name});
197 }
198 else {
199 1 croak("retrieve_pdls could not find data associated with '$name'");
200 }
201 }
202
203 # In list context, return all the piddles
204 7 ▶ return @to_return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 7 7
205
206 # Scalar context only makes sense if they asked for a single name
207 7 ▶ return $to_return[0] if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 7 0 7
208
209 # We're here if they asked for multiple names but assigned the result
210 # to a single scalar, which is probably not what they meant:
211 0 carp("retrieve_pdls: requested many piddles... in scalar context?");
212 0 return $to_return[0];
213 }
214  
215 # Now for a nasty hack: this code modifies PDL::IO::FastRaw's symbol table
216 # so that it adds the "mmapped_filename" key to the piddle's header before
217 # returning the result. As long as the user says "use PDL::IO::FastRaw"
218 # *after* using this module, this will allow for transparent sharing of both
219 # memory mapped and standard piddles.
220  
221 {
222 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
223 4 my $old_sub = \&PDL::IO::FastRaw::mapfraw;
224 *PDL::IO::FastRaw::mapfraw = sub {
225 0 ▶ my $name = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
226 0 my $to_return = $old_sub->(@_);
227 0 $to_return->hdr->{mmapped_filename} = $name;
228 0 return $to_return;
229 4 };
230 }
231  
232 4 1;
233  
234 __END__
« _build/lib/Module/Build/Custom.pm ↑ Summary blib/lib/PDL/Parallel/threads/SIMD.pm »