Changeset 5413


Ignore:
Timestamp:
Apr 18, 2017, 10:41:50 PM (2 years ago)
Author:
cameron
Message:

Factor out CountOnly? and InvertMatches? as separate kernels

Location:
icGREP/icgrep-devel/icgrep
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5411 r5413  
    526526    pxDriver.addKernelCall(linebreakK, {BasisBits}, {LineBreakStream});
    527527   
    528     kernel::ICgrepKernelBuilder icgrepK(iBuilder, re_ast, CountOnly);
     528    StreamSetBuffer * MatchResults = nullptr;
     529#ifdef CUDA_ENABLED
     530    if (codegen::NVPTX){
     531        MatchResults = pxDriver.addExternalBuffer(make_unique<ExternalFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), addrSpace), outputStream);
     532
     533    }
     534    else {
     535#endif
     536    MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     537#ifdef CUDA_ENABLED
     538    }
     539#endif
     540    kernel::ICgrepKernelBuilder icgrepK(iBuilder, re_ast);
     541    pxDriver.addKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
     542   
     543    kernel::InvertMatchesKernel invertK(iBuilder);
     544    if (AlgorithmOptionIsSet(re::InvertMatches)) {
     545        StreamSetBuffer * OriginalMatches = MatchResults;
     546        MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     547        pxDriver.addKernelCall(invertK, {OriginalMatches, LineBreakStream}, {MatchResults});
     548    }
    529549
    530550    if (CountOnly) {
    531        
    532         pxDriver.addKernelCall(icgrepK, {BasisBits, LineBreakStream}, {});
    533 
     551        kernel::PopcountKernel popcountK(iBuilder);
     552        pxDriver.addKernelCall(popcountK, {MatchResults}, {});
    534553        pxDriver.generatePipelineIR();
    535 
    536         iBuilder->CreateRet(icgrepK.createGetAccumulatorCall("matchedLineCount"));
     554        iBuilder->CreateRet(popcountK.createGetAccumulatorCall("countResult"));
    537555
    538556        pxDriver.linkAndFinalize();
     
    542560        #ifdef CUDA_ENABLED
    543561        if (codegen::NVPTX){
    544             ExternalFileBuffer * MatchResults = pxDriver.addExternalBuffer(make_unique<ExternalFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), addrSpace), outputStream);
    545 
    546             pxDriver.addKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
    547562
    548563            pxDriver.generatePipelineIR();
     
    555570
    556571        if (CPU_Only) {
    557 
    558             StreamSetBuffer * MatchResults = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
    559 
    560             pxDriver.addKernelCall(icgrepK, {BasisBits, LineBreakStream}, {MatchResults});
    561572
    562573            kernel::ScanMatchKernel scanMatchK(iBuilder, grepType, encodingBits);
     
    674685    kernel::StreamsMerge streamsMergeK(iBuilder, 1, REs.size());
    675686    pxDriver.addKernelCall(streamsMergeK, MatchResultsBufs, {MergedResults});
    676 
     687   
     688    kernel::InvertMatchesKernel invertK(iBuilder);
     689    if (AlgorithmOptionIsSet(re::InvertMatches)) {
     690        StreamSetBuffer * OriginalMatches = MergedResults;
     691        MergedResults = pxDriver.addBuffer(make_unique<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments));
     692        pxDriver.addKernelCall(invertK, {OriginalMatches, LineBreakStream}, {MergedResults});
     693    }
    677694    if (CountOnly) {
    678695        kernel::MatchCount matchCountK(iBuilder);
  • icGREP/icgrep-devel/icgrep/kernels/grep_kernel.cpp

    r5410 r5413  
    99#include <re/re_toolchain.h>
    1010#include <pablo/pablo_toolchain.h>
     11#include <IR_Gen/idisa_builder.h>  // for IDISA_Builder
     12#include <pablo/builder.hpp>  // for PabloBuilder
     13#include <pablo/pe_count.h>
    1114
    1215using namespace kernel;
     
    2629}
    2730
    28 inline std::string makeSignature(RE * const re_ast, const bool CountOnly) {
     31inline std::string makeSignature(RE * const re_ast) {
    2932    std::string signature = Printer_RE::PrintRE(re_ast);
    30     if (CountOnly) {
    31         signature += "-c";
    32     }
    33     if (AlgorithmOptionIsSet(InvertMatches)) {
    34         signature += "-v";
    35     }
    3633    return signature;
    3734}
    3835
    39 ICgrepKernelBuilder::ICgrepKernelBuilder (IDISA::IDISA_Builder * const iBuilder, RE * const re_ast, const bool CountOnly)
     36ICgrepKernelBuilder::ICgrepKernelBuilder (IDISA::IDISA_Builder * const iBuilder, RE * const re_ast)
    4037: PabloKernel(iBuilder, "",
    4138              {Binding{iBuilder->getStreamSetTy(8), "basis"}, Binding{iBuilder->getStreamSetTy(1, 1), "linebreak"}},
    42               CountOnly ? std::vector<Binding>{} : std::vector<Binding>{Binding{iBuilder->getStreamSetTy(1, 1), "matches"}},
     39              {Binding{iBuilder->getStreamSetTy(1, 1), "matches"}},
    4340              {},
    44               CountOnly ? std::vector<Binding>{Binding{iBuilder->getSizeTy(), "matchedLineCount"}} : std::vector<Binding>{})
    45 , mCountOnly(CountOnly)
     41              {})
    4642, mRE(re_ast)
    47 , mSignature(makeSignature(re_ast, CountOnly)) {
     43, mSignature(makeSignature(re_ast)) {
    4844    setName("Parabix:" + sha1sum(mSignature));
    4945}
     
    5450
    5551void ICgrepKernelBuilder::prepareKernel() {
    56     re2pablo_compiler(this, regular_expression_passes(mRE), mCountOnly);
     52    re2pablo_compiler(this, regular_expression_passes(mRE));
    5753    pablo_function_passes(this);
    5854    PabloKernel::prepareKernel();
    5955}
     56
     57void InvertMatchesKernel::generateDoBlockMethod() {
     58    Value * input = loadInputStreamBlock("matchedLines", iBuilder->getInt32(0));
     59    Value * lbs = loadInputStreamBlock("lineBreaks", iBuilder->getInt32(0));
     60    Value * inverted = iBuilder->CreateXor(input, lbs);
     61    storeOutputStreamBlock("nonMatches", iBuilder->getInt32(0), inverted);
     62}
     63
     64InvertMatchesKernel::InvertMatchesKernel(IDISA::IDISA_Builder * builder)
     65: BlockOrientedKernel(builder, "Invert", {Binding{builder->getStreamSetTy(1, 1), "matchedLines"}, Binding{builder->getStreamSetTy(1, 1), "lineBreaks"}}, {Binding{builder->getStreamSetTy(1, 1), "nonMatches"}}, {}, {}, {}) {
     66    setNoTerminateAttribute(true);
     67   
     68}
     69
     70
     71PopcountKernel::PopcountKernel (IDISA::IDISA_Builder * const iBuilder)
     72: PabloKernel(iBuilder, "Popcount",
     73              {Binding{iBuilder->getStreamSetTy(1), "toCount"}},
     74              {},
     75              {},
     76              {Binding{iBuilder->getSizeTy(), "countResult"}}) {
     77   
     78    auto pb = this->getEntryBlock();
     79    const auto toCount = pb->createExtract(getInputStreamVar("toCount"), pb->getInteger(0));
     80    pablo::Var * countResult = getOutputScalarVar("countResult");
     81    pb->createAssign(countResult, pb->createCount(toCount));
     82}
  • icGREP/icgrep-devel/icgrep/kernels/grep_kernel.h

    r5408 r5413  
    1414class ICgrepKernelBuilder: public pablo::PabloKernel {
    1515public:
    16     ICgrepKernelBuilder(IDISA::IDISA_Builder * const iBuilder, re::RE * const re_ast, const bool CountOnly = false);
     16    ICgrepKernelBuilder(IDISA::IDISA_Builder * const iBuilder, re::RE * const re_ast);
    1717   
    1818    std::string generateKernelSignature(std::string moduleId) override;
     
    2121
    2222private:
    23     const bool      mCountOnly;
    2423    re::RE * const  mRE;
    2524    std::string     mSignature;
    2625};
    2726
     27class InvertMatchesKernel : public BlockOrientedKernel {
     28public:
     29    InvertMatchesKernel(IDISA::IDISA_Builder * builder);
     30private:
     31    void generateDoBlockMethod() override;
     32};
     33
     34
     35class PopcountKernel : public pablo::PabloKernel {
     36public:
     37    PopcountKernel(IDISA::IDISA_Builder * builder);
     38};
     39
    2840}
    2941#endif
  • icGREP/icgrep-devel/icgrep/re/re_compiler.cpp

    r5404 r5413  
    198198}
    199199
    200 void RE_Compiler::finalizeMatchResult(MarkerType match_result, bool InvertMatches) {
     200void RE_Compiler::finalizeMatchResult(MarkerType match_result) {
    201201    PabloAST * match_follow = mPB.createMatchStar(markerVar(match_result), mAny);
    202     if (InvertMatches) {
    203         match_follow = mPB.createNot(match_follow);
    204     }
    205     PabloAST * matches = mPB.createAnd(match_follow, mLineBreak, "matches");
    206     if (mCountOnly) {
    207         Var * const output = mKernel->getOutputScalarVar("matchedLineCount");
    208         PabloBuilder nestedCount = PabloBuilder::Create(mPB);
    209         mPB.createIf(matches, nestedCount);
    210         nestedCount.createAssign(output, nestedCount.createCount(matches));
    211     } else {
    212         Var * const output = mKernel->getOutputStreamVar("matches");
    213         mPB.createAssign(mPB.createExtract(output, mPB.getInteger(0)), matches);
    214     }
     202    PabloAST * matches = mPB.createAnd(match_follow, mLineBreak, "matchedLine3s");
     203    Var * const output = mKernel->getOutputStreamVar("matches");
     204    mPB.createAssign(mPB.createExtract(output, mPB.getInteger(0)), matches);
    215205}
    216206
     
    625615   
    626616
    627 RE_Compiler::RE_Compiler(PabloKernel * kernel, cc::CC_Compiler & ccCompiler, bool CountOnly)
     617RE_Compiler::RE_Compiler(PabloKernel * kernel, cc::CC_Compiler & ccCompiler)
    628618: mKernel(kernel)
    629 , mCountOnly(CountOnly)
    630619, mCCCompiler(ccCompiler)
    631620, mLineBreak(nullptr)
  • icGREP/icgrep-devel/icgrep/re/re_compiler.h

    r5357 r5413  
    6060public:
    6161
    62     RE_Compiler(pablo::PabloKernel * kernel, cc::CC_Compiler & ccCompiler, bool CountOnly = false);
     62    RE_Compiler(pablo::PabloKernel * kernel, cc::CC_Compiler & ccCompiler);
    6363    void initializeRequiredStreams(const unsigned encodingBits, pablo::Var * linebreak);
    6464    void compileUnicodeNames(RE *& re);
    65     void finalizeMatchResult(MarkerType match_result, bool InvertMatches = false);
     65    void finalizeMatchResult(MarkerType match_result);
    6666    MarkerType compile(RE * re) {
    6767        return compile(re, mPB);
  • icGREP/icgrep-devel/icgrep/re/re_toolchain.cpp

    r5357 r5413  
    8585}
    8686   
    87 void re2pablo_compiler(PabloKernel * kernel, RE * re_ast, const bool CountOnly) {
     87void re2pablo_compiler(PabloKernel * kernel, RE * re_ast) {
    8888    Var * const basis = kernel->getInputStreamVar("basis");
    8989    Var * const linebreak = kernel->getInputStreamVar("linebreak");
    9090    cc::CC_Compiler cc_compiler(kernel, basis);
    91     re::RE_Compiler re_compiler(kernel, cc_compiler, CountOnly);
     91    re::RE_Compiler re_compiler(kernel, cc_compiler);
    9292    re_compiler.initializeRequiredStreams(basis->getType()->getArrayNumElements(), linebreak);
    9393    re_compiler.compileUnicodeNames(re_ast);
    94     re_compiler.finalizeMatchResult(re_compiler.compile(re_ast), AlgorithmOptions.isSet(InvertMatches));
     94    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
    9595}
    9696
  • icGREP/icgrep-devel/icgrep/re/re_toolchain.h

    r5310 r5413  
    3131RE * regular_expression_passes(RE * re_ast);
    3232
    33 void re2pablo_compiler(pablo::PabloKernel * kernel, RE * re_ast, const bool CountOnly = false);
     33void re2pablo_compiler(pablo::PabloKernel * kernel, RE * re_ast);
    3434   
    3535}
Note: See TracChangeset for help on using the changeset viewer.