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

Last change on this file since 5957 was 5939, checked in by cameron, 18 months ago

Some cleanups

File size: 6.5 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
65    std::string fileName = inputFile;
66
67    std::ifstream f(fileName, std::ios::binary | std::ios::ate);
68    if (f.fail()) {
69        return -1;
70    }
71    size_t mFilesize = f.tellg();
72
73    boost::iostreams::mapped_file_source mappedFile;
74    // Since mmap offset has to be multiples of pages, we can't use it to skip headers.
75    mappedFile.open(fileName , mFilesize);
76
77    char *fileBuffer = const_cast<char *>(mappedFile.data());
78
79    if (codegen::SegmentSize < 2) {
80        codegen::SegmentSize = 2;
81    }
82
83    const int inputBufferBlocks = codegen::BufferSegments * codegen::ThreadNum * 16;
84
85    ParabixDriver pxDriver("character_deletion");
86    auto & iBuilder = pxDriver.getBuilder();
87    Module * M = iBuilder->getModule();
88    Type * const sizeTy = iBuilder->getSizeTy();
89    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
90    Type * const voidTy = iBuilder->getVoidTy();
91    Type * const inputType = iBuilder->getInt8PtrTy();
92
93    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, nullptr));
94    main->setCallingConv(CallingConv::C);
95    Function::arg_iterator args = main->arg_begin();
96    Value* inputStream = &*(args++);
97    inputStream->setName("input");
98
99    Value* fileSize = &*(args++);
100    fileSize->setName("fileSize");
101
102    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
103
104    // GeneratePipeline
105    StreamSetBuffer * ByteStream = pxDriver.addBuffer<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
106    StreamSetBuffer * BasisBits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), inputBufferBlocks);
107
108    kernel::Kernel * sourceK = pxDriver.addKernelInstance<MemorySourceKernel>(iBuilder, iBuilder->getInt8PtrTy());
109    sourceK->setInitialArguments({inputStream, fileSize});
110    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
111
112    Kernel * s2pk = pxDriver.addKernelInstance<S2PKernel>(iBuilder, /*aligned = */ true);
113    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
114
115    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), inputBufferBlocks);
116    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(characterToBeDeleted)}, 8);
117    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
118
119
120//    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks);
121//    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks);
122    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks, 1);
123    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks, 1);
124    Kernel * delK = pxDriver.addKernelInstance<SwizzledDeleteByPEXTkernel>(iBuilder, 64, 8);
125    pxDriver.makeKernelCall(delK, {CharacterMarkerBuffer, BasisBits}, {u16Swizzle0, u16Swizzle1});
126
127    // Produce unswizzled bit streams
128    StreamSetBuffer * extractedbits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), inputBufferBlocks);
129    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
130
131    pxDriver.makeKernelCall(unSwizzleK, {u16Swizzle0, u16Swizzle1}, {extractedbits});
132
133    StreamSetBuffer * const DecompressedByteStream = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), inputBufferBlocks);
134    Kernel * p2sK = pxDriver.addKernelInstance<P2SKernel>(iBuilder);
135    pxDriver.makeKernelCall(p2sK, {extractedbits}, {DecompressedByteStream});
136
137    // --------------------------------------------------------
138    // End
139
140    Kernel * outK = pxDriver.addKernelInstance<StdOutKernel>(iBuilder, 8);
141    pxDriver.makeKernelCall(outK, {DecompressedByteStream}, {});
142
143    /*
144    Kernel * outK = pxDriver.addKernelInstance<FileSink>(iBuilder, 8);
145    outK->setInitialArguments({iBuilder->GetString(outputFile)});
146    pxDriver.makeKernelCall(outK, {DecompressedByteStream}, {});
147    */
148
149
150    pxDriver.generatePipelineIR();
151
152    pxDriver.deallocateBuffers();
153
154    iBuilder->CreateRetVoid();
155
156    pxDriver.finalizeObject();
157
158
159    auto mainFunc = reinterpret_cast<MainFunctionType>(pxDriver.getMain());
160
161
162    mainFunc(fileBuffer, mFilesize);
163
164    mappedFile.close();
165    return 0;
166}
Note: See TracBrowser for help on using the repository browser.