/usr/share/doc/libmath-prime-util-perl/examples/fibprime-mce.pl is in libmath-prime-util-perl 0.46-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/env perl
use strict;
use warnings;
# Overkill, but let's try to select a good bigint module.
my $bigint_class;
if (eval { require Math::GMPz; 1; }) {
$bigint_class = "Math::GMPz";
} elsif (eval { require Math::GMP; 1; }) {
$bigint_class = "Math::GMP";
} else {
require Math::BigInt;
Math::BigInt->import(try=>"GMP,Pari");
$bigint_class = "Math::BigInt";
}
use Math::Prime::Util ':all';
use Time::HiRes qw(gettimeofday tv_interval);
use MCE::Util qw(get_ncpu);
use MCE;
$| = 1;
# Find Fibonacci primes in parallel, using Math::Prime::Util and MCE.
#
# Dana Jacobsen, 2012.
# Mario Roy, 2014.
#
# Runs about the same speed as the threads version, but doesn't need
# a threaded Perl.
#
# n32 ( F50833) in 4776s on 3930k 4.2GHz, SERIAL
# n32 ( F50833) in 754s on 3930k 4.2GHz, 12 CPU
# n32 ( F50833) in 472s on EC2 c3.8xlarge, 32 CPU
#
# n36 (F148091) in 26245s on 3930k 4.2GHz, 12 CPU
# n36 (F148091) in 14380s on EC2 c3.8xlarge, 32 CPU
#
my $time_start = [gettimeofday];
my $nworkers = get_ncpu();
warn "Using $nworkers CPUs\n";
prime_precalc(10_000_000);
sub fib_n {
my ($n, $fibstate) = @_;
@$fibstate = (1, $bigint_class->new(0), $bigint_class->new(1))
unless defined $fibstate->[0];
my ($curn, $a, $b) = @$fibstate;
die "fib_n only increases" if $n < $curn;
do { ($a, $b) = ($b, $a+$b); } for (1 .. $n-$curn);
@$fibstate = ($n, $a, $b);
$b;
}
sub nth_iter {
my $n = 0; my $order_id = 1; my %tmp;
return sub {
$tmp{$_[0]} = $_[1]; ## @_ = ( $nth, [ $k, $time_int ] )
while (1) {
last if not exists $tmp{$order_id};
if (defined $tmp{$order_id}) {
my ($k, $time_int) = @{ $tmp{$order_id} };
printf "%3d %7d %20.5f\n", ++$n, $k, $time_int;
}
delete $tmp{$order_id++};
}
}
}
my $mce = MCE->new(
max_workers => $nworkers, gather => nth_iter,
user_func => sub {
my @fibstate; my $nth = MCE->wid();
while (1) {
# Exploit knowledge that excepting k=4, all prime F_k have a prime k.
my $k = ($nth <= 2) ? 2 + $nth : nth_prime($nth);
my $Fk = fib_n($k, \@fibstate);
if (is_prob_prime($Fk)) {
MCE->gather($nth, [ $k, tv_interval($time_start) ]);
} else {
MCE->gather($nth, undef);
}
$nth += $nworkers;
}
}
)->run;
|