blib/lib/Algorithm/Classifier/IsolationForest/App/Command/accel.pm

filter: Statementstmt 84.3% 54 / 64 Branchbran 40.0% 16 / 40 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 77.7% 7 / 9 Podpod 100% 5 / 5 totaltotal 63.9% 85 / 133 CC 24 SCAR 38.1
CC
24
Cov
65%
execute
59.7
abstract
2.0
description
2.0
CRAP 45.1
« blib/lib/Algorithm/Classifier/IsolationForest/App/Command/fit.pm ↑ Summary blib/lib/Algorithm/Classifier/IsolationForest/App/Command/explain.pm »
1 package Algorithm::Classifier::IsolationForest::App::Command::accel;
2  
3 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 86 ▶use Algorithm::Classifier::IsolationForest ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 86 ▶use Algorithm::Classifier::IsolationForest::App -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub opt_spec { () }
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
9  
10 0 ▶sub abstract { 'Report which (if any) native acceleration backend is active' }
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
11  
12 0 ▶sub description {
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
13 0 'Initialises Algorithm::Classifier::IsolationForest, fits
14 a tiny synthetic dataset to exercise the optional native code path, then
15 reports which acceleration (if any) is wired up:
16  
17 * Inline::C -- C scoring backend compiled at module load
18 * OpenMP -- parallel tree-walk across CPU cores (requires libgomp)
19 * SIMD -- vectorised oblique dot product in extended mode
20 (gated on OpenMP 4.0+; relies on `#pragma omp simd`)
21  
22 The detection happens automatically the first time the module is loaded.
23 When the distribution was installed with Inline::C available, a prebuilt
24 object compiled at `make` time is loaded directly; otherwise the backend
25 is compiled on first load and cached under _Inline/. If no backend is
26 active the module falls back to a pure-Perl implementation.
27  
28 Build flags are tunable via environment variables set before first load
29 (and, to pick what gets baked into the prebuilt object, before
30 `perl Makefile.PL`):
31  
32 * IF_ARCH=<value> -- -march=<value> (e.g. x86-64-v3, skylake, znver3)
33 * IF_NATIVE=1 -- shorthand for IF_ARCH=native; ignored if IF_ARCH is set
34 * IF_OPT=<-Olevel> -- override the default -O3
35 * IF_NO_OPENMP=1 -- serial C backend: no libgomp linkage at all
36 * IF_RUNTIME_BUILD=1 -- ignore the prebuilt object, compile at first load
37 * IF_NO_C=1 -- skip the C backend entirely
38  
39 See "NATIVE ACCELERATION" in perldoc Algorithm::Classifier::IsolationForest
40 for details and tradeoffs (in particular, why IF_NATIVE is not always a
41 safe default choice).
42 ';
43 } ## end sub description
44  
45 1 ▶sub validate { 1 }
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
46  
47 1 ▶sub execute {
Subroutine execute: called CC=24 SCAR=40.9
Pod documented
48 1 my ( $self, $opt, $args ) = @_;
49  
50 # Tiny deterministic dataset. Fitting + scoring confirms the chosen
51 # backend is callable end-to-end, not merely that it compiled. We
52 # exercise both axis mode (covers score_all_xs's axis branch) and
53 # extended mode (covers the oblique branch -- where the
54 # `#pragma omp simd` reduction lives, so this is the only path
55 # SIMD actually matters for).
56 1 srand(1);
57 1 my @data = map { [ rand(), rand(), rand() ] } 1 .. 30;
58 1 push @data, [ 10, 10, 10 ], [ -10, -10, -10 ];
59  
60 1 my $axis = Algorithm::Classifier::IsolationForest->new(
61 n_trees => 10,
62 sample_size => 32,
63 seed => 1,
64 );
65 1 $axis->fit( \@data );
66 1 $axis->score_samples( [ [ 0.5, 0.5, 0.5 ] ] );
67  
68 1 my $ext = Algorithm::Classifier::IsolationForest->new(
69 n_trees => 10,
70 sample_size => 32,
71 seed => 1,
72 mode => 'extended',
73 );
74 1 $ext->fit( \@data );
75 1 $ext->score_samples( [ [ 0.5, 0.5, 0.5 ] ] );
76  
77 1 ▶ my $has_c = $Algorithm::Classifier::IsolationForest::HAS_C ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Algorithm::Classifier::IsolationForest::HAS_C ? : 1 0 1
78 1 ▶ my $has_openmp = $Algorithm::Classifier::IsolationForest::HAS_OPENMP ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Algorithm::Classifier::IsolationForest::HAS_OPENMP ? : 1 0 1
79 1 ▶ my $has_simd = $Algorithm::Classifier::IsolationForest::HAS_SIMD ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$Algorithm::Classifier::IsolationForest::HAS_SIMD ? : 1 0 1
80  
81 1 my $c_source = $Algorithm::Classifier::IsolationForest::C_SOURCE;
82  
83 # Whether a prebuilt object was installed with the dist at all --
84 # independent of whether this process ended up using it.
85 1 my $prebuilt_installed = 0;
86 {
87 1 local $@;
88 1 my $bf = eval {
89 1 require Algorithm::Classifier::IsolationForest::BuildFlags;
90 1 Algorithm::Classifier::IsolationForest::BuildFlags::flags();
91 };
92 1 ▶ $prebuilt_installed = 1 if ref $bf eq 'HASH' && $bf->{prebuilt};
Branch: 1 branch
BranchTrueFalseTotal
if ref $bf eq "HASH" and $bf->{"prebuilt"} 0 1 1
Condition33%1/3
ref $bf eq "HASH" and $bf->{"prebuilt"}
!ll&&!rl&&r
010
MC/DC0%0/2
ref $bf eq "HASH" and $bf->{"prebuilt"}
ref $bf eq "HASH" $bf->{"prebuilt"}
Truth table33%1/3
A and B
A= ref $bf eq "HASH"B= $bf->{"prebuilt"}
ABresult
0X0
100
111
93 }
94  
95 1 ▶ my $source_desc
Branch: 2 branches
BranchTrueFalseTotal
$c_source eq 'prebuilt' ? : 0 1 1
$c_source eq 'runtime' ? : 1 0 1
96 = $c_source eq 'prebuilt' ? 'prebuilt at install time'
97 : $c_source eq 'runtime' ? 'compiled at run time (cached under _Inline/)'
98 : 'none (pure-Perl fallback)';
99  
100 1 print "Algorithm::Classifier::IsolationForest acceleration status\n";
101 1 ▶ print " Inline::C : ", ( $has_c ? "available\n" : "not available\n" );
Branch: 1 branch
BranchTrueFalseTotal
$has_c ? : 1 0 1
102 1 ▶ print " OpenMP : ", ( $has_openmp ? "available\n" : "not available\n" );
Branch: 1 branch
BranchTrueFalseTotal
$has_openmp ? : 1 0 1
103 1 ▶ print " SIMD : ", ( $has_simd ? "available\n" : "not available\n" );
Branch: 1 branch
BranchTrueFalseTotal
$has_simd ? : 1 0 1
104 1 print " C object : $source_desc\n";
105 1 ▶ if ( $c_source ne 'prebuilt' && $prebuilt_installed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_source ne "prebuilt" and $prebuilt_installed) 0 1 1
Condition33%1/3
$c_source ne "prebuilt" and $prebuilt_installed
!ll&&!rl&&r
010
MC/DC0%0/2
$c_source ne "prebuilt" and $prebuilt_installed
$c_source ne "prebuilt" $prebuilt_installed
Truth table33%1/3
A and B
A= $c_source ne "prebuilt"B= $prebuilt_installed
ABresult
0X0
100
111
106 0 my @why;
107 0 ▶ push @why, 'IF_RUNTIME_BUILD' if $ENV{IF_RUNTIME_BUILD};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"IF_RUNTIME_BUILD"} 0 0 0
108 0 ▶ push @why, 'IF_NO_C' if $ENV{IF_NO_C};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"IF_NO_C"} 0 0 0
109 0 push @why, grep { defined $ENV{$_} } qw(IF_OPT IF_ARCH IF_NATIVE IF_NO_OPENMP);
110 0 ▶ print " (a prebuilt object is installed but was not used",
Branch: 1 branch
BranchTrueFalseTotal
@why ? : 0 0 0
111 (
112 @why
113 ? '; runtime overrides active: ' . join( ', ', @why )
114 : '; it failed to load'
115 ),
116 ")\n";
117 } ## end if ( $c_source ne 'prebuilt' && $prebuilt_installed)
118 1 ▶ if ($has_c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_c) 1 0 1
119 1 printf " Build flags: %s\n", $Algorithm::Classifier::IsolationForest::OPT_LEVEL;
120 }
121 1 print "\n";
122  
123 # Build a one-line backend summary that lists every active feature
124 # and where the C object came from.
125 1 my @features;
126 1 ▶ push @features, 'OpenMP' if $has_openmp;
Branch: 1 branch
BranchTrueFalseTotal
if $has_openmp 1 0 1
127 1 ▶ push @features, 'SIMD' if $has_simd;
Branch: 1 branch
BranchTrueFalseTotal
if $has_simd 1 0 1
128  
129 1 ▶ my $suffix
Branch: 2 branches
BranchTrueFalseTotal
$c_source eq 'prebuilt' ? : 0 1 1
$c_source eq 'runtime' ? : 1 0 1
130 = $c_source eq 'prebuilt' ? ' -- prebuilt at install time'
131 : $c_source eq 'runtime' ? ' -- compiled at run time'
132 : '';
133  
134 1 ▶ if ( $has_c && @features ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_c and @features) { } 1 0 1
elsif ($has_c) { } 0 0 0
Condition33%1/3
$has_c and @features
!ll&&!rl&&r
001
MC/DC0%0/2
$has_c and @features
$has_c @features
Truth table33%1/3
A and B
A= $has_cB= @features
ABresult
0X0
100
111
135 1 printf "Active backend: Inline::C with %s%s\n", join( ' + ', @features ), $suffix;
136 } elsif ($has_c) {
137 0 print "Active backend: Inline::C (serial, scalar)$suffix\n";
138 } else {
139 0 print "Active backend: pure Perl (no native acceleration)\n";
140 }
141  
142 1 return 1;
143 } ## end sub execute
144  
145 =head1 NAME
146  
147 Algorithm::Classifier::IsolationForest::App::Command::accel - Report which (if any) native acceleration backend is active
148  
149 =head1 DESCRIPTION
150  
151 Fits and scores a tiny synthetic dataset in both axis and extended mode,
152 then reports which of Inline::C, OpenMP and SIMD are actually wired up.
153 Both modes are exercised on purpose: the oblique split is the only path
154 the SIMD reduction affects, so an axis-only probe would not prove it.
155  
156 Fitting and scoring, rather than merely checking that the backend
157 compiled, is what makes this a usable answer to "did this build pick up
158 the optional dependencies on this machine?".
159  
160 Run it as C<iforest accel>; C<iforest help accel> lists every option.
161  
162 =head1 METHODS
163  
164 L<App::Cmd> calls these while dispatching the subcommand. Nothing else
165 should.
166  
167 =head2 opt_spec
168  
169 Returns this command's option specifications, as the list of arrayrefs
170 L<Getopt::Long::Descriptive> expects.
171  
172 =head2 abstract
173  
174 Returns the one-line summary C<iforest commands> prints beside the
175 command name.
176  
177 =head2 description
178  
179 Returns the long help text C<iforest help accel> prints under the option
180 list.
181  
182 =head2 validate
183  
184 Checks the parsed options before anything is read or written, so a
185 mistake costs nothing.
186  
187 C<accel> takes no options, so there is nothing to check.
188  
189 Takes the parsed options hashref and the arrayref of remaining
190 arguments. Calls C<usage_error>, which prints the usage and exits, on
191 the first problem it finds, and returns 1 when everything checks out.
192  
193 =head2 execute
194  
195 Runs the probe fits and prints the backend report, including the build
196 flags baked into the object, to STDOUT.
197  
198 Takes the parsed options hashref and the arrayref of remaining
199 arguments, and returns 1.
200  
201 =cut
202  
203 86 return 1;
« blib/lib/Algorithm/Classifier/IsolationForest/App/Command/fit.pm ↑ Summary blib/lib/Algorithm/Classifier/IsolationForest/App/Command/explain.pm »