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

Last change on this file since 5547 was 5547, checked in by cameron, 23 months ago

Use DynamicBuffer? in base64 for testing

File size: 5.5 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/toolchain.h>
14#include <toolchain/cpudriver.h>
15#include <IR_Gen/idisa_target.h>
16#include <kernels/source_kernel.h>
17#include <kernels/streamset.h>
18#include <kernels/radix64.h>
19#include <kernels/stdout_kernel.h>
20#include <kernels/kernel_builder.h>
21#include <boost/interprocess/mapped_region.hpp>
22#include <boost/interprocess/anonymous_shared_memory.hpp>
23#include <sys/stat.h>
24#include <fcntl.h>
25
26using namespace llvm;
27
28static cl::OptionCategory base64Options("base64 Options",
29                                            "Transcoding control options.");
30
31static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore, cl::cat(base64Options));
32
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
40void base64PipelineGen(ParabixDriver & pxDriver) {
41       
42    auto & iBuilder = pxDriver.getBuilder();
43    Module * mod = iBuilder->getModule();
44    Type * bitBlockType = iBuilder->getBitBlockType();
45
46    Type * const voidTy = iBuilder->getVoidTy();
47    Type * const int32Ty = iBuilder->getInt32Ty();
48    Type * const outputType = PointerType::get(ArrayType::get(ArrayType::get(bitBlockType, 8), 1), 0);
49   
50   
51    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", voidTy, int32Ty, outputType, nullptr));
52    main->setCallingConv(CallingConv::C);
53    Function::arg_iterator args = main->arg_begin();
54   
55    Value * const fileDescriptor = &*(args++);
56    fileDescriptor->setName("fileDescriptor");
57    Value * const outputStream = &*(args++);
58    outputStream->setName("outputStream");
59    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main,0));
60
61    //Round up to a multiple of 3.
62    const unsigned initSegSize = ((codegen::SegmentSize + 2)/3) * 3;
63    const unsigned bufferSize = initSegSize * 4/3 * codegen::BufferSegments;
64
65    StreamSetBuffer * ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
66
67    Kernel * mmapK = pxDriver.addKernelInstance(make_unique<MMapSourceKernel>(iBuilder, initSegSize));
68    mmapK->setInitialArguments({fileDescriptor});
69    pxDriver.makeKernelCall(mmapK, {}, {ByteStream});
70   
71    StreamSetBuffer * Expanded3_4Out = pxDriver.addBuffer(make_unique<DynamicBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), bufferSize));
72    Kernel * expandK = pxDriver.addKernelInstance(make_unique<expand3_4Kernel>(iBuilder));
73    pxDriver.makeKernelCall(expandK, {ByteStream}, {Expanded3_4Out});
74   
75    StreamSetBuffer * Radix64out = pxDriver.addBuffer(make_unique<DynamicBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), bufferSize));
76    Kernel * radix64K = pxDriver.addKernelInstance(make_unique<radix64Kernel>(iBuilder));
77    pxDriver.makeKernelCall(radix64K, {Expanded3_4Out}, {Radix64out});
78   
79    StreamSetBuffer * Base64out = pxDriver.addBuffer(make_unique<DynamicBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), bufferSize));
80    Kernel * base64K = pxDriver.addKernelInstance(make_unique<base64Kernel>(iBuilder));
81    pxDriver.makeKernelCall(base64K, {Radix64out}, {Base64out});
82   
83    Kernel * outK = pxDriver.addKernelInstance(make_unique<StdOutKernel>(iBuilder, 8));
84    pxDriver.makeKernelCall(outK, {Base64out}, {});
85   
86    pxDriver.generatePipelineIR();
87
88    iBuilder->CreateRetVoid();
89
90    pxDriver.finalizeObject();
91}
92
93
94typedef void (*base64FunctionType)(const uint32_t fd, char * outputBuffer);
95
96size_t file_size(const int fd) {
97    struct stat st;
98    if (LLVM_UNLIKELY(fstat(fd, &st) != 0)) {
99        st.st_size = 0;
100    }
101    return st.st_size;
102}
103
104void base64(base64FunctionType fn_ptr, const std::string & fileName) {
105
106    const int fd = open(fileName.c_str(), O_RDONLY);
107    if (LLVM_UNLIKELY(fd == -1)) {
108        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
109        return;
110    }
111    if (mMapBuffering) {
112        boost::interprocess::mapped_region outputBuffer(boost::interprocess::anonymous_shared_memory(2 * file_size(fd)));
113        outputBuffer.advise(boost::interprocess::mapped_region::advice_willneed);
114        outputBuffer.advise(boost::interprocess::mapped_region::advice_sequential);
115        fn_ptr(fd, static_cast<char*>(outputBuffer.get_address()));
116    } else if (memAlignBuffering) {
117        char * outputBuffer;
118        if (posix_memalign(reinterpret_cast<void **>(&outputBuffer), 32, 2 * file_size(fd))) {
119            throw std::bad_alloc();
120        }
121        fn_ptr(fd, outputBuffer);
122        free(reinterpret_cast<void *>(outputBuffer));
123    } else { /* No external output buffer */
124        fn_ptr(fd, nullptr);
125    }
126    close(fd);
127   
128}
129
130int main(int argc, char *argv[]) {
131    codegen::ParseCommandLineOptions(argc, argv, {&base64Options, codegen::codegen_flags()});
132
133    ParabixDriver pxDriver("base64");
134    base64PipelineGen(pxDriver);
135    auto main = reinterpret_cast<base64FunctionType>(pxDriver.getMain());
136
137    for (unsigned i = 0; i != inputFiles.size(); ++i) {
138        base64(main, inputFiles[i]);
139    }
140
141    return 0;
142}
143
Note: See TracBrowser for help on using the repository browser.