Ignore:
Timestamp:
Apr 28, 2017, 2:50:59 PM (2 years ago)
Author:
nmedfort
Message:

Refactored source kernels. icgrep from stdin should now be able to handle any file size.

File:
1 edited

Legend:

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

    r5425 r5429  
    1212#include <llvm/IR/Function.h>
    1313#include <llvm/IR/Module.h>
    14 #include <llvm/ExecutionEngine/ExecutionEngine.h>
    15 #include "llvm/Linker/Linker.h"
    1614#include <llvm/Support/CommandLine.h>
    1715#include <cc/cc_compiler.h>
     
    2119#include <IR_Gen/idisa_target.h>
    2220#include <kernels/streamset.h>
    23 #include <kernels/mmap_kernel.h>
    24 #include <kernels/stdin_kernel.h>
     21#include <kernels/source_kernel.h>
    2522#include <kernels/s2p_kernel.h>
    2623#include <editd/editdscan_kernel.h>
     
    6865
    6966void run_second_filter(int total_len, int pattern_segs, float errRate){
    70    
     67
    7168    if(matchList.empty()) return;
    7269
     
    126123}
    127124
    128 extern "C" {
    129 std::mutex store_mutex;
    130 void wrapped_report_pos(size_t match_pos, int dist) {
    131         struct matchPosition curMatch;
    132         curMatch.pos = match_pos;
    133         curMatch.dist = dist;
    134 
    135         store_mutex.lock();
    136         matchList.push_back(curMatch);
    137         if(ShowPositions)
    138             std::cout << "pos: " << match_pos << ", dist:" << dist << "\n";
    139         store_mutex.unlock();
    140     }
    141 
    142 }
    143 
    144 void icgrep_Linking(Module * m, ExecutionEngine * e) {
    145     Module::FunctionListType & fns = m->getFunctionList();
    146     for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
    147         std::string fnName = it->getName().str();
    148         if (fnName == "wrapped_report_pos") {
    149             e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_pos);
    150         }
    151     }
    152 }
    153 
    154125void get_editd_pattern(int & pattern_segs, int & total_len) {
    155  
     126
    156127    if (PatternFilename != "") {
    157128        std::ifstream pattFile(PatternFilename.c_str());
     
    160131            while (std::getline(pattFile, r)) {
    161132                pattVector.push_back(r);
    162                 pattern_segs ++; 
    163                 total_len += r.size(); 
     133                pattern_segs ++;
     134                total_len += r.size();
    164135            }
    165136            std::sort(pattVector.begin(), pattVector.end());
     
    171142                    pattGroup.push_back(pattVector[i]);
    172143                    i++;
    173                 } 
     144                }
    174145                pattGroups.push_back(pattGroup);
    175146            }
     
    177148        }
    178149    }
    179    
     150
    180151    // if there are no regexes specified through -e or -f, the first positional argument
    181152    // must be a regex, not an input file.
    182    
     153
    183154    if (pattVector.size() == 0) {
    184155        pattVector.push_back(inputFiles[0]);
     
    205176}
    206177
     178std::mutex store_mutex;
     179void wrapped_report_pos(size_t match_pos, int dist) {
     180    struct matchPosition curMatch;
     181    curMatch.pos = match_pos;
     182    curMatch.dist = dist;
     183
     184    store_mutex.lock();
     185    matchList.push_back(curMatch);
     186    if(ShowPositions)
     187        std::cout << "pos: " << match_pos << ", dist:" << dist << "\n";
     188    store_mutex.unlock();
     189}
    207190
    208191void editdPipeline(ParabixDriver & pxDriver, const std::vector<std::string> & patterns) {
    209    
    210     IDISA::IDISA_Builder * const iBuilder = pxDriver.getIDISA_Builder();
    211     Module * const m = iBuilder->getModule();
    212     Type * const sizeTy = iBuilder->getSizeTy();
    213     Type * const voidTy = iBuilder->getVoidTy();
    214     Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(iBuilder->getBitBlockType(), 8), 1), 0);
    215    
     192
     193    IDISA::IDISA_Builder * const idb = pxDriver.getIDISA_Builder();
     194    Module * const m = idb->getModule();
     195    Type * const sizeTy = idb->getSizeTy();
     196    Type * const voidTy = idb->getVoidTy();
     197    Type * const inputType = PointerType::get(ArrayType::get(ArrayType::get(idb->getBitBlockType(), 8), 1), 0);
     198
     199    idb->LinkFunction("wrapped_report_pos", &wrapped_report_pos);
     200
    216201    Function * const main = cast<Function>(m->getOrInsertFunction("Main", voidTy, inputType, sizeTy, nullptr));
    217202    main->setCallingConv(CallingConv::C);
     
    221206    Value * const fileSize = &*(args++);
    222207    fileSize->setName("fileSize");
    223     iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main,0));
    224 
    225 
    226     auto ChStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(4)));
    227 
    228     auto mmapK = pxDriver.addKernelInstance(make_unique<kernel::FileSourceKernel>(iBuilder, inputType));
     208    idb->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main,0));
     209
     210    auto ChStream = pxDriver.addBuffer(make_unique<SourceBuffer>(idb, idb->getStreamSetTy(4)));
     211
     212    auto mmapK = pxDriver.addKernelInstance(make_unique<MemorySourceKernel>(idb, inputType));
    229213    mmapK->setInitialArguments({inputStream, fileSize});
    230214
    231215    pxDriver.makeKernelCall(mmapK, {}, {ChStream});
    232216
    233     auto MatchResults = pxDriver.addBuffer(make_unique<SingleBlockBuffer>(iBuilder, iBuilder->getStreamSetTy(editDistance + 1)));
     217    auto MatchResults = pxDriver.addBuffer(make_unique<SingleBlockBuffer>(idb, idb->getStreamSetTy(editDistance + 1)));
    234218
    235219    auto editdk = pxDriver.addKernelInstance(make_unique<PabloKernel>(
    236         iBuilder, "editd", std::vector<Binding>{{iBuilder->getStreamSetTy(4), "pat"}}, std::vector<Binding>{{iBuilder->getStreamSetTy(editDistance + 1), "E"}}
     220        idb, "editd", std::vector<Binding>{{idb->getStreamSetTy(4), "pat"}}, std::vector<Binding>{{idb->getStreamSetTy(editDistance + 1), "E"}}
    237221        ));
    238222
     
    240224    pxDriver.makeKernelCall(editdk, {ChStream}, {MatchResults});
    241225
    242     auto editdScanK = pxDriver.addKernelInstance(make_unique<editdScanKernel>(iBuilder, editDistance));
     226    auto editdScanK = pxDriver.addKernelInstance(make_unique<editdScanKernel>(idb, editDistance));
    243227    pxDriver.makeKernelCall(editdScanK, {MatchResults}, {});
    244        
     228
    245229    pxDriver.generatePipelineIR();
    246230
    247     iBuilder->CreateRetVoid();
     231    idb->CreateRetVoid();
    248232
    249233    pxDriver.linkAndFinalize();
     
    275259    Module * m = iBuilder->getModule();
    276260    Type * mBitBlockType = iBuilder->getBitBlockType();
    277    
     261
    278262
    279263    Type * const voidTy = iBuilder->getVoidTy();
    280264    Type * const int32Ty = iBuilder->getInt32Ty();
    281265    Type * const outputType = PointerType::get(ArrayType::get(mBitBlockType, 4), 0);
    282    
     266
    283267    Function * const main = cast<Function>(m->getOrInsertFunction("Main", voidTy, int32Ty, outputType, nullptr));
    284268    main->setCallingConv(CallingConv::C);
    285269    Function::arg_iterator args = main->arg_begin();
    286    
     270
    287271    Value * const fileDescriptor = &*(args++);
    288272    fileDescriptor->setName("fileDescriptor");
     
    292276    iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", main));
    293277
    294     auto ByteStream = pxDriver.addBuffer(make_unique<SourceFileBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
    295 
    296     auto mmapK = pxDriver.addKernelInstance(make_unique<kernel::MMapSourceKernel>(iBuilder, 1));
     278    auto ByteStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8)));
     279
     280    auto mmapK = pxDriver.addKernelInstance(make_unique<MMapSourceKernel>(iBuilder, 1));
    297281    mmapK->setInitialArguments({fileDescriptor});
    298282    pxDriver.makeKernelCall(mmapK, {}, {ByteStream});
     
    302286    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
    303287
    304     auto CCResults = pxDriver.addExternalBuffer(make_unique<ExternalFileBuffer>(iBuilder, iBuilder->getStreamSetTy(4)), outputStream);
    305 
    306 
     288    auto CCResults = pxDriver.addExternalBuffer(make_unique<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(4), outputStream));
    307289
    308290    auto ccck = pxDriver.addKernelInstance(make_unique<PabloKernel>(
    309291        iBuilder, "ccc", std::vector<Binding>{{iBuilder->getStreamSetTy(8), "basis"}}, std::vector<Binding>{{iBuilder->getStreamSetTy(4), "pat"}}
    310         ));
     292    ));
     293
    311294    buildPreprocessKernel(reinterpret_cast<PabloKernel *>(ccck));
     295
    312296    pxDriver.makeKernelCall(ccck, {BasisBits}, {CCResults});
    313              
     297
    314298    pxDriver.generatePipelineIR();
    315299
     
    322306typedef void (*preprocessFunctionType)(const int fd, char * output_data);
    323307
    324 preprocessFunctionType preprocessCodeGen() {                           
     308preprocessFunctionType preprocessCodeGen() {
    325309    ParabixDriver pxDriver("preprocess");
    326310    preprocessPipeline(pxDriver);
     
    330314typedef void (*editdFunctionType)(char * byte_data, size_t filesize);
    331315
    332 editdFunctionType editdCodeGen(const std::vector<std::string> & patterns) {                           
     316editdFunctionType editdCodeGen(const std::vector<std::string> & patterns) {
    333317    ParabixDriver pxDriver("editd");
    334318    editdPipeline(pxDriver, patterns);
     
    362346    fn_ptr(fd, chStream);
    363347    close(fd);
    364     return chStream;   
     348    return chStream;
    365349}
    366350
    367351void editd(editdFunctionType fn_ptr, char * inputStream, size_t size) {
    368  
    369     if (size == 0) {
    370         inputStream = nullptr;
    371     }
    372 
    373352    fn_ptr(inputStream, size);
    374    
    375353}
    376354
     
    398376}
    399377
    400 #ifdef CUDA_ENABLED 
     378#ifdef CUDA_ENABLED
    401379
    402380#define GROUPTHREADS 64
    403381#define GROUPBLOCKS 64
    404382
    405 void editdGPUCodeGen(unsigned patternLen){ 
     383void editdGPUCodeGen(unsigned patternLen){
    406384    LLVMContext TheContext;
    407385    Module * M = new Module("editd-gpu", TheContext);
     
    426404    mmapK.generateKernel({}, {&CCStream});
    427405
    428     kernel::editdGPUKernel editdk(iBuilder, editDistance, patternLen); 
     406    kernel::editdGPUKernel editdk(iBuilder, editDistance, patternLen);
    429407    editdk.generateKernel({&CCStream}, {&ResultStream});
    430408
     
    432410    main->setCallingConv(CallingConv::C);
    433411    Function::arg_iterator args = main->arg_begin();
    434    
     412
    435413    Value * const inputStream = &*(args++);
    436414    inputStream->setName("input");
     
    443421    Value * const stridesPtr = &*(args++);
    444422    stridesPtr->setName("stridesPtr");
    445    
     423
    446424    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main,0));
    447425
     
    467445
    468446    editdk.setInitialArguments({pattStream, strideCarry});
    469    
     447
    470448    generatePipelineLoop(iBuilder, {&mmapK, &editdk});
    471        
     449
    472450    iBuilder->CreateRetVoid();
    473    
     451
    474452    MDNode * Node = MDNode::get(M->getContext(),
    475453                                {llvm::ValueAsMetadata::get(main),
    476                                  MDString::get(M->getContext(), "kernel"), 
     454                                 MDString::get(M->getContext(), "kernel"),
    477455                                 ConstantAsMetadata::get(ConstantInt::get(iBuilder->getInt32Ty(), 1))});
    478456    NamedMDNode *NMD = M->getOrInsertNamedMetadata("nvvm.annotations");
     
    499477    main->setCallingConv(CallingConv::C);
    500478    Function::arg_iterator args = main->arg_begin();
    501    
     479
    502480    Value * const resultStream = &*(args++);
    503481    resultStream->setName("resultStream");
     
    509487    BasicBlock * strideLoopBody = BasicBlock::Create(iBuilder->getContext(), "strideLoopBody", main, 0);
    510488    BasicBlock * stridesDone = BasicBlock::Create(iBuilder->getContext(), "stridesDone", main, 0);
    511    
     489
    512490    iBuilder->SetInsertPoint(entryBlock);
    513491
     
    528506    Value * notDone = iBuilder->CreateICmpULT(strideNo, strides);
    529507    iBuilder->CreateCondBr(notDone, strideLoopBody, stridesDone);
    530  
     508
    531509    iBuilder->SetInsertPoint(strideLoopBody);
    532510    Value * myResultStreamPtr = iBuilder->CreateGEP(resultStreamPtr, {iBuilder->CreateMul(strideBlocks, strideNo)});
     
    535513        Value * nextStreamPtr = iBuilder->CreateGEP(myResultStreamPtr, {iBuilder->CreateMul(outputBlocks, iBuilder->getInt32(i)), bid});
    536514        myResultStream = iBuilder->CreateOr(myResultStream, iBuilder->CreateLoad(nextStreamPtr));
    537     }   
     515    }
    538516    iBuilder->CreateStore(myResultStream, iBuilder->CreateGEP(myResultStreamPtr, {iBuilder->getInt32(0), bid}));
    539517    strideNo->addIncoming(iBuilder->CreateAdd(strideNo, ConstantInt::get(int32ty, 1)), strideLoopBody);
    540518    iBuilder->CreateBr(strideLoopCond);
    541    
     519
    542520    iBuilder->SetInsertPoint(stridesDone);
    543521    iBuilder->CreateRetVoid();
    544    
     522
    545523    MDNode * Node = MDNode::get(M->getContext(),
    546524                                {llvm::ValueAsMetadata::get(main),
    547                                  MDString::get(M->getContext(), "kernel"), 
     525                                 MDString::get(M->getContext(), "kernel"),
    548526                                 ConstantAsMetadata::get(ConstantInt::get(iBuilder->getInt32Ty(), 1))});
    549527    NamedMDNode *NMD = M->getOrInsertNamedMetadata("nvvm.annotations");
     
    555533
    556534editdFunctionType editdScanCPUCodeGen() {
    557                            
     535
    558536    LLVMContext TheContext;
    559537    Module * M = new Module("editd", TheContext);
     
    572550
    573551    kernel::editdScanKernel editdScanK(iBuilder, editDistance);
    574     editdScanK.generateKernel({&MatchResults}, {});               
    575    
     552    editdScanK.generateKernel({&MatchResults}, {});
     553
    576554    Function * const main = cast<Function>(M->getOrInsertFunction("CPU_Main", voidTy, inputType, size_ty, nullptr));
    577555    main->setCallingConv(CallingConv::C);
    578556    Function::arg_iterator args = main->arg_begin();
    579    
     557
    580558    Value * const inputStream = &*(args++);
    581559    inputStream->setName("input");
    582560    Value * const fileSize = &*(args++);
    583561    fileSize->setName("fileSize");
    584    
     562
    585563    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main,0));
    586564
    587565    MatchResults.setStreamSetBuffer(inputStream);
    588566    mmapK.setInitialArguments({fileSize});
    589    
     567
    590568    generatePipelineLoop(iBuilder, {&mmapK, &editdScanK});
    591        
     569
    592570    iBuilder->CreateRetVoid();
    593571
    594572    editdEngine = JIT_to_ExecutionEngine(M);
    595    
     573
    596574    editdEngine->finalizeObject();
    597575
     
    610588    get_editd_pattern(pattern_segs, total_len);
    611589
    612 #ifdef CUDA_ENABLED 
     590#ifdef CUDA_ENABLED
    613591    codegen::BlockSize = 64;
    614592#endif
     
    617595    preprocess(preprocess_ptr);
    618596
    619 #ifdef CUDA_ENABLED 
    620     setNVPTXOption();   
     597#ifdef CUDA_ENABLED
     598    setNVPTXOption();
    621599    if(codegen::NVPTX){
    622600
     
    625603            std::cerr << "Error: cannot open " << PatternFilename << " for processing. Skipped.\n";
    626604            exit(-1);
    627         } 
     605        }
    628606        std::string patterns((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
    629607
     
    637615
    638616        editd(editd_ptr, (char*)rslt, size);
    639        
     617
    640618        run_second_filter(pattern_segs, total_len, 0.15);
    641619
     
    643621    }
    644622#endif
    645    
     623
    646624    if(pattVector.size() == 1){
    647625        editdFunctionType editd_ptr = editdCodeGen(pattVector);
Note: See TracChangeset for help on using the changeset viewer.