blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler/ResolveDeps/AssignTaskDeps.pm

filter: Statementstmt 14.0% 16 / 114 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 22.2% 4 / 18 Podpod 40.0% 4 / 10 totaltotal 13.8% 24 / 173 CC 18 SCAR 22.1
CC
18
Cov
11%
scheduler_ids_by_batch
20.0
update_job_scheduler_deps_by_task
12.0
batch_scheduler_ids_by_task
12.0
CRAP 9.1
« blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler.pm ↑ Summary blib/lib/HPC/Runner/Command/stats/Logger/JSON/Summary/TableOutput.pm »
1 package HPC::Runner::Command::submit_jobs::Utils::Scheduler::ResolveDeps::AssignTaskDeps;
2  
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use List::MoreUtils qw(first_index indexes uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head3 update_job_scheduler_ids_by_task
10  
11 #TODO do this after the all batches for a single job have been passed
12  
13 #DEPRACATED job_scheduler_ids_by_array
14  
15 for job at jobs
16 for batch at batches
17 for task at tasks
18  
19 =cut
20  
21 sub update_job_scheduler_deps_by_task {
22 0 ▶ my $self = shift;
Subroutine update_job_scheduler_deps_by_task: not called CC=3 SCAR=24.8
Pod undocumented
23  
24 0 $self->app_log->info(
25 'Updating task dependencies. This may take some time...');
26  
27 0 foreach my $job ( $self->all_schedules ) {
28 0 ▶ next if $self->jobs->{$job}->submission_failure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->jobs->{$job}->submission_failure 0 0 0
29 0 $self->current_job($job);
30 0 $self->batch_scheduler_ids_by_task;
31 }
32  
33 ##TODO consider changing this to each schedule
34 0 $self->update_job_deps;
35 }
36  
37 sub batch_scheduler_ids_by_task {
38 0 ▶ my $self = shift;
Subroutine batch_scheduler_ids_by_task: not called CC=3 SCAR=24.8
Pod undocumented
39  
40 0 ▶ return unless $self->jobs->{ $self->current_job }->has_deps;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->jobs->{$self->current_job}->has_deps 0 0 0
41  
42 $self->batch_counter(
43 0 $self->jobs->{ $self->current_job }->{batch_index_start} );
44  
45 0 my $scheduler_index = $self->process_all_batch_deps;
46  
47 0 ▶ while ( my ( $dep_job, $v ) = each %{$scheduler_index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$scheduler_index;}) 0 0 0
48 0 my @dep_jobs = @{$v};
49 0 my $dep_indices = $scheduler_index->{$dep_job};
50 0 $self->dep_scheduler_ids_by_task( $dep_job, $dep_indices );
51 }
52  
53 }
54  
55 has 'dep_scheduler_ids_by_task_cache' => (
56 is => 'rw',
57 isa => 'HashRef',
58 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 1 clearer => 'clear_dep_scheduler_ids_by_task_cache',
60 );
61  
62 sub dep_scheduler_ids_by_task {
63 0 ▶ my $self = shift;
Subroutine dep_scheduler_ids_by_task: not called CC=3 SCAR=24.8
Pod undocumented
64 0 my $dep_job = shift;
65 0 my $dep_indices = shift;
66  
67 0 ▶ for ( my $y = 0 ; $y < scalar @{$dep_indices} ; $y++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < scalar @{$dep_indices;}) 0 0 0
68 ##This is the current_batch_index
69  
70 0 my $batch_ref =
71 $self->check_find_dep_indexes_cache( $self->current_job, $y );
72  
73 0 ▶ for ( my $z = 0 ; $z < scalar @{ $dep_indices->[$y] } ; $z++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z < scalar @{$dep_indices->[$y];}) 0 0 0
74 #This is the dependency_batch_index
75  
76 0 my $dep_index = $dep_indices->[$y]->[$z];
77 0 my $dep_ref =
78 $self->check_find_dep_indexes_cache( $dep_job, $dep_index );
79  
80 0 my $array_dep = $self->build_task_deps(
81 $batch_ref->[0], $dep_ref->[0],
82 $batch_ref->[1], $dep_ref->[1],
83 );
84  
85 0 $self->push_array_deps($array_dep);
86 }
87 }
88  
89 0 $self->clean_array_deps;
90 }
91  
92 =head3 assign_scheduler_deps
93  
94 Jobs should only depend upon all jobs they need - not all jobs from the previous dep
95  
96 =cut
97  
98 sub assign_scheduler_deps {
99 0 ▶ my $self = shift;
Subroutine assign_scheduler_deps: not called CC=1 SCAR=6.9
Pod documented
100 0 my $batch_scheduler_id = shift;
101 0 my $dep_scheduler_id = shift;
102 # my $batch_task_index = shift;
103 # my $dep_task_index = shift;
104  
105 0 my $array_dep = [ $batch_scheduler_id, $dep_scheduler_id, ];
106  
107 0 return $array_dep;
108 }
109  
110 sub check_find_dep_indexes_cache {
111 0 ▶ my $self = shift;
Subroutine check_find_dep_indexes_cache: not called CC=2 SCAR=17.9
Pod undocumented
112 0 my $job = shift;
113 0 my $index = shift;
114  
115 0 ▶ if ( exists $self->dep_scheduler_ids_by_task_cache->{$job}->{$index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->dep_scheduler_ids_by_task_cache->{$job}{$index}) { } 0 0 0
116 0 return $self->dep_scheduler_ids_by_task_cache->{$job}->{$index};
117 }
118 else {
119 my $scheduler_id =
120 0 $self->jobs->{$job}->{batches}->[$index]->{scheduler_id};
121  
122 my $task_index =
123 $self->jobs->{$job}->batches->[$index]->cmd_start +
124 0 $self->jobs->{$job}->{cmd_start};
125  
126 0 $self->dep_scheduler_ids_by_task_cache->{$job}->{$index} =
127 [ $scheduler_id, $task_index ];
128  
129 0 return $self->dep_scheduler_ids_by_task_cache->{$job}->{$index};
130 }
131 }
132  
133 sub push_array_deps {
134 0 ▶ my $self = shift;
Subroutine push_array_deps: not called CC=2 SCAR=17.9
Pod undocumented
135 0 my $array_dep = shift;
136  
137 0 ▶ if ( $self->exists_array_dep( $array_dep->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists_array_dep($array_dep->[0])) { } 0 0 0
138 0 push( @{ $self->array_deps->{ $array_dep->[0] } }, $array_dep->[1] );
139 }
140 else {
141 0 $self->array_deps->{ $array_dep->[0] } = [ $array_dep->[1] ];
142 }
143 }
144  
145 sub clean_array_deps {
146 0 ▶ my $self = shift;
Subroutine clean_array_deps: not called CC=2 SCAR=17.9
Pod undocumented
147  
148 0 ▶ while ( my ( $k, $v ) = each %{ $self->array_deps } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->array_deps;}) 0 0 0
149 0 my @uniq = uniq( @{$v} );
150 0 @uniq = sort @uniq;
151 0 $self->array_deps->{$k} = \@uniq;
152 }
153 }
154  
155 =head3 update_scheduler_ids_by_array
156  
157 Update the scheduler ids by the task/batch
158  
159 #TODO There must be a better way to do this
160  
161 =cut
162  
163 sub update_scheduler_ids_by_array {
164 0 ▶ my $self = shift;
Subroutine update_scheduler_ids_by_array: not called CC=3 SCAR=24.8
Pod documented
165  
166 0 my $current_batch_index = $self->batch_counter - 1;
167  
168 0 my $index_in_batch =
169 $self->index_in_batch( $self->current_job, $current_batch_index );
170  
171 0 ▶ if ( !defined $index_in_batch ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index_in_batch) 0 0 0
172 0 $self->app_log->warn( "Job "
173 . $self->current_job
174 . " does not have an appropriate index. If you think are reaching this in error please report the issue to github.\n"
175 );
176 0 return;
177 }
178  
179 my $batch_scheduler_id =
180 0 $self->jobs->{ $self->current_job }->scheduler_ids->[$index_in_batch];
181  
182 ##IF there is no batch id, that means something went wrong with submission
183 0 ▶ $self->current_batch->scheduler_id($batch_scheduler_id)
Branch: 1 branch
BranchTrueFalseTotal
if $batch_scheduler_id 0 0 0
184 if $batch_scheduler_id;
185 }
186  
187 =head3 index_in_batch
188  
189 Using job arrays each job is divided into one or batches of size self->max_array_size
190  
191 max_array_size = 10
192 001_job.sh --array=1-10
193 002_job.sh --array=10-11
194  
195 self->jobs->{a_job}->all_batch_indexes
196  
197 job001 => [
198 {batch_index_start => 1, batch_index_end => 10 },
199 {batch_index_start => 11, batch_index_end => 20}
200 ]
201  
202 The index argument is zero indexed, and our counters (job_counter, batch_counter) are 1 indexed
203  
204 =cut
205  
206 sub index_in_batch {
207 0 ▶ my $self = shift;
Subroutine index_in_batch: not called CC=1 SCAR=6.9
Pod documented
208 0 my $job = shift;
209 0 my $index = shift;
210  
211 0 $index++;
212  
213 0 my $batches = $self->jobs->{$job}->batch_indexes;
214 0 return check_batch_index( $batches, $index );
215 }
216  
217 1 memoize('check_batch_index');
218  
219 sub check_batch_index {
220 0 ▶ my $batches = shift;
Subroutine check_batch_index: not called CC=2 SCAR=17.9
221 0 my $search_index = shift;
222  
223 my $x = first_index {
224 0 ▶ search_index( $_, $search_index );
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 }
226 0 @{$batches};
227  
228 0 ▶ return $x if defined $x;
Branch: 1 branch
BranchTrueFalseTotal
if defined $x 0 0 0
229 0 return undef;
230 }
231  
232 1 memoize('search_index');
233  
234 sub search_index {
235 0 ▶ my $batch_index = shift;
Subroutine search_index: not called CC=3 SCAR=24.8
236 0 my $search_index = shift;
237 0 my $batch_start = $batch_index->{batch_index_start};
238 0 my $batch_end = $batch_index->{batch_index_end};
239  
240 0 ▶ if ( $search_index >= $batch_start && $search_index <= $batch_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_index >= $batch_start and $search_index <= $batch_end) 0 0 0
Condition0%0/3
$search_index >= $batch_start and $search_index <= $batch_end
!ll&&!rl&&r
000
MC/DC0%0/2
$search_index >= $batch_start and $search_index <= $batch_end
$search_index >= $batch_start $search_index <= $batch_end
Truth table0%0/3
A and B
A= $search_index >= $batch_startB= $search_index <= $batch_end
ABresult
0X0
100
111
241 0 return 1;
242 }
243 0 return undef;
244 }
245  
246 =head3 scheduler_ids_by_batch
247  
248 ##DEPRACATED
249  
250 =cut
251  
252 sub scheduler_ids_by_batch {
253 0 ▶ my $self = shift;
Subroutine scheduler_ids_by_batch: not called CC=4 SCAR=30.0
Pod documented
254  
255 0 my $scheduler_index = $self->process_batch_deps( $self->current_batch );
256  
257 0 my @jobs = keys %{$scheduler_index};
258  
259 0 my @scheduler_ids = ();
260  
261 0 foreach my $job (@jobs) {
262 0 my $batch_index = $scheduler_index->{$job};
263 0 my $dep_scheduler_ids = $self->jobs->{$job}->scheduler_ids;
264  
265 0 foreach my $index ( @{$batch_index} ) {
266 0 push( @scheduler_ids, $dep_scheduler_ids->[$index] );
267 }
268 }
269  
270 0 ▶ $self->scheduler_ids( \@scheduler_ids ) if @scheduler_ids;
Branch: 1 branch
BranchTrueFalseTotal
if @scheduler_ids 0 0 0
271 }
272  
273 1 1;
« blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler.pm ↑ Summary blib/lib/HPC/Runner/Command/stats/Logger/JSON/Summary/TableOutput.pm »