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

Last change on this file since 5997 was 5985, checked in by nmedfort, 17 months ago

Restructured MultiBlock? kernel. Removal of Swizzled buffers. Inclusion of PopCount? rates / non-linear access. Modifications to several kernels to better align them with the kernel and pipeline changes.

File size: 6.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/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    if (codegen::SegmentSize < 2) {
79        codegen::SegmentSize = 2;
80    }
81
82    const int inputBufferBlocks = codegen::BufferSegments * codegen::ThreadNum * 16;
83
84    ParabixDriver pxDriver("character_deletion");
85    auto & iBuilder = pxDriver.getBuilder();
86    Module * M = iBuilder->getModule();
87    Type * const sizeTy = iBuilder->getSizeTy();
88    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
89    Type * const voidTy = iBuilder->getVoidTy();
90    Type * const inputType = iBuilder->getInt8PtrTy();
91
92    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, nullptr));
93    main->setCallingConv(CallingConv::C);
94    Function::arg_iterator args = main->arg_begin();
95    Value* inputStream = &*(args++);
96    inputStream->setName("input");
97
98    Value* fileSize = &*(args++);
99    fileSize->setName("fileSize");
100
101    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
102
103    // GeneratePipeline
104    StreamSetBuffer * ByteStream = pxDriver.addBuffer<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
105    StreamSetBuffer * BasisBits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), inputBufferBlocks);
106
107    kernel::Kernel * sourceK = pxDriver.addKernelInstance<MemorySourceKernel>(iBuilder, iBuilder->getInt8PtrTy());
108    sourceK->setInitialArguments({inputStream, fileSize});
109    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
110
111    Kernel * s2pk = pxDriver.addKernelInstance<S2PKernel>(iBuilder, /*aligned = */ true);
112    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
113
114    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), inputBufferBlocks);
115    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(characterToBeDeleted)}, 8);
116    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
117
118
119    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks, 1);
120    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks, 1);
121    Kernel * delK = pxDriver.addKernelInstance<SwizzledDeleteByPEXTkernel>(iBuilder, 8);
122    pxDriver.makeKernelCall(delK, {CharacterMarkerBuffer, BasisBits}, {u16Swizzle0, u16Swizzle1});
123
124    // Produce unswizzled bit streams
125    StreamSetBuffer * extractedbits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), inputBufferBlocks);
126    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
127
128    pxDriver.makeKernelCall(unSwizzleK, {u16Swizzle0, u16Swizzle1}, {extractedbits});
129
130    StreamSetBuffer * const DecompressedByteStream = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), inputBufferBlocks);
131    Kernel * p2sK = pxDriver.addKernelInstance<P2SKernel>(iBuilder);
132    pxDriver.makeKernelCall(p2sK, {extractedbits}, {DecompressedByteStream});
133
134    // --------------------------------------------------------
135    // End
136
137    Kernel * outK = pxDriver.addKernelInstance<StdOutKernel>(iBuilder, 8);
138    pxDriver.makeKernelCall(outK, {DecompressedByteStream}, {});
139
140    pxDriver.generatePipelineIR();
141
142    pxDriver.deallocateBuffers();
143
144    iBuilder->CreateRetVoid();
145
146    pxDriver.finalizeObject();
147
148
149    auto mainFunc = reinterpret_cast<MainFunctionType>(pxDriver.getMain());
150
151    mainFunc(fileBuffer, mFilesize);
152
153    mappedFile.close();
154    return 0;
155}
Note: See TracBrowser for help on using the repository browser.