souffle  2.0.2-371-g6315b36
SipsMetric.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file SipsMetric.h
12  *
13  * Defines the SipsMetric class, which specifies cost functions for atom orderings in a clause.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include <memory>
20 #include <vector>
21 
22 namespace souffle::ast::analysis {
23 class IOTypeAnalysis;
24 class ProfileUseAnalysis;
25 class RelationDetailCacheAnalysis;
26 } // namespace souffle::ast::analysis
27 namespace souffle::ast {
28 
29 class Atom;
30 class BindingStore;
31 class Clause;
32 class TranslationUnit;
33 
34 /**
35  * Class for SIPS cost-metric functions
36  * Each subclass represents a different heuristic used for evaluating
37  * the cost of choosing an atom next in the schedule.
38  */
39 class SipsMetric {
40 public:
41  virtual ~SipsMetric() = default;
42 
43  /**
44  * Determines the new ordering of a clause after the SIPS is applied.
45  * @param clause clause to reorder
46  * @return the vector of new positions; v[i] = j iff atom j moves to pos i
47  */
48  std::vector<unsigned int> getReordering(const Clause* clause) const;
49 
50  /** Create a SIPS metric based on a given heuristic. */
51  static std::unique_ptr<SipsMetric> create(const std::string& heuristic, const TranslationUnit& tu);
52 
53 protected:
54  /**
55  * Evaluates the cost of choosing each atom next in the current schedule
56  * @param atoms atoms to choose from; may be nullptr
57  * @param bindingStore the variables already bound to a value
58  */
59  virtual std::vector<double> evaluateCosts(
60  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const = 0;
61 };
62 
63 /** Goal: Always choose the left-most atom */
64 class StrictSips : public SipsMetric {
65 public:
66  StrictSips() = default;
67 
68 protected:
69  std::vector<double> evaluateCosts(
70  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
71 };
72 
73 /** Goal: Prioritise atoms with all arguments bound */
74 class AllBoundSips : public SipsMetric {
75 public:
76  AllBoundSips() = default;
77 
78 protected:
79  std::vector<double> evaluateCosts(
80  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
81 };
82 
83 /** Goal: Prioritise (1) all bound, then (2) atoms with at least one bound argument, then (3) left-most */
84 class NaiveSips : public SipsMetric {
85 public:
86  NaiveSips() = default;
87 
88 protected:
89  std::vector<double> evaluateCosts(
90  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
91 };
92 
93 /** Goal: prioritise (1) all-bound, then (2) max number of bound vars, then (3) left-most */
94 class MaxBoundSips : public SipsMetric {
95 public:
96  MaxBoundSips() = default;
97 
98 protected:
99  std::vector<double> evaluateCosts(
100  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
101 };
102 
103 /** Goal: prioritise max ratio of bound args */
104 class MaxRatioSips : public SipsMetric {
105 public:
106  MaxRatioSips() = default;
107 
108 protected:
109  std::vector<double> evaluateCosts(
110  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
111 };
112 
113 /** Goal: choose the atom with the least number of unbound arguments */
114 class LeastFreeSips : public SipsMetric {
115 public:
116  LeastFreeSips() = default;
117 
118 protected:
119  std::vector<double> evaluateCosts(
120  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
121 };
122 
123 /** Goal: choose the atom with the least amount of unbound variables */
125 public:
126  LeastFreeVarsSips() = default;
127 
128 protected:
129  std::vector<double> evaluateCosts(
130  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
131 };
132 
133 /**
134  * Goal: reorder based on the given profiling information
135  * Metric: cost(atom_R) = log(|atom_R|) * #free/#args
136  * - exception: propositions are prioritised
137  */
138 class ProfileUseSips : public SipsMetric {
139 public:
141 
142 protected:
143  std::vector<double> evaluateCosts(
144  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
145 
146 private:
148 };
149 
150 /** Goal: prioritise (1) all-bound, then (2) deltas, and then (3) left-most */
151 class DeltaSips : public SipsMetric {
152 public:
153  DeltaSips() = default;
154 
155 protected:
156  std::vector<double> evaluateCosts(
157  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
158 };
159 
160 /** Goal: prioritise (1) all-bound, then (2) input, and then (3) left-most */
161 class InputSips : public SipsMetric {
162 public:
165 
166 protected:
167  std::vector<double> evaluateCosts(
168  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
169 
170 private:
173 };
174 
175 /** Goal: prioritise (1) all-bound, then (2) deltas, then (3) input, and then (4) left-most */
176 class DeltaInputSips : public SipsMetric {
177 public:
181 
182 protected:
183  std::vector<double> evaluateCosts(
184  const std::vector<Atom*> atoms, const BindingStore& bindingStore) const override;
185 
186 private:
189 };
190 
191 } // namespace souffle::ast
souffle::ast::NaiveSips
Goal: Prioritise (1) all bound, then (2) atoms with at least one bound argument, then (3) left-most.
Definition: SipsMetric.h:84
souffle::ast::DeltaSips
Goal: prioritise (1) all-bound, then (2) deltas, and then (3) left-most.
Definition: SipsMetric.h:151
souffle::ast::LeastFreeSips::LeastFreeSips
LeastFreeSips()=default
souffle::ast::StrictSips
Goal: Always choose the left-most atom.
Definition: SipsMetric.h:64
souffle::ast::analysis::IOTypeAnalysis
Definition: IOType.h:39
souffle::ast::MaxBoundSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:151
souffle::ast::DeltaSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:270
souffle::ast::InputSips::InputSips
InputSips(const analysis::RelationDetailCacheAnalysis &relDetail, const analysis::IOTypeAnalysis &ioTypes)
Definition: SipsMetric.h:163
souffle::ast::InputSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:295
souffle::ast::MaxBoundSips::MaxBoundSips
MaxBoundSips()=default
souffle::ast::LeastFreeSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:205
souffle::ast::MaxRatioSips::MaxRatioSips
MaxRatioSips()=default
souffle::ast::DeltaInputSips
Goal: prioritise (1) all-bound, then (2) deltas, then (3) input, and then (4) left-most.
Definition: SipsMetric.h:176
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::AllBoundSips
Goal: Prioritise atoms with all arguments bound.
Definition: SipsMetric.h:74
souffle::ast::LeastFreeVarsSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:220
souffle::ast::DeltaInputSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:321
souffle::ast::analysis::ProfileUseAnalysis
Analysis that loads profile data and has a profile query interface.
Definition: ProfileUse.h:43
souffle::ast::analysis::RelationDetailCacheAnalysis
Analysis pass mapping identifiers with relations and clauses.
Definition: RelationDetailCache.h:47
souffle::ast::ProfileUseSips
Goal: reorder based on the given profiling information Metric: cost(atom_R) = log(|atom_R|) * #free/#...
Definition: SipsMetric.h:138
souffle::ast::SipsMetric::~SipsMetric
virtual ~SipsMetric()=default
souffle::ast::SipsMetric::create
static std::unique_ptr< SipsMetric > create(const std::string &heuristic, const TranslationUnit &tu)
Create a SIPS metric based on a given heuristic.
Definition: SipsMetric.cpp:68
souffle::ast::InputSips::ioTypes
const analysis::IOTypeAnalysis & ioTypes
Definition: SipsMetric.h:172
souffle::ast::AllBoundSips::AllBoundSips
AllBoundSips()=default
souffle::ast::DeltaInputSips::relDetail
const analysis::RelationDetailCacheAnalysis & relDetail
Definition: SipsMetric.h:187
souffle::ast::MaxBoundSips
Goal: prioritise (1) all-bound, then (2) max number of bound vars, then (3) left-most.
Definition: SipsMetric.h:94
souffle::ast::LeastFreeVarsSips::LeastFreeVarsSips
LeastFreeVarsSips()=default
souffle::ast::DeltaSips::DeltaSips
DeltaSips()=default
souffle::ast::DeltaInputSips::DeltaInputSips
DeltaInputSips(const analysis::RelationDetailCacheAnalysis &relDetail, const analysis::IOTypeAnalysis &ioTypes)
Definition: SipsMetric.h:178
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::ast::InputSips::relDetail
const analysis::RelationDetailCacheAnalysis & relDetail
Definition: SipsMetric.h:171
souffle::ast::MaxRatioSips
Goal: prioritise max ratio of bound args.
Definition: SipsMetric.h:104
souffle::ast::NaiveSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:127
souffle::ast::LeastFreeSips
Goal: choose the atom with the least number of unbound arguments.
Definition: SipsMetric.h:114
souffle::ast::SipsMetric::evaluateCosts
virtual std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const =0
Evaluates the cost of choosing each atom next in the current schedule.
souffle::ast::AllBoundSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:109
souffle::ast::StrictSips::StrictSips
StrictSips()=default
souffle::ast::SipsMetric::getReordering
std::vector< unsigned int > getReordering(const Clause *clause) const
Determines the new ordering of a clause after the SIPS is applied.
Definition: SipsMetric.cpp:39
souffle::ast::LeastFreeVarsSips
Goal: choose the atom with the least amount of unbound variables.
Definition: SipsMetric.h:124
souffle::ast::MaxRatioSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:178
souffle::ast::SipsMetric
Class for SIPS cost-metric functions Each subclass represents a different heuristic used for evaluati...
Definition: SipsMetric.h:39
souffle::ast::analysis
Definition: Aggregate.cpp:39
souffle::ast::NaiveSips::NaiveSips
NaiveSips()=default
souffle::ast::StrictSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:98
souffle::ast::DeltaInputSips::ioTypes
const analysis::IOTypeAnalysis & ioTypes
Definition: SipsMetric.h:188
souffle::ast
Definition: Aggregator.h:35
souffle::ast::BindingStore
Definition: BindingStore.h:38
souffle::ast::InputSips
Goal: prioritise (1) all-bound, then (2) input, and then (3) left-most.
Definition: SipsMetric.h:161
souffle::ast::ProfileUseSips::ProfileUseSips
ProfileUseSips(const analysis::ProfileUseAnalysis &profileUse)
Definition: SipsMetric.h:140
souffle::ast::ProfileUseSips::profileUse
const analysis::ProfileUseAnalysis & profileUse
Definition: SipsMetric.h:147
souffle::ast::ProfileUseSips::evaluateCosts
std::vector< double > evaluateCosts(const std::vector< Atom * > atoms, const BindingStore &bindingStore) const override
Evaluates the cost of choosing each atom next in the current schedule.
Definition: SipsMetric.cpp:242