source: icGREP/icgrep-devel/icgrep/kernels/kernel.h @ 5255

Last change on this file since 5255 was 5252, checked in by cameron, 3 years ago

Separate doSegment/final segment processing in pipeline loop; check optional NoTerminateAttribute?

File size: 7.4 KB
Line 
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_BUILDER_H
7#define KERNEL_BUILDER_H
8
9#include "streamset.h"
10#include "interface.h"
11#include <vector>
12#include <llvm/IR/Type.h>
13#include <IR_Gen/idisa_builder.h>
14#include <boost/container/flat_map.hpp>
15
16const std::string blockNoScalar = "blockNo";
17const std::string logicalSegmentNoScalar = "logicalSegNo";
18const std::string processedItemCountSuffix = "_processedItemCount";
19const std::string producedItemCountSuffix = "_producedItemCount";
20const std::string terminationSignal = "terminationSignal";
21const std::string structPtrSuffix = "_structPtr";
22const std::string blkMaskSuffix = "_blkMask";
23
24using namespace parabix;
25namespace kernel {
26   
27class KernelBuilder : public KernelInterface {
28    using NameMap = boost::container::flat_map<std::string, unsigned>;
29public:
30   
31    // Create a module for the kernel, including the kernel state type declaration and
32    // the full implementation of all required methods.     
33    //
34    std::unique_ptr<Module> createKernelModule(const std::vector<StreamSetBuffer *> & inputs, const std::vector<StreamSetBuffer *> & outputs);
35   
36    // Generate the Kernel to the current module (iBuilder->getModule()).
37    void generateKernel(const std::vector<StreamSetBuffer *> & inputs, const std::vector<StreamSetBuffer *> & outputs);
38   
39    void createInstance() override;
40
41    Function * generateThreadFunction(const std::string & name) const;
42
43    Value * getBlockNo(Value * self) const;
44    virtual Value * getProcessedItemCount(Value * self, const std::string & ssName) const override;
45    virtual Value * getProducedItemCount(Value * self, const std::string & ssName) const override;
46   
47    bool hasNoTerminateAttribute() { return mNoTerminateAttribute;}
48   
49    Value * getTerminationSignal(Value * self) const override;
50   
51    inline IntegerType * getSizeTy() const {
52        return getBuilder()->getSizeTy();
53    }
54
55    inline Type * getStreamTy(const unsigned FieldWidth = 1) {
56        return getBuilder()->getStreamTy(FieldWidth);
57    }
58   
59    inline Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
60        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
61    }
62   
63    // Synchronization actions for executing a kernel for a particular logical segment.
64    //
65    // Before the segment is processed, acquireLogicalSegmentNo must be used to load
66    // the segment number of the kernel state to ensure that the previous segment is
67    // complete (by checking that the acquired segment number is equal to the desired segment
68    // number).
69    // After all segment processing actions for the kernel are complete, and any necessary
70    // data has been extracted from the kernel for further pipeline processing, the
71    // segment number must be incremented and stored using releaseLogicalSegmentNo.
72    LoadInst * acquireLogicalSegmentNo(Value * self) const;
73
74    void releaseLogicalSegmentNo(Value * self, Value * newFieldVal) const;
75
76    virtual ~KernelBuilder() = 0;
77   
78    std::vector<StreamSetBuffer *> getStreamSetInputBuffers() {return mStreamSetInputBuffers;}
79    std::vector<StreamSetBuffer *> getStreamSetOutputBuffers() {return mStreamSetOutputBuffers;}
80
81
82protected:
83
84    // Constructor
85    KernelBuilder(IDISA::IDISA_Builder * builder,
86                    std::string kernelName,
87                    std::vector<Binding> stream_inputs,
88                    std::vector<Binding> stream_outputs,
89                    std::vector<Binding> scalar_parameters,
90                    std::vector<Binding> scalar_outputs,
91                    std::vector<Binding> internal_scalars);
92
93    //
94    // Kernel builder subtypes define their logic of kernel construction
95    // in terms of 3 virtual methods for
96    // (a) preparing the Kernel state data structure
97    // (b) defining the logic of the doBlock function, and
98    // (c) defining the logic of the finalBlock function.
99    //
100    // Note: the kernel state data structure must only be finalized after
101    // all scalar fields have been added.   If there are no fields to
102    // be added, the default method for preparing kernel state may be used.
103   
104    void setNoTerminateAttribute(bool noTerminate = true) {mNoTerminateAttribute = noTerminate;}
105   
106    virtual void prepareKernel();
107   
108    // Each kernel builder subtype must provide its own logic for generating
109    // doBlock calls.
110    virtual void generateDoBlockMethod() const = 0;
111
112    virtual void generateDoBlockLogic(Value * self, Value * blockNo) const;
113
114    // Each kernel builder subtypre must also specify the logic for processing the
115    // final block of stream data, if there is any special processing required
116    // beyond simply calling the doBlock function.   In the case that the final block
117    // processing may be trivially implemented by dispatching to the doBlock method
118    // without additional preparation, the default generateFinalBlockMethod need
119    // not be overridden.
120   
121    virtual void generateFinalBlockMethod() const;
122   
123    virtual void generateInitMethod() const;
124   
125    virtual void generateDoSegmentMethod() const;
126   
127    // Add an additional scalar field to the KernelState struct.
128    // Must occur before any call to addKernelDeclarations or createKernelModule.
129    unsigned addScalar(Type * type, const std::string & name);
130
131    unsigned getScalarCount() const;
132
133    // Run-time access of Kernel State and parameters of methods for
134    // use in implementing kernels.
135   
136    // Get the index of a named scalar field within the kernel state struct.
137    ConstantInt * getScalarIndex(const std::string & name) const;
138   
139    // Get the value of a scalar field for a given instance.
140    Value * getScalarField(Value * self, const std::string & fieldName) const;
141
142    // Set the value of a scalar field for a given instance.
143    void setScalarField(Value * self, const std::string & fieldName, Value * newFieldVal) const;
144   
145    // Get a parameter by name.
146    Value * getParameter(Function * f, const std::string & paramName) const;
147
148    Value * getStream(Value * self, const std::string & name, Value * blockNo, const unsigned index) {
149        return getStream(self, name, blockNo, iBuilder->getInt32(index));
150    }
151
152    Value * getStream(Value * self, const std::string & name, Value * blockNo, Value * index);
153   
154    // Stream set helpers.
155    unsigned getStreamSetIndex(const std::string & name) const;
156   
157    Value * getScalarFieldPtr(Value * self, const std::string & name) const;
158
159    Value * getStreamSetStructPtr(Value * self, const std::string & name) const;
160
161    size_t getStreamSetBufferSize(Value * self, const std::string & name) const;
162
163    Value * getStreamSetBlockPtr(Value * self, const std::string & name, Value * blockNo) const;
164   
165    void setBlockNo(Value * self, Value * newFieldVal) const;
166
167    virtual void setProcessedItemCount(Value * self, const std::string & ssName, Value * newFieldVal) const;
168
169    virtual void setProducedItemCount(Value * self, const std::string & ssName, Value * newFieldVal) const;
170
171    void setTerminationSignal(Value * self) const;
172
173protected:
174
175    std::vector<Type *>             mKernelFields;
176    NameMap                         mKernelMap;
177    NameMap                         mStreamSetNameMap;
178    std::vector<StreamSetBuffer *>  mStreamSetInputBuffers;
179    std::vector<StreamSetBuffer *>  mStreamSetOutputBuffers;
180    bool                            mNoTerminateAttribute;
181
182};
183}
184#endif
Note: See TracBrowser for help on using the repository browser.