source: icGREP/icgrep-devel/icgrep/array-test.cpp @ 5245

Last change on this file since 5245 was 5245, checked in by nmedfort, 22 months ago

Work on bracket matching problem

File size: 6.2 KB
Line 
1/*
2 *  Copyright (c) 2015 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 <iomanip>
10#include <fstream>
11#include <sstream>
12
13
14#include <toolchain.h>
15#include <pablo/pablo_toolchain.h>
16#include <llvm/IR/Function.h>
17#include <llvm/IR/Module.h>
18#include <llvm/ExecutionEngine/ExecutionEngine.h>
19#include <llvm/ExecutionEngine/MCJIT.h>
20#include "llvm/Linker/Linker.h"
21#include <llvm/IR/Verifier.h>
22#include <llvm/Support/CommandLine.h>
23#include <llvm/Support/raw_ostream.h>
24
25#include <pablo/pablo_kernel.h>
26#include <IR_Gen/idisa_builder.h>
27#include <IR_Gen/idisa_target.h>
28#include <kernels/streamset.h>
29#include <kernels/interface.h>
30#include <kernels/kernel.h>
31#include <kernels/pipeline.h>
32#include <pablo/builder.hpp>
33#include <pablo/pablo_compiler.h>
34#include <pablo/pablo_toolchain.h>
35#include <kernels/s2p_kernel.h>
36#include <kernels/stdout_kernel.h>
37#include <boost/filesystem.hpp>
38#include <boost/iostreams/device/mapped_file.hpp>
39
40static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
41
42using namespace pablo;
43using namespace kernel;
44using namespace parabix;
45
46void generate(PabloKernel * kernel, const unsigned size) {
47
48    PabloBuilder pb(kernel->getEntryBlock());
49
50    Var * input = kernel->addInput("input", kernel->getStreamSetTy(8));
51    Var * matches = kernel->addOutput("matches", kernel->getStreamSetTy(size));
52
53    PabloAST * basis[8];
54    for (int i = 0; i < 8; ++i) {
55        basis[i] = pb.createExtract(input, i);
56    }
57
58    PabloAST * temp1 = pb.createOr(basis[0], basis[1]);
59    PabloAST * temp2 = pb.createAnd(basis[2], pb.createNot(basis[3]));
60    PabloAST * temp3 = pb.createAnd(temp2, pb.createNot(temp1));
61    PabloAST * temp4 = pb.createAnd(basis[4], pb.createNot(basis[5]));
62    PabloAST * temp5 = pb.createOr(basis[6], basis[7]);
63    PabloAST * temp6 = pb.createAnd(temp4, pb.createNot(temp5));
64    PabloAST * lparen = pb.createAnd(temp3, temp6, "lparens");
65    PabloAST * temp7 = pb.createAnd(basis[7], pb.createNot(basis[6]));
66    PabloAST * temp8 = pb.createAnd(temp4, temp7);
67    PabloAST * rparen = pb.createAnd(temp3, temp8, "rparens");
68    PabloAST * parens = pb.createOr(lparen, rparen);
69
70    PabloAST * pscan = pb.createScanTo(pb.createAdvance(lparen, 1), parens, "pscan");
71
72    PabloAST * closed = pb.createAnd(pscan, rparen, "closed");
73
74    pb.createAssign(pb.createExtract(matches, 0), closed);
75
76    Var * all_closed = pb.createVar("all_closed", closed);
77    Var * pending_lparen = pb.createVar("pending_lparen", pb.createAnd(pscan, lparen));
78    Var * unmatched_rparen = pb.createVar("unmatched_rparen", pb.createAnd(rparen, pb.createNot(closed)));
79    Var * in_play = pb.createVar("in_play", pb.createOr(pending_lparen, unmatched_rparen));
80
81
82    Integer * one = pb.getInteger(1);
83
84    Var * index = pb.createVar("i", one);
85
86    PabloBuilder body = PabloBuilder::Create(pb);
87
88    pb.createWhile(pending_lparen, body);
89
90        pscan = body.createScanTo(body.createAdvance(pending_lparen, 1), in_play);
91        closed = body.createAnd(pscan, rparen);
92        body.createAssign(body.createExtract(matches, index), closed);
93        body.createAssign(pending_lparen, body.createAnd(pscan, lparen));
94        body.createAssign(all_closed, body.createOr(all_closed, closed));
95        body.createAssign(unmatched_rparen, body.createAnd(rparen, body.createNot(all_closed)));
96        body.createAssign(in_play, body.createOr(pending_lparen, unmatched_rparen));
97        body.createAssign(index, body.createAdd(index, one));
98
99
100    pb.print(errs());
101
102}
103
104Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count) {
105
106    Type * byteStreamTy = iBuilder->getStreamSetTy(1, 8);
107
108    ExternalFileBuffer ByteStream(iBuilder, byteStreamTy);
109    SingleBlockBuffer BasisBits(iBuilder, iBuilder->getStreamSetTy(8, 1));
110    SingleBlockBuffer matches(iBuilder, iBuilder->getStreamSetTy(count, 1));
111
112    S2PKernel  s2pk(iBuilder);
113    s2pk.generateKernel({&ByteStream}, {&BasisBits});
114
115    PabloKernel bm(iBuilder, "MatchParens");
116    generate(&bm, count);
117
118    bm.generateKernel({&BasisBits}, {&matches});
119
120    Module * const mod = iBuilder->getModule();
121
122    Function * const main = cast<Function>(mod->getOrInsertFunction("Main", iBuilder->getVoidTy(), byteStreamTy->getPointerTo(), iBuilder->getSizeTy(), nullptr));
123    main->setCallingConv(CallingConv::C);
124    Function::arg_iterator args = main->arg_begin();
125
126    Value * const inputStream = &*(args++);
127    inputStream->setName("input");
128    Value * const fileSize = &*(args++);
129    fileSize->setName("fileSize");
130
131    iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", main, 0));
132
133    ByteStream.setStreamSetBuffer(inputStream, fileSize);
134    BasisBits.allocateBuffer();
135    matches.allocateBuffer();
136
137    generatePipelineLoop(iBuilder, {&s2pk, &bm});
138    iBuilder->CreateRetVoid();
139
140    return main;
141}
142
143typedef void (*MatchParens)(char * byteStream, size_t fileSize);
144
145MatchParens generateAlgorithm() {
146    LLVMContext ctx;
147    Module * M = new Module("mp", ctx);
148    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
149
150    llvm::Function * f = pipeline(idb, 10);
151
152    verifyModule(*M, &dbgs());
153
154    ExecutionEngine * wcEngine = JIT_to_ExecutionEngine(M);
155
156    wcEngine->finalizeObject();
157
158    delete idb;
159
160    return reinterpret_cast<MatchParens>(wcEngine->getPointerToFunction(f));
161}
162
163void run(MatchParens f, const std::string & fileName) {
164    const boost::filesystem::path file(fileName);
165    if (exists(file)) {
166        if (is_directory(file)) {
167            return;
168        }
169        size_t fileSize = file_size(file);
170        boost::iostreams::mapped_file_source mappedFile;
171        if (fileSize > 0) {
172            mappedFile.open(fileName);
173            char * fileBuffer = const_cast<char *>(mappedFile.data());
174            f(fileBuffer, fileSize);
175            mappedFile.close();
176        }
177    } else {
178        std::cerr << "Error: cannot open " << fileName << " for processing. Skipped.\n";
179    }
180}
181
182int main(int argc, char *argv[]) {
183    cl::ParseCommandLineOptions(argc, argv);
184    auto f = generateAlgorithm();
185    for (const auto & inputFile : inputFiles) {
186        run(f, inputFile);
187    }
188    return 0;
189}
Note: See TracBrowser for help on using the repository browser.