Ignore:
Timestamp:
Mar 29, 2017, 2:29:52 PM (2 years ago)
Author:
nmedfort
Message:

Support for stdin. Needs more testing.

File:
1 edited

Legend:

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

    r5373 r5377  
    3131#include <util/papi_helper.hpp>
    3232#endif
     33#include <poll.h>
     34
     35inline bool hasInputFromStdIn() {
     36    pollfd stdin_poll;
     37    stdin_poll.fd = STDIN_FILENO;
     38    stdin_poll.events = POLLIN | POLLRDBAND | POLLRDNORM | POLLPRI;
     39    return poll(&stdin_poll, 1, 0) == 1;
     40}
    3341
    3442using namespace llvm;
     
    200208
    201209    while (fileIdx < allFiles.size()){
    202         grepEngine->doGrep(allFiles[fileIdx], fileIdx, CountOnly, total_CountOnly, UTF_16);
     210        grepEngine->doGrep(allFiles[fileIdx], fileIdx, CountOnly, total_CountOnly);
    203211       
    204212        count_mutex.lock();
     
    384392        return 0;   // icgrep is called again, so we need to end this process.
    385393    }
    386    
     394
     395    const bool usingStdIn = hasInputFromStdIn();
     396
    387397    GrepEngine grepEngine;
    388     if(MultiGrepKernels){
     398    if (MultiGrepKernels) {
    389399        grepEngine.multiGrepCodeGen(module_name, RELists, CountOnly, UTF_16);
    390     }
    391     else{
    392         grepEngine.grepCodeGen(module_name, re_ast, CountOnly, UTF_16);
    393     }
    394 
    395     allFiles = getFullFileList(inputFiles);
    396    
    397     if (FileNamesOnly && NonMatchingFileNamesOnly) {
    398         // Strange request: print names of all matching files and all non-matching files: i.e., all of them.
    399         // (Although GNU grep prints nothing.)
    400         for (auto & f : allFiles) {
    401             if (boost::filesystem::exists(f)) {
    402                 std::cout << f << "\n";
    403             } else {
    404                 std::cerr << "Error: cannot open " << f << " for processing. Skipped.\n";
    405             }
    406         }
    407         exit(0);
    408     }
    409     if (FileNamesOnly) {
    410         llvm::report_fatal_error("Sorry, -l/-files-with-matches not yet supported\n.");
    411     }
    412     if (NonMatchingFileNamesOnly) {
    413         llvm::report_fatal_error("Sorry, -L/-files-without-match not yet supported\n.");
    414     }
    415    
    416     initResult(allFiles);
    417     for (unsigned i=0; i < allFiles.size(); ++i){
     400    } else {
     401        grepEngine.grepCodeGen(module_name, re_ast, CountOnly, UTF_16, GrepType::Normal, usingStdIn);
     402    }
     403
     404    if (usingStdIn)  {
     405
     406        allFiles = { "stdin" };
     407        initFileResult(allFiles);
    418408        total_CountOnly.push_back(0);
    419     }
    420 
    421     if (Threads <= 1) {
    422 
    423         #ifdef PRINT_TIMING_INFORMATION
    424         // PAPI_RES_STL, PAPI_STL_CCY, PAPI_FUL_CCY, PAPI_MEM_WCY
    425         // PAPI_RES_STL, PAPI_BR_MSP, PAPI_LST_INS, PAPI_L1_TCM
    426         papi::PapiCounter<4> papiCounters({PAPI_RES_STL, PAPI_STL_CCY, PAPI_FUL_CCY, PAPI_MEM_WCY});
    427         #endif
    428         for (unsigned i = 0; i != allFiles.size(); ++i) {
     409        grepEngine.doGrep(0, CountOnly, total_CountOnly);
     410
     411    } else {
     412
     413        allFiles = getFullFileList(inputFiles);
     414
     415        if (FileNamesOnly && NonMatchingFileNamesOnly) {
     416            // Strange request: print names of all matching files and all non-matching files: i.e., all of them.
     417            // (Although GNU grep prints nothing.)
     418            for (auto & f : allFiles) {
     419                if (boost::filesystem::exists(f)) {
     420                    std::cout << f << "\n";
     421                } else {
     422                    std::cerr << "Error: cannot open " << f << " for processing. Skipped.\n";
     423                }
     424            }
     425            exit(0);
     426        }
     427
     428        if (FileNamesOnly) {
     429            llvm::report_fatal_error("Sorry, -l/-files-with-matches not yet supported\n.");
     430        }
     431        if (NonMatchingFileNamesOnly) {
     432            llvm::report_fatal_error("Sorry, -L/-files-without-match not yet supported\n.");
     433        }
     434        initFileResult(allFiles);
     435
     436        for (unsigned i=0; i < allFiles.size(); ++i){
     437            total_CountOnly.push_back(0);
     438        }
     439
     440        if (Threads <= 1) {
     441
    429442            #ifdef PRINT_TIMING_INFORMATION
    430             papiCounters.start();
    431             const timestamp_t execution_start = read_cycle_counter();
     443            // PAPI_RES_STL, PAPI_STL_CCY, PAPI_FUL_CCY, PAPI_MEM_WCY
     444            // PAPI_RES_STL, PAPI_BR_MSP, PAPI_LST_INS, PAPI_L1_TCM
     445            papi::PapiCounter<4> papiCounters({PAPI_RES_STL, PAPI_STL_CCY, PAPI_FUL_CCY, PAPI_MEM_WCY});
    432446            #endif
    433             grepEngine.doGrep(allFiles[i], i, CountOnly, total_CountOnly, UTF_16);
    434             #ifdef PRINT_TIMING_INFORMATION
    435             const timestamp_t execution_end = read_cycle_counter();
    436             papiCounters.stop();
    437             std::cerr << "EXECUTION TIME: " << allFiles[i] << ":" << "CYCLES|" << (execution_end - execution_start) << papiCounters << std::endl;
    438             #endif
    439         }       
    440     } else if (Threads > 1) {
    441         const unsigned numOfThreads = Threads; // <- convert the command line value into an integer to allow stack allocation
    442         pthread_t threads[numOfThreads];
    443 
    444         for(unsigned long i = 0; i < numOfThreads; ++i){
    445             const int rc = pthread_create(&threads[i], nullptr, DoGrep, (void *)&grepEngine);
    446             if (rc) {
    447                 llvm::report_fatal_error("Failed to create thread: code " + std::to_string(rc));
    448             }
    449         }
    450 
    451         for(unsigned i = 0; i < numOfThreads; ++i) {
    452             void * status = nullptr;
    453             const int rc = pthread_join(threads[i], &status);
    454             if (rc) {
    455                 llvm::report_fatal_error("Failed to join thread: code " + std::to_string(rc));
    456             }
    457         }
    458     }
     447            for (unsigned i = 0; i != allFiles.size(); ++i) {
     448                #ifdef PRINT_TIMING_INFORMATION
     449                papiCounters.start();
     450                const timestamp_t execution_start = read_cycle_counter();
     451                #endif
     452                grepEngine.doGrep(allFiles[i], i, CountOnly, total_CountOnly);
     453                #ifdef PRINT_TIMING_INFORMATION
     454                const timestamp_t execution_end = read_cycle_counter();
     455                papiCounters.stop();
     456                std::cerr << "EXECUTION TIME: " << allFiles[i] << ":" << "CYCLES|" << (execution_end - execution_start) << papiCounters << std::endl;
     457                #endif
     458            }
     459        } else if (Threads > 1) {
     460            const unsigned numOfThreads = Threads; // <- convert the command line value into an integer to allow stack allocation
     461            pthread_t threads[numOfThreads];
     462
     463            for(unsigned long i = 0; i < numOfThreads; ++i){
     464                const int rc = pthread_create(&threads[i], nullptr, DoGrep, (void *)&grepEngine);
     465                if (rc) {
     466                    llvm::report_fatal_error("Failed to create thread: code " + std::to_string(rc));
     467                }
     468            }
     469
     470            for(unsigned i = 0; i < numOfThreads; ++i) {
     471                void * status = nullptr;
     472                const int rc = pthread_join(threads[i], &status);
     473                if (rc) {
     474                    llvm::report_fatal_error("Failed to join thread: code " + std::to_string(rc));
     475                }
     476            }
     477        }
     478
     479    }
     480   
     481
    459482   
    460483    PrintResult(CountOnly, total_CountOnly);
Note: See TracChangeset for help on using the changeset viewer.