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

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

Allow for override of kernel init method.

File size: 7.0 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    Value * getTerminationSignal(Value * self) const override;
47   
48    inline IntegerType * getSizeTy() const {
49        return getBuilder()->getSizeTy();
50    }
51
52    inline Type * getStreamTy(const unsigned FieldWidth = 1) {
53        return getBuilder()->getStreamTy(FieldWidth);
54    }
55   
56    inline Type * getStreamSetTy(const unsigned NumElements = 1, const unsigned FieldWidth = 1) {
57        return getBuilder()->getStreamSetTy(NumElements, FieldWidth);
58    }
59   
60    // Synchronization actions for executing a kernel for a particular logical segment.
61    //
62    // Before the segment is processed, acquireLogicalSegmentNo must be used to load
63    // the segment number of the kernel state to ensure that the previous segment is
64    // complete (by checking that the acquired segment number is equal to the desired segment
65    // number).
66    // After all segment processing actions for the kernel are complete, and any necessary
67    // data has been extracted from the kernel for further pipeline processing, the
68    // segment number must be incremented and stored using releaseLogicalSegmentNo.
69    LoadInst * acquireLogicalSegmentNo(Value * self) const;
70
71    void releaseLogicalSegmentNo(Value * self, Value * newFieldVal) const;
72
73    virtual ~KernelBuilder() = 0;
74
75protected:
76
77    // Constructor
78    KernelBuilder(IDISA::IDISA_Builder * builder,
79                    std::string kernelName,
80                    std::vector<Binding> stream_inputs,
81                    std::vector<Binding> stream_outputs,
82                    std::vector<Binding> scalar_parameters,
83                    std::vector<Binding> scalar_outputs,
84                    std::vector<Binding> internal_scalars);
85
86    //
87    // Kernel builder subtypes define their logic of kernel construction
88    // in terms of 3 virtual methods for
89    // (a) preparing the Kernel state data structure
90    // (b) defining the logic of the doBlock function, and
91    // (c) defining the logic of the finalBlock function.
92    //
93    // Note: the kernel state data structure must only be finalized after
94    // all scalar fields have been added.   If there are no fields to
95    // be added, the default method for preparing kernel state may be used.
96   
97    virtual void prepareKernel();
98   
99    // Each kernel builder subtype must provide its own logic for generating
100    // doBlock calls.
101    virtual void generateDoBlockMethod() const = 0;
102
103    virtual void generateDoBlockLogic(Value * self, Value * blockNo) const;
104
105    // Each kernel builder subtypre must also specify the logic for processing the
106    // final block of stream data, if there is any special processing required
107    // beyond simply calling the doBlock function.   In the case that the final block
108    // processing may be trivially implemented by dispatching to the doBlock method
109    // without additional preparation, the default generateFinalBlockMethod need
110    // not be overridden.
111   
112    virtual void generateFinalBlockMethod() const;
113   
114    virtual void generateInitMethod() const;
115   
116    virtual void generateDoSegmentMethod() const;
117   
118    // Add an additional scalar field to the KernelState struct.
119    // Must occur before any call to addKernelDeclarations or createKernelModule.
120    unsigned addScalar(Type * type, const std::string & name);
121
122    unsigned getScalarCount() const;
123
124    // Run-time access of Kernel State and parameters of methods for
125    // use in implementing kernels.
126   
127    // Get the index of a named scalar field within the kernel state struct.
128    ConstantInt * getScalarIndex(const std::string & name) const;
129   
130    // Get the value of a scalar field for a given instance.
131    Value * getScalarField(Value * self, const std::string & fieldName) const;
132
133    // Set the value of a scalar field for a given instance.
134    void setScalarField(Value * self, const std::string & fieldName, Value * newFieldVal) const;
135   
136    // Get a parameter by name.
137    Value * getParameter(Function * f, const std::string & paramName) const;
138
139    Value * getStream(Value * self, const std::string & name, Value * blockNo, const unsigned index) {
140        return getStream(self, name, blockNo, iBuilder->getInt32(index));
141    }
142
143    Value * getStream(Value * self, const std::string & name, Value * blockNo, Value * index);
144   
145    // Stream set helpers.
146    unsigned getStreamSetIndex(const std::string & name) const;
147   
148    Value * getScalarFieldPtr(Value * self, const std::string & name) const;
149
150    Value * getStreamSetStructPtr(Value * self, const std::string & name) const;
151
152    size_t getStreamSetBufferSize(Value * self, const std::string & name) const;
153
154    Value * getStreamSetBlockPtr(Value * self, const std::string & name, Value * blockNo) const;
155   
156    void setBlockNo(Value * self, Value * newFieldVal) const;
157
158    virtual void setProcessedItemCount(Value * self, const std::string & ssName, Value * newFieldVal) const;
159
160    virtual void setProducedItemCount(Value * self, const std::string & ssName, Value * newFieldVal) const;
161
162    void setTerminationSignal(Value * self) const;
163
164protected:
165
166    std::vector<Type *>             mKernelFields;
167    NameMap                         mKernelMap;
168    NameMap                         mStreamSetNameMap;
169    std::vector<StreamSetBuffer *>  mStreamSetInputBuffers;
170    std::vector<StreamSetBuffer *>  mStreamSetOutputBuffers;
171
172};
173}
174#endif
Note: See TracBrowser for help on using the repository browser.