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
RevLine 
[5047]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
[5260]9#include <string>  // for string
10#include <vector>  // for vector
11namespace IDISA { class IDISA_Builder; }
[5307]12namespace llvm { class ConstantInt; }
[5285]13namespace llvm { class Function; }
[5260]14namespace llvm { class Module; }
[5287]15namespace llvm { class PointerType; }
[5260]16namespace llvm { class StructType; }
17namespace llvm { class Type; }
18namespace llvm { class Value; }
[5047]19
[5307]20
[5325]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) {}
[5307]38
[5325]39    const ClassTypeId       mClassTypeId;
40};
41
[5327]42//
[5325]43// FixedRatio(m, n) means that the number of items processed or produced for a principal
[5327]44// stream set of length L are ceiling(L*m/n)
45//
[5325]46struct FixedRatio : ProcessingRate {
47    FixedRatio(unsigned strmItems = 1, unsigned perPrincipalInputItems = 1)
48    : ProcessingRate(ClassTypeId::FixedRatio), thisStreamItems(strmItems), principalInputItems(perPrincipalInputItems) {
[5307]49    }
[5325]50    static inline bool classof(const ProcessingRate * e) {
51        return e->getClassTypeId() == ClassTypeId::FixedRatio;
52    }
53   
54    unsigned thisStreamItems;
55    unsigned principalInputItems;
56};
[5307]57
[5325]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
[5307]78    llvm::Type *        type;
79    std::string         name;
[5325]80    ProcessingRate *    rate;
[5047]81};
[5060]82
[5047]83class KernelInterface {
84
85public:
[5174]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     */
[5251]94   
[5297]95    const std::string & getName() const { return mKernelName; }
[5246]96       
[5297]97    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
[5285]98
[5297]99    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
[5285]100
[5297]101    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
[5285]102
[5297]103    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
[5292]104       
[5174]105    // Add ExternalLinkage method declarations for the kernel to a given client module.
[5292]106    void addKernelDeclarations(llvm::Module * client);
[5285]107
[5246]108    virtual void createInstance() = 0;
[5285]109
[5260]110    void setInitialArguments(std::vector<llvm::Value *> args);
[5285]111
[5246]112    llvm::Value * getInstance() const { return mKernelInstance; }
[5174]113
[5202]114    unsigned getLookAhead() const {
115        return mLookAheadPositions;
116    }
[5174]117   
[5202]118    IDISA::IDISA_Builder * getBuilder() const {
119        return iBuilder;
120    }
121
[5292]122    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const = 0;
[5285]123
[5292]124    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
[5285]125
[5292]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;
[5174]133   
[5202]134    void setLookAhead(unsigned lookAheadPositions) {
135        mLookAheadPositions = lookAheadPositions;
136    }
137
[5287]138    llvm::Function * getInitFunction() const;
139
[5285]140    llvm::Function * getDoSegmentFunction() const;
[5202]141
[5287]142    llvm::Function * getAccumulatorFunction(const std::string & accumName) const;
143
[5174]144protected:
[5202]145
[5047]146    KernelInterface(IDISA::IDISA_Builder * builder,
[5267]147                    std::string && kernelName,
[5283]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)
[5267]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    }
[5047]165   
[5292]166    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType) {}
[5287]167
[5202]168protected:
[5053]169   
[5202]170    IDISA::IDISA_Builder * const iBuilder;
[5267]171    const std::string mKernelName;
[5260]172    std::vector<llvm::Value *> mInitialArguments;
[5202]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;
[5227]178    llvm::StructType * mKernelStateType;
[5220]179    llvm::Value * mKernelInstance;
[5141]180    unsigned mLookAheadPositions;
181   
[5047]182};
[5285]183
[5047]184#endif
Note: See TracBrowser for help on using the repository browser.