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

Last change on this file since 5370 was 5370, checked in by xuedongx, 2 years ago

Add1 processing rate; pablo Count only up to EOFbit

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