Ignore:
Timestamp:
Oct 21, 2017, 9:44:16 AM (2 years ago)
Author:
cameron
Message:

Refactoring, clean-up, set initial threads to 2

File:
1 edited

Legend:

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

    r5700 r5703  
    4242using namespace parabix;
    4343using namespace llvm;
    44 static cl::opt<int> Threads("t", cl::desc("Total number of threads."), cl::init(1));
    45 
     44static cl::opt<int> Threads("t", cl::desc("Total number of threads."), cl::init(2));
    4645
    4746namespace grep {
     
    7170}
    7271   
    73 void GrepEngine::run() {
     72bool GrepEngine::searchAllFiles() {
    7473   
    7574    if (Threads <= 1) {
    7675        for (unsigned i = 0; i != inputFiles.size(); ++i) {
    7776            size_t grepResult = doGrep(inputFiles[i], i);
    78             if (grepResult > 0) grepMatchFound = true;
     77            if (grepResult > 0) {
     78                grepMatchFound = true;
     79                if (QuietMode) break;
     80            }
    7981        }
    8082    } else if (Threads > 1) {
     
    9698        }
    9799    }
     100    return grepMatchFound;
    98101}
    99102       
     
    102105//  input.  However, if the final line is not terminated, a new line is appended.
    103106
    104 
    105107class EmitMatch : public MatchAccumulator {
     108    friend class EmitMatchesEngine;
    106109public:
    107110    EmitMatch(std::string linePrefix, std::stringstream * strm) : mLinePrefix(linePrefix), mLineCount(0), mPrevious_line_end(nullptr), mResultStr(strm) {}
    108111    void accumulate_match(const size_t lineNum, char * line_start, char * line_end) override;
    109112    void finalize_match(char * buffer_end) override;
     113protected:
    110114    std::string mLinePrefix;
    111115    size_t mLineCount;
    112116    char * mPrevious_line_end;
    113117    std::stringstream* mResultStr;
    114    
    115118};
    116 
    117119
    118120void EmitMatch::accumulate_match (const size_t lineNum, char * line_start, char * line_end) {
     
    176178}
    177179
    178    
    179 int32_t openFile(const std::string & fileName, std::stringstream & msgstrm) {
     180// Open a file and return its file desciptor.
     181int32_t GrepEngine::openFile(const std::string & fileName, std::stringstream & msgstrm) {
    180182    if (fileName == "-") {
    181183        return STDIN_FILENO;
     
    218220}
    219221
    220 uint64_t GrepEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
     222uint64_t EmitMatchesEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
    221223    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
    222224   
     
    234236}
    235237
    236 uint64_t CountOnlyGrepEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
     238uint64_t CountOnlyEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
    237239    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
    238    
    239240    if (fileDescriptor == -1) return 0;
    240241   
     
    250251}
    251252
    252 uint64_t MatchOnlyGrepEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
     253uint64_t MatchOnlyEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) {
    253254    int32_t fileDescriptor = openFile(fileName, mResultStrs[fileIdx]);
    254    
    255255    if (fileDescriptor == -1) return 0;
    256256   
     
    272272}
    273273
    274 void GrepEngine::initFileResult(std::vector<std::string> filenames){
    275     grepMatchFound = false;
     274void GrepEngine::initFileResult(std::vector<std::string> & filenames){
    276275    const int n = filenames.size();
    277276    mResultStrs.resize(n);
     
    279278}
    280279
    281    
    282 void GrepEngine::PrintResults(){
    283    
    284     for (unsigned i = 0; i < inputFiles.size(); ++i){
    285         std::cout << mResultStrs[i].str();
    286     }
    287     exit(grepMatchFound ? MatchFoundExitCode : MatchNotFoundExitCode);
    288 }
    289 
    290    
    291    
    292    
    293280std::pair<StreamSetBuffer *, StreamSetBuffer *> grepPipeline(Driver * grepDriver, std::vector<re::RE *> & REs, StreamSetBuffer * ByteStream) {
    294281    auto & idb = grepDriver->getBuilder();
     
    364351}
    365352
    366     void GrepEngine::grepCodeGen(std::vector<re::RE *> REs) {
    367        
    368         assert (mGrepDriver == nullptr);
    369         mGrepDriver = new ParabixDriver("engine");
    370         auto & idb = mGrepDriver->getBuilder();
    371         Module * M = idb->getModule();
    372        
    373         const unsigned segmentSize = codegen::SegmentSize;
    374         const unsigned encodingBits = 8;
    375        
    376         Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getInt64Ty(), idb->getInt32Ty(), idb->getIntAddrTy(), nullptr));
    377         mainFunc->setCallingConv(CallingConv::C);
    378         idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
    379         auto args = mainFunc->arg_begin();
    380        
    381         Value * const fileDescriptor = &*(args++);
    382         fileDescriptor->setName("fileDescriptor");
    383         Value * match_accumulator = &*(args++);
    384         match_accumulator->setName("match_accumulator");
    385        
    386         StreamSetBuffer * ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, encodingBits)));
    387         kernel::Kernel * sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::FDSourceKernel>(idb, segmentSize));
    388         sourceK->setInitialArguments({fileDescriptor});
    389         mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
    390        
    391         StreamSetBuffer * LineBreakStream;
    392         StreamSetBuffer * Matches;
    393         std::tie(LineBreakStream, Matches) = grepPipeline(mGrepDriver, REs, ByteStream);
    394        
    395         kernel::Kernel * scanMatchK = mGrepDriver->addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb));
    396         scanMatchK->setInitialArguments({match_accumulator});
    397         mGrepDriver->makeKernelCall(scanMatchK, {Matches, LineBreakStream, ByteStream}, {});
    398         mGrepDriver->LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
    399         mGrepDriver->LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
    400        
    401        
    402         mGrepDriver->generatePipelineIR();
    403         mGrepDriver->deallocateBuffers();
    404        
    405         idb->CreateRet(idb->getInt64(0));
    406         mGrepDriver->finalizeObject();
    407     }
    408    
    409     void CountOnlyGrepEngine::grepCodeGen(std::vector<re::RE *> REs) {
    410        
    411         assert (mGrepDriver == nullptr);
    412         mGrepDriver = new ParabixDriver("engine");
    413         auto & idb = mGrepDriver->getBuilder();
    414         Module * M = idb->getModule();
    415        
    416         const unsigned segmentSize = codegen::SegmentSize;
    417         const unsigned encodingBits = 8;
    418        
    419         Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getInt64Ty(), idb->getInt32Ty(), nullptr));
    420         mainFunc->setCallingConv(CallingConv::C);
    421         idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
    422         auto args = mainFunc->arg_begin();
    423        
    424         Value * const fileDescriptor = &*(args++);
    425         fileDescriptor->setName("fileDescriptor");
    426        
    427         StreamSetBuffer * ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, encodingBits)));
    428         kernel::Kernel * sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::FDSourceKernel>(idb, segmentSize));
    429         sourceK->setInitialArguments({fileDescriptor});
    430         mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
    431        
    432         StreamSetBuffer * LineBreakStream;
    433         StreamSetBuffer * Matches;
    434         std::tie(LineBreakStream, Matches) = grepPipeline(mGrepDriver, REs, ByteStream);
    435        
    436         kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance(make_unique<kernel::PopcountKernel>(idb));
    437         mGrepDriver->makeKernelCall(matchCountK, {Matches}, {});
    438         mGrepDriver->generatePipelineIR();
    439         idb->setKernel(matchCountK);
    440         Value * matchedLineCount = idb->getAccumulator("countResult");
    441         matchedLineCount = idb->CreateZExt(matchedLineCount, idb->getInt64Ty());
    442         mGrepDriver->deallocateBuffers();
    443         idb->CreateRet(matchedLineCount);
    444         mGrepDriver->finalizeObject();
    445     }
    446    
    447 
     353void EmitMatchesEngine::grepCodeGen(std::vector<re::RE *> REs) {
     354    assert (mGrepDriver == nullptr);
     355    mGrepDriver = new ParabixDriver("engine");
     356    auto & idb = mGrepDriver->getBuilder();
     357    Module * M = idb->getModule();
     358   
     359    const unsigned segmentSize = codegen::SegmentSize;
     360    const unsigned encodingBits = 8;
     361   
     362    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getInt64Ty(), idb->getInt32Ty(), idb->getIntAddrTy(), nullptr));
     363    mainFunc->setCallingConv(CallingConv::C);
     364    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
     365    auto args = mainFunc->arg_begin();
     366   
     367    Value * const fileDescriptor = &*(args++);
     368    fileDescriptor->setName("fileDescriptor");
     369    Value * match_accumulator = &*(args++);
     370    match_accumulator->setName("match_accumulator");
     371   
     372    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, encodingBits)));
     373    kernel::Kernel * sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::FDSourceKernel>(idb, segmentSize));
     374    sourceK->setInitialArguments({fileDescriptor});
     375    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
     376   
     377    StreamSetBuffer * LineBreakStream;
     378    StreamSetBuffer * Matches;
     379    std::tie(LineBreakStream, Matches) = grepPipeline(mGrepDriver, REs, ByteStream);
     380   
     381    kernel::Kernel * scanMatchK = mGrepDriver->addKernelInstance(make_unique<kernel::ScanMatchKernel>(idb));
     382    scanMatchK->setInitialArguments({match_accumulator});
     383    mGrepDriver->makeKernelCall(scanMatchK, {Matches, LineBreakStream, ByteStream}, {});
     384    mGrepDriver->LinkFunction(*scanMatchK, "accumulate_match_wrapper", &accumulate_match_wrapper);
     385    mGrepDriver->LinkFunction(*scanMatchK, "finalize_match_wrapper", &finalize_match_wrapper);
     386   
     387    mGrepDriver->generatePipelineIR();
     388    mGrepDriver->deallocateBuffers();
     389    idb->CreateRet(idb->getInt64(0));
     390    mGrepDriver->finalizeObject();
     391}
     392
     393void GrepEngine::grepCodeGen(std::vector<re::RE *> REs) {
     394   
     395    assert (mGrepDriver == nullptr);
     396    mGrepDriver = new ParabixDriver("engine");
     397    auto & idb = mGrepDriver->getBuilder();
     398    Module * M = idb->getModule();
     399   
     400    const unsigned segmentSize = codegen::SegmentSize;
     401    const unsigned encodingBits = 8;
     402   
     403    Function * mainFunc = cast<Function>(M->getOrInsertFunction("Main", idb->getInt64Ty(), idb->getInt32Ty(), nullptr));
     404    mainFunc->setCallingConv(CallingConv::C);
     405    idb->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", mainFunc, 0));
     406    auto args = mainFunc->arg_begin();
     407   
     408    Value * const fileDescriptor = &*(args++);
     409    fileDescriptor->setName("fileDescriptor");
     410   
     411    StreamSetBuffer * ByteStream = mGrepDriver->addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(1, encodingBits)));
     412    kernel::Kernel * sourceK = mGrepDriver->addKernelInstance(make_unique<kernel::FDSourceKernel>(idb, segmentSize));
     413    sourceK->setInitialArguments({fileDescriptor});
     414    mGrepDriver->makeKernelCall(sourceK, {}, {ByteStream});
     415   
     416    StreamSetBuffer * LineBreakStream;
     417    StreamSetBuffer * Matches;
     418    std::tie(LineBreakStream, Matches) = grepPipeline(mGrepDriver, REs, ByteStream);
     419   
     420    kernel::Kernel * matchCountK = mGrepDriver->addKernelInstance(make_unique<kernel::PopcountKernel>(idb));
     421    mGrepDriver->makeKernelCall(matchCountK, {Matches}, {});
     422    mGrepDriver->generatePipelineIR();
     423    idb->setKernel(matchCountK);
     424    Value * matchedLineCount = idb->getAccumulator("countResult");
     425    matchedLineCount = idb->CreateZExt(matchedLineCount, idb->getInt64Ty());
     426    mGrepDriver->deallocateBuffers();
     427    idb->CreateRet(matchedLineCount);
     428    mGrepDriver->finalizeObject();
     429}
     430
     431void GrepEngine::writeMatches(){
     432    for (unsigned i = 0; i < inputFiles.size(); ++i){
     433        std::cout << mResultStrs[i].str();
     434    }
     435}
     436
     437GrepEngine::GrepEngine() :
     438    mGrepDriver(nullptr),
     439    grepMatchFound(false),
     440    fileCount(0) {}
     441   
    448442GrepEngine::~GrepEngine() {
    449443    delete mGrepDriver;
    450444}
    451445
    452 }
     446EmitMatchesEngine::EmitMatchesEngine() : GrepEngine()
     447    {mFileSuffix = InitialTabFlag ? "\t:" : ":";}
     448   
     449CountOnlyEngine::CountOnlyEngine() :
     450    GrepEngine() {mFileSuffix = ":";}
     451
     452MatchOnlyEngine::MatchOnlyEngine(bool showFilesWithoutMatch) :
     453    GrepEngine(), mRequiredCount(showFilesWithoutMatch)
     454    {mFileSuffix = NullFlag ? std::string("\0", 1) : "\n";}
     455}
Note: See TracChangeset for help on using the changeset viewer.