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

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

Separate processedItemCounts and producedItemCounts for each stream set

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