source: icGREP/icgrep-devel/icgrep/editd/editd_gpu_kernel.cpp @ 5212

Last change on this file since 5212 was 5212, checked in by lindanl, 3 years ago

editd for GPU.

File size: 6.1 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#include "editd_gpu_kernel.h"
6#include <kernels/kernel.h>
7#include <IDISA/idisa_builder.h>
8#include <llvm/Support/raw_ostream.h>
9#include <iostream>
10
11namespace kernel {
12using namespace llvm;
13
14
15Value * bitblock_advance_ci_co(IDISA::IDISA_Builder * iBuilder, Value * val, unsigned shift, Value * stideCarryArr, unsigned carryIdx){   
16    Value * ptr = iBuilder->CreateGEP(stideCarryArr, {iBuilder->getInt32(0), iBuilder->getInt32(carryIdx)});
17    Value * ci = iBuilder->CreateLoad(ptr);
18    std::pair<Value *, Value *> rslt = iBuilder->bitblock_advance(val, ci, shift);
19    iBuilder->CreateStore(std::get<0>(rslt), ptr);
20    return std::get<1>(rslt);
21}
22
23void editdGPUKernel::generateFinalBlockMethod() {
24    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
25    Module * m = iBuilder->getModule();
26    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
27    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
28    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
29    // Final Block arguments: self, remaining, then the standard DoBlock args.
30    Function::arg_iterator args = finalBlockFunction->arg_begin();
31    Value * self = &*(args++);
32    Value * remaining = &*(args++);
33    std::vector<Value *> doBlockArgs = {self};
34    while (args != finalBlockFunction->arg_end()){
35        doBlockArgs.push_back(&*args++);
36    }
37    setScalarField(self, "EOFmask", iBuilder->bitblock_mask_from(remaining));
38    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
39    /* Adjust the produced item count */
40    Value * produced = getProducedItemCount(self);
41    produced = iBuilder->CreateSub(produced, ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride()));
42    setProducedItemCount(self, iBuilder->CreateAdd(produced, remaining));
43    iBuilder->CreateRetVoid();
44    iBuilder->restoreIP(savePoint);
45}
46   
47void editdGPUKernel::generateDoBlockMethod() {
48    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
49    Module * m = iBuilder->getModule(); 
50
51    Type * const int32ty = iBuilder->getInt32Ty();
52    Type * const int8ty = iBuilder->getInt8Ty();
53
54    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
55       
56    BasicBlock * entryBlock = BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0);
57   
58    iBuilder->SetInsertPoint(entryBlock);
59
60    Value * kernelStuctParam = getParameter(doBlockFunction, "self");
61    Value * pattBuf = getScalarField(kernelStuctParam, "pattStream");
62    Value * stideCarryArr = getScalarField(kernelStuctParam, "srideCarry");
63    Value * blockNo = getScalarField(kernelStuctParam, blockNoScalar);
64    Value * ccStreamPtr = getStreamSetBlockPtr(kernelStuctParam, "CCStream", blockNo);
65    Value * resultStreamPtr = getStreamSetBlockPtr(kernelStuctParam, "ResultStream", blockNo);
66    Value * pattLen = ConstantInt::get(int32ty, mPatternLen+1);
67    Value * pattPos = ConstantInt::get(int32ty, 0);
68   
69    unsigned carryIdx = 0;
70
71    std::vector<std::vector<Value *>> e(mPatternLen+1, std::vector<Value *>(mEditDistance+1));
72    Function * bidFunc = cast<Function>(m->getOrInsertFunction("llvm.nvvm.read.ptx.sreg.ctaid.x", int32ty, nullptr));
73    Value * bid = iBuilder->CreateCall(bidFunc);
74    Value * pattStartPtr = iBuilder->CreateGEP(pattBuf, {iBuilder->CreateMul(pattLen, bid)});
75    Value * pattPtr = iBuilder->CreateGEP(pattStartPtr, {pattPos});
76    Value * pattCh = iBuilder->CreateLoad(pattPtr);
77    Value * pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
78    Value * pattStreamPtr = iBuilder->CreateGEP(ccStreamPtr, {iBuilder->getInt32(0), iBuilder->CreateZExt(pattIdx, int32ty)});
79    Value * pattStream = iBuilder->CreateLoad(pattStreamPtr);
80    pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
81
82    e[0][0] = pattStream;
83    for(unsigned j = 1; j <= mEditDistance; j++){
84      e[0][j] = iBuilder->allOnes();
85    }
86
87    for(unsigned i = 1; i<mPatternLen; i++){     
88        pattPtr = iBuilder->CreateGEP(pattStartPtr, {pattPos});
89        pattCh = iBuilder->CreateLoad(pattPtr);
90        pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
91        pattStreamPtr = iBuilder->CreateGEP(ccStreamPtr, {iBuilder->getInt32(0), iBuilder->CreateZExt(pattIdx, int32ty)});
92        pattStream = iBuilder->CreateLoad(pattStreamPtr);
93
94        Value * adv = bitblock_advance_ci_co(iBuilder, e[i-1][0], 1, stideCarryArr, carryIdx++);
95        e[i][0] = iBuilder->CreateAnd(adv, pattStream);
96        for(unsigned j = 1; j<= mEditDistance; j++){
97            Value * adv0 = bitblock_advance_ci_co(iBuilder, e[i-1][j], 1, stideCarryArr, carryIdx++);
98            Value * adv1 = bitblock_advance_ci_co(iBuilder, e[i-1][j-1], 1, stideCarryArr, carryIdx++);
99            Value * adv2 = bitblock_advance_ci_co(iBuilder, e[i][j-1], 1, stideCarryArr, carryIdx++);
100            Value * tmp1 = iBuilder->CreateAnd(adv0, pattStream);
101            Value * tmp2 = iBuilder->CreateAnd(adv1, iBuilder->CreateNot(pattStream));
102            Value * tmp3 = iBuilder->CreateOr(adv2, e[i-1][j-1]);
103            e[i][j] = iBuilder->CreateOr(iBuilder->CreateOr(tmp1, tmp2), tmp3);
104
105        }
106        pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
107    }
108
109    Value * ptr = iBuilder->CreateGEP(resultStreamPtr, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
110    iBuilder->CreateStore(e[mPatternLen-1][0], ptr);
111    for(unsigned j = 1; j<= mEditDistance; j++){
112        ptr = iBuilder->CreateGEP(resultStreamPtr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
113        iBuilder->CreateStore(iBuilder->CreateAnd(e[mPatternLen-1][j], iBuilder->CreateNot(e[mPatternLen-1][j-1])), ptr);
114    }
115
116    Value * produced = getProducedItemCount(kernelStuctParam);
117    produced = iBuilder->CreateAdd(produced, ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride()));
118    setProducedItemCount(kernelStuctParam, produced); 
119       
120    iBuilder->CreateRetVoid();
121    iBuilder->restoreIP(savePoint);
122}
123
124}
125
126
Note: See TracBrowser for help on using the repository browser.