source: icGREP/icgrep-devel/icgrep/base64.cpp @ 5307

Last change on this file since 5307 was 5307, checked in by nmedfort, 2 years ago

Continued work on eliminating BlockNo?

File size: 6.9 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <iostream>
8#include <llvm/IR/Function.h>
9#include <llvm/IR/Module.h>
10#include <llvm/ExecutionEngine/ExecutionEngine.h>
11#include <llvm/IR/Verifier.h>
12#include <llvm/Support/CommandLine.h>
13#include <toolchain.h>
14#include <IR_Gen/idisa_builder.h>
15#include <IR_Gen/idisa_target.h>
16#include <kernels/pipeline.h>
17#include <kernels/mmap_kernel.h>
18#include <kernels/streamset.h>
19#include <kernels/radix64.h>
20#include <kernels/stdout_kernel.h>
21#include <boost/filesystem.hpp>
22#include <boost/iostreams/device/mapped_file.hpp>
23#include <boost/interprocess/anonymous_shared_memory.hpp>
24
25using namespace llvm;
26
27static cl::OptionCategory base64Options("base64 Options",
28                                            "Transcoding control options.");
29
30static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore, cl::cat(base64Options));
31
32static cl::opt<bool> segmentPipelineParallel("enable-segment-pipeline-parallel", cl::desc("Enable multithreading with segment pipeline parallelism."), cl::cat(base64Options));
33static cl::opt<bool> mMapBuffering("mmap-buffering", cl::desc("Enable mmap buffering."), cl::cat(base64Options));
34static cl::opt<bool> memAlignBuffering("memalign-buffering", cl::desc("Enable posix_memalign buffering."), cl::cat(base64Options));
35
36
37using namespace kernel;
38using namespace parabix;
39
40Function * base64Pipeline(Module * mMod, IDISA::IDISA_Builder * iBuilder) {
41    Type * mBitBlockType = iBuilder->getBitBlockType();
42
43    Type * const size_ty = iBuilder->getSizeTy();
44    Type * const voidTy = Type::getVoidTy(mMod->getContext());
45    Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(mBitBlockType, 8), 1), 0);
46    Type * const outputType = PointerType::get(ArrayType::get(ArrayType::get(mBitBlockType, 8), 1), 0);
47   
48   
49    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", voidTy, inputType, outputType, size_ty, nullptr));
50    main->setCallingConv(CallingConv::C);
51    Function::arg_iterator args = main->arg_begin();
52   
53    Value * const inputStream = &*(args++);
54    inputStream->setName("inputStream");
55    Value * const outputStream = &*(args++);
56    outputStream->setName("outputStream");
57    Value * const fileSize = &*(args++);
58    fileSize->setName("fileSize");
59
60    //Round up to a multiple of 3.
61    const unsigned segmentSize = ((codegen::SegmentSize + 2)/3) * 3;
62   
63    const unsigned bufferSegments = codegen::BufferSegments;
64   
65    ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
66
67    CircularBuffer Expanded3_4Out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * 4/3 * bufferSegments);
68    CircularBuffer Radix64out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * 4/3 * bufferSegments);
69    CircularCopybackBuffer Base64out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * 4/3 * bufferSegments, 1);
70   
71    MMapSourceKernel mmapK(iBuilder, segmentSize);
72    mmapK.generateKernel({}, {&ByteStream});
73    mmapK.setInitialArguments({fileSize});
74       
75    expand3_4Kernel expandK(iBuilder);
76    expandK.generateKernel({&ByteStream}, {&Expanded3_4Out});
77
78    radix64Kernel radix64K(iBuilder);
79    radix64K.generateKernel({&Expanded3_4Out}, {&Radix64out});
80
81    base64Kernel base64K(iBuilder);
82    base64K.generateKernel({&Radix64out}, {&Base64out});
83   
84    StdOutKernel stdoutK(iBuilder, 8);
85    stdoutK.generateKernel({&Base64out}, {});
86   
87    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
88
89    ByteStream.setStreamSetBuffer(inputStream, fileSize);
90    Expanded3_4Out.allocateBuffer();
91    Radix64out.allocateBuffer();
92    Base64out.allocateBuffer();
93
94    if (segmentPipelineParallel) {
95        generateSegmentParallelPipeline(iBuilder, {&mmapK, &expandK, &radix64K, &base64K, &stdoutK});
96    } else {
97        generatePipelineLoop(iBuilder, {&mmapK, &expandK, &radix64K, &base64K, &stdoutK});
98    }
99
100    iBuilder->CreateRetVoid();
101    return main;
102}
103
104
105typedef void (*base64FunctionType)(char * byte_data, char * output_data, size_t filesize);
106
107static ExecutionEngine * base64Engine = nullptr;
108
109base64FunctionType base64CodeGen(void) {
110    LLVMContext TheContext;                           
111    Module * M = new Module("base64", TheContext);
112    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
113
114   
115    llvm::Function * main_IR = base64Pipeline(M, idb);
116   
117    verifyModule(*M, &dbgs());
118    //std::cerr << "ExecuteKernels(); done\n";
119    base64Engine = JIT_to_ExecutionEngine(M);
120   
121    base64Engine->finalizeObject();
122    //std::cerr << "finalizeObject(); done\n";
123
124    delete idb;
125    return reinterpret_cast<base64FunctionType>(base64Engine->getPointerToFunction(main_IR));
126}
127
128void base64(base64FunctionType fn_ptr, const std::string & fileName) {
129    std::string mFileName = fileName;
130    size_t mFileSize;
131    char * mFileBuffer;
132
133    const boost::filesystem::path file(mFileName);
134    if (exists(file)) {
135        if (is_directory(file)) {
136            return;
137        }
138    } else {
139        std::cerr << "Error: cannot open " << mFileName << " for processing. Skipped.\n";
140        return;
141    }
142   
143    mFileSize = file_size(file);
144    boost::iostreams::mapped_file_source mFile;
145    if (mFileSize == 0) {
146        mFileBuffer = nullptr;
147    }
148    else {
149        try {
150            mFile.open(mFileName);
151        } catch (std::exception &e) {
152            std::cerr << "Error: Boost mmap of " << mFileName << ": " << e.what() << std::endl;
153            return;
154        }
155        mFileBuffer = const_cast<char *>(mFile.data());
156    }
157
158    if (mMapBuffering) {
159        boost::interprocess::mapped_region outputBuffer(boost::interprocess::anonymous_shared_memory(2*mFileSize));
160        outputBuffer.advise(boost::interprocess::mapped_region::advice_willneed);
161        outputBuffer.advise(boost::interprocess::mapped_region::advice_sequential);
162        std::cerr << "outputBuffer " << std::hex << (intptr_t)(outputBuffer.get_address()) << std::dec << "\n";
163        fn_ptr(mFileBuffer, static_cast<char*>(outputBuffer.get_address()), mFileSize);
164    }
165    else if (memAlignBuffering) {
166        char * outputBuffer;
167        if (posix_memalign(reinterpret_cast<void **>(&outputBuffer), 32, 2*mFileSize)) {
168            throw std::bad_alloc();
169        }
170        fn_ptr(mFileBuffer, outputBuffer, mFileSize);
171        free(reinterpret_cast<void *>(outputBuffer));
172    }
173    else {
174        /* No external output buffer */
175        fn_ptr(mFileBuffer, nullptr, mFileSize);
176    }
177    mFile.close();
178   
179}
180
181
182int main(int argc, char *argv[]) {
183    cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>{&base64Options, codegen::codegen_flags()});
184    cl::ParseCommandLineOptions(argc, argv);
185
186    base64FunctionType fn_ptr = base64CodeGen();
187
188    for (unsigned i = 0; i != inputFiles.size(); ++i) {
189        base64(fn_ptr, inputFiles[i]);
190    }
191
192    return 0;
193}
194
Note: See TracBrowser for help on using the repository browser.