Repository navigation
Expand file tree
/
Copy pathsimulation_run.pl
More file actions
158 lines (133 loc) · 4.4 KB
/
Copy pathsimulation_run.pl
File metadata and controls
158 lines (133 loc) · 4.4 KB
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
151
152
153
154
155
156
157
158
#! /usr/bin/perl
#-------------------------------------------------------------------------------
# Script for starting the simulation. Without arguments, will ask for the first
# and last number of partitions. You can override it by passing it as arguments.
# Usage : perl simulation_run.pl [--start=NB --end=NB2] [--startend==NB]
#-------------------------------------------------------------------------------
use warnings;
use strict;
use SimulationRun; # Contains $bash and $mpirun variables
use Getopt::Long; # Command-line arguments
use File::Copy;
my $n_start = '-1';
my $n_end = '-1';
my $no_run = '-1';
my $config = 'config';
my $logdir = 'log';
#-------------------------------------------------------------------------------
# Functions definitions
#-------------------------------------------------------------------------------
# Check the nb of partitions are sorted
sub get_correct_nb_run() {
my ($n_start, $n_end) = get_nb_run();
while ($n_start > $n_end) {
print "n_start should be less than n_end\n";
($n_start, $n_end) = get_nb_run();
}
return $n_start, $n_end;
}
# Get first and last number of partitions
sub get_nb_run() {
print "Number of partitions (start): \n";
chomp(my $n_start = <>);
while (!check_nb_parts($n_start)) { chomp($n_start = <>); }
print "Number of partitions (end): \n";
chomp(my $n_end = <>);
while (!check_nb_parts($n_end)) { chomp($n_end = <>); }
return ($n_start, $n_end);
}
# Check number of partitions. Return 0 if wrong
sub check_nb_parts {
while ($_[0] < 1) {
print "Value should be > 0\n";
return 0;
}
while ($_[0] > 1 && $_[0] %3 != 0) {
print "Value should be multiple of 3 or 1\n";
return 0;
}
return 1;
}
# Start a mpi run directly
# Arguments : n_start, n_end, config, no-run, logdir
sub run_simu {
print "Simulation \n";
my $conf_new = $_[2].".new";
my $flags = "--config=$conf_new";
if ($_[3] > 0) { $flags .= " --no-run"; }
for (my $n = $_[0]; $n <= $_[1]; $n=$n+3) {
# Log file : append perl PID number to have a unique log name
my $log = $_[4]."/log_$n.$$";
print "Nb partitions = $n\n";
substitute($_[2], $conf_new, "nb_partitions.*","nb_partitions = $n", );
# Allow log file to be updated
#system("$mpirun -np $n src/pangolin $flags &> $log ");
system("$mpirun -np $n bin/pangolin $flags");
}
}
# Arguments : n_start, n_end, config
sub generate_config {
print "Generating config files\n";
my $folder = "tmp";
mkdir $folder;
for (my $n = $_[0]; $n <= $_[1]; $n=$n+3) {
my $conf_new = "$folder/config_$n";
my @args = ("nb_partitions.*","nb_partitions = $n",
"ratio_paral.*","ratio_paral_$n",
"output_u.*","output_u = u_$n",
"output_v.*","output_v = v_$n");
substitute($_[2], $conf_new, @args);
}
}
# Start a run with a batch job
# Arguments : n_start, n_end, config, no-run
sub run_batch {
# Log file is set inside the batch job
my $batch_new = $batch.".new";
my @args = ("nb_min=.*","nb_min=$_[0]",
"nb_max=.*","nb_max=$_[1]",
"select=.*","select=$_[1]");
substitute($batch, $batch_new, @args);
system("$mpirun $batch_new");
}
# Substitute a list of strings in a file. Create a backup which will used for
# the simulation.
# Arguments : file, new_file, [string_in, string_out, ...]
sub substitute {
open my $in, '<', $_[0] or die "Can't read $_[0]: $!";
my $fnew = defined($_[1])? "$_[1]" : "$_[0].new" ;
open my $out, '>', $fnew or die "Can't write $fnew: $!";
while( <$in> ) {
# Replace several strings
for (my $i = 2; $i < $#_; $i+=2) {
s/$_[$i]/$_[$i+1]/g;
}
print $out $_;
}
close $in;
close $out;
}
#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------
GetOptions ('start=i' => \$n_start,
'end=i' => \$n_end,
'startend=i' => sub {$n_start=$_[1]; $n_end=$n_start},
'no-run' => \$no_run,
'config=s' => \$config,
'logdir=s' => \$logdir);
if ($n_start < 0 || $n_end < 0) {
print "Enter the number of processus: \n";
($n_start, $n_end) = get_correct_nb_run();
}
else {
die if (!check_nb_parts($n_start));
die if (!check_nb_parts($n_end));
}
if ($batch eq "") {
run_simu($n_start, $n_end, $config, $no_run, $logdir);
}
else {
generate_config($n_start, $n_end, $config);
run_batch($n_start, $n_end, $config, $no_run);
}