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

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

Eliminate explicit storage of produced/processed counts for FixedRatio? streams

File size: 6.1 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; }
12namespace llvm { class ConstantInt; }
13namespace llvm { class Function; }
14namespace llvm { class Module; }
15namespace llvm { class PointerType; }
16namespace llvm { class StructType; }
17namespace llvm { class Type; }
18namespace llvm { class Value; }
19
20
21// Processing rate attributes are required for all stream set bindings for a kernel.
22// These attributes describe the number of items that are processed or produced as
23// a ratio in comparison to the principal input stream set (or the principal output
24// stream set if there is no input.
25//
26// The default ratio is FixedRatio(1) which means that there is one item processed or
27// produced for every item of the principal input or output item.
28//
29// Kernels which produce a variable number of items use MaxRatio(n), for a maximum
30// of n items produced or consumed per principal input or output item.
31struct ProcessingRate {
32    enum class ClassTypeId : unsigned {FixedRatio, MaxRatio, Unknown};
33    inline ClassTypeId getClassTypeId() const noexcept {
34        return mClassTypeId;
35    }
36   
37    ProcessingRate(ClassTypeId t = ClassTypeId::Unknown) : mClassTypeId(t) {}
38
39    const ClassTypeId       mClassTypeId;
40};
41
42//
43// FixedRatio(m, n) means that the number of items processed or produced for a principal
44// stream set of length L are ceiling(L*m/n)
45//
46struct FixedRatio : ProcessingRate {
47    FixedRatio(unsigned strmItems = 1, unsigned perPrincipalInputItems = 1)
48    : ProcessingRate(ClassTypeId::FixedRatio), thisStreamItems(strmItems), principalInputItems(perPrincipalInputItems) {
49    }
50    static inline bool classof(const ProcessingRate * e) {
51        return e->getClassTypeId() == ClassTypeId::FixedRatio;
52    }
53   
54    unsigned thisStreamItems;
55    unsigned principalInputItems;
56};
57
58struct MaxRatio : ProcessingRate {
59    MaxRatio(unsigned strmItems, unsigned perPrincipalInputItems = 1) 
60    : ProcessingRate(ClassTypeId::MaxRatio), thisStreamItems(strmItems), principalInputItems(perPrincipalInputItems) {
61    }
62    static inline bool classof(const ProcessingRate * e) {
63        return e->getClassTypeId() == ClassTypeId::MaxRatio;
64    }
65   
66    unsigned thisStreamItems;
67    unsigned principalInputItems;
68};
69
70       
71
72struct Binding {
73    Binding(llvm::Type * type, const std::string & name, ProcessingRate * r = nullptr)
74    : type(type), name(name) {
75        rate = (r == nullptr) ? new FixedRatio(1, 1) : r;
76    }
77
78    llvm::Type *        type;
79    std::string         name;
80    ProcessingRate *    rate;
81};
82
83class KernelInterface {
84
85public:
86    /*
87     
88     This class defines the methods to be used to generate the code 
89     necessary for declaring, allocating, calling and synchronizing
90     kernels.   The methods to be used for constructing kernels are defined
91     within the KernelBuilder class of kernel.h
92     
93     */
94   
95    const std::string & getName() const { return mKernelName; }
96       
97    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
98
99    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
100
101    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
102
103    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
104       
105    // Add ExternalLinkage method declarations for the kernel to a given client module.
106    void addKernelDeclarations(llvm::Module * client);
107
108    virtual void createInstance() = 0;
109
110    void setInitialArguments(std::vector<llvm::Value *> args);
111
112    llvm::Value * getInstance() const { return mKernelInstance; }
113
114    unsigned getLookAhead() const {
115        return mLookAheadPositions;
116    }
117   
118    IDISA::IDISA_Builder * getBuilder() const {
119        return iBuilder;
120    }
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 * getProducedItemCount(llvm::Value * instance, const std::string & name) const = 0;
127
128    virtual void setProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
129
130    virtual llvm::Value * getTerminationSignal(llvm::Value * instance) const = 0;
131
132    virtual void setTerminationSignal(llvm::Value * instance) const = 0;
133   
134    void setLookAhead(unsigned lookAheadPositions) {
135        mLookAheadPositions = lookAheadPositions;
136    }
137
138    llvm::Function * getInitFunction() const;
139
140    llvm::Function * getDoSegmentFunction() const;
141
142    llvm::Function * getAccumulatorFunction(const std::string & accumName) const;
143
144protected:
145
146    KernelInterface(IDISA::IDISA_Builder * builder,
147                    std::string && kernelName,
148                    std::vector<Binding> && stream_inputs,
149                    std::vector<Binding> && stream_outputs,
150                    std::vector<Binding> && scalar_inputs,
151                    std::vector<Binding> && scalar_outputs,
152                    std::vector<Binding> && internal_scalars)
153    : iBuilder(builder)
154    , mKernelName(kernelName)
155    , mStreamSetInputs(stream_inputs)
156    , mStreamSetOutputs(stream_outputs)
157    , mScalarInputs(scalar_inputs)
158    , mScalarOutputs(scalar_outputs)
159    , mInternalScalars(internal_scalars)
160    , mKernelStateType(nullptr)
161    , mKernelInstance(nullptr)
162    , mLookAheadPositions(0) {
163
164    }
165   
166    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType) {}
167
168protected:
169   
170    IDISA::IDISA_Builder * const iBuilder;
171    const std::string mKernelName;
172    std::vector<llvm::Value *> mInitialArguments;
173    std::vector<Binding> mStreamSetInputs;
174    std::vector<Binding> mStreamSetOutputs;
175    std::vector<Binding> mScalarInputs;
176    std::vector<Binding> mScalarOutputs;
177    std::vector<Binding> mInternalScalars;
178    llvm::StructType * mKernelStateType;
179    llvm::Value * mKernelInstance;
180    unsigned mLookAheadPositions;
181   
182};
183
184#endif
Note: See TracBrowser for help on using the repository browser.