source: icGREP/icgrep-devel/icgrep/character_deposit.cpp @ 6184

Last change on this file since 6184 was 6184, checked in by nmedfort, 5 months ago

Initial version of PipelineKernel? + revised StreamSet? model.

File size: 10.0 KB
Line 
1
2/*
3 *  Copyright (c) 2017 International Characters.
4 *  This software is licensed to the public under the Open Software License 3.0.
5 *  icgrep is a trademark of International Characters.
6 */
7
8#include <llvm/IR/Module.h>
9#include <llvm/IR/Function.h>
10#include <llvm/Linker/Linker.h>
11#include <llvm/Support/PrettyStackTrace.h>
12#include <llvm/Support/Signals.h>
13#include <llvm/Support/ManagedStatic.h>
14#include <toolchain/toolchain.h>
15
16#include <IR_Gen/idisa_target.h>
17#include <boost/filesystem.hpp>
18#include <boost/iostreams/device/mapped_file.hpp>
19
20#include <cc/cc_compiler.h>
21#include <kernels/cc_kernel.h>
22#include <kernels/streamset.h>
23#include <kernels/s2p_kernel.h>
24#include <kernels/source_kernel.h>
25#include <kernels/stdout_kernel.h>
26#include <kernels/swizzle.h>
27
28#include <kernels/kernel_builder.h>
29#include <kernels/p2s_kernel.h>
30#include <toolchain/cpudriver.h>
31#include <iostream>
32#include <fstream>
33#include <kernels/deletion.h>
34#include <kernels/pdep_kernel.h>
35#include <kernels/bitstream_pdep_kernel.h>
36
37namespace re { class CC; }
38
39using namespace llvm;
40using namespace kernel;
41
42static cl::OptionCategory characterDepositFlags("Command Flags", "deletion options");
43static cl::opt<char> characterToBeDeposit(cl::Positional, cl::desc("<character to be depositted>"), cl::Required, cl::cat(characterDepositFlags));
44static cl::opt<std::string> inputFile(cl::Positional, cl::desc("<input file>"), cl::Required, cl::cat(characterDepositFlags));
45static cl::opt<std::string> outputFile(cl::Positional, cl::desc("<output file>"), cl::Required, cl::cat(characterDepositFlags));
46static cl::opt<bool> useSwizzledDeposit("swizzle-deposit", cl::desc("Use swizzle deletion"), cl::init(false), cl::cat(characterDepositFlags));
47static cl::opt<bool> UseStreamDepositCompiler("UseStreamDepositCompiler", cl::desc("Use the StreamDepositCompiler deletion"), cl::init(false), cl::cat(characterDepositFlags));
48
49
50typedef void (*MainFunctionType)(char * byte_data, size_t filesize);
51
52/*
53 * Usage:
54 *     character_deposit <character to be deposit> <input file name> <output file name>
55 *
56 * Assume we have a input file with 6 character:
57 *     qbwbeb
58 * Then, the pipeline will first deleted all of character 'b' from input:
59 *     qbwbeb => qwe
60 * And then it will deposit the deleted result (qwe) to the original position of b:
61 *     qwe => \0 q \0 w \0 e (spaces are not included)
62 * and store the result to output file
63 * */
64
65StreamSetBuffer * loadBasisBits(CPUDriver & pxDriver, Value* inputStream, Value* fileSize, int bufferBlocks) {
66    auto & iBuilder = pxDriver.getBuilder();
67
68    StreamSetBuffer * ByteStream = pxDriver.addBuffer<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
69    StreamSetBuffer * BasisBits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), bufferBlocks);
70
71    kernel::Kernel * sourceK = pxDriver.addKernelInstance<MemorySourceKernel>(iBuilder);
72    sourceK->setInitialArguments({inputStream, fileSize});
73    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
74    Kernel * s2pk = pxDriver.addKernelInstance<S2PKernel>(iBuilder);
75    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
76
77    return BasisBits;
78}
79
80StreamSetBuffer * generateSwizzledDeposit(CPUDriver & pxDriver, StreamSetBuffer * BasisBits, int bufferBlocks) {
81    auto & iBuilder = pxDriver.getBuilder();
82
83    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), bufferBlocks);
84    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(characterToBeDeposit)}, 8);
85    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
86
87
88    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(4), bufferBlocks, 1);
89    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(4), bufferBlocks, 1);
90    Kernel * delK = pxDriver.addKernelInstance<SwizzledDeleteByPEXTkernel>(iBuilder, 8);
91    pxDriver.makeKernelCall(delK, {CharacterMarkerBuffer, BasisBits}, {u16Swizzle0, u16Swizzle1});
92
93    StreamSetBuffer * depositedSwizzle0 = pxDriver.addBuffer<DynamicBuffer>(iBuilder, iBuilder->getStreamSetTy(4), bufferBlocks, 1);
94    Kernel * pdep0K = pxDriver.addKernelInstance<PDEPkernel>(iBuilder, 4, "pdep0");
95    pxDriver.makeKernelCall(pdep0K, {CharacterMarkerBuffer, u16Swizzle0}, {depositedSwizzle0});
96
97
98    StreamSetBuffer * depositedSwizzle1 = pxDriver.addBuffer<DynamicBuffer>(iBuilder, iBuilder->getStreamSetTy(4), bufferBlocks, 1);
99    Kernel * pdep1K = pxDriver.addKernelInstance<PDEPkernel>(iBuilder, 4, "pdep1");
100    pxDriver.makeKernelCall(pdep1K, {CharacterMarkerBuffer, u16Swizzle1}, {depositedSwizzle1});
101
102    // Produce unswizzled bit streams
103    StreamSetBuffer * resultbits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), bufferBlocks);
104    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
105
106    pxDriver.makeKernelCall(unSwizzleK, {depositedSwizzle0, depositedSwizzle1}, {resultbits});
107    return resultbits;
108}
109
110StreamSetBuffer * generateBitStreamDeposit(CPUDriver & pxDriver, StreamSetBuffer * BasisBits, int bufferBlocks) {
111    auto & iBuilder = pxDriver.getBuilder();
112
113    StreamSetBuffer * const deletionMarker = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), bufferBlocks);
114    Kernel * ccK1 = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "deletionMarker", std::vector<re::CC *>{re::subtractCC(re::makeByte(0, 255), re::makeCC(characterToBeDeposit))}, 8);
115    pxDriver.makeKernelCall(ccK1, {BasisBits}, {deletionMarker});
116
117    StreamSetBuffer * const depositMarker = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), bufferBlocks);
118    Kernel * ccK2 = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(characterToBeDeposit)}, 8);
119    pxDriver.makeKernelCall(ccK2, {BasisBits}, {depositMarker});
120
121    // Deletion
122    StreamSetBuffer * deletedBits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), bufferBlocks);
123    StreamSetBuffer * deletionCounts = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), bufferBlocks);
124
125    Kernel * delK = pxDriver.addKernelInstance<PEXTFieldCompressKernel>(iBuilder, 64, 8);
126    pxDriver.makeKernelCall(delK, {BasisBits, deletionMarker}, {deletedBits, deletionCounts});
127
128    StreamSetBuffer * compressedBits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), bufferBlocks);
129    Kernel * streamCompressionK = pxDriver.addKernelInstance<StreamCompressKernel>(iBuilder, 64, 8);
130    pxDriver.makeKernelCall(streamCompressionK, {deletedBits, deletionCounts}, {compressedBits});
131
132    // Deposit
133    StreamSetBuffer * depositedBits = pxDriver.addBuffer<DynamicBuffer>(iBuilder, iBuilder->getStreamSetTy(8), bufferBlocks, 1);
134    if (UseStreamDepositCompiler) {
135        StreamDepositCompiler depositCompiler(pxDriver, 8, 0, 8, bufferBlocks);
136        depositCompiler.makeCall(depositMarker, compressedBits, depositedBits);
137    } else {
138        Kernel * pdepK = pxDriver.addKernelInstance<BitStreamPDEPKernel>(iBuilder, 8);
139        pxDriver.makeKernelCall(pdepK, {depositMarker, compressedBits}, {depositedBits});
140    }
141    return depositedBits;
142}
143
144int main(int argc, char *argv[]) {
145    // This boilerplate provides convenient stack traces and clean LLVM exit
146    // handling. It also initializes the built in support for convenient
147    // command line option handling.
148    sys::PrintStackTraceOnErrorSignal(argv[0]);
149    llvm::PrettyStackTraceProgram X(argc, argv);
150    llvm_shutdown_obj shutdown;
151    codegen::ParseCommandLineOptions(argc, argv, {&characterDepositFlags, codegen::codegen_flags()});
152
153    std::string fileName = inputFile;
154
155    std::ifstream f(fileName, std::ios::binary | std::ios::ate);
156    if (f.fail()) {
157        return -1;
158    }
159    size_t mFilesize = f.tellg();
160
161    boost::iostreams::mapped_file_source mappedFile;
162    // Since mmap offset has to be multiples of pages, we can't use it to skip headers.
163    mappedFile.open(fileName , mFilesize);
164
165    char *fileBuffer = const_cast<char *>(mappedFile.data());
166
167    const auto bufferBlocks = codegen::ThreadNum * codegen::SegmentSize;
168
169    CPUDriver pxDriver("character_deletion");
170    auto & iBuilder = pxDriver.getBuilder();
171    Module * M = iBuilder->getModule();
172    Type * const sizeTy = iBuilder->getSizeTy();
173    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
174    Type * const voidTy = iBuilder->getVoidTy();
175    Type * const inputType = iBuilder->getInt8PtrTy();
176
177    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, nullptr));
178    main->setCallingConv(CallingConv::C);
179    Function::arg_iterator args = main->arg_begin();
180    Value* inputStream = &*(args++);
181    inputStream->setName("input");
182
183    Value* fileSize = &*(args++);
184    fileSize->setName("fileSize");
185
186    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
187
188    StreamSetBuffer * BasisBits = loadBasisBits(pxDriver, inputStream, fileSize, bufferBlocks);
189    StreamSetBuffer * resultbits = NULL;
190    if (useSwizzledDeposit) {
191        resultbits = generateSwizzledDeposit(pxDriver, BasisBits, bufferBlocks);
192    } else {
193        resultbits = generateBitStreamDeposit(pxDriver, BasisBits, bufferBlocks);
194    }
195
196    StreamSetBuffer * const ResultBytes = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), bufferBlocks);
197    Kernel * p2sK = pxDriver.addKernelInstance<P2SKernel>(iBuilder);
198    pxDriver.makeKernelCall(p2sK, {resultbits}, {ResultBytes});
199
200    Kernel * outK = pxDriver.addKernelInstance<FileSink>(iBuilder, 8);
201    outK->setInitialArguments({iBuilder->GetString(outputFile)});
202    pxDriver.makeKernelCall(outK, {ResultBytes}, {});
203
204    pxDriver.generatePipelineIR();
205    iBuilder->CreateRetVoid();
206
207    pxDriver.finalizeObject();
208
209
210    auto mainFunc = reinterpret_cast<MainFunctionType>(pxDriver.getMain());
211
212
213    mainFunc(fileBuffer, mFilesize);
214
215    mappedFile.close();
216    return 0;
217}
Note: See TracBrowser for help on using the repository browser.