source: icGREP/icgrep-devel/icgrep/kernels/interface.h @ 5398

Last change on this file since 5398 was 5398, checked in by nmedfort, 2 years ago

Continued work on processing stdin input. Partial integration of ParabixDriver? methods into icgrep and editd. Object cache does not currently work for recursive REs.

File size: 7.0 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6#ifndef KERNEL_INTERFACE_H
7#define KERNEL_INTERFACE_H
8
9#include <string>  // for string
10#include <vector>  // for vector
11namespace IDISA { class IDISA_Builder; }
12//namespace llvm { class ConstantInt; }
13#include <llvm/IR/Constants.h>
14namespace llvm { class Function; }
15namespace llvm { class Module; }
16namespace llvm { class PointerType; }
17namespace llvm { class StructType; }
18namespace llvm { class Type; }
19namespace llvm { class Value; }
20
21
22// Processing rate attributes are required for all stream set bindings for a kernel.
23// These attributes describe the number of items that are processed or produced as
24// a ratio in comparison to the principal input stream set (or the principal output
25// stream set if there is no input.
26//
27// The default ratio is FixedRatio(1) which means that there is one item processed or
28// produced for every item of the principal input or output stream.
29// FixedRatio(m, n) means that for every group of n items of the principal stream,
30// there are m items in the output stream (rounding up).
31//
32// Kernels which produce a variable number of items use MaxRatio(n), for a maximum
33// of n items produced or consumed per principal input or output item.  MaxRatio(m, n)
34// means there are at most m items for every n items of the principal stream.
35//
36// RoundUpToMultiple(n) means that number of items produced is the same as the
37// number of input items, rounded up to an exact multiple of n.
38//
39
40struct ProcessingRate  {
41    enum ProcessingRateKind : uint8_t {Fixed, RoundUp, Max, Add1, Unknown};
42    ProcessingRate() {}
43    ProcessingRateKind getKind() const {return mKind;}
44    bool isExact() const {return (mKind == Fixed)||(mKind == RoundUp)||(mKind == Add1) ;}
45    bool isUnknown() const { return !isExact(); }
46    llvm::Value * CreateRatioCalculation(IDISA::IDISA_Builder * b, llvm::Value * principalInputItems, llvm::Value * doFinal = nullptr) const;
47    friend ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string referenceStreamSet);
48    friend ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string referenceStreamSet);
49    friend ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string referenceStreamSet);   
50    friend ProcessingRate Add1(std::string referenceStreamSet);
51    friend ProcessingRate UnknownRate();
52    std::string referenceStreamSet() const { return mReferenceStreamSet;}
53protected:
54    ProcessingRate(ProcessingRateKind k, unsigned numerator, unsigned denominator, std::string referenceStreamSet) 
55    : mKind(k), mRatioNumerator(numerator), mRatioDenominator(denominator), mReferenceStreamSet(referenceStreamSet) {}
56private:
57    ProcessingRateKind mKind;
58    uint16_t mRatioNumerator;
59    uint16_t mRatioDenominator;
60    std::string mReferenceStreamSet;
61}; 
62
63ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1, std::string referenceStreamSet = "");
64ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1, std::string referenceStreamSet = "");
65ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string referenceStreamSet = "");
66ProcessingRate Add1(std::string referenceStreamSet = "");
67ProcessingRate UnknownRate();
68
69struct Binding {
70    Binding(llvm::Type * type, const std::string & name, ProcessingRate r = FixedRatio(1))
71    : type(type), name(name), rate(r) { }
72    llvm::Type *        type;
73    std::string         name;
74    ProcessingRate      rate;
75};
76
77class KernelInterface {
78
79public:
80    /*
81     
82     This class defines the methods to be used to generate the code 
83     necessary for declaring, allocating, calling and synchronizing
84     kernels.   The methods to be used for constructing kernels are defined
85     within the KernelBuilder class of kernel.h
86     
87     */
88   
89    const std::string & getName() const { return mKernelName; }
90
91    void setName(std::string newName) { mKernelName = newName; }
92       
93    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
94
95    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
96
97    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
98
99    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
100       
101    // Add ExternalLinkage method declarations for the kernel to a given client module.
102    void addKernelDeclarations(llvm::Module * client);
103
104    virtual void createInstance() = 0;
105
106    void setInitialArguments(std::vector<llvm::Value *> args);
107
108    llvm::Value * getInstance() const { return mKernelInstance; }
109
110    unsigned getLookAhead() const {
111        return mLookAheadPositions;
112    }
113   
114    IDISA::IDISA_Builder * getBuilder() const {
115        return iBuilder;
116    }
117
118    virtual llvm::Value * getProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * doFinal = nullptr) const = 0;
119
120    virtual void setProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
121
122    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const = 0;
123
124    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
125
126    virtual llvm::Value * getTerminationSignal(llvm::Value * instance) const = 0;
127
128    virtual void setTerminationSignal(llvm::Value * instance) const = 0;
129   
130    void setLookAhead(unsigned lookAheadPositions) {
131        mLookAheadPositions = lookAheadPositions;
132    }
133
134    llvm::Function * getInitFunction() const;
135
136    llvm::Function * getDoSegmentFunction() const;
137
138    llvm::Function * getAccumulatorFunction(const std::string & accumName) const;
139
140protected:
141
142    KernelInterface(IDISA::IDISA_Builder * builder,
143                    std::string kernelName,
144                    std::vector<Binding> && stream_inputs,
145                    std::vector<Binding> && stream_outputs,
146                    std::vector<Binding> && scalar_inputs,
147                    std::vector<Binding> && scalar_outputs,
148                    std::vector<Binding> && internal_scalars)
149    : iBuilder(builder)
150    , mKernelName(kernelName)
151    , mStreamSetInputs(stream_inputs)
152    , mStreamSetOutputs(stream_outputs)
153    , mScalarInputs(scalar_inputs)
154    , mScalarOutputs(scalar_outputs)
155    , mInternalScalars(internal_scalars)
156    , mKernelStateType(nullptr)
157    , mKernelInstance(nullptr)
158    , mLookAheadPositions(0) {
159
160    }
161   
162//    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType) {}
163
164protected:
165   
166    IDISA::IDISA_Builder * const iBuilder;
167    std::string mKernelName;
168    std::vector<llvm::Value *> mInitialArguments;
169    std::vector<Binding> mStreamSetInputs;
170    std::vector<Binding> mStreamSetOutputs;
171    std::vector<Binding> mScalarInputs;
172    std::vector<Binding> mScalarOutputs;
173    std::vector<Binding> mInternalScalars;
174    llvm::StructType * mKernelStateType;
175    llvm::Value * mKernelInstance;
176    unsigned mLookAheadPositions;
177   
178};
179
180#endif
Note: See TracBrowser for help on using the repository browser.