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

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

Removed non-functional CUDA code from icgrep and consolidated grep and multigrep mode into a single function; allowed segment parallel pipeline to utilize process as its initial thread; modified MMapSourceKernel to map and perform mmap directly and advise the OS to drop consumed data streams.

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