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

Last change on this file since 5477 was 5474, checked in by nmedfort, 2 years ago

Eliminated ExecutionEngine? memory leak. Intentionally broke compatibility with prior versions to ensure unchecked in projects are restructured.

File size: 5.4 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
60    //Round up to a multiple of 3.
61    const unsigned segmentSize = ((codegen::SegmentSize + 2)/3) * 3;
62   
63    const unsigned bufferSegments = codegen::BufferSegments;
64   
65    SourceBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
66
67    CircularBuffer Expanded3_4Out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * 4/3 * bufferSegments);
68    CircularBuffer Radix64out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * 4/3 * bufferSegments);
69    CircularCopybackBuffer Base64out(iBuilder, iBuilder->getStreamSetTy(1, 8), segmentSize * 4/3 * bufferSegments, 1);
70   
71    MMapSourceKernel mmapK(iBuilder, segmentSize);
72    mmapK.setInitialArguments({fileDescriptor});
73    pxDriver.addKernelCall(mmapK, {}, {&ByteStream});
74   
75    expand3_4Kernel expandK(iBuilder);
76    pxDriver.addKernelCall(expandK, {&ByteStream}, {&Expanded3_4Out});
77
78    radix64Kernel radix64K(iBuilder);
79    pxDriver.addKernelCall(radix64K, {&Expanded3_4Out}, {&Radix64out});
80
81    base64Kernel base64K(iBuilder);
82    pxDriver.addKernelCall(base64K, {&Radix64out}, {&Base64out});
83   
84    StdOutKernel stdoutK(iBuilder, 8);
85    pxDriver.addKernelCall(stdoutK, {&Base64out}, {});
86   
87    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main,0));
88
89    ByteStream.allocateBuffer(iBuilder);
90    Expanded3_4Out.allocateBuffer(iBuilder);
91    Radix64out.allocateBuffer(iBuilder);
92    Base64out.allocateBuffer(iBuilder);
93
94    pxDriver.generatePipelineIR();
95
96    iBuilder->CreateRetVoid();
97
98    pxDriver.finalizeObject();
99}
100
101
102typedef void (*base64FunctionType)(const uint32_t fd, char * outputBuffer);
103
104size_t file_size(const int fd) {
105    struct stat st;
106    if (LLVM_UNLIKELY(fstat(fd, &st) != 0)) {
107        st.st_size = 0;
108    }
109    return st.st_size;
110}
111
112void base64(base64FunctionType fn_ptr, const std::string & fileName) {
113
114    const int fd = open(fileName.c_str(), O_RDONLY);
115    if (LLVM_UNLIKELY(fd == -1)) {
116        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
117        return;
118    }
119    if (mMapBuffering) {
120        boost::interprocess::mapped_region outputBuffer(boost::interprocess::anonymous_shared_memory(2 * file_size(fd)));
121        outputBuffer.advise(boost::interprocess::mapped_region::advice_willneed);
122        outputBuffer.advise(boost::interprocess::mapped_region::advice_sequential);
123        fn_ptr(fd, static_cast<char*>(outputBuffer.get_address()));
124    } else if (memAlignBuffering) {
125        char * outputBuffer;
126        if (posix_memalign(reinterpret_cast<void **>(&outputBuffer), 32, 2 * file_size(fd))) {
127            throw std::bad_alloc();
128        }
129        fn_ptr(fd, outputBuffer);
130        free(reinterpret_cast<void *>(outputBuffer));
131    } else { /* No external output buffer */
132        fn_ptr(fd, nullptr);
133    }
134    close(fd);
135   
136}
137
138int main(int argc, char *argv[]) {
139    AddParabixVersionPrinter();
140    cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>{&base64Options, codegen::codegen_flags()});
141    cl::ParseCommandLineOptions(argc, argv);
142
143    ParabixDriver pxDriver("base64");
144    base64PipelineGen(pxDriver);
145    auto main = reinterpret_cast<base64FunctionType>(pxDriver.getMain());
146
147    for (unsigned i = 0; i != inputFiles.size(); ++i) {
148        base64(main, inputFiles[i]);
149    }
150
151    return 0;
152}
153
Note: See TracBrowser for help on using the repository browser.