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

Last change on this file since 5402 was 5356, checked in by cameron, 3 years ago

Eliminate obsolete DoBlockUpdatesProducedItemCounts? attribute

File size: 4.7 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 <IR_Gen/idisa_builder.h>
7#include <llvm/IR/Module.h>
8
9using namespace llvm;
10
11namespace kernel {
12
13void 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){   
14    if (!calculated[i][j]) {
15        Value * ptr = iBuilder->CreateGEP(stideCarryArr, {iBuilder->getInt32(0), iBuilder->getInt32(carryIdx)});
16        Value * ci = iBuilder->CreateLoad(ptr);
17        std::pair<Value *, Value *> rslt = iBuilder->bitblock_advance(val, ci, shift);
18        iBuilder->CreateStore(std::get<0>(rslt), ptr);
19        adv[i][j] = std::get<1>(rslt);
20        calculated[i][j] = 1;
21    }
22}
23
24void editdGPUKernel::generateDoBlockMethod() {
25
26    IntegerType * const int32ty = iBuilder->getInt32Ty();
27    IntegerType * const int8ty = iBuilder->getInt8Ty();
28    Value * pattLen = iBuilder->getInt32(mPatternLen + 1);
29    Value * pattPos = iBuilder->getInt32(0);
30    Value * pattBuf = getScalarField("pattStream");
31    Value * strideCarryArr = getScalarField("strideCarry");
32   
33    unsigned carryIdx = 0;
34
35    std::vector<std::vector<Value *>> e(mPatternLen, std::vector<Value *>(mEditDistance + 1));
36    std::vector<std::vector<Value *>> adv(mPatternLen, std::vector<Value *>(mEditDistance + 1));
37    std::vector<std::vector<int>> calculated(mPatternLen, std::vector<int>(mEditDistance + 1, 0));
38
39    Module * m = iBuilder->getModule();
40    Function * bidFunc = cast<Function>(m->getOrInsertFunction("llvm.nvvm.read.ptx.sreg.ctaid.x", int32ty, nullptr));
41    Value * bid = iBuilder->CreateCall(bidFunc);
42    Value * pattStartPtr = iBuilder->CreateGEP(pattBuf, iBuilder->CreateMul(pattLen, bid));
43    Value * pattPtr = iBuilder->CreateGEP(pattStartPtr, pattPos);
44    Value * pattCh = iBuilder->CreateLoad(pattPtr);
45    Value * pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
46    Value * pattStream = loadInputStreamBlock("CCStream", iBuilder->CreateZExt(pattIdx, int32ty));
47    pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
48
49    e[0][0] = pattStream;
50    for(unsigned j = 1; j <= mEditDistance; j++){
51        e[0][j] = iBuilder->allOnes();
52    }
53    for(unsigned i = 1; i < mPatternLen; i++){
54        pattPtr = iBuilder->CreateGEP(pattStartPtr, pattPos);
55        pattCh = iBuilder->CreateLoad(pattPtr);
56        pattIdx = iBuilder->CreateAnd(iBuilder->CreateLShr(pattCh, 1), ConstantInt::get(int8ty, 3));
57        pattStream = loadInputStreamBlock("CCStream", iBuilder->CreateZExt(pattIdx, int32ty));
58        bitblock_advance_ci_co(iBuilder, e[i-1][0], 1, strideCarryArr, carryIdx++, adv, calculated, i-1, 0);
59        e[i][0] = iBuilder->CreateAnd(adv[i-1][0], pattStream); 
60        for(unsigned j = 1; j<= mEditDistance; j++){
61            bitblock_advance_ci_co(iBuilder, e[i-1][j], 1, strideCarryArr, carryIdx++, adv, calculated, i-1, j);
62            bitblock_advance_ci_co(iBuilder, e[i-1][j-1], 1, strideCarryArr, carryIdx++, adv, calculated, i-1, j-1);
63            bitblock_advance_ci_co(iBuilder, e[i][j-1], 1, strideCarryArr, carryIdx++, adv, calculated, i, j-1);
64            Value * tmp1 = iBuilder->CreateAnd(adv[i-1][j], pattStream);
65            Value * tmp2 = iBuilder->CreateAnd(adv[i-1][j-1], iBuilder->CreateNot(pattStream));
66            Value * tmp3 = iBuilder->CreateOr(adv[i][j-1], e[i-1][j-1]);
67            e[i][j] = iBuilder->CreateOr(iBuilder->CreateOr(tmp1, tmp2), tmp3);
68        }
69        pattPos = iBuilder->CreateAdd(pattPos, ConstantInt::get(int32ty, 1));
70    }
71    storeOutputStreamBlock("ResultStream", iBuilder->getInt32(0), e[mPatternLen-1][0]);
72    for(unsigned j = 1; j<= mEditDistance; j++){
73        storeOutputStreamBlock("ResultStream", iBuilder->getInt32(j), iBuilder->CreateAnd(e[mPatternLen - 1][j], iBuilder->CreateNot(e[mPatternLen - 1][j - 1])));
74    }
75}
76
77void editdGPUKernel::generateFinalBlockMethod(Value * remainingBytes) {
78    setScalarField("EOFmask", iBuilder->bitblock_mask_from(remainingBytes));
79    CreateDoBlockMethodCall();
80}
81
82editdGPUKernel::editdGPUKernel(IDISA::IDISA_Builder * b, unsigned dist, unsigned pattLen) :
83BlockOrientedKernel(b, "editd_gpu",
84              {Binding{b->getStreamSetTy(4), "CCStream"}},
85              {Binding{b->getStreamSetTy(dist + 1), "ResultStream"}},
86              {Binding{PointerType::get(b->getInt8Ty(), 1), "pattStream"},
87              Binding{PointerType::get(ArrayType::get(b->getBitBlockType(), pattLen * (dist + 1) * 4), 0), "strideCarry"}},
88              {},
89              {Binding{b->getBitBlockType(), "EOFmask"}})
90, mEditDistance(dist)
91, mPatternLen(pattLen) {
92}
93
94}
95
96
Note: See TracBrowser for help on using the repository browser.