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

Last change on this file since 5202 was 5202, checked in by nmedfort, 2 years ago

Initial work on adding types to PabloAST and mutable Var objects.

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 = iBuilder->CreateAlloca(sharedStructType);
122    Value * sizePtr = iBuilder->CreateGEP(sharedStruct, {iBuilder->getInt32(0), iBuilder->getInt32(0)});
123    iBuilder->CreateStore(fileSize, sizePtr);
124    for (unsigned i = 0; i < instances.size(); i++) {
125        Value * ptr = iBuilder->CreateGEP(sharedStruct, {iBuilder->getInt32(0), iBuilder->getInt32(i+1)});
126        iBuilder->CreateStore(instances[i], ptr);
127    }
128
129    std::vector<Function *> thread_functions;
130    const auto ip = iBuilder->saveIP();
131    for (unsigned i = 0; i < threadNum; i++) {
132        thread_functions.push_back(generateSegmentParallelPipelineThreadFunction("thread"+std::to_string(i), iBuilder, kernels, sharedStructType, i));
133    }
134    iBuilder->restoreIP(ip);
135
136    Function * pthreadCreateFunc = m->getFunction("pthread_create");
137    Function * pthreadJoinFunc = m->getFunction("pthread_join");
138
139    for (unsigned i = 0; i < threadNum; i++) {
140        iBuilder->CreateCall(pthreadCreateFunc, std::vector<Value *>({pthreadsPtrs[i], nullVal, thread_functions[i], iBuilder->CreateBitCast(sharedStruct, int8PtrTy)}));
141    }
142
143    std::vector<Value *> threadIDs;
144    for (unsigned i = 0; i < threadNum; i++) { 
145        threadIDs.push_back(iBuilder->CreateLoad(pthreadsPtrs[i]));
146    }
147   
148    for (unsigned i = 0; i < threadNum; i++) { 
149        iBuilder->CreateCall(pthreadJoinFunc, std::vector<Value *>({threadIDs[i], status}));
150    }
151
152}
153
154void generatePipelineParallel(IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels, std::vector<Value *> instances) {
155 
156    Module * m = iBuilder->getModule();
157
158    Type * pthreadTy = iBuilder->getSizeTy();     
159    Type * const voidPtrTy = TypeBuilder<void *, false>::get(m->getContext());
160    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
161
162    Type * const pthreadsTy = ArrayType::get(pthreadTy, kernels.size());
163    AllocaInst * const pthreads = iBuilder->CreateAlloca(pthreadsTy);
164    std::vector<Value *> pthreadsPtrs;
165    for (unsigned i = 0; i < kernels.size(); i++) {
166        pthreadsPtrs.push_back(iBuilder->CreateGEP(pthreads, {iBuilder->getInt32(0), iBuilder->getInt32(i)}));
167    }
168    Value * nullVal = Constant::getNullValue(voidPtrTy);
169    AllocaInst * const status = iBuilder->CreateAlloca(int8PtrTy);
170
171    std::vector<Function *> kernel_functions;
172    const auto ip = iBuilder->saveIP();
173    for (unsigned i = 0; i < kernels.size(); i++) {
174        kernel_functions.push_back(kernels[i]->generateThreadFunction("k_"+std::to_string(i)));
175    }
176    iBuilder->restoreIP(ip);
177
178    Function * pthreadCreateFunc = m->getFunction("pthread_create");
179    Function * pthreadJoinFunc = m->getFunction("pthread_join");
180
181    for (unsigned i = 0; i < kernels.size(); i++) {
182        iBuilder->CreateCall(pthreadCreateFunc, std::vector<Value *>({pthreadsPtrs[i], nullVal, kernel_functions[i], iBuilder->CreateBitCast(instances[i], int8PtrTy)}));
183    }
184
185    std::vector<Value *> threadIDs;
186    for (unsigned i = 0; i < kernels.size(); i++) { 
187        threadIDs.push_back(iBuilder->CreateLoad(pthreadsPtrs[i]));
188    }
189   
190    for (unsigned i = 0; i < kernels.size(); i++) { 
191        iBuilder->CreateCall(pthreadJoinFunc, std::vector<Value *>({threadIDs[i], status}));
192    }
193}
194
195
196void generatePipelineLoop(IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels, std::vector<Value *> instances, Value * fileSize) {
197   
198    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
199    Function * main = entryBlock->getParent();
200       
201    const unsigned segmentSize = codegen::SegmentSize;
202    Type * const size_ty = iBuilder->getSizeTy();
203
204    // Create the basic blocks for the loop.
205    BasicBlock * segmentBlock = BasicBlock::Create(iBuilder->getContext(), "segmentLoop", main, 0);
206    BasicBlock * exitBlock = BasicBlock::Create(iBuilder->getContext(), "exitBlock", main, 0);
207    iBuilder->CreateBr(segmentBlock);
208    iBuilder->SetInsertPoint(segmentBlock);
209    Constant * segBlocks = ConstantInt::get(size_ty, segmentSize * iBuilder->getStride() / iBuilder->getBitBlockWidth());
210    for (unsigned i = 0; i < kernels.size(); i++) {
211        kernels[i]->createDoSegmentCall(instances[i], segBlocks);
212    }
213    Value * endSignal = kernels[kernels.size()-1]->getTerminationSignal(instances[kernels.size()-1]);
214    iBuilder->CreateCondBr(endSignal, exitBlock, segmentBlock);
215    iBuilder->SetInsertPoint(exitBlock);
216
217}
Note: See TracBrowser for help on using the repository browser.