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

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

Add pthread create, join and exit support into CBuilder

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