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

Last change on this file since 5857 was 5857, checked in by xwa163, 15 months ago
  1. Fix crash of pdep_kernel
  2. add initial version for character_deposit
File size: 7.3 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#include <kernels/pdep_kernel.h>
36
37namespace re { class CC; }
38
39using namespace llvm;
40using namespace parabix;
41using namespace kernel;
42
43static cl::OptionCategory lz4dFlags("Command Flags", "deletion options");
44//static cl::opt<char> characterToBeExtract(cl::Positional, cl::desc("<character to be extracted>"), cl::Required, cl::cat(lz4dFlags));
45static cl::opt<char> characterToBeDeposit(cl::Positional, cl::desc("<character to be depositted>"), cl::Required, cl::cat(lz4dFlags));
46static cl::opt<std::string> inputFile(cl::Positional, cl::desc("<input file>"), cl::Required, cl::cat(lz4dFlags));
47static cl::opt<std::string> outputFile(cl::Positional, cl::desc("<output file>"), cl::Required, cl::cat(lz4dFlags));
48
49
50typedef void (*MainFunctionType)(char * byte_data, size_t filesize);
51
52/*
53 * Usage:
54 *   character_deletion <character to be deleted> <input file name>
55 * It will delete the character from the input file and then print the output to stdout
56 * */
57
58int main(int argc, char *argv[]) {
59    // This boilerplate provides convenient stack traces and clean LLVM exit
60    // handling. It also initializes the built in support for convenient
61    // command line option handling.
62    sys::PrintStackTraceOnErrorSignal(argv[0]);
63    llvm::PrettyStackTraceProgram X(argc, argv);
64    llvm_shutdown_obj shutdown;
65    codegen::ParseCommandLineOptions(argc, argv, {&lz4dFlags, codegen::codegen_flags()});
66
67    std::string fileName = inputFile;
68
69    std::ifstream f(fileName, std::ios::binary | std::ios::ate);
70    if (f.fail()) {
71        return -1;
72    }
73    size_t mFilesize = f.tellg();
74
75    boost::iostreams::mapped_file_source mappedFile;
76    // Since mmap offset has to be multiples of pages, we can't use it to skip headers.
77    mappedFile.open(fileName , mFilesize);
78
79    char *fileBuffer = const_cast<char *>(mappedFile.data());
80
81    if (codegen::SegmentSize < 2) {
82        codegen::SegmentSize = 2;
83    }
84
85    const int inputBufferBlocks = codegen::BufferSegments * codegen::ThreadNum * 8;
86    const int outputBufferBlocks = inputBufferBlocks * 2;
87
88    ParabixDriver pxDriver("character_deletion");
89    auto & iBuilder = pxDriver.getBuilder();
90    Module * M = iBuilder->getModule();
91    Type * const sizeTy = iBuilder->getSizeTy();
92    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
93    Type * const voidTy = iBuilder->getVoidTy();
94    Type * const inputType = iBuilder->getInt8PtrTy();
95
96    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, nullptr));
97    main->setCallingConv(CallingConv::C);
98    Function::arg_iterator args = main->arg_begin();
99    Value* inputStream = &*(args++);
100    inputStream->setName("input");
101
102    Value* fileSize = &*(args++);
103    fileSize->setName("fileSize");
104
105    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
106
107
108    // GeneratePipeline
109    StreamSetBuffer * ByteStream = pxDriver.addBuffer<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
110    StreamSetBuffer * BasisBits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), inputBufferBlocks);
111
112    kernel::Kernel * sourceK = pxDriver.addKernelInstance<MemorySourceKernel>(iBuilder, iBuilder->getInt8PtrTy(), codegen::SegmentSize);
113    sourceK->setInitialArguments({inputStream, fileSize});
114    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
115    Kernel * s2pk = pxDriver.addKernelInstance<S2PKernel>(iBuilder, /*aligned = */ true);
116    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
117
118
119    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), inputBufferBlocks);
120    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(characterToBeDeposit)}, 8);
121    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
122
123
124    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks, 1);
125    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks, 1);
126    Kernel * delK = pxDriver.addKernelInstance<SwizzledDeleteByPEXTkernel>(iBuilder, 64, 8);
127    pxDriver.makeKernelCall(delK, {CharacterMarkerBuffer, BasisBits}, {u16Swizzle0, u16Swizzle1});
128
129
130    StreamSetBuffer * depositedSwizzle0 = pxDriver.addBuffer<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), outputBufferBlocks, 1);
131    Kernel * pdep0K = pxDriver.addKernelInstance<PDEPkernel>(iBuilder, 4, 4, 64, "pdep0");
132    pxDriver.makeKernelCall(pdep0K, {CharacterMarkerBuffer, u16Swizzle0}, {depositedSwizzle0});
133
134
135    StreamSetBuffer * depositedSwizzle1 = pxDriver.addBuffer<SwizzledCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), outputBufferBlocks, 1);
136    Kernel * pdep1K = pxDriver.addKernelInstance<PDEPkernel>(iBuilder, 4, 4, 64, "pdep1");
137    pxDriver.makeKernelCall(pdep1K, {CharacterMarkerBuffer, u16Swizzle1}, {depositedSwizzle1});
138
139    // Produce unswizzled bit streams
140    StreamSetBuffer * resultbits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), outputBufferBlocks);
141    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
142
143    pxDriver.makeKernelCall(unSwizzleK, {depositedSwizzle0, depositedSwizzle1}, {resultbits});
144//    pxDriver.makeKernelCall(unSwizzleK, {u16Swizzle0, u16Swizzle1}, {resultbits});
145
146    StreamSetBuffer * const ResultBytes = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), outputBufferBlocks);
147    Kernel * p2sK = pxDriver.addKernelInstance<P2SKernel>(iBuilder);
148    pxDriver.makeKernelCall(p2sK, {resultbits}, {ResultBytes});
149
150    // --------------------------------------------------------
151    // End
152/*
153    Kernel * outK = pxDriver.addKernelInstance<StdOutKernel>(iBuilder, 8);
154    pxDriver.makeKernelCall(outK, {DecompressedByteStream}, {});
155*/
156    Kernel * outK = pxDriver.addKernelInstance<FileSink>(iBuilder, 8);
157    outK->setInitialArguments({iBuilder->GetString(outputFile)});
158    pxDriver.makeKernelCall(outK, {ResultBytes}, {});
159
160    pxDriver.generatePipelineIR();
161
162    iBuilder->CreateRetVoid();
163
164    pxDriver.finalizeObject();
165
166
167    auto mainFunc = reinterpret_cast<MainFunctionType>(pxDriver.getMain());
168
169
170    mainFunc(fileBuffer, mFilesize);
171
172    mappedFile.close();
173    return 0;
174}
Note: See TracBrowser for help on using the repository browser.