source: icGREP/icgrep-devel/icgrep/editd/editd_cpu_kernel.cpp @ 5247

Last change on this file since 5247 was 5247, checked in by cameron, 2 years ago

Separate processedItemCounts and producedItemCounts for each stream set

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