mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add --program-cache option
This commit is contained in:
parent
3b49fbb8a5
commit
98db50ccac
@ -159,7 +159,7 @@ BOOST_FIXTURE_TEST_CASE(build_should_create_metric_of_the_right_type, FitnessMet
|
|||||||
{
|
{
|
||||||
m_options.metric = MetricChoice::RelativeCodeSize;
|
m_options.metric = MetricChoice::RelativeCodeSize;
|
||||||
m_options.metricAggregator = MetricAggregatorChoice::Sum;
|
m_options.metricAggregator = MetricAggregatorChoice::Sum;
|
||||||
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, {m_programs[0]});
|
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, {m_programs[0]}, {nullptr});
|
||||||
BOOST_REQUIRE(metric != nullptr);
|
BOOST_REQUIRE(metric != nullptr);
|
||||||
|
|
||||||
auto sumMetric = dynamic_cast<FitnessMetricSum*>(metric.get());
|
auto sumMetric = dynamic_cast<FitnessMetricSum*>(metric.get());
|
||||||
@ -177,7 +177,7 @@ BOOST_FIXTURE_TEST_CASE(build_should_respect_chromosome_repetitions_option, Fitn
|
|||||||
m_options.metric = MetricChoice::CodeSize;
|
m_options.metric = MetricChoice::CodeSize;
|
||||||
m_options.metricAggregator = MetricAggregatorChoice::Average;
|
m_options.metricAggregator = MetricAggregatorChoice::Average;
|
||||||
m_options.chromosomeRepetitions = 5;
|
m_options.chromosomeRepetitions = 5;
|
||||||
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, {m_programs[0]});
|
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, {m_programs[0]}, {nullptr});
|
||||||
BOOST_REQUIRE(metric != nullptr);
|
BOOST_REQUIRE(metric != nullptr);
|
||||||
|
|
||||||
auto averageMetric = dynamic_cast<FitnessMetricAverage*>(metric.get());
|
auto averageMetric = dynamic_cast<FitnessMetricAverage*>(metric.get());
|
||||||
@ -195,7 +195,7 @@ BOOST_FIXTURE_TEST_CASE(build_should_set_relative_metric_scale, FitnessMetricFac
|
|||||||
m_options.metric = MetricChoice::RelativeCodeSize;
|
m_options.metric = MetricChoice::RelativeCodeSize;
|
||||||
m_options.metricAggregator = MetricAggregatorChoice::Average;
|
m_options.metricAggregator = MetricAggregatorChoice::Average;
|
||||||
m_options.relativeMetricScale = 10;
|
m_options.relativeMetricScale = 10;
|
||||||
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, {m_programs[0]});
|
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, {m_programs[0]}, {nullptr});
|
||||||
BOOST_REQUIRE(metric != nullptr);
|
BOOST_REQUIRE(metric != nullptr);
|
||||||
|
|
||||||
auto averageMetric = dynamic_cast<FitnessMetricAverage*>(metric.get());
|
auto averageMetric = dynamic_cast<FitnessMetricAverage*>(metric.get());
|
||||||
@ -210,7 +210,11 @@ BOOST_FIXTURE_TEST_CASE(build_should_set_relative_metric_scale, FitnessMetricFac
|
|||||||
|
|
||||||
BOOST_FIXTURE_TEST_CASE(build_should_create_metric_for_each_input_program, FitnessMetricFactoryFixture)
|
BOOST_FIXTURE_TEST_CASE(build_should_create_metric_for_each_input_program, FitnessMetricFactoryFixture)
|
||||||
{
|
{
|
||||||
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, m_programs);
|
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(
|
||||||
|
m_options,
|
||||||
|
m_programs,
|
||||||
|
vector<shared_ptr<ProgramCache>>(m_programs.size(), nullptr)
|
||||||
|
);
|
||||||
BOOST_REQUIRE(metric != nullptr);
|
BOOST_REQUIRE(metric != nullptr);
|
||||||
|
|
||||||
auto combinedMetric = dynamic_cast<FitnessMetricCombination*>(metric.get());
|
auto combinedMetric = dynamic_cast<FitnessMetricCombination*>(metric.get());
|
||||||
@ -218,6 +222,31 @@ BOOST_FIXTURE_TEST_CASE(build_should_create_metric_for_each_input_program, Fitne
|
|||||||
BOOST_REQUIRE(combinedMetric->metrics().size() == m_programs.size());
|
BOOST_REQUIRE(combinedMetric->metrics().size() == m_programs.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(build_should_pass_program_caches_to_metrics, FitnessMetricFactoryFixture)
|
||||||
|
{
|
||||||
|
assert(m_programs.size() == 3);
|
||||||
|
vector<shared_ptr<ProgramCache>> caches = {
|
||||||
|
make_shared<ProgramCache>(m_programs[0]),
|
||||||
|
make_shared<ProgramCache>(m_programs[1]),
|
||||||
|
make_shared<ProgramCache>(m_programs[2]),
|
||||||
|
};
|
||||||
|
|
||||||
|
m_options.metric = MetricChoice::RelativeCodeSize;
|
||||||
|
unique_ptr<FitnessMetric> metric = FitnessMetricFactory::build(m_options, m_programs, caches);
|
||||||
|
BOOST_REQUIRE(metric != nullptr);
|
||||||
|
|
||||||
|
auto combinedMetric = dynamic_cast<FitnessMetricCombination*>(metric.get());
|
||||||
|
BOOST_REQUIRE(combinedMetric != nullptr);
|
||||||
|
BOOST_REQUIRE(combinedMetric->metrics().size() == caches.size());
|
||||||
|
|
||||||
|
for (size_t i = 0; i < caches.size(); ++i)
|
||||||
|
{
|
||||||
|
auto programBasedMetric = dynamic_cast<ProgramBasedMetric*>(combinedMetric->metrics()[i].get());
|
||||||
|
BOOST_REQUIRE(programBasedMetric != nullptr);
|
||||||
|
BOOST_TEST(programBasedMetric->programCache() == caches[i].get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
BOOST_AUTO_TEST_SUITE(PopulationFactoryTest)
|
BOOST_AUTO_TEST_SUITE(PopulationFactoryTest)
|
||||||
|
|
||||||
|
@ -158,9 +158,11 @@ FitnessMetricFactory::Options FitnessMetricFactory::Options::fromCommandLine(po:
|
|||||||
|
|
||||||
unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
||||||
Options const& _options,
|
Options const& _options,
|
||||||
vector<Program> _programs
|
vector<Program> _programs,
|
||||||
|
vector<shared_ptr<ProgramCache>> _programCaches
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
assert(_programCaches.size() == _programs.size());
|
||||||
assert(_programs.size() > 0 && "Validations should prevent this from being executed with zero files.");
|
assert(_programs.size() > 0 && "Validations should prevent this from being executed with zero files.");
|
||||||
|
|
||||||
vector<shared_ptr<FitnessMetric>> metrics;
|
vector<shared_ptr<FitnessMetric>> metrics;
|
||||||
@ -168,10 +170,10 @@ unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
|||||||
{
|
{
|
||||||
case MetricChoice::CodeSize:
|
case MetricChoice::CodeSize:
|
||||||
{
|
{
|
||||||
for (Program& program: _programs)
|
for (size_t i = 0; i < _programs.size(); ++i)
|
||||||
metrics.push_back(make_unique<ProgramSize>(
|
metrics.push_back(make_unique<ProgramSize>(
|
||||||
move(program),
|
_programCaches[i] != nullptr ? optional<Program>{} : move(_programs[i]),
|
||||||
nullptr,
|
move(_programCaches[i]),
|
||||||
_options.chromosomeRepetitions
|
_options.chromosomeRepetitions
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -179,10 +181,10 @@ unique_ptr<FitnessMetric> FitnessMetricFactory::build(
|
|||||||
}
|
}
|
||||||
case MetricChoice::RelativeCodeSize:
|
case MetricChoice::RelativeCodeSize:
|
||||||
{
|
{
|
||||||
for (Program& program: _programs)
|
for (size_t i = 0; i < _programs.size(); ++i)
|
||||||
metrics.push_back(make_unique<RelativeProgramSize>(
|
metrics.push_back(make_unique<RelativeProgramSize>(
|
||||||
move(program),
|
_programCaches[i] != nullptr ? optional<Program>{} : move(_programs[i]),
|
||||||
nullptr,
|
move(_programCaches[i]),
|
||||||
_options.relativeMetricScale,
|
_options.relativeMetricScale,
|
||||||
_options.chromosomeRepetitions
|
_options.chromosomeRepetitions
|
||||||
));
|
));
|
||||||
@ -528,6 +530,19 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
|
|||||||
;
|
;
|
||||||
keywordDescription.add(metricsDescription);
|
keywordDescription.add(metricsDescription);
|
||||||
|
|
||||||
|
po::options_description cacheDescription("CACHE", lineLength, minDescriptionLength);
|
||||||
|
cacheDescription.add_options()
|
||||||
|
(
|
||||||
|
"program-cache",
|
||||||
|
po::bool_switch(),
|
||||||
|
"Enables caching of intermediate programs corresponding to chromosome prefixes.\n"
|
||||||
|
"This speeds up fitness evaluation by a lot but eats tons of memory if the chromosomes are long. "
|
||||||
|
"Disabled by default since there's currently no way to set an upper limit on memory usage but "
|
||||||
|
"highly recommended if your computer has enough RAM."
|
||||||
|
)
|
||||||
|
;
|
||||||
|
keywordDescription.add(cacheDescription);
|
||||||
|
|
||||||
po::positional_options_description positionalDescription;
|
po::positional_options_description positionalDescription;
|
||||||
positionalDescription.add("input-files", -1);
|
positionalDescription.add("input-files", -1);
|
||||||
|
|
||||||
@ -583,12 +598,15 @@ AlgorithmRunner::Options Phaser::buildAlgorithmRunnerOptions(po::variables_map c
|
|||||||
void Phaser::runAlgorithm(po::variables_map const& _arguments)
|
void Phaser::runAlgorithm(po::variables_map const& _arguments)
|
||||||
{
|
{
|
||||||
auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments);
|
auto programOptions = ProgramFactory::Options::fromCommandLine(_arguments);
|
||||||
|
auto cacheOptions = ProgramCacheFactory::Options::fromCommandLine(_arguments);
|
||||||
auto metricOptions = FitnessMetricFactory::Options::fromCommandLine(_arguments);
|
auto metricOptions = FitnessMetricFactory::Options::fromCommandLine(_arguments);
|
||||||
auto populationOptions = PopulationFactory::Options::fromCommandLine(_arguments);
|
auto populationOptions = PopulationFactory::Options::fromCommandLine(_arguments);
|
||||||
auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments);
|
auto algorithmOptions = GeneticAlgorithmFactory::Options::fromCommandLine(_arguments);
|
||||||
|
|
||||||
vector<Program> programs = ProgramFactory::build(programOptions);
|
vector<Program> programs = ProgramFactory::build(programOptions);
|
||||||
unique_ptr<FitnessMetric> fitnessMetric = FitnessMetricFactory::build(metricOptions, move(programs));
|
vector<shared_ptr<ProgramCache>> programCaches = ProgramCacheFactory::build(cacheOptions, programs);
|
||||||
|
|
||||||
|
unique_ptr<FitnessMetric> fitnessMetric = FitnessMetricFactory::build(metricOptions, move(programs), programCaches);
|
||||||
Population population = PopulationFactory::build(populationOptions, move(fitnessMetric));
|
Population population = PopulationFactory::build(populationOptions, move(fitnessMetric));
|
||||||
|
|
||||||
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
|
unique_ptr<GeneticAlgorithm> geneticAlgorithm = GeneticAlgorithmFactory::build(
|
||||||
@ -596,6 +614,6 @@ void Phaser::runAlgorithm(po::variables_map const& _arguments)
|
|||||||
population.individuals().size()
|
population.individuals().size()
|
||||||
);
|
);
|
||||||
|
|
||||||
AlgorithmRunner algorithmRunner(population, vector<shared_ptr<ProgramCache>>(programs.size(), nullptr), buildAlgorithmRunnerOptions(_arguments), cout);
|
AlgorithmRunner algorithmRunner(population, move(programCaches), buildAlgorithmRunnerOptions(_arguments), cout);
|
||||||
algorithmRunner.run(*geneticAlgorithm);
|
algorithmRunner.run(*geneticAlgorithm);
|
||||||
}
|
}
|
||||||
|
@ -120,7 +120,8 @@ public:
|
|||||||
|
|
||||||
static std::unique_ptr<FitnessMetric> build(
|
static std::unique_ptr<FitnessMetric> build(
|
||||||
Options const& _options,
|
Options const& _options,
|
||||||
std::vector<Program> _programs
|
std::vector<Program> _programs,
|
||||||
|
std::vector<std::shared_ptr<ProgramCache>> _programCaches
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user