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

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

Stream set buffer maps

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