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

Last change on this file since 5135 was 5135, checked in by lindanl, 3 years ago

Add pipeline parallel strategy to the framework.

File size: 4.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
10#include "streamset.h"
11#include "interface.h"
12#include <vector>
13#include <llvm/IR/Type.h>
14#include <IDISA/idisa_builder.h>
15#include <boost/container/flat_map.hpp>
16
17const std::string blockNoScalar = "blockNo";
18const std::string basePtrSuffix = "_basePtr";
19const std::string blkMaskSuffix = "_blkMask";
20
21using namespace parabix;
22namespace kernel {
23   
24class KernelBuilder : public KernelInterface {
25    using NameMap = boost::container::flat_map<std::string, unsigned>;
26
27public:
28    KernelBuilder(IDISA::IDISA_Builder * builder,
29                    std::string kernelName,
30                    std::vector<StreamSetBinding> stream_inputs,
31                    std::vector<StreamSetBinding> stream_outputs,
32                    std::vector<ScalarBinding> scalar_parameters,
33                    std::vector<ScalarBinding> scalar_outputs,
34                    std::vector<ScalarBinding> internal_scalars);
35   
36    // Create a module for the kernel, including the kernel state type declaration and
37    // the full implementation of all required methods.     
38    //
39    std::unique_ptr<llvm::Module> createKernelModule(std::vector<StreamSetBuffer *> input_buffers, std::vector<StreamSetBuffer *> output_buffers);
40   
41    // Generate the Kernel to the current module (iBuilder->getModule()).
42    void generateKernel(std::vector<StreamSetBuffer *> input_buffers, std::vector<StreamSetBuffer *> output_buffers);
43   
44    llvm::Value * createInstance(std::vector<Value *> args) override;
45
46    Function * generateThreadFunction(std::string name);
47   
48protected:
49    //
50    // Kernel builder subtypes define their logic of kernel construction
51    // in terms of 3 virtual methods for
52    // (a) preparing the Kernel state data structure
53    // (b) defining the logic of the doBlock function, and
54    // (c) defining the logic of the finalBlock function.
55    //
56    // Note: the kernel state data structure must only be finalized after
57    // all scalar fields have been added.   If there are no fields to
58    // be added, the default method for preparing kernel state may be used.
59   
60    virtual void prepareKernel();
61   
62    // Each kernel builder subtype must provide its own logic for generating
63    // doBlock calls.
64    virtual void generateDoBlockMethod() = 0;
65   
66    // Each kernel builder subtypre must also specify the logic for processing the
67    // final block of stream data, if there is any special processing required
68    // beyond simply calling the doBlock function.   In the case that the final block
69    // processing may be trivially implemented by dispatching to the doBlock method
70    // without additional preparation, the default generateFinalBlockMethod need
71    // not be overridden.
72   
73    virtual void generateFinalBlockMethod();
74   
75    virtual void generateDoSegmentMethod();
76   
77    // Add an additional scalar field to the KernelState struct.
78    // Must occur before any call to addKernelDeclarations or createKernelModule.
79    void addScalar(llvm::Type * t, std::string scalarName);
80   
81    // Run-time access of Kernel State and parameters of methods for
82    // use in implementing kernels.
83   
84    // Get the index of a named scalar field within the kernel state struct.
85    llvm::Value * getScalarIndex(std::string);
86   
87    // Get the value of a scalar field for a given instance.
88    llvm::Value * getScalarField(llvm::Value * self, std::string fieldName);
89   
90    // Set the value of a scalar field for a given instance.
91    void setScalarField(llvm::Value * self, std::string fieldName, llvm::Value * newFieldVal);
92   
93    // Get a parameter by name.
94    llvm::Value * getParameter(llvm::Function * f, std::string paramName);
95   
96    // Stream set helpers.
97    unsigned getStreamSetIndex(std::string ssName);
98   
99    llvm::Value * getStreamSetBasePtr(Value * self, std::string ssName);
100    size_t getStreamSetBufferSize(Value * self, std::string ssName);
101
102    llvm::Value * getStreamSetBlockPtr(Value * self, std::string ssName, Value * blockNo);
103
104       
105protected:
106
107    std::vector<llvm::Type *>  mKernelFields;
108    NameMap                    mInternalStateNameMap;
109    NameMap                    mStreamSetNameMap;
110    std::vector<StreamSetBuffer *> mStreamSetInputBuffers;
111    std::vector<StreamSetBuffer *> mStreamSetOutputBuffers;
112
113};
114}
115#endif
Note: See TracBrowser for help on using the repository browser.