Changeset 5244


Ignore:
Timestamp:
Dec 30, 2016, 10:17:29 AM (22 months ago)
Author:
lindanl
Message:

editd:simple multithreading with separate compilation in each thread.

File:
1 edited

Legend:

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

    r5240 r5244  
    4343#include <boost/iostreams/device/mapped_file.hpp>
    4444#include <fcntl.h>
     45#include <mutex>
    4546
    4647#ifdef CUDA_ENABLED
     
    6162static cl::opt<int> prefixLen("prefix", cl::desc("Prefix length"), cl::init(4));
    6263static cl::opt<bool> ShowPositions("display", cl::desc("Display the match positions."), cl::init(false));
     64
     65static cl::opt<int> Threads("threads", cl::desc("Total number of threads."), cl::init(1));
    6366
    6467using namespace kernel;
     
    137140
    138141extern "C" {
     142std::mutex store_mutex;
    139143void wrapped_report_pos(size_t match_pos, int dist) {
    140144        struct matchPosition curMatch;
    141145        curMatch.pos = match_pos;
    142146        curMatch.dist = dist;
     147
     148        store_mutex.lock();
    143149        matchList.push_back(curMatch);
    144150        if(ShowPositions)
    145151            std::cout << "pos: " << match_pos << ", dist:" << dist << "\n";
     152        store_mutex.unlock();
    146153    }
    147154
     
    211218}
    212219
     220
    213221Function * editdPipeline(Module * mMod, IDISA::IDISA_Builder * iBuilder, const std::vector<std::string> & patterns) {
    214222   
     
    420428    fn_ptr(inputStream, size);
    421429   
     430}
     431
     432std::mutex count_mutex;
     433size_t groupCount;
     434void *DoEditd(void *threadid)
     435{
     436    size_t groupIdx;
     437    count_mutex.lock();
     438    groupIdx = groupCount;
     439    groupCount++;
     440    count_mutex.unlock();
     441
     442    while (groupIdx < pattGroups.size()){
     443        editdFunctionType editd_ptr = editdCodeGen(pattGroups[groupIdx]);
     444        editd(editd_ptr, chStream, size);
     445
     446        count_mutex.lock();
     447        groupIdx = groupCount;
     448        groupCount++;
     449        count_mutex.unlock();
     450    }
     451
     452    pthread_exit(NULL);
    422453}
    423454
     
    664695    }
    665696    else{
    666         for(unsigned i=0; i<pattGroups.size(); i++){
    667             editdFunctionType editd_ptr = editdCodeGen(pattGroups[i]);
    668             editd(editd_ptr, chStream, size);
     697        if (Threads == 1) {
     698            for(unsigned i=0; i<pattGroups.size(); i++){
     699                editdFunctionType editd_ptr = editdCodeGen(pattGroups[i]);
     700                editd(editd_ptr, chStream, size);
     701            }
     702        }
     703        else{
     704            const unsigned numOfThreads = Threads;
     705            pthread_t threads[numOfThreads];
     706            groupCount = 0;
     707
     708            for(unsigned long i = 0; i < numOfThreads; ++i){
     709                const int rc = pthread_create(&threads[i], NULL, DoEditd, (void *)i);
     710                if (rc) {
     711                    llvm::report_fatal_error("Failed to create thread: code " + std::to_string(rc));
     712                }
     713            }
     714
     715            for(unsigned i = 0; i < numOfThreads; ++i) {
     716                void * status = nullptr;
     717                const int rc = pthread_join(threads[i], &status);
     718                if (rc) {
     719                    llvm::report_fatal_error("Failed to join thread: code " + std::to_string(rc));
     720                }
     721            }
     722
    669723        }
    670724        run_second_filter(pattern_segs, total_len, 0.15);
Note: See TracChangeset for help on using the changeset viewer.