Algorithm-Networksort-Chooser
view release on metacpan or search on metacpan
Makefile.PL view on Meta::CPAN
my %args = (
NAME => 'Algorithm::Networksort::Chooser',
VERSION_FROM => 'lib/Algorithm/Networksort/Chooser.pm',
EXE_FILES => [ 'bin/algorithm-networksort-chooser', ],
PREREQ_PM => {
'common::sense' => 0,
'Getopt::Long' => 0,
'Math::Combinatorics' => 0,
## Assumes default when there is no best to be batcher, changed to this in 1.30
'Algorithm::Networksort' => 1.30,
},
LIBS => [],
DEFINE => '',
LICENSE => 'perl',
dist => {
PREOP => 'pod2text bin/algorithm-networksort-chooser > $(DISTVNAME)/README',
},
);
NAME
algorithm-networksort-chooser - Helper utility for
Algorithm::Networksort
SYNOPSIS
The "algorithm-networksort-chooser" script helps you find the best
sorting network for your particular use-case.
$ algorithm-networksort-chooser 9 ## find best sorting network for array size 9
$ algorithm-networksort-chooser 9 --all ## show all candiate networks
$ algorithm-networksort-chooser 9 --algorithms=batcher,bitonic ## only consider batcher and bitonic algos
$ algorithm-networksort-chooser 9 --opt=comparators ## optimise for comparators (default)
$ algorithm-networksort-chooser 9 --opt=stages ## optimise for stages
$ algorithm-networksort-chooser 9 --opt=swaps ## optimise for average swaps
$ algorithm-networksort-chooser 9 --median ## best median network
$ algorithm-networksort-chooser 9 --selection=4 ## also best median network
$ algorithm-networksort-chooser 9 --selection=0,1,2 ## top-3 elements selection net
$ algorithm-networksort-chooser 9 --validate ## run 0-1 validation test
$ algorithm-networksort-chooser 9 --show ## show network as ASCII diagram
$ algorithm-networksort-chooser 9 --raw ## show network as raw comparators
DESCRIPTION
This module uses Algorithm::Networksort to experiment with sorting
networks.
Introduction To Sorting Networks
<http://hoytech.github.io/sorting-networks/>
By default this script examines the output of all implemented algorithms
and the currently best known special-cases, and chooses the one that
best meets your specified criteria.
This module allows you to trim sorting networks into median or selection
networks.
You can then choose the optimal net based on comparators (total number
of operations) or on stages (number of operations considering
parallelism).
Normally the output is something like this:
$ algorithm-networksort-chooser --median 22
Network size: 22
Network type: Median network
Optimisation criteria: stages
Optimal network:
Algorithm "best":
Comparators: 86
Stages: 12
For the description of the various algorithms and best-known special
cases, see Algorithm::Networksort's documentation and source code.
In order to use this output in another program, there is a "--raw"
switch. Its output is "eval"able perl and is valid JSON:
$ algorithm-networksort-chooser --median 7 --raw
[[0,4],[1,5],[2,6],[0,2],[1,3],[4,6],[2,4],[3,5],[0,1],[2,3],[4,5],[1,4],[3,6],[3,4]]
Algorithm::Networksort's ASCII output can be seen with "--show":
o--|--|--|-----v--|--^--v--|--^--^--o
| | | | | | | |
o--v--|--|--^-----v--|--^--v--|--v--o
| | | | | |
o-----v--|--|--------v--v-----|-----o
| | |
o--------v--v-----------------v-----o
The "--all" switch shows all networks that were considered.
Sometimes which algorithm or which best special-case network is
surprising. For instance, selecting the top-3 elements in a size-9 array
is best done by adapting Hibbard's algorithm, even though there is a
special best (by comparators) network for size 9:
$ algorithm-networksort-chooser 9 --selection=0,1,2 --all
Network size: 9
Network type: Selection network: 0,1,2
Optimisation criteria: comparators
Optimal network:
Algorithm "hibbard":
Comparators: 18
Stages: 7
Additional candidate networks:
Algorithm "batcher":
Comparators: 20
Stages: 8
Algorithm "bosenelson":
Comparators: 22
Stages: 10
Algorithm "best":
Comparators: 23
Stages: 9
Algorithm "bitonic":
Comparators: 24
Stages: 8
Algorithm "bubble":
Comparators: 36
Stages: 15
FUTURE IDEAS
bin/algorithm-networksort-chooser view on Meta::CPAN
my @network = @{ $candidate->{network} };
my @grouped_network = Algorithm::Networksort::nw_group(\@network, $network_size, grouping=>'parallel');
$candidate->{comparators} = (0+@network);
$candidate->{stages} = (0+@grouped_network);
}
#### Remove 'best' network if it's the same as batcher
my $batcher = [grep { $_->{algo} eq 'batcher' } @candidates]->[0];
my $best = [grep { $_->{algo} eq 'best' } @candidates]->[0];
if ($batcher->{comparators} == $best->{comparators} && $batcher->{stages} == $best->{stages}) {
@candidates = grep { $_->{algo} ne 'best' } @candidates;
}
#### Sort by optimisation criteria
my @sorted_candidates;
if ($opt->{opt} eq 'comparators') {
bin/algorithm-networksort-chooser view on Meta::CPAN
__END__
=encoding utf-8
=head1 NAME
algorithm-networksort-chooser - Helper utility for Algorithm::Networksort
=head1 SYNOPSIS
The C<algorithm-networksort-chooser> script helps you find the best sorting network for your particular use-case.
$ algorithm-networksort-chooser 9 ## find best sorting network for array size 9
$ algorithm-networksort-chooser 9 --all ## show all candiate networks
$ algorithm-networksort-chooser 9 --algorithms=batcher,bitonic ## only consider batcher and bitonic algos
$ algorithm-networksort-chooser 9 --opt=comparators ## optimise for comparators (default)
$ algorithm-networksort-chooser 9 --opt=stages ## optimise for stages
$ algorithm-networksort-chooser 9 --opt=swaps ## optimise for average swaps
$ algorithm-networksort-chooser 9 --median ## best median network
$ algorithm-networksort-chooser 9 --selection=4 ## also best median network
$ algorithm-networksort-chooser 9 --selection=0,1,2 ## top-3 elements selection net
$ algorithm-networksort-chooser 9 --validate ## run 0-1 validation test
$ algorithm-networksort-chooser 9 --show ## show network as ASCII diagram
$ algorithm-networksort-chooser 9 --raw ## show network as raw comparators
=head1 DESCRIPTION
This module uses L<Algorithm::Networksort> to experiment with sorting networks.
L<Introduction To Sorting Networks|http://hoytech.github.io/sorting-networks/>
By default this script examines the output of all implemented algorithms and the currently best known special-cases, and chooses the one that best meets your specified criteria.
This module allows you to trim sorting networks into median or selection networks.
You can then choose the optimal net based on comparators (total number of operations) or on stages (number of operations considering parallelism).
Normally the output is something like this:
$ algorithm-networksort-chooser --median 22
Network size: 22
Network type: Median network
Optimisation criteria: stages
Optimal network:
Algorithm "best":
Comparators: 86
Stages: 12
For the description of the various algorithms and best-known special cases, see L<Algorithm::Networksort>'s documentation and source code.
In order to use this output in another program, there is a C<--raw> switch. Its output is C<eval>able perl and is valid JSON:
$ algorithm-networksort-chooser --median 7 --raw
[[0,4],[1,5],[2,6],[0,2],[1,3],[4,6],[2,4],[3,5],[0,1],[2,3],[4,5],[1,4],[3,6],[3,4]]
L<Algorithm::Networksort>'s ASCII output can be seen with C<--show>:
$ algorithm-networksort-chooser --median 7 --show
Network size: 7
bin/algorithm-networksort-chooser view on Meta::CPAN
| | | | | | | |
o--v--|--|--^-----v--|--^--v--|--v--o
| | | | | |
o-----v--|--|--------v--v-----|-----o
| | |
o--------v--v-----------------v-----o
The C<--all> switch shows all networks that were considered.
Sometimes which algorithm or which best special-case network is surprising. For instance, selecting the top-3 elements in a size-9 array is best done by adapting Hibbard's algorithm, even though there is a special best (by comparators) network for si...
$ algorithm-networksort-chooser 9 --selection=0,1,2 --all
Network size: 9
Network type: Selection network: 0,1,2
Optimisation criteria: comparators
Optimal network:
Algorithm "hibbard":
Comparators: 18
Stages: 7
Additional candidate networks:
Algorithm "batcher":
Comparators: 20
Stages: 8
Algorithm "bosenelson":
Comparators: 22
Stages: 10
Algorithm "best":
Comparators: 23
Stages: 9
Algorithm "bitonic":
Comparators: 24
Stages: 8
Algorithm "bubble":
Comparators: 36
Stages: 15
use strict;
use Test::More tests => 1;
use Config;
my $perlpath = $Config{perlpath};
my $network = eval `$perlpath -I lib bin/algorithm-networksort-chooser 9 --raw`;
is(0+@$network, 25, "Found Floyd's best known network for size 9");
( run in 0.996 second using v1.01-cache-2.11-cpan-4e96b696675 )