line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# The script calculates primes using Arch::SharedIndex package. |
4
|
|
|
|
|
|
|
# It creates multiple processes that all modify the same index. |
5
|
|
|
|
|
|
|
# Some quirks are created on purpose to fully test the package. |
6
|
|
|
|
|
|
|
|
7
|
5
|
|
|
5
|
|
6755
|
use FindBin qw($Bin); |
|
5
|
|
|
|
|
7150
|
|
|
5
|
|
|
|
|
775
|
|
8
|
5
|
|
|
5
|
|
4320
|
use lib "$Bin/../perllib"; |
|
5
|
|
|
|
|
4595
|
|
|
5
|
|
|
|
|
40
|
|
9
|
|
|
|
|
|
|
|
10
|
5
|
|
|
|
|
15
|
my $auto_test = !@ARGV; |
11
|
5
|
|
50
|
|
|
40
|
my $max_multiplier = shift || 10; |
12
|
5
|
|
|
|
|
15
|
my $max_prime = $max_multiplier * $max_multiplier; |
13
|
|
|
|
|
|
|
|
14
|
5
|
50
|
|
5
|
|
7410
|
use Test::More tests => @ARGV? 2: 9; |
|
5
|
|
|
|
|
110850
|
|
|
5
|
|
|
|
|
90
|
|
15
|
5
|
|
|
5
|
|
30
|
use_ok("Arch::SharedIndex"); |
|
5
|
|
|
|
|
6150
|
|
|
5
|
|
|
|
|
20
|
|
|
5
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
140
|
|
16
|
|
|
|
|
|
|
|
17
|
5
|
|
|
|
|
4905
|
my $file_name = "/tmp/shared-primes-1-$$"; |
18
|
5
|
|
|
|
|
40
|
my $shared_index = Arch::SharedIndex->new( |
19
|
|
|
|
|
|
|
max_size => $max_prime, |
20
|
|
|
|
|
|
|
can_create => 1, |
21
|
|
|
|
|
|
|
file => $file_name, |
22
|
|
|
|
|
|
|
); |
23
|
5
|
|
|
|
|
30
|
is(ref($shared_index), 'Arch::SharedIndex', "create index to calculate primes"); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# populate the index with all candidates (value 1 means prime) |
26
|
5
|
|
|
|
|
3015
|
$shared_index->store(map { $_ => 0 } 2 .. $max_prime); |
|
495
|
|
|
|
|
715
|
|
27
|
5
|
|
|
|
|
60
|
$shared_index->store(map { $_ => 1 } 2 .. $max_prime); |
|
495
|
|
|
|
|
640
|
|
28
|
|
|
|
|
|
|
|
29
|
5
|
|
|
|
|
75
|
for (my $num = 3; $num <= $max_multiplier; $num += 2) { |
30
|
14
|
|
|
|
|
37539
|
my $is_parent = fork(); |
31
|
14
|
100
|
|
|
|
1104
|
next if $is_parent; |
32
|
4
|
50
|
|
|
|
283
|
die "Can't fork: $!\n" unless defined $is_parent; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# in child, dismiss all multiples of $num |
35
|
4
|
|
|
|
|
450
|
my %multiples = map { $_ * $num => 1 } 2 .. int($max_prime / $num); |
|
74
|
|
|
|
|
1446
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# delete all *5 numbers from the index |
38
|
4
|
100
|
|
|
|
96
|
if ($num == 5) { |
39
|
1
|
|
|
99
|
|
115
|
$shared_index->filter(sub { $multiples{$_[0]} }); |
|
99
|
|
|
|
|
273
|
|
40
|
1
|
|
|
|
|
413
|
exit 0; |
41
|
|
|
|
|
|
|
} |
42
|
3
|
|
|
181
|
|
833
|
$shared_index->update(0, sub { $multiples{$_[0]} }); |
|
181
|
|
|
|
|
498
|
|
43
|
3
|
|
|
|
|
1504
|
exit 0; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# delete all even numbers from the index |
47
|
1
|
|
|
|
|
202
|
$shared_index->delete(map { $_ * 2 } 2 .. int($max_prime / 2)); |
|
49
|
|
|
|
|
341
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# wait for children |
50
|
1
|
|
|
|
|
880986
|
while (wait() > 0) {} |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
142
|
my @keys = $shared_index->keys; |
53
|
1
|
|
|
|
|
127
|
my @values1 = $shared_index->values; |
54
|
1
|
|
|
|
|
14
|
my @values2 = $shared_index->fetch(@keys); |
55
|
1
|
|
|
|
|
13
|
my @primes = sort { $a <=> $b } $shared_index->grep; |
|
24
|
|
|
|
|
50
|
|
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
5
|
my $num_keys = @keys; |
58
|
1
|
|
|
|
|
3
|
my $num_values1 = @values1; |
59
|
1
|
|
|
|
|
3
|
my $num_values2 = @values2; |
60
|
1
|
|
|
|
|
6
|
my $values1_str = join('', @values1); |
61
|
1
|
|
|
|
|
6
|
my $values2_str = join('', @values2); |
62
|
1
|
|
|
|
|
2
|
my $num_primes = @primes; |
63
|
1
|
|
|
|
|
5
|
my $primes_str = join(', ', @primes); |
64
|
|
|
|
|
|
|
|
65
|
1
|
50
|
|
|
|
4
|
if ($auto_test) { |
66
|
1
|
|
|
|
|
3
|
my $exp_num_keys = 41; |
67
|
1
|
|
|
|
|
6
|
my $exp_values_str = "11110111101011010111001011010110101010010"; |
68
|
1
|
|
|
|
|
1
|
my $exp_num_primes = 25; |
69
|
1
|
|
|
|
|
6
|
my $exp_primes_str = "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97"; |
70
|
1
|
|
|
|
|
13
|
is($num_keys, $exp_num_keys, "verify num_keys (odd non *5 numbers)"); |
71
|
1
|
|
|
|
|
1809
|
is($num_values1, $exp_num_keys, "verify number of values (1)"); |
72
|
1
|
|
|
|
|
1285
|
is($values1_str, $exp_values_str, "verify values (1)"); |
73
|
1
|
|
|
|
|
1043
|
is($num_values1, $exp_num_keys, "verify number of values (2)"); |
74
|
1
|
|
|
|
|
2067
|
is($values2_str, $exp_values_str, "verify values (2)"); |
75
|
1
|
|
|
|
|
1007
|
is($num_primes, $exp_num_primes, "verify number of primes"); |
76
|
1
|
|
|
|
|
885
|
is($primes_str, $exp_primes_str, "verify primes"); |
77
|
|
|
|
|
|
|
} else { |
78
|
0
|
|
|
|
|
0
|
printf "Number of keys (odd non *5 numbers): %d\n", $num_keys; |
79
|
0
|
|
|
|
|
0
|
printf "Values (1-st): %d - %s\n", $num_values1, $values1_str; |
80
|
0
|
|
|
|
|
0
|
printf "Values (2-nd): %d - %s\n", $num_values2, $values2_str; |
81
|
0
|
|
|
|
|
0
|
printf "Primes: %d - %s\n", $num_primes, $primes_str; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
2041
|
unlink($file_name); |