-
Notifications
You must be signed in to change notification settings - Fork 2
/
Run_Bsub.pm
executable file
·151 lines (106 loc) · 2.99 KB
/
Run_Bsub.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/local/bin/perl
package Run_Bsub;
use strict;
use warnings;
use Bsub;
use vars qw ($log_dir $keep_logs_on_failure);
use Cwd;
use Carp;
our $CMDS_PER_NODE; # by default, this is computed below. Set here specifically as needed.
my $NUM_NODES = 1000;
BEGIN {
$log_dir = cwd;
$keep_logs_on_failure = 1;
}
my $queue = "week";
my $memory = 4;
my $mount_test = undef;
my $max_nodes = undef;
sub set_queue {
my ($q) = @_;
$queue = $q;
return;
}
sub set_memory {
## Should be in G
my ($m) = @_;
$memory = $m;
}
sub set_mount_test {
my ($mt) = @_;
$mount_test = $mt;
}
sub set_max_nodes {
my ($mn) = @_;
$max_nodes = $mn;
return;
}
sub set_log_dir {
my ($log_dir_setting) = @_;
unless (-d $log_dir_setting) {
confess "Error, cannot find log directory $log_dir_setting";
}
$log_dir = $log_dir_setting;
return;
}
sub run {
my @cmds = @_;
my $num_cmds = scalar @cmds;
my $targeted_cmds_per_node = ($CMDS_PER_NODE)
? $CMDS_PER_NODE
: (int($num_cmds / $NUM_NODES) + 1);
#if ($CMDS_PER_NODE && $CMDS_PER_NODE < $targeted_cmds_per_node) {
# $targeted_cmds_per_node = $CMDS_PER_NODE;
#}
my $bsubber = new Bsub({cmds=>\@cmds,
log_dir => $log_dir,
cmds_per_node => $targeted_cmds_per_node,
queue => $queue,
memory => $memory,
mount_test => $mount_test,
max_nodes => $max_nodes,
}
);
$bsubber->bsub_jobs();
my $total_cmds = scalar (@cmds);
if (my @failed_cmds = $bsubber->get_failed_cmds()) {
my $num_failed_cmds = scalar (@failed_cmds);
my $msg = "Sorry, $num_failed_cmds of $total_cmds failed = " . ($num_failed_cmds
/ $total_cmds * 100) . " % failure.\n";
print $msg;
print STDERR $msg;
my @failed_cmd_lines;
open (my $failed_fh, ">failed_cmds.$$") or die $!;
foreach my $failed_cmd (@failed_cmds) {
my $cmd = $failed_cmd->{cmd};
my $ret = $failed_cmd->{ret};
print $failed_fh "$cmd\nRET($ret)\n\n";
push (@failed_cmd_lines, $cmd);
}
close $failed_fh;
$bsubber->clean_logs() unless $keep_logs_on_failure;
print "View file \'failed_cmds.$$\' for list of commands that failed.\n\n";
return (@failed_cmd_lines); # at least one job failed.
}
else {
$bsubber->clean_logs();
print "All $total_cmds completed successfully.\n\n";
return(); # all good.
}
}
package main;
use strict;
use warnings;
use File::Basename;
#### Test routine, run by executing module directly:
if (basename($0) eq 'Run_Bsub.pm') {
my $ret = &Run_Bsub::run("ls");
if ($ret) {
die "Error, test failed.\n";
}
else {
print "Test ran successfully.\n";
}
exit($ret);
}
1;