Ignore:
Timestamp:
Jun 15, 2017, 1:10:27 PM (2 years ago)
Author:
lindanl
Message:

editd GPU (bug unresolved).

File:
1 edited

Legend:

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

    r5486 r5511  
    2828#include <mutex>
    2929#ifdef CUDA_ENABLED
     30#include <toolchain/NVPTXDriver.h>
    3031#include <editd/EditdCudaDriver.h>
    3132#include <editd/editd_gpu_kernel.h>
     
    5354using namespace parabix;
    5455
    55 const static std::string IRFilename = "editd.ll";
    5656const static std::string PTXFilename = "editd.ptx";
    5757
     
    382382
    383383void editdGPUCodeGen(unsigned patternLen){
    384     LLVMContext TheContext;
    385     Module * M = new Module("editd-gpu", TheContext);
    386     IDISA::IDISA_Builder * iBuilder = IDISA::GetIDISA_GPU_Builder(M->getContext());
    387     M->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
    388     M->setTargetTriple("nvptx64-nvidia-cuda");
    389     unsigned addrSpace = 1;
     384    NVPTXDriver pxDriver("editd");
     385    auto & iBuilder = pxDriver.getBuilder();
     386    Module * M = iBuilder->getModule();
     387
     388    const unsigned segmentSize = codegen::SegmentSize;
    390389
    391390    Type * const mBitBlockType = iBuilder->getBitBlockType();
     
    398397    Type * const stridesTy = PointerType::get(int32ty, 1);
    399398
    400     ExternalFileBuffer CCStream(iBuilder, iBuilder->getStreamSetTy(4), addrSpace);
    401     ExternalFileBuffer ResultStream(iBuilder, iBuilder->getStreamSetTy( editDistance+1, 1), addrSpace);
    402 
    403     MMapSourceKernel mmapK(iBuilder);
    404     mmapK.generateKernel({}, {&CCStream});
    405 
    406     kernel::editdGPUKernel editdk(iBuilder, editDistance, patternLen);
    407     editdk.generateKernel({&CCStream}, {&ResultStream});
    408 
    409     Function * const main = cast<Function>(M->getOrInsertFunction("GPU_Main", voidTy, inputTy, inputSizeTy, patternPtrTy, outputTy, stridesTy, nullptr));
     399    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputTy, inputSizeTy, patternPtrTy, outputTy, stridesTy, nullptr));
    410400    main->setCallingConv(CallingConv::C);
    411401    Function::arg_iterator args = main->arg_begin();
     
    426416    Function * tidFunc = M->getFunction("llvm.nvvm.read.ptx.sreg.tid.x");
    427417    Value * tid = iBuilder->CreateCall(tidFunc);
    428     Value * inputThreadPtr = iBuilder->CreateGEP(inputStream, tid);
    429 
    430418    Function * bidFunc = cast<Function>(M->getOrInsertFunction("llvm.nvvm.read.ptx.sreg.ctaid.x", int32ty, nullptr));
    431419    Value * bid = iBuilder->CreateCall(bidFunc);
     420
     421    Value * inputThreadPtr = iBuilder->CreateGEP(inputStream, tid);
    432422    Value * strides = iBuilder->CreateLoad(stridesPtr);
    433423    Value * outputBlocks = iBuilder->CreateMul(strides, ConstantInt::get(int32ty, GROUPTHREADS));
    434424    Value * resultStreamPtr = iBuilder->CreateGEP(resultStream, iBuilder->CreateAdd(iBuilder->CreateMul(bid, outputBlocks), tid));
    435 
    436425    Value * inputSize = iBuilder->CreateLoad(inputSizePtr);
    437     CCStream.setStreamSetBuffer(inputThreadPtr);
    438     ResultStream.setStreamSetBuffer(resultStreamPtr);
    439     mmapK.setInitialArguments({inputSize});
    440 
     426
     427    StreamSetBuffer * CCStream = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(4), 1));
     428    kernel::Kernel * sourceK = pxDriver.addKernelInstance(make_unique<kernel::MemorySourceKernel>(iBuilder, inputTy, segmentSize));
     429    sourceK->setInitialArguments({inputThreadPtr, inputSize});
     430    pxDriver.makeKernelCall(sourceK, {}, {CCStream});
     431
     432    ExternalBuffer * ResultStream = pxDriver.addExternalBuffer(make_unique<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(editDistance+1), resultStreamPtr, 1));   
     433    kernel::Kernel * editdk = pxDriver.addKernelInstance(make_unique<kernel::editdGPUKernel>(iBuilder, editDistance, patternLen));
     434     
    441435    const unsigned numOfCarries = patternLen * (editDistance + 1) * 4;
    442436    Type * strideCarryTy = ArrayType::get(mBitBlockType, numOfCarries);
     
    444438    iBuilder->CreateStore(Constant::getNullValue(strideCarryTy), strideCarry);
    445439
    446     editdk.setInitialArguments({pattStream, strideCarry});
    447 
    448     generatePipelineLoop(iBuilder, {&mmapK, &editdk});
     440    editdk->setInitialArguments({pattStream, strideCarry});
     441    pxDriver.makeKernelCall(editdk, {CCStream}, {ResultStream});
     442
     443    pxDriver.generatePipelineIR();
    449444
    450445    iBuilder->CreateRetVoid();
    451446
    452     MDNode * Node = MDNode::get(M->getContext(),
    453                                 {llvm::ValueAsMetadata::get(main),
    454                                  MDString::get(M->getContext(), "kernel"),
    455                                  ConstantAsMetadata::get(ConstantInt::get(iBuilder->getInt32Ty(), 1))});
    456     NamedMDNode *NMD = M->getOrInsertNamedMetadata("nvvm.annotations");
    457     NMD->addOperand(Node);
    458 
    459     Compile2PTX(M, IRFilename, PTXFilename);
     447    pxDriver.finalizeObject();
    460448
    461449}
    462450
    463451void mergeGPUCodeGen(){
    464         LLVMContext TheContext;
    465     Module * M = new Module("editd-gpu", TheContext);
    466     IDISA::IDISA_Builder * iBuilder = IDISA::GetIDISA_GPU_Builder(M->getContext());
    467     M->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64");
    468     M->setTargetTriple("nvptx64-nvidia-cuda");
     452    NVPTXDriver pxDriver("merge");
     453    auto & iBuilder = pxDriver.getBuilder();
     454    Module * M = iBuilder->getModule();
    469455
    470456    Type * const mBitBlockType = iBuilder->getBitBlockType();
     
    474460    Type * const stridesTy = PointerType::get(int32ty, 1);
    475461
    476     Function * const main = cast<Function>(M->getOrInsertFunction("mergeResult", voidTy, resultTy, stridesTy, nullptr));
     462    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, resultTy, stridesTy, nullptr));
    477463    main->setCallingConv(CallingConv::C);
    478464    Function::arg_iterator args = main->arg_begin();
     
    492478    Function * tidFunc = M->getFunction("llvm.nvvm.read.ptx.sreg.tid.x");
    493479    Value * tid = iBuilder->CreateCall(tidFunc);
    494 
    495480    Function * bidFunc = cast<Function>(M->getOrInsertFunction("llvm.nvvm.read.ptx.sreg.ctaid.x", int32ty, nullptr));
    496481    Value * bid = iBuilder->CreateCall(bidFunc);
     482
    497483    Value * strides = iBuilder->CreateLoad(stridesPtr);
    498484    Value * strideBlocks = ConstantInt::get(int32ty, iBuilder->getStride() / iBuilder->getBitBlockWidth());
     
    521507    iBuilder->CreateRetVoid();
    522508
    523     MDNode * Node = MDNode::get(M->getContext(),
    524                                 {llvm::ValueAsMetadata::get(main),
    525                                  MDString::get(M->getContext(), "kernel"),
    526                                  ConstantAsMetadata::get(ConstantInt::get(iBuilder->getInt32Ty(), 1))});
    527     NamedMDNode *NMD = M->getOrInsertNamedMetadata("nvvm.annotations");
    528     NMD->addOperand(Node);
    529 
    530     Compile2PTX(M, "merge.ll", "merge.ptx");
     509    pxDriver.finalizeObject();
    531510
    532511}
     
    534513editdFunctionType editdScanCPUCodeGen() {
    535514
    536     LLVMContext TheContext;
    537     Module * M = new Module("editd", TheContext);
    538     IDISA::IDISA_Builder * iBuilder = IDISA::GetIDISA_Builder(M);
    539     ExecutionEngine * editdEngine = nullptr;
     515    ParabixDriver pxDriver("scan");
     516    auto & iBuilder = pxDriver.getBuilder();
     517    Module * M = iBuilder->getModule();
     518
     519    const unsigned segmentSize = codegen::SegmentSize;
    540520
    541521    Type * mBitBlockType = iBuilder->getBitBlockType();
     
    544524    Type * const inputType = PointerType::get(ArrayType::get(mBitBlockType, editDistance+1), 0);
    545525
    546     ExternalFileBuffer MatchResults(iBuilder, iBuilder->getStreamSetTy( editDistance+1, 1));
    547 
    548     MMapSourceKernel mmapK(iBuilder);
    549     mmapK.generateKernel({}, {&MatchResults});
    550 
    551     kernel::editdScanKernel editdScanK(iBuilder, editDistance);
    552     editdScanK.generateKernel({&MatchResults}, {});
    553 
    554     Function * const main = cast<Function>(M->getOrInsertFunction("CPU_Main", voidTy, inputType, size_ty, nullptr));
     526    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, size_ty, nullptr));
    555527    main->setCallingConv(CallingConv::C);
     528    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
    556529    Function::arg_iterator args = main->arg_begin();
    557 
    558530    Value * const inputStream = &*(args++);
    559531    inputStream->setName("input");
     
    561533    fileSize->setName("fileSize");
    562534
    563     iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main,0));
    564 
    565     MatchResults.setStreamSetBuffer(inputStream);
    566     mmapK.setInitialArguments({fileSize});
    567 
    568     generatePipelineLoop(iBuilder, {&mmapK, &editdScanK});
    569 
     535
     536    StreamSetBuffer * MatchResults = pxDriver.addBuffer(make_unique<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(editDistance+1)));
     537    kernel::Kernel * sourceK = pxDriver.addKernelInstance(make_unique<kernel::MemorySourceKernel>(iBuilder, inputType, segmentSize));
     538    sourceK->setInitialArguments({inputStream, fileSize});
     539    pxDriver.makeKernelCall(sourceK, {}, {MatchResults});
     540
     541    auto editdScanK = pxDriver.addKernelInstance(make_unique<editdScanKernel>(iBuilder, editDistance));
     542    pxDriver.makeKernelCall(editdScanK, {MatchResults}, {});
     543       
     544    pxDriver.generatePipelineIR();
    570545    iBuilder->CreateRetVoid();
    571546
    572     editdEngine = JIT_to_ExecutionEngine(M);
    573 
    574     editdEngine->finalizeObject();
    575 
    576     return reinterpret_cast<editdFunctionType>(editdEngine->getPointerToFunction(main));
     547    pxDriver.LinkFunction(*editdScanK, "wrapped_report_pos", &wrapped_report_pos);
     548    pxDriver.finalizeObject();
     549
     550    return reinterpret_cast<editdFunctionType>(pxDriver.getMain());
     551
    577552}
    578553
Note: See TracChangeset for help on using the changeset viewer.