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

Last change on this file since 5235 was 5232, checked in by xwa163, 3 years ago

Add based64 related kernels

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