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

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

IR_Gen subdirectory for all IR generation utility functions

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