source: icGREP/icgrep-devel/icgrep/character_deletion.cpp @ 6025

Last change on this file since 6025 was 6008, checked in by nmedfort, 16 months ago

Removed temporary buffers from pipeline and placed them in the source kernels.

File size: 5.9 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/CommandLine.h>
12#include <llvm/Support/PrettyStackTrace.h>
13#include <llvm/Support/Signals.h>
14#include <llvm/Support/ManagedStatic.h>
15#include <toolchain/toolchain.h>
16
17#include <IR_Gen/idisa_target.h>
18#include <boost/filesystem.hpp>
19#include <boost/iostreams/device/mapped_file.hpp>
20
21#include <cc/cc_compiler.h>
22#include <kernels/cc_kernel.h>
23#include <kernels/streamset.h>
24#include <kernels/s2p_kernel.h>
25#include <kernels/source_kernel.h>
26#include <kernels/stdout_kernel.h>
27#include <kernels/swizzle.h>
28
29#include <kernels/kernel_builder.h>
30#include <kernels/p2s_kernel.h>
31#include <toolchain/cpudriver.h>
32#include <iostream>
33#include <fstream>
34#include <kernels/deletion.h>
35
36namespace re { class CC; }
37
38using namespace llvm;
39using namespace parabix;
40using namespace kernel;
41
42static cl::OptionCategory lz4dFlags("Command Flags", "deletion options");
43static cl::opt<char> characterToBeDeleted(cl::Positional, cl::desc("<character to be deletion>"), cl::Required, cl::cat(lz4dFlags));
44static cl::opt<std::string> inputFile(cl::Positional, cl::desc("<input file>"), cl::Required, cl::cat(lz4dFlags));
45
46
47typedef void (*MainFunctionType)(char * byte_data, size_t filesize);
48
49/*
50 * Usage:
51 *   character_deletion <character to be deleted> <input file name>
52 * It will delete the character from the input file and then print the output to stdout
53 * */
54
55int main(int argc, char *argv[]) {
56    // This boilerplate provides convenient stack traces and clean LLVM exit
57    // handling. It also initializes the built in support for convenient
58    // command line option handling.
59    sys::PrintStackTraceOnErrorSignal(argv[0]);
60    llvm::PrettyStackTraceProgram X(argc, argv);
61    llvm_shutdown_obj shutdown;
62    codegen::ParseCommandLineOptions(argc, argv, {&lz4dFlags, codegen::codegen_flags()});
63
64    const std::string fileName = inputFile;
65
66    std::ifstream f(fileName, std::ios::binary | std::ios::ate);
67    if (f.fail()) {
68        return -1;
69    }
70    size_t mFilesize = f.tellg();
71
72    boost::iostreams::mapped_file_source mappedFile;
73    // Since mmap offset has to be multiples of pages, we can't use it to skip headers.
74    mappedFile.open(fileName , mFilesize);
75
76    char *fileBuffer = const_cast<char *>(mappedFile.data());
77
78    const int inputBufferBlocks = codegen::BufferSegments * codegen::ThreadNum * 16;
79
80    ParabixDriver pxDriver("character_deletion");
81    auto & iBuilder = pxDriver.getBuilder();
82    Module * M = iBuilder->getModule();
83    Type * const sizeTy = iBuilder->getSizeTy();
84    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
85    Type * const voidTy = iBuilder->getVoidTy();
86    Type * const inputType = iBuilder->getInt8PtrTy();
87
88    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, nullptr));
89    main->setCallingConv(CallingConv::C);
90    Function::arg_iterator args = main->arg_begin();
91    Value* inputStream = &*(args++);
92    inputStream->setName("input");
93
94    Value* fileSize = &*(args++);
95    fileSize->setName("fileSize");
96
97    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
98
99    // GeneratePipeline
100    StreamSetBuffer * ByteStream = pxDriver.addBuffer<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
101    StreamSetBuffer * BasisBits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), inputBufferBlocks);
102
103    kernel::Kernel * sourceK = pxDriver.addKernelInstance<MemorySourceKernel>(iBuilder);
104    sourceK->setInitialArguments({inputStream, fileSize});
105    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
106
107    Kernel * s2pk = pxDriver.addKernelInstance<S2PKernel>(iBuilder, /*aligned = */ true);
108    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
109
110    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), inputBufferBlocks);
111    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(characterToBeDeleted)}, 8);
112    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
113
114
115    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks, 1);
116    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks, 1);
117    Kernel * delK = pxDriver.addKernelInstance<SwizzledDeleteByPEXTkernel>(iBuilder, 8);
118    pxDriver.makeKernelCall(delK, {CharacterMarkerBuffer, BasisBits}, {u16Swizzle0, u16Swizzle1});
119
120    // Produce unswizzled bit streams
121    StreamSetBuffer * extractedbits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), inputBufferBlocks);
122    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
123
124    pxDriver.makeKernelCall(unSwizzleK, {u16Swizzle0, u16Swizzle1}, {extractedbits});
125
126    StreamSetBuffer * const DecompressedByteStream = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), inputBufferBlocks);
127    Kernel * p2sK = pxDriver.addKernelInstance<P2SKernel>(iBuilder);
128    pxDriver.makeKernelCall(p2sK, {extractedbits}, {DecompressedByteStream});
129
130    // --------------------------------------------------------
131    // End
132
133    Kernel * outK = pxDriver.addKernelInstance<StdOutKernel>(iBuilder, 8);
134    pxDriver.makeKernelCall(outK, {DecompressedByteStream}, {});
135
136    pxDriver.generatePipelineIR();
137
138    pxDriver.deallocateBuffers();
139
140    iBuilder->CreateRetVoid();
141
142    pxDriver.finalizeObject();
143
144
145    auto mainFunc = reinterpret_cast<MainFunctionType>(pxDriver.getMain());
146
147    mainFunc(fileBuffer, mFilesize);
148
149    mappedFile.close();
150    return 0;
151}
Note: See TracBrowser for help on using the repository browser.