source: icGREP/icgrep-devel/icgrep/kernels/processing_rate.h @ 6184

Last change on this file since 6184 was 6184, checked in by nmedfort, 9 months ago

Initial version of PipelineKernel? + revised StreamSet? model.

File size: 5.3 KB
Line 
1#ifndef PROCESSING_RATE_H
2#define PROCESSING_RATE_H
3
4#include <string>
5#include <assert.h>
6#include <boost/rational.hpp>
7
8namespace llvm { class raw_ostream; }
9
10namespace kernel {
11
12// Processing rate attributes are required for all stream set bindings. They describe
13// the relationship between processed items (inputs) and produced items (outputs).
14//
15// For example, the 3-to-4 kernel converts every 3 input items into 4 output items.
16// Thus it has a FixedRate(3) for its input stream and FixedRate(4) for its output
17// stream. Processing these every 3 items individually would be time consuming. Instead
18// the kernel processes a strides' worth of "iterations" and automatically scales the
19// FixedRates accordingly.
20//
21// NOTE: fixed and bounded rates should be the smallest number of input items for the
22// smallest number of output items that can be logically produced by a kernel.
23
24class Kernel;
25
26struct ProcessingRate  {
27
28    friend struct Binding;
29
30    enum class KindId {
31        Fixed, Bounded, PopCount, NegatedPopCount, Unknown, Relative, Greedy
32    };
33
34    using RateValue = boost::rational<unsigned>;
35
36    KindId getKind() const { return mKind; }
37
38    RateValue getRate() const {
39        return mLowerBound;
40    }
41
42    RateValue getLowerBound() const {
43        return mLowerBound;
44    }
45
46    RateValue getUpperBound() const {
47        assert (!isGreedy());
48        return mUpperBound;
49    }
50
51    const std::string & getReference() const {
52        assert (hasReference());
53        return mReference;
54    }
55
56    bool isFixed() const {
57        return mKind == KindId::Fixed;
58    }
59
60    bool isBounded() const {
61        return mKind == KindId::Bounded;
62    }
63
64    bool isGreedy() const {
65        return mKind == KindId::Greedy;
66    }
67
68    bool isRelative() const {
69        return mKind == KindId::Relative;
70    }
71
72    bool isPopCount() const {
73        return mKind == KindId::PopCount;
74    }
75
76    bool isNegatedPopCount() const {
77        return mKind == KindId::NegatedPopCount;
78    }
79
80    bool isUnknown() const {
81        return mKind == KindId::Unknown;
82    }
83
84    bool hasReference() const {
85        return isRelative() || isPopCount() || isNegatedPopCount();
86    }
87
88    bool isDerived() const {
89        return isRelative();
90    }
91
92    bool operator == (const ProcessingRate & other) const {
93        return mKind == other.mKind && mLowerBound == other.mLowerBound && mUpperBound == other.mUpperBound && mReference == other.mReference;
94    }
95
96    bool operator != (const ProcessingRate & other) const {
97        return !(*this == other);
98    }
99
100    bool operator < (const ProcessingRate & other) const {
101        if (mKind < other.mKind) {
102            return true;
103        } else if (mLowerBound < other.mLowerBound) {
104            return true;
105        } else if (mUpperBound < other.mUpperBound) {
106            return true;
107        } else {
108            return mReference < other.mReference;
109        }
110    }
111
112    friend ProcessingRate FixedRate(const unsigned);   
113    friend ProcessingRate BoundedRate(const unsigned, const unsigned);
114    friend ProcessingRate UnknownRate(const unsigned);
115    friend ProcessingRate RateEqualTo(std::string);
116    friend ProcessingRate PopcountOf(std::string);
117    friend ProcessingRate PopcountOfNot(std::string);
118    friend ProcessingRate Greedy();
119
120    ProcessingRate(ProcessingRate &&) = default;
121    ProcessingRate(const ProcessingRate &) = default;
122    ProcessingRate & operator = (const ProcessingRate & other) = default;
123
124protected:   
125
126    ProcessingRate(const KindId k, const RateValue lb, const RateValue ub, const std::string && ref = "")
127    : mKind(k)
128    , mLowerBound(lb)
129    , mUpperBound(ub)
130    , mReference(ref) {
131
132    }
133
134    void print(const Kernel * const kernel, llvm::raw_ostream & out) const noexcept;
135
136private:
137    const KindId mKind;
138    const RateValue mLowerBound;
139    const RateValue mUpperBound;
140    const std::string mReference;
141};
142
143inline ProcessingRate FixedRate(const unsigned rate = 1) {
144    assert (rate > 0);
145    return ProcessingRate(ProcessingRate::KindId::Fixed, rate, rate);
146}
147
148inline ProcessingRate BoundedRate(const unsigned lower, const unsigned upper) {
149    if (lower == upper) {
150        return FixedRate(lower);
151    } else {
152        assert (upper > lower);
153        return ProcessingRate(ProcessingRate::KindId::Bounded, ProcessingRate::RateValue(lower), ProcessingRate::RateValue(upper));
154    }
155}
156
157inline ProcessingRate UnknownRate(const unsigned lower = 0) {
158    return ProcessingRate(ProcessingRate::KindId::Unknown, lower, 0);
159}
160
161inline ProcessingRate RateEqualTo(std::string ref) {
162    return ProcessingRate(ProcessingRate::KindId::Relative, 1, 1, std::move(ref));
163}
164
165inline ProcessingRate PopcountOf(std::string ref) {
166    return ProcessingRate(ProcessingRate::KindId::PopCount, 0, 1, std::move(ref));
167}
168
169inline ProcessingRate PopcountOfNot(std::string ref) {
170    return ProcessingRate(ProcessingRate::KindId::NegatedPopCount, 0, 1, std::move(ref));
171}
172
173inline ProcessingRate Greedy() {
174    return ProcessingRate(ProcessingRate::KindId::Greedy, 0, 0);
175}
176
177
178ProcessingRate::RateValue lcm(const ProcessingRate::RateValue & x, const ProcessingRate::RateValue & y);
179
180ProcessingRate::RateValue gcd(const ProcessingRate::RateValue & x, const ProcessingRate::RateValue & y);
181
182unsigned floor(const ProcessingRate::RateValue & r);
183
184unsigned ceiling(const ProcessingRate::RateValue & r);
185
186}
187
188#endif // PROCESSING_RATE_H
Note: See TracBrowser for help on using the repository browser.