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

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

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

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