source: icGREP/icgrep-devel/icgrep/kernels/streamset.h @ 5191

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

mmap and posix_memalign buffering options for u8u16

File size: 6.3 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 STREAMSET_H
7#define STREAMSET_H
8
9#include <string>
10#include <vector>
11#include <IDISA/idisa_builder.h>
12#include <llvm/IR/Type.h>
13
14namespace parabix {
15   
16enum FieldType {i1 = 1, i2 = 2, i4 = 4, i8 = 8, i16 = 16, i32 = 32, i64 = 64, i128 = 128, i256 = 256};
17
18class StreamSetType {
19public:
20    StreamSetType(int count, int width) : mStreamCount(count), mFieldWidth(width) {}
21    int StreamCount() { return mStreamCount;}
22    int StreamFieldWidth() { return mFieldWidth;}
23    bool operator== (StreamSetType& other) {return (mStreamCount == other.mStreamCount) && (mFieldWidth == other.mFieldWidth);}
24   
25    llvm::Type * getStreamSetBlockType(IDISA::IDISA_Builder * iBuilder);
26   
27private:
28    int mStreamCount;
29    int mFieldWidth;
30};
31
32   
33// Stream Set Structs hold information about the current state
34// of a stream set buffer.
35   
36llvm::Value * getProducerPosPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr);   
37llvm::Value * getConsumerPosPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr);
38llvm::Value * hasEndOfInputPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr);   
39llvm::Value * getStreamSetBufferPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr);   
40   
41class StreamSetBuffer {
42public:
43    enum class BufferKind : unsigned {BlockBuffer, ExternalFileBuffer, CircularBuffer, LinearCopybackBuffer};
44    inline BufferKind getBufferKind() const {return mBufferKind;}
45    inline StreamSetType& getBufferStreamSetType() {return mStreamSetType;}
46
47    llvm::PointerType * getStreamBufferPointerType();
48
49    size_t getBufferSize() { return mBufferBlocks;}
50   
51    virtual llvm::Value * allocateBuffer();
52   
53    llvm::Value * getStreamSetBasePtr() {return mStreamSetBufferPtr;}
54   
55    // Get the buffer pointer for a given block of the stream.
56    virtual llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) = 0;
57   
58    llvm::Value * getProducerPosPtr(Value * bufferStructPtr);
59
60    void setProducerPos(Value * bufferStructPtr, Value * pos);
61
62    llvm::Value * getConsumerPosPtr(Value * bufferStructPtr);
63
64    virtual void setConsumerPos(Value * bufferStructPtr, Value * pos);
65
66    llvm::Value * hasEndOfInputPtr(Value * bufferStructPtr);
67
68    void setEndOfInput(Value * bufferStructPtr);
69   
70    llvm::Value * getStreamSetBufferPtrPtr(Value * bufferStructPtr);
71
72    virtual llvm::PointerType * getStreamSetStructPointerType();
73
74    virtual llvm::Value * getStreamSetStructPtr();
75   
76protected:
77    StreamSetBuffer(BufferKind k, IDISA::IDISA_Builder * b, StreamSetType ss_type, unsigned blocks, unsigned AddressSpace = 0) :
78        mBufferKind(k), iBuilder(b), mStreamSetType(ss_type), mBufferBlocks(blocks), mAddrSpace(AddressSpace), mStreamSetBufferPtr(nullptr) {
79            mStreamSetStructType =
80                StructType::get(iBuilder->getContext(),
81                                std::vector<Type *>({iBuilder->getSizeTy(),
82                                                    iBuilder->getSizeTy(),
83                                                    iBuilder->getInt1Ty(),
84                                                    PointerType::get(mStreamSetType.getStreamSetBlockType(iBuilder), AddressSpace)}));
85    }
86   
87    const BufferKind       mBufferKind;
88    IDISA::IDISA_Builder * iBuilder;
89    StreamSetType mStreamSetType;
90    size_t mBufferBlocks;
91    unsigned mAddrSpace;
92    llvm::Value * mStreamSetBufferPtr;
93    llvm::Value * mStreamSetStructPtr;
94    llvm::Type * mStreamSetStructType;
95};   
96   
97
98class SingleBlockBuffer : public StreamSetBuffer {
99public:
100    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::BlockBuffer;}
101   
102    SingleBlockBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type) :
103    StreamSetBuffer(BufferKind::BlockBuffer, b, ss_type, 1, 0) {}
104    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) override;
105};
106   
107class ExternalFileBuffer : public StreamSetBuffer {
108public:
109    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::ExternalFileBuffer;}
110   
111    ExternalFileBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type, unsigned AddressSpace = 0) :
112        StreamSetBuffer(BufferKind::ExternalFileBuffer, b, ss_type, 0, AddressSpace) {}
113
114    void setStreamSetBuffer(llvm::Value * ptr, llvm::Value * fileSize);
115    void setEmptyBuffer(llvm::Value * buffer_ptr);
116   
117    // Can't allocate - raise an error. */
118    llvm::Value * allocateBuffer() override;
119    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) override;
120
121};
122   
123class CircularBuffer : public StreamSetBuffer {
124public:
125    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::CircularBuffer;}
126 
127    CircularBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type, size_t bufferBlocks, unsigned AddressSpace = 0) :
128        StreamSetBuffer(BufferKind::CircularBuffer, b, ss_type, bufferBlocks, AddressSpace) {
129            if (((bufferBlocks - 1) & bufferBlocks) != 0) {
130                throw std::runtime_error("CircularStreamSetBuffer: number of blocks must be a power of 2!");
131            }
132        }
133    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) override;
134};
135   
136// Linear buffers extending from the current ConsumerPos forward.   Within the buffer, the
137// offset of the block containing the current consumer position is always zero.
138//
139class LinearCopybackBuffer : public StreamSetBuffer {
140public:
141    static inline bool classof(const StreamSetBuffer * b) {return b->getBufferKind() == BufferKind::LinearCopybackBuffer;}
142   
143    LinearCopybackBuffer(IDISA::IDISA_Builder * b, StreamSetType ss_type, size_t bufferBlocks, unsigned AddressSpace = 0) :
144        StreamSetBuffer(BufferKind::LinearCopybackBuffer, b, ss_type, bufferBlocks, AddressSpace) {}
145   
146    llvm::Value * getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) override;
147   
148    // Reset the buffer to contain data starting at the base block of new_consumer_pos,
149    // copying back any data beyond that position.
150    void setConsumerPos(Value * bufferStructPtr, Value * new_consumer_pos) override;
151};
152   
153   
154
155}
156#endif // STREAMSET_H
Note: See TracBrowser for help on using the repository browser.