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

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

MaxReferenceItemsCalculation? initial check-in

File size: 7.8 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
[5370]9#include <llvm/IR/Constants.h>
[5418]10#include <string>
11#include <vector>
[5047]12
[5418]13namespace IDISA { class IDISA_Builder; }
[5307]14
[5325]15// Processing rate attributes are required for all stream set bindings for a kernel.
16// These attributes describe the number of items that are processed or produced as
17// a ratio in comparison to the principal input stream set (or the principal output
18// stream set if there is no input.
19//
20// The default ratio is FixedRatio(1) which means that there is one item processed or
[5328]21// produced for every item of the principal input or output stream.
22// FixedRatio(m, n) means that for every group of n items of the principal stream,
23// there are m items in the output stream (rounding up).
[5325]24//
25// Kernels which produce a variable number of items use MaxRatio(n), for a maximum
[5328]26// of n items produced or consumed per principal input or output item.  MaxRatio(m, n)
27// means there are at most m items for every n items of the principal stream.
[5327]28//
[5328]29// RoundUpToMultiple(n) means that number of items produced is the same as the
30// number of input items, rounded up to an exact multiple of n.
31//
[5307]32
[5328]33struct ProcessingRate  {
[5433]34    enum class ProcessingRateKind : uint8_t { FixedRatio, RoundUp, Add1, MaxRatio, Unknown };
[5328]35    ProcessingRateKind getKind() const {return mKind;}
[5433]36    bool isExact() const {return (mKind == ProcessingRateKind::FixedRatio)||(mKind == ProcessingRateKind::RoundUp)||(mKind == ProcessingRateKind::Add1) ;}
[5398]37    bool isUnknown() const { return !isExact(); }
[5390]38    llvm::Value * CreateRatioCalculation(IDISA::IDISA_Builder * b, llvm::Value * principalInputItems, llvm::Value * doFinal = nullptr) const;
[5433]39    llvm::Value * CreateMaxReferenceItemsCalculation(IDISA::IDISA_Builder * b, llvm::Value * outputItems, llvm::Value * doFinal) const;
[5401]40    friend ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string && referenceStreamSet);
41    friend ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string && referenceStreamSet);
42    friend ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string && referenceStreamSet);
43    friend ProcessingRate Add1(std::string && referenceStreamSet);
[5328]44    friend ProcessingRate UnknownRate();
[5375]45    std::string referenceStreamSet() const { return mReferenceStreamSet;}
[5328]46protected:
[5401]47    ProcessingRate(ProcessingRateKind k, unsigned numerator, unsigned denominator, std::string && referenceStreamSet)
[5398]48    : mKind(k), mRatioNumerator(numerator), mRatioDenominator(denominator), mReferenceStreamSet(referenceStreamSet) {}
49private:
[5401]50    const ProcessingRateKind mKind;
51    const uint16_t mRatioNumerator;
52    const uint16_t mRatioDenominator;
53    const std::string mReferenceStreamSet;
[5328]54}; 
[5325]55
[5401]56ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1, std::string && referenceStreamSet = "");
57ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems = 1, std::string && referenceStreamSet = "");
58ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string &&referenceStreamSet = "");
59ProcessingRate Add1(std::string && referenceStreamSet = "");
[5328]60ProcessingRate UnknownRate();
[5325]61
62struct Binding {
[5328]63    Binding(llvm::Type * type, const std::string & name, ProcessingRate r = FixedRatio(1))
64    : type(type), name(name), rate(r) { }
[5401]65    llvm::Type * const        type;
66    const std::string         name;
67    const ProcessingRate      rate;
[5047]68};
[5060]69
[5047]70class KernelInterface {
71public:
[5174]72    /*
73     
74     This class defines the methods to be used to generate the code 
75     necessary for declaring, allocating, calling and synchronizing
76     kernels.   The methods to be used for constructing kernels are defined
77     within the KernelBuilder class of kernel.h
78     
79     */
[5251]80   
[5297]81    const std::string & getName() const { return mKernelName; }
[5346]82
83    void setName(std::string newName) { mKernelName = newName; }
[5246]84       
[5431]85    virtual bool isCachable() const = 0;
86
87    virtual std::string makeSignature() = 0;
88
[5297]89    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
[5285]90
[5297]91    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
[5285]92
[5297]93    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
[5285]94
[5297]95    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
[5292]96       
[5174]97    // Add ExternalLinkage method declarations for the kernel to a given client module.
[5431]98    void addKernelDeclarations();
[5285]99
[5431]100    virtual void linkExternalMethods() = 0;
101
[5408]102    virtual llvm::Value * createInstance() = 0;
[5285]103
[5408]104    virtual void initializeInstance() = 0;
105
[5418]106    virtual void finalizeInstance() = 0;
[5411]107
[5260]108    void setInitialArguments(std::vector<llvm::Value *> args);
[5285]109
[5408]110    llvm::Value * getInstance() const {
111        return mKernelInstance;
112    }
[5174]113
[5202]114    unsigned getLookAhead() const {
115        return mLookAheadPositions;
116    }
[5174]117   
[5408]118    virtual llvm::Value * getProducedItemCount(const std::string & name, llvm::Value * doFinal = nullptr) const = 0;
[5285]119
[5408]120    virtual void setProducedItemCount(const std::string & name, llvm::Value * value) const = 0;
[5285]121
[5408]122    virtual llvm::Value * getProcessedItemCount(const std::string & name) const = 0;
[5292]123
[5408]124    virtual void setProcessedItemCount(const std::string & name, llvm::Value * value) const = 0;
[5390]125
[5418]126    virtual llvm::Value * getConsumedItemCount(const std::string & name) const = 0;
127
128    virtual void setConsumedItemCount(const std::string & name, llvm::Value * value) const = 0;
129
[5408]130    virtual llvm::Value * getTerminationSignal() const = 0;
[5292]131
[5408]132    virtual void setTerminationSignal() const = 0;
[5174]133   
[5202]134    void setLookAhead(unsigned lookAheadPositions) {
135        mLookAheadPositions = lookAheadPositions;
136    }
137
[5431]138    IDISA::IDISA_Builder * getBuilder() const {
139        return iBuilder;
140    }
[5287]141
[5431]142    void setBuilder(IDISA::IDISA_Builder * const builder) {
143        iBuilder = builder;
144    }
[5202]145
[5174]146protected:
[5202]147
[5431]148    llvm::Function * getInitFunction(llvm::Module * const module) const;
[5425]149
[5431]150    llvm::Function * getDoSegmentFunction(llvm::Module * const module) const;
151
152    llvm::Function * getTerminateFunction(llvm::Module * const module) const;
153
154    KernelInterface(IDISA::IDISA_Builder * const builder,
[5391]155                    std::string kernelName,
[5283]156                    std::vector<Binding> && stream_inputs,
157                    std::vector<Binding> && stream_outputs,
158                    std::vector<Binding> && scalar_inputs,
159                    std::vector<Binding> && scalar_outputs,
160                    std::vector<Binding> && internal_scalars)
[5267]161    : iBuilder(builder)
[5408]162    , mKernelInstance(nullptr)
163    , mKernelStateType(nullptr)
164    , mLookAheadPositions(0)
[5267]165    , mKernelName(kernelName)
166    , mStreamSetInputs(stream_inputs)
167    , mStreamSetOutputs(stream_outputs)
168    , mScalarInputs(scalar_inputs)
169    , mScalarOutputs(scalar_outputs)
170    , mInternalScalars(internal_scalars)
[5408]171    {
[5267]172
173    }
[5047]174   
[5408]175    void setInstance(llvm::Value * const instance) {
176        assert ("kernel instance cannot be null!" && instance);
177        assert ("kernel instance must point to a valid kernel state type!" && (instance->getType()->getPointerElementType() == mKernelStateType));
178        mKernelInstance = instance;
179    }
180
[5202]181protected:
[5053]182   
[5431]183    IDISA::IDISA_Builder *          iBuilder;
[5408]184    llvm::Value *                   mKernelInstance;
185    llvm::StructType *              mKernelStateType;
186    unsigned                        mLookAheadPositions;
187    std::string                     mKernelName;
188    std::vector<llvm::Value *>      mInitialArguments;
189    std::vector<Binding>            mStreamSetInputs;
190    std::vector<Binding>            mStreamSetOutputs;
191    std::vector<Binding>            mScalarInputs;
192    std::vector<Binding>            mScalarOutputs;
193    std::vector<Binding>            mInternalScalars;
[5047]194};
[5285]195
[5047]196#endif
Note: See TracBrowser for help on using the repository browser.