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

Last change on this file since 5706 was 5706, checked in by nmedfort, 19 months ago

First stage of MultiBlockKernel? and pipeline restructuring

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