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

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

Minor changes in preparation of incorporating a consumed stream set position into select kernels.

File size: 7.2 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; }
[5370]12//namespace llvm { class ConstantInt; }
13#include <llvm/IR/Constants.h>
[5285]14namespace llvm { class Function; }
[5260]15namespace llvm { class Module; }
[5287]16namespace llvm { class PointerType; }
[5260]17namespace llvm { class StructType; }
18namespace llvm { class Type; }
19namespace llvm { class Value; }
[5047]20
[5307]21
[5325]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
[5328]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).
[5325]31//
32// Kernels which produce a variable number of items use MaxRatio(n), for a maximum
[5328]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.
[5327]35//
[5328]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//
[5307]39
[5328]40struct ProcessingRate  {
[5370]41    enum ProcessingRateKind : uint8_t {Fixed, RoundUp, Max, Add1, Unknown};
[5328]42    ProcessingRate() {}
43    ProcessingRateKind getKind() const {return mKind;}
[5370]44    bool isExact() const {return (mKind == Fixed)||(mKind == RoundUp)||(mKind == Add1) ;}
[5390]45    llvm::Value * CreateRatioCalculation(IDISA::IDISA_Builder * b, llvm::Value * principalInputItems, llvm::Value * doFinal = nullptr) const;
[5375]46    friend ProcessingRate FixedRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string referenceStreamSet);
47    friend ProcessingRate MaxRatio(unsigned strmItemsPer, unsigned perPrincipalInputItems, std::string referenceStreamSet);
48    friend ProcessingRate RoundUpToMultiple(unsigned itemMultiple, std::string referenceStreamSet);   
49    friend ProcessingRate Add1(std::string referenceStreamSet);
[5328]50    friend ProcessingRate UnknownRate();
[5375]51    std::string referenceStreamSet() const { return mReferenceStreamSet;}
[5325]52   
[5328]53protected:
[5375]54    ProcessingRate(ProcessingRateKind k, unsigned numerator, unsigned denominator, std::string referenceStreamSet) 
55    : mKind(k), ratio_numerator(numerator), ratio_denominator(denominator), mReferenceStreamSet(referenceStreamSet) {}
[5328]56    ProcessingRateKind mKind;
57    uint16_t ratio_numerator;
58    uint16_t ratio_denominator;
[5375]59    std::string mReferenceStreamSet;
[5328]60    bool isVariableRate();
61}; 
[5325]62
[5375]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 = "");
[5328]67ProcessingRate UnknownRate();
[5325]68
69struct Binding {
[5328]70    Binding(llvm::Type * type, const std::string & name, ProcessingRate r = FixedRatio(1))
71    : type(type), name(name), rate(r) { }
[5307]72    llvm::Type *        type;
73    std::string         name;
[5328]74    ProcessingRate      rate;
[5047]75};
[5060]76
[5047]77class KernelInterface {
78
79public:
[5174]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     */
[5251]88   
[5297]89    const std::string & getName() const { return mKernelName; }
[5346]90
91    void setName(std::string newName) { mKernelName = newName; }
[5246]92       
[5297]93    const std::vector<Binding> & getStreamInputs() const { return mStreamSetInputs; }
[5285]94
[5297]95    const std::vector<Binding> & getStreamOutputs() const { return mStreamSetOutputs; }
[5285]96
[5297]97    const std::vector<Binding> & getScalarInputs() const { return mScalarInputs; }
[5285]98
[5297]99    const std::vector<Binding> & getScalarOutputs() const { return mScalarOutputs; }
[5292]100       
[5174]101    // Add ExternalLinkage method declarations for the kernel to a given client module.
[5292]102    void addKernelDeclarations(llvm::Module * client);
[5285]103
[5246]104    virtual void createInstance() = 0;
[5285]105
[5260]106    void setInitialArguments(std::vector<llvm::Value *> args);
[5285]107
[5246]108    llvm::Value * getInstance() const { return mKernelInstance; }
[5174]109
[5202]110    unsigned getLookAhead() const {
111        return mLookAheadPositions;
112    }
[5174]113   
[5202]114    IDISA::IDISA_Builder * getBuilder() const {
115        return iBuilder;
116    }
117
[5390]118    virtual llvm::Value * getProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * doFinal = nullptr) const = 0;
[5285]119
[5390]120    virtual void setProducedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
[5285]121
[5390]122    virtual llvm::Value * getConsumedItemCount(llvm::Value * instance, const std::string & name) const = 0;
[5292]123
[5390]124    virtual void setConsumedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
[5370]125
[5390]126    virtual llvm::Value * getProcessedItemCount(llvm::Value * instance, const std::string & name) const = 0;
[5292]127
[5390]128    virtual void setProcessedItemCount(llvm::Value * instance, const std::string & name, llvm::Value * value) const = 0;
129
[5292]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   
[5347]166//    virtual void addAdditionalKernelDeclarations(llvm::Module * module, llvm::PointerType * selfType) {}
[5287]167
[5202]168protected:
[5053]169   
[5202]170    IDISA::IDISA_Builder * const iBuilder;
[5346]171    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.