Ignore:
Timestamp:
Jan 3, 2017, 3:59:33 PM (18 months ago)
Author:
nmedfort
Message:

Work on bracket matching problem

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/array-test.cpp

    r5238 r5245  
    3333#include <pablo/pablo_compiler.h>
    3434#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>
    3539
    36 #include <kernels/stdout_kernel.h>
    37 
    38 #include <llvm/Support/raw_os_ostream.h>
    39 
    40 #include <pthread.h>
     40static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
    4141
    4242using namespace pablo;
     
    4444using namespace parabix;
    4545
    46 //
    47 //
    48 
    4946void generate(PabloKernel * kernel, const unsigned size) {
    5047
    5148    PabloBuilder pb(kernel->getEntryBlock());
    5249
    53     Var * input = kernel->addInput("input", kernel->getStreamSetTy(size));
    54     Var * output = kernel->addOutput("output", kernel->getStreamSetTy(size));
    55     Var * index = pb.createVar("idx", pb.getInteger(0));
     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
    5681
    5782    Integer * one = pb.getInteger(1);
    5883
     84    Var * index = pb.createVar("i", one);
     85
    5986    PabloBuilder body = PabloBuilder::Create(pb);
    6087
    61     pb.createWhile(pb.createLessThan(index, pb.getInteger(size)), body);
     88    pb.createWhile(pending_lparen, body);
    6289
    63         PabloAST * adv = body.createAdvance(body.createExtract(input, index), one, "adv");
    64         body.createAssign(body.createExtract(output, index), adv);
    65 
     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));
    6697        body.createAssign(index, body.createAdd(index, one));
    6798
    6899
     100    pb.print(errs());
    69101
    70102}
    71103
    72 Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count = 10) {
     104Function * pipeline(IDISA::IDISA_Builder * iBuilder, const unsigned count) {
    73105
    74     PabloKernel main(iBuilder, "at");
    75     generate(&main, count);
     106    Type * byteStreamTy = iBuilder->getStreamSetTy(1, 8);
    76107
    77     SingleBlockBuffer input(iBuilder, iBuilder->getStreamSetTy(count));
     108    ExternalFileBuffer ByteStream(iBuilder, byteStreamTy);
     109    SingleBlockBuffer BasisBits(iBuilder, iBuilder->getStreamSetTy(8, 1));
     110    SingleBlockBuffer matches(iBuilder, iBuilder->getStreamSetTy(count, 1));
    78111
    79     SingleBlockBuffer output(iBuilder, iBuilder->getStreamSetTy(count));
     112    S2PKernel  s2pk(iBuilder);
     113    s2pk.generateKernel({&ByteStream}, {&BasisBits});
    80114
    81     main.generateKernel({&input}, {&output});
     115    PabloKernel bm(iBuilder, "MatchParens");
     116    generate(&bm, count);
     117
     118    bm.generateKernel({&BasisBits}, {&matches});
    82119
    83120    Module * const mod = iBuilder->getModule();
    84121
    85     Function * const f = cast<Function>(mod->getOrInsertFunction("main", iBuilder->getVoidTy(), nullptr));
    86     f->setCallingConv(CallingConv::C);
     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();
    87125
    88     iBuilder->SetInsertPoint(BasicBlock::Create(mod->getContext(), "entry", f, 0));
    89     input.allocateBuffer();
    90     output.allocateBuffer();
    91     generatePipelineLoop(iBuilder, {&main});
     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});
    92138    iBuilder->CreateRetVoid();
    93139
    94     return f;
     140    return main;
    95141}
    96142
    97 typedef void (*AtFunctionType)();
     143typedef void (*MatchParens)(char * byteStream, size_t fileSize);
    98144
    99 void * arrayTest(void *) {
     145MatchParens generateAlgorithm() {
    100146    LLVMContext ctx;
    101     Module * M = new Module("at", ctx);
     147    Module * M = new Module("mp", ctx);
    102148    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
    103149
    104     llvm::Function * main_IR = pipeline(idb);
     150    llvm::Function * f = pipeline(idb, 10);
    105151
    106152    verifyModule(*M, &dbgs());
     
    112158    delete idb;
    113159
    114     return wcEngine->getPointerToFunction(main_IR);
     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    }
    115180}
    116181
    117182int main(int argc, char *argv[]) {
    118 
    119183    cl::ParseCommandLineOptions(argc, argv);
    120 
    121     pthread_t t1, t2;
    122 
    123     pthread_create(&t1, nullptr, arrayTest, nullptr);
    124 
    125     pthread_create(&t2, nullptr, arrayTest, nullptr);
    126 
    127     void * r1, * r2;
    128 
    129     pthread_join(t1, &r1);
    130 
    131     pthread_join(t2, &r2);
    132 
     184    auto f = generateAlgorithm();
     185    for (const auto & inputFile : inputFiles) {
     186        run(f, inputFile);
     187    }
    133188    return 0;
    134189}
Note: See TracChangeset for help on using the changeset viewer.