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

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

Support for stdin. Needs more testing.

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