source: icGREP/icgrep-devel/icgrep/pablo/optimizers/pablo_automultiplexing.hpp @ 4896

Last change on this file since 4896 was 4896, checked in by nmedfort, 3 years ago

Work on coalescing algorithm + minor changes.

File size: 4.4 KB
Line 
1#ifndef PABLO_AUTOMULTIPLEXING_HPP
2#define PABLO_AUTOMULTIPLEXING_HPP
3
4#include <pablo/codegenstate.h>
5#include <slab_allocator.h>
6#include <boost/graph/adjacency_list.hpp>
7#include <boost/graph/adjacency_matrix.hpp>
8#include <boost/container/flat_set.hpp>
9#include <boost/container/flat_map.hpp>
10#include <boost/numeric/ublas/matrix.hpp>
11#include <random>
12#include <stdint.h>
13#include <llvm/ADT/DenseMap.h>
14
15typedef int BDD;
16
17namespace pablo {
18
19class PabloBuilder;
20class PabloFunction;
21struct OrderingVerifier;
22
23class MultiplexingPass {
24
25    using CharacterizationMap = llvm::DenseMap<const PabloAST *, BDD>;
26    using ConstraintGraph = boost::adjacency_matrix<boost::directedS>;
27    using ConstraintVertex = ConstraintGraph::vertex_descriptor;
28    using RNG = std::mt19937;
29    using IntDistribution = std::uniform_int_distribution<RNG::result_type>;
30    using MultiplexSetGraph = boost::adjacency_list<boost::hash_setS, boost::vecS, boost::bidirectionalS>;
31    using MultiplexVector = std::vector<MultiplexSetGraph::vertex_descriptor>;
32    using SubsetGraph = boost::adjacency_list<boost::hash_setS, boost::vecS, boost::bidirectionalS>;
33    using SubsetEdgeIterator = boost::graph_traits<SubsetGraph>::edge_iterator;
34    using CliqueGraph = boost::adjacency_list<boost::hash_setS, boost::vecS, boost::undirectedS>;
35    using CliqueSet = boost::container::flat_set<CliqueGraph::vertex_descriptor>;
36    using CliqueSets = boost::container::flat_set<std::vector<CliqueGraph::vertex_descriptor>>;
37
38    using AdvanceVector = std::vector<Advance *>;
39    using AdvanceDepth = std::vector<int>;
40    using AdvanceVariable = std::vector<BDD>;
41    using VertexVector = std::vector<ConstraintVertex>;
42
43public:
44
45    static bool optimize(PabloFunction & function, const unsigned limit = std::numeric_limits<unsigned>::max(), const unsigned maxSelections = 100, const unsigned windowSize = 1, const bool independent = false);
46
47protected:
48
49    unsigned initialize(PabloFunction & function, const bool independent);
50    void initializeBaseConstraintGraph(PabloBlock * const block, const unsigned statements, const unsigned advances);
51    void initializeAdvanceDepth(PabloBlock * const block, const unsigned advances) ;
52
53    void characterize(PabloBlock * const block);
54    BDD characterize(Statement * const stmt);
55    BDD characterize(Advance * const adv, const BDD Ik);
56    bool independent(const ConstraintVertex i, const ConstraintVertex j) const;
57    bool exceedsWindowSize(const ConstraintVertex i, const ConstraintVertex j) const;
58
59    void generateUsageWeightingGraph();
60    CliqueSets findMaximalCliques(const CliqueGraph & G);
61    void findMaximalCliques(const CliqueGraph & G, CliqueSet & R, CliqueSet && P, CliqueSet && X, CliqueSets & S);
62
63    bool generateCandidateSets();
64    void addCandidateSet(const VertexVector & S);
65    void selectMultiplexSets();
66
67    void eliminateSubsetConstraints();
68    void doTransitiveReductionOfSubsetGraph();
69
70    MultiplexVector orderMultiplexSet(const MultiplexSetGraph::vertex_descriptor u);
71    void multiplexSelectedSets(PabloFunction & function);
72
73    static void topologicalSort(PabloFunction & function);
74    static void topologicalSort(PabloBlock * block, OrderingVerifier & parent);
75
76    BDD & get(const PabloAST * const expr);
77
78    inline MultiplexingPass(const RNG::result_type seed, const unsigned limit, const unsigned maxSelections, const unsigned windowSize)
79    : mMultiplexingSetSizeLimit(limit)
80    , mMaxMultiplexingSetSelections(maxSelections)
81    , mWindowSize(windowSize)
82    , mTestConstrainedAdvances(true)
83    , mVariables(0)
84    , mRNG(seed)
85    , mConstraintGraph(0)
86    , mAdvance(0, nullptr)
87    , mAdvanceDepth(0, 0)
88    , mAdvanceNegatedVariable(0, 0)
89    {
90
91    }
92
93private:
94    const unsigned              mMultiplexingSetSizeLimit;
95    const unsigned              mMaxMultiplexingSetSelections;
96    const unsigned              mWindowSize;
97    const bool                  mTestConstrainedAdvances;
98    unsigned                    mVariables;
99    RNG                         mRNG;
100    CharacterizationMap         mCharacterization;
101    ConstraintGraph             mConstraintGraph;   
102    AdvanceVector               mAdvance;
103    AdvanceDepth                mAdvanceDepth;
104    AdvanceVariable             mAdvanceNegatedVariable;
105    SubsetGraph                 mSubsetGraph;
106    CliqueGraph                 mUsageGraph;
107    MultiplexSetGraph           mMultiplexSetGraph;
108};
109
110}
111
112#endif // PABLO_AUTOMULTIPLEXING_HPP
Note: See TracBrowser for help on using the repository browser.