source: icGREP/icgrep-devel/icgrep/kernels/pipeline.cpp @ 5194

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

Restructuring pipeline control to use termination signals

File size: 9.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
6
7#include <toolchain.h>
8#include "pipeline.h"
9
10#include <IDISA/idisa_builder.h>
11
12#include <kernels/interface.h>
13#include <kernels/kernel.h>
14#include <kernels/s2p_kernel.h>
15
16#include <llvm/IR/TypeBuilder.h>
17#include <iostream>
18
19using namespace kernel;
20
21Function * generateSegmentParallelPipelineThreadFunction(std::string name, IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels, Type * sharedStructType, int id) {
22
23    Module * m = iBuilder->getModule();
24    Type * const size_ty = iBuilder->getSizeTy();
25    Type * const voidTy = Type::getVoidTy(m->getContext());
26    Type * const voidPtrTy = TypeBuilder<void *, false>::get(m->getContext());
27    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
28
29    Function * const threadFunc = cast<Function>(m->getOrInsertFunction(name, voidTy, int8PtrTy, nullptr));
30    threadFunc->setCallingConv(CallingConv::C);
31    Function::arg_iterator args = threadFunc->arg_begin();
32
33    Value * const input = &*(args++);
34    input->setName("input");
35
36    unsigned threadNum = codegen::ThreadNum;
37
38     // Create the basic blocks for the thread function.
39    BasicBlock * entryBlock = BasicBlock::Create(iBuilder->getContext(), "entry", threadFunc, 0);
40    BasicBlock * segmentLoop = BasicBlock::Create(iBuilder->getContext(), "segmentLoop", threadFunc, 0);
41    BasicBlock * exitThreadBlock = BasicBlock::Create(iBuilder->getContext(), "exitThread", threadFunc, 0);
42    std::vector<BasicBlock *> segmentWait;
43    std::vector<BasicBlock *> segmentLoopBody;
44    for (unsigned i = 0; i < kernels.size(); i++) {
45        segmentWait.push_back(BasicBlock::Create(iBuilder->getContext(), "segmentWait"+std::to_string(i), threadFunc, 0));
46        segmentLoopBody.push_back(BasicBlock::Create(iBuilder->getContext(), "segmentWait"+std::to_string(i), threadFunc, 0));
47    }
48
49    iBuilder->SetInsertPoint(entryBlock);
50    Value * sharedStruct = iBuilder->CreateBitCast(input, PointerType::get(sharedStructType, 0));
51    Constant * myThreadId = ConstantInt::get(size_ty, id);
52    std::vector<Value *> instancePtrs;
53    for (unsigned i = 0; i < kernels.size(); i++) {
54        Value * ptr = iBuilder->CreateGEP(sharedStruct, {iBuilder->getInt32(0), iBuilder->getInt32(i + 1)});
55        instancePtrs.push_back(iBuilder->CreateLoad(ptr));
56    }
57   
58    // Some important constant values.
59    int segmentSize = codegen::SegmentSize;
60    Constant * segmentBlocks = ConstantInt::get(size_ty, segmentSize);
61    iBuilder->CreateBr(segmentLoop);
62
63    iBuilder->SetInsertPoint(segmentLoop);
64    PHINode * segNo = iBuilder->CreatePHI(size_ty, 2, "segNo");
65    segNo->addIncoming(myThreadId, entryBlock);
66    unsigned last_kernel = kernels.size() - 1;
67    Value * alreadyDone = kernels[last_kernel]->getTerminationSignal(instancePtrs[last_kernel]);
68    iBuilder->CreateCondBr(alreadyDone, exitThreadBlock, segmentWait[0]);
69
70    for (unsigned i = 0; i < kernels.size(); i++) {
71        iBuilder->SetInsertPoint(segmentWait[i]);
72        Value * processedSegmentCount = kernels[i]->getLogicalSegmentNo(instancePtrs[i]);
73        Value * cond = iBuilder->CreateICmpEQ(segNo, processedSegmentCount);
74        iBuilder->CreateCondBr(cond, segmentLoopBody[i], segmentWait[i]);
75
76        iBuilder->SetInsertPoint(segmentLoopBody[i]);
77        kernels[i]->createDoSegmentCall(instancePtrs[i], segmentBlocks);
78        if (i == last_kernel) break;
79        iBuilder->CreateBr(segmentWait[i+1]);
80    }
81   
82    segNo->addIncoming(iBuilder->CreateAdd(segNo, ConstantInt::get(size_ty, threadNum)), segmentLoopBody[last_kernel]);
83    Value * endSignal = kernels[last_kernel]->getTerminationSignal(instancePtrs[last_kernel]);
84    iBuilder->CreateCondBr(endSignal, exitThreadBlock, segmentLoop);
85   
86    iBuilder->SetInsertPoint(exitThreadBlock);
87    Value * nullVal = Constant::getNullValue(voidPtrTy);
88    Function * pthreadExitFunc = m->getFunction("pthread_exit");
89    CallInst * exitThread = iBuilder->CreateCall(pthreadExitFunc, {nullVal});
90    exitThread->setDoesNotReturn();
91    iBuilder->CreateRetVoid();
92
93    return threadFunc;
94}
95
96void generateSegmentParallelPipeline(IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels, std::vector<Value *> instances, Value * fileSize) {
97   
98    unsigned threadNum = codegen::ThreadNum;
99
100    Module * m = iBuilder->getModule();
101
102    Type * const size_ty = iBuilder->getSizeTy();
103    Type * const voidPtrTy = TypeBuilder<void *, false>::get(m->getContext());
104    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
105    Type * const pthreadsTy = ArrayType::get(size_ty, threadNum);
106    AllocaInst * const pthreads = iBuilder->CreateAlloca(pthreadsTy);
107    std::vector<Value *> pthreadsPtrs;
108    for (unsigned i = 0; i < threadNum; i++) {
109        pthreadsPtrs.push_back(iBuilder->CreateGEP(pthreads, {iBuilder->getInt32(0), iBuilder->getInt32(i)}));
110    }
111    Value * nullVal = Constant::getNullValue(voidPtrTy);
112    AllocaInst * const status = iBuilder->CreateAlloca(int8PtrTy);
113
114    std::vector<Type *> structTypes;
115    structTypes.push_back(size_ty);//input size
116    for (unsigned i = 0; i < instances.size(); i++) {
117        structTypes.push_back(instances[i]->getType());
118    }
119    Type * sharedStructType = StructType::get(m->getContext(), structTypes);
120
121    AllocaInst * sharedStruct;
122    sharedStruct = iBuilder->CreateAlloca(sharedStructType);
123    Value * sizePtr = iBuilder->CreateGEP(sharedStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
124    iBuilder->CreateStore(fileSize, sizePtr);
125    for (unsigned i = 0; i < instances.size(); i++) {
126        Value * ptr = iBuilder->CreateGEP(sharedStruct, {iBuilder->getInt32(0), iBuilder->getInt32(i+1)});
127        iBuilder->CreateStore(instances[i], ptr);
128    }
129
130    std::vector<Function *> thread_functions;
131    const auto ip = iBuilder->saveIP();
132    for (unsigned i = 0; i < threadNum; i++) {
133        thread_functions.push_back(generateSegmentParallelPipelineThreadFunction("thread"+std::to_string(i), iBuilder, kernels, sharedStructType, i));
134    }
135    iBuilder->restoreIP(ip);
136
137    Function * pthreadCreateFunc = m->getFunction("pthread_create");
138    Function * pthreadJoinFunc = m->getFunction("pthread_join");
139
140    for (unsigned i = 0; i < threadNum; i++) {
141        iBuilder->CreateCall(pthreadCreateFunc, std::vector<Value *>({pthreadsPtrs[i], nullVal, thread_functions[i], iBuilder->CreateBitCast(sharedStruct, int8PtrTy)}));
142    }
143
144    std::vector<Value *> threadIDs;
145    for (unsigned i = 0; i < threadNum; i++) { 
146        threadIDs.push_back(iBuilder->CreateLoad(pthreadsPtrs[i]));
147    }
148   
149    for (unsigned i = 0; i < threadNum; i++) { 
150        iBuilder->CreateCall(pthreadJoinFunc, std::vector<Value *>({threadIDs[i], status}));
151    }
152
153}
154
155void generatePipelineParallel(IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels, std::vector<Value *> instances) {
156 
157    Module * m = iBuilder->getModule();
158
159    Type * pthreadTy = iBuilder->getSizeTy();     
160    Type * const voidPtrTy = TypeBuilder<void *, false>::get(m->getContext());
161    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
162
163    Type * const pthreadsTy = ArrayType::get(pthreadTy, kernels.size());
164    AllocaInst * const pthreads = iBuilder->CreateAlloca(pthreadsTy);
165    std::vector<Value *> pthreadsPtrs;
166    for (unsigned i = 0; i < kernels.size(); i++) {
167        pthreadsPtrs.push_back(iBuilder->CreateGEP(pthreads, {iBuilder->getInt32(0), iBuilder->getInt32(i)}));
168    }
169    Value * nullVal = Constant::getNullValue(voidPtrTy);
170    AllocaInst * const status = iBuilder->CreateAlloca(int8PtrTy);
171
172    std::vector<Function *> kernel_functions;
173    const auto ip = iBuilder->saveIP();
174    for (unsigned i = 0; i < kernels.size(); i++) {
175        kernel_functions.push_back(kernels[i]->generateThreadFunction("k_"+std::to_string(i)));
176    }
177    iBuilder->restoreIP(ip);
178
179    Function * pthreadCreateFunc = m->getFunction("pthread_create");
180    Function * pthreadJoinFunc = m->getFunction("pthread_join");
181
182    for (unsigned i = 0; i < kernels.size(); i++) {
183        iBuilder->CreateCall(pthreadCreateFunc, std::vector<Value *>({pthreadsPtrs[i], nullVal, kernel_functions[i], iBuilder->CreateBitCast(instances[i], int8PtrTy)}));
184    }
185
186    std::vector<Value *> threadIDs;
187    for (unsigned i = 0; i < kernels.size(); i++) { 
188        threadIDs.push_back(iBuilder->CreateLoad(pthreadsPtrs[i]));
189    }
190   
191    for (unsigned i = 0; i < kernels.size(); i++) { 
192        iBuilder->CreateCall(pthreadJoinFunc, std::vector<Value *>({threadIDs[i], status}));
193    }
194}
195
196
197void generatePipelineLoop(IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels, std::vector<Value *> instances, Value * fileSize) {
198   
199    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
200    Function * main = entryBlock->getParent();
201       
202    const unsigned segmentSize = codegen::SegmentSize;
203    Type * const size_ty = iBuilder->getSizeTy();
204
205    // Create the basic blocks for the loop.
206    BasicBlock * segmentBlock = BasicBlock::Create(iBuilder->getContext(), "segmentLoop", main, 0);
207    BasicBlock * exitBlock = BasicBlock::Create(iBuilder->getContext(), "exitBlock", main, 0);
208    iBuilder->CreateBr(segmentBlock);
209    iBuilder->SetInsertPoint(segmentBlock);
210    Constant * segBlocks = ConstantInt::get(size_ty, segmentSize * iBuilder->getStride() / iBuilder->getBitBlockWidth());
211    for (unsigned i = 0; i < kernels.size(); i++) {
212        kernels[i]->createDoSegmentCall(instances[i], segBlocks);
213    }
214    Value * endSignal = kernels[kernels.size()-1]->getTerminationSignal(instances[kernels.size()-1]);
215    iBuilder->CreateCondBr(endSignal, exitBlock, segmentBlock);
216    iBuilder->SetInsertPoint(exitBlock);
217
218}
Note: See TracBrowser for help on using the repository browser.