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

Last change on this file since 5255 was 5255, checked in by cameron, 2 years ago

Fixes for mmap kernel for blocksPerSegment, codeUnitWidth, new doSegment interface

File size: 7.3 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 <IR_Gen/idisa_builder.h>
25#include <IR_Gen/idisa_target.h>
26#include <kernels/pipeline.h>
27#include <kernels/mmap_kernel.h>
28#include <kernels/interface.h>
29#include <kernels/kernel.h>
30#include <kernels/radix64.h>
31#include <kernels/stdout_kernel.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    Type * const size_ty = iBuilder->getSizeTy();
57    Type * const voidTy = Type::getVoidTy(mMod->getContext());
58    Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(mBitBlockType, 8), 1), 0);
59    Type * const outputType = PointerType::get(ArrayType::get(ArrayType::get(mBitBlockType, 8), 1), 0);
60   
61   
62    Function * const main = cast<Function>(mMod->getOrInsertFunction("Main", voidTy, inputType, outputType, size_ty, nullptr));
63    main->setCallingConv(CallingConv::C);
64    Function::arg_iterator args = main->arg_begin();
65   
66    Value * const inputStream = &*(args++);
67    inputStream->setName("inputStream");
68    Value * const outputStream = &*(args++);
69    outputStream->setName("outputStream");
70    Value * const fileSize = &*(args++);
71    fileSize->setName("fileSize");
72
73    //Round up to a multiple of 4.
74    const unsigned segmentSize = ((codegen::SegmentSize + 3)/4) * 4;
75   
76    const unsigned bufferSegments = codegen::BufferSegments;
77   
78    ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
79
80    CircularBuffer Expanded3_4Out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * bufferSegments * 16);
81    CircularBuffer Radix64out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * bufferSegments * 16);
82    LinearCopybackBuffer Base64out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * bufferSegments * 16 + 2);
83   
84    MMapSourceKernel mmapK(iBuilder, segmentSize * bufferSegments * 16); 
85    mmapK.generateKernel({}, {&ByteStream});
86    mmapK.setInitialArguments({fileSize});
87       
88    expand3_4Kernel expandK(iBuilder);
89    expandK.generateKernel({&ByteStream}, {&Expanded3_4Out});
90
91    radix64Kernel radix64K(iBuilder);
92    radix64K.generateKernel({&Expanded3_4Out}, {&Radix64out});
93
94    base64Kernel base64K(iBuilder);
95    base64K.generateKernel({&Radix64out}, {&Base64out});
96   
97    StdOutKernel stdoutK(iBuilder, 8);
98    stdoutK.generateKernel({&Base64out}, {});
99
100   
101    iBuilder->SetInsertPoint(BasicBlock::Create(mMod->getContext(), "entry", main,0));
102
103    ByteStream.setStreamSetBuffer(inputStream, fileSize);
104//    Radix64out.setEmptyBuffer(iBuilder->CreatePointerCast(outputStream, outputType));
105    Expanded3_4Out.allocateBuffer();
106    Radix64out.allocateBuffer();
107    Base64out.allocateBuffer();
108
109
110    if (segmentPipelineParallel){
111        generateSegmentParallelPipeline(iBuilder, {&mmapK, &expandK, &radix64K, &base64K, &stdoutK});
112    }
113    else{
114        generatePipelineLoop(iBuilder, {&mmapK, &expandK, &radix64K, &base64K, &stdoutK});
115    }
116
117    iBuilder->CreateRetVoid();
118    return main;
119}
120
121
122typedef void (*base64FunctionType)(char * byte_data, char * output_data, size_t filesize);
123
124static ExecutionEngine * base64Engine = nullptr;
125
126base64FunctionType base64CodeGen(void) {
127    LLVMContext TheContext;                           
128    Module * M = new Module("base64", TheContext);
129    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
130
131   
132    llvm::Function * main_IR = base64Pipeline(M, idb);
133   
134    verifyModule(*M, &dbgs());
135    //std::cerr << "ExecuteKernels(); done\n";
136    base64Engine = JIT_to_ExecutionEngine(M);
137   
138    base64Engine->finalizeObject();
139    //std::cerr << "finalizeObject(); done\n";
140
141    delete idb;
142    return reinterpret_cast<base64FunctionType>(base64Engine->getPointerToFunction(main_IR));
143}
144
145void base64(base64FunctionType fn_ptr, const std::string & fileName) {
146    std::string mFileName = fileName;
147    size_t mFileSize;
148    char * mFileBuffer;
149
150    const boost::filesystem::path file(mFileName);
151    if (exists(file)) {
152        if (is_directory(file)) {
153            return;
154        }
155    } else {
156        std::cerr << "Error: cannot open " << mFileName << " for processing. Skipped.\n";
157        return;
158    }
159   
160    mFileSize = file_size(file);
161    boost::iostreams::mapped_file_source mFile;
162    if (mFileSize == 0) {
163        mFileBuffer = nullptr;
164    }
165    else {
166        try {
167            mFile.open(mFileName);
168        } catch (std::exception &e) {
169            std::cerr << "Error: Boost mmap of " << mFileName << ": " << e.what() << std::endl;
170            return;
171        }
172        mFileBuffer = const_cast<char *>(mFile.data());
173    }
174
175    if (mMapBuffering) {
176        boost::interprocess::mapped_region outputBuffer(boost::interprocess::anonymous_shared_memory(2*mFileSize));
177        outputBuffer.advise(boost::interprocess::mapped_region::advice_willneed);
178        outputBuffer.advise(boost::interprocess::mapped_region::advice_sequential);
179        std::cerr << "outputBuffer " << std::hex << (intptr_t)(outputBuffer.get_address()) << std::dec << "\n";
180        fn_ptr(mFileBuffer, static_cast<char*>(outputBuffer.get_address()), mFileSize);
181    }
182    else if (memAlignBuffering) {
183        char * outputBuffer;
184        if (posix_memalign(reinterpret_cast<void **>(&outputBuffer), 32, 2*mFileSize)) {
185            throw std::bad_alloc();
186        }
187        fn_ptr(mFileBuffer, outputBuffer, mFileSize);
188        free(reinterpret_cast<void *>(outputBuffer));
189    }
190    else {
191        /* No external output buffer */
192        fn_ptr(mFileBuffer, nullptr, mFileSize);
193    }
194    mFile.close();
195   
196}
197
198
199int main(int argc, char *argv[]) {
200    cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>{&base64Options, codegen::codegen_flags()});
201    cl::ParseCommandLineOptions(argc, argv);
202
203    base64FunctionType fn_ptr = base64CodeGen();
204
205    for (unsigned i = 0; i != inputFiles.size(); ++i) {
206        base64(fn_ptr, inputFiles[i]);
207    }
208
209    return 0;
210}
211
Note: See TracBrowser for help on using the repository browser.