-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_mapgen.sh
executable file
·96 lines (84 loc) · 2.15 KB
/
parallel_mapgen.sh
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
#!/bin/bash
# Set default values for arguments
start_offset=0
total_count=2179583646
scanner="pattern"
num_threads=$(nproc)
num_processes=$((num_threads - 1))
target=""
# Function to display usage information
usage() {
echo "Usage: $0 [--scanner <${scanner}>] [--start <${start_offset}>] [--count <${total_count}>] [--target <int>] [--threads <${num_processes}>]"
exit 1
}
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--scanner)
scanner="$2"
shift
shift
;;
--count)
total_count="$2"
shift
shift
;;
--start)
start_offset="$2"
shift
shift
;;
--threads)
num_processes="$2"
shift
shift
;;
--target)
target="$2"
shift
shift
;;
*)
usage
;;
esac
done
# Cap total_count
if ((total_count < 1)); then
total_count=1
fi
if ((total_count > 4294967295)); then
total_count=4294967295
fi
# Define the batch size (number of IDs to scan per process)
batch_size=$((total_count / num_processes + 1))
# Define the command to run
command="./diablo-mapgen --scanner $scanner"
# Add target argument if provided
if [ ! -z "$target" ]; then
command+=" --target $target"
fi
# Function to send SIGTERM to the process group ID of the main script
sigint_handler() {
kill -- -$$
exit 1
}
trap 'sigint_handler' SIGINT
# Loop through batches and run the command with different offsets
for ((i = 0; i < num_processes; i++)); do
# Calculate the start offset for this batch
offset=$((start_offset + i * batch_size))
# Calculate the actual count for this batch
end_offset=$((offset + batch_size))
if ((end_offset > start_offset + total_count)); then
actual_count=$((start_offset + total_count - offset))
else
actual_count=$((batch_size))
fi
# Run the command with the current offset and count
$command --start $offset --count $actual_count &
done
# Wait for the processes to finish
wait