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

Last change on this file since 5385 was 5375, checked in by cameron, 2 years ago

Processing rates can be defined with respect to named stream sets.

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    llvm::Value * CreateRatioCalculation(IDISA::IDISA_Builder * b, llvm::Value * principalInputItems, llvm::Value * doFinal) const;
46    llvm::Value * CreateRatioCalculation(IDISA::IDISA_Builder * b, llvm::Value * principalInputItems) 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;}
53   
54protected:
55    ProcessingRate(ProcessingRateKind k, unsigned numerator, unsigned denominator, std::string referenceStreamSet) 
56    : mKind(k), ratio_numerator(numerator), ratio_denominator(denominator), mReferenceStreamSet(referenceStreamSet) {}
57    ProcessingRateKind mKind;
58    uint16_t ratio_numerator;
59    uint16_t ratio_denominator;
60    std::string mReferenceStreamSet;
61    bool isVariableRate();
62}; 
63
64ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1, std::string referenceStreamSet = "");
65ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1, std::string referenceStreamSet = "");
66ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string referenceStreamSet = "");
67ProcessingRate Add1(std::string referenceStreamSet = "");
68ProcessingRate UnknownRate();
69
70struct Binding {
71    Binding(llvm::Type * type, const std::string & name, ProcessingRate r = FixedRatio(1))
72    : type(type), name(name), rate(r) { }
73    llvm::Type *        type;
74    std::string         name;
75    ProcessingRate      rate;
76};
77
78class KernelInterface {
79
80public:
81    /*
82     
83     This class defines the methods to be used to generate the code 
84     necessary for declaring, allocating, calling and synchronizing
85     kernels.   The methods to be used for constructing kernels are defined
86     within the KernelBuilder class of kernel.h
87     
88     */
89   
90    const std::string & getName() const { return mKernelName; }
91
92    void setName(std::string newName) { mKernelName = newName; }
93       
94    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
95
96    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
97
98    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
99
100    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
101       
102    // Add ExternalLinkage method declarations for the kernel to a given client module.
103    void addKernelDeclarations(llvm::Module * client);
104
105    virtual void createInstance() = 0;
106
107    void setInitialArguments(std::vector<llvm::Value *> args);
108
109    llvm::Value * getInstance() const { return mKernelInstance; }
110
111    unsigned getLookAhead() const {
112        return mLookAheadPositions;
113    }
114   
115    IDISA::IDISA_Builder * getBuilder() const {
116        return iBuilder;
117    }
118
119    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const = 0;
120
121    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
122
123
124
125    virtual void setProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
126
127    virtual llvm::Value * getTerminationSignal(llvm::Value * instance) const = 0;
128
129    virtual void setTerminationSignal(llvm::Value * instance) const = 0;
130   
131    void setLookAhead(unsigned lookAheadPositions) {
132        mLookAheadPositions = lookAheadPositions;
133    }
134
135    llvm::Function * getInitFunction() const;
136
137    llvm::Function * getDoSegmentFunction() const;
138
139    llvm::Function * getAccumulatorFunction(const std::string & accumName) const;
140
141protected:
142
143    KernelInterface(IDISA::IDISA_Builder * builder,
144                    std::string && kernelName,
145                    std::vector<Binding> && stream_inputs,
146                    std::vector<Binding> && stream_outputs,
147                    std::vector<Binding> && scalar_inputs,
148                    std::vector<Binding> && scalar_outputs,
149                    std::vector<Binding> && internal_scalars)
150    : iBuilder(builder)
151    , mKernelName(kernelName)
152    , mStreamSetInputs(stream_inputs)
153    , mStreamSetOutputs(stream_outputs)
154    , mScalarInputs(scalar_inputs)
155    , mScalarOutputs(scalar_outputs)
156    , mInternalScalars(internal_scalars)
157    , mKernelStateType(nullptr)
158    , mKernelInstance(nullptr)
159    , mLookAheadPositions(0) {
160
161    }
162   
163//    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType) {}
164
165protected:
166   
167    IDISA::IDISA_Builder * const iBuilder;
168    std::string mKernelName;
169    std::vector<llvm::Value *> mInitialArguments;
170    std::vector<Binding> mStreamSetInputs;
171    std::vector<Binding> mStreamSetOutputs;
172    std::vector<Binding> mScalarInputs;
173    std::vector<Binding> mScalarOutputs;
174    std::vector<Binding> mInternalScalars;
175    llvm::StructType * mKernelStateType;
176    llvm::Value * mKernelInstance;
177    unsigned mLookAheadPositions;
178   
179};
180
181#endif
Note: See TracBrowser for help on using the repository browser.