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

Last change on this file since 5755 was 5755, checked in by nmedfort, 16 months ago

Bug fixes and simplified MultiBlockKernel? logic

File size: 4.8 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 kernel {
9
10// Processing rate attributes are required for all stream set bindings for a kernel.
11// These attributes describe the number of items that are processed or produced as
12// a ratio in comparison to a reference stream set, normally the principal input stream set
13// by default (or the principal output stream set if there is no input).
14//
15// The default ratio is FixedRatio(1) which means that there is one item processed or
16// produced for every item of the reference stream.
17// FixedRatio(m, n) means that for every group of n items of the refrence stream,
18// there are m items in the output stream (rounding up).
19//
20// Kernels which produce a variable number of items use MaxRatio(n), for a maximum
21// of n items produced or consumed per principal input or output item.  MaxRatio(m, n)
22// means there are at most m items for every n items of the reference stream.
23//
24// RoundUpToMultiple(n) means that number of items produced is the same as the
25// number of reference items, rounded up to an exact multiple of n.
26//
27
28struct ProcessingRate  {
29
30    friend struct Binding;
31
32    enum class KindId {
33        Fixed, Bounded, Unknown, Relative, PopCount
34    };
35
36    using RateValue = boost::rational<unsigned>;
37
38    KindId getKind() const { return mKind; }
39
40    RateValue getRate() const {
41        assert (isFixed() || isRelative());
42        return mLowerBound;
43    }
44
45    RateValue getLowerBound() const {
46        assert (isFixed() || isBounded() || isUnknown());
47        return mLowerBound;
48    }
49
50    RateValue getUpperBound() const {
51        assert (isFixed() || isBounded());
52        assert (isFixed() ? mUpperBound == mLowerBound : mUpperBound > mLowerBound);
53        return mUpperBound;
54    }
55
56    const std::string & getReference() const {
57        assert (isRelative());
58        return mReference;
59    }
60
61    bool isFixed() const {
62        return mKind == KindId::Fixed;
63    }
64
65    bool isBounded() const {
66        return mKind == KindId::Bounded;
67    }
68
69    bool isRelative() const {
70        return mKind == KindId::Relative;
71    }
72
73    bool isPopCount() const {
74        return mKind == KindId::PopCount;
75    }
76
77    bool isUnknown() const {
78        return mKind == KindId::Unknown;
79    }
80
81    bool isDerived() const {
82        return isRelative(); // isFixed() ||
83    }
84
85    bool operator == (const ProcessingRate & other) const {
86        return mKind == other.mKind && mLowerBound == other.mLowerBound && mUpperBound == other.mUpperBound && mReference == other.mReference;
87    }
88
89    bool operator != (const ProcessingRate & other) const {
90        return !(*this == other);
91    }
92
93    friend ProcessingRate FixedRate(const unsigned);
94    friend ProcessingRate BoundedRate(const unsigned, const unsigned);
95    friend ProcessingRate UnknownRate(const unsigned);
96    friend ProcessingRate RateEqualTo(std::string);
97    friend ProcessingRate PopcountOf(std::string, const ProcessingRate::RateValue);
98
99    ProcessingRate(ProcessingRate &&) = default;
100    ProcessingRate(const ProcessingRate &) = default;
101    ProcessingRate & operator = (const ProcessingRate & other) = default;
102
103protected:   
104    ProcessingRate(const KindId k, const unsigned n, const unsigned m, const std::string && ref = "") : mKind(k), mLowerBound(n), mUpperBound(m), mReference(ref) {}
105    ProcessingRate(const KindId k, const RateValue n, const RateValue m, const std::string && ref = "") : mKind(k), mLowerBound(n), mUpperBound(m), mReference(ref) {}
106private:
107    KindId mKind;
108    RateValue mLowerBound;
109    RateValue mUpperBound;
110    std::string mReference;
111};
112
113inline ProcessingRate FixedRate(const unsigned rate = 1) {
114    return ProcessingRate(ProcessingRate::KindId::Fixed, rate, rate);
115}
116
117inline ProcessingRate BoundedRate(const unsigned lower, const unsigned upper) {
118    if (lower == upper) {
119        return FixedRate(lower);
120    } else {
121        return ProcessingRate(ProcessingRate::KindId::Bounded, {lower}, {upper});
122    }
123}
124
125/**
126 * @brief UnknownRate
127 *
128 * The produced item count per stride should never be dependent on an unknown rate input stream.
129 */
130inline ProcessingRate UnknownRate(const unsigned lower = 0) {
131    return ProcessingRate(ProcessingRate::KindId::Unknown, lower, 0);
132}
133
134inline ProcessingRate RateEqualTo(std::string ref) {
135    return ProcessingRate(ProcessingRate::KindId::Relative, 1, 0, std::move(ref));
136}
137
138inline ProcessingRate PopcountOf(std::string ref, const ProcessingRate::RateValue ratio = ProcessingRate::RateValue{1}) {
139    return ProcessingRate(ProcessingRate::KindId::PopCount, ratio, ProcessingRate::RateValue{0}, std::move(ref));
140}
141
142ProcessingRate::RateValue lcm(const ProcessingRate::RateValue & x, const ProcessingRate::RateValue & y);
143
144ProcessingRate::RateValue gcd(const ProcessingRate::RateValue & x, const ProcessingRate::RateValue & y);
145
146}
147
148#endif // PROCESSING_RATE_H
Note: See TracBrowser for help on using the repository browser.