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

Last change on this file since 5261 was 5261, checked in by cameron, 9 months ago

Move responsibility for ProducedItemCount? into doSegment unless overridden

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