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

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

Modified memory alignment mechanism for GetPropertyValueGrepString? + misc. changes.

File size: 9.6 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#include "pipeline.h"
7#include <toolchain.h>
8#include <IDISA/idisa_builder.h>
9#include <kernels/interface.h>
10#include <kernels/kernel.h>
11#include <kernels/s2p_kernel.h>
12#include <iostream>
13
14using namespace kernel;
15
16Function * generateSegmentParallelPipelineThreadFunction(std::string name, IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels, Type * sharedStructType, int id) {
17
18    Module * m = iBuilder->getModule();
19    Type * const size_ty = iBuilder->getSizeTy();
20    Type * const voidTy = iBuilder->getVoidTy();
21    Type * const voidPtrTy = iBuilder->getVoidPtrTy();
22    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
23
24    Function * const threadFunc = cast<Function>(m->getOrInsertFunction(name, voidTy, int8PtrTy, nullptr));
25    threadFunc->setCallingConv(CallingConv::C);
26    Function::arg_iterator args = threadFunc->arg_begin();
27
28    Value * const input = &*(args++);
29    input->setName("input");
30
31    unsigned threadNum = codegen::ThreadNum;
32
33     // Create the basic blocks for the thread function.
34    BasicBlock * entryBlock = BasicBlock::Create(iBuilder->getContext(), "entry", threadFunc, 0);
35    BasicBlock * segmentLoop = BasicBlock::Create(iBuilder->getContext(), "segmentLoop", threadFunc, 0);
36    BasicBlock * exitThreadBlock = BasicBlock::Create(iBuilder->getContext(), "exitThread", threadFunc, 0);
37    std::vector<BasicBlock *> segmentWait;
38    std::vector<BasicBlock *> segmentLoopBody;
39    for (unsigned i = 0; i < kernels.size(); i++) {
40        segmentWait.push_back(BasicBlock::Create(iBuilder->getContext(), "segmentWait"+std::to_string(i), threadFunc, 0));
41        segmentLoopBody.push_back(BasicBlock::Create(iBuilder->getContext(), "segmentWait"+std::to_string(i), threadFunc, 0));
42    }
43
44    iBuilder->SetInsertPoint(entryBlock);
45    Value * sharedStruct = iBuilder->CreateBitCast(input, PointerType::get(sharedStructType, 0));
46    Constant * myThreadId = ConstantInt::get(size_ty, id);
47    std::vector<Value *> instancePtrs;
48    for (unsigned i = 0; i < kernels.size(); i++) {
49        Value * ptr = iBuilder->CreateGEP(sharedStruct, {iBuilder->getInt32(0), iBuilder->getInt32(i)});
50        instancePtrs.push_back(iBuilder->CreateLoad(ptr));
51    }
52   
53    // Some important constant values.
54    int segmentSize = codegen::SegmentSize;
55    Constant * segmentBlocks = ConstantInt::get(size_ty, segmentSize);
56    iBuilder->CreateBr(segmentLoop);
57
58    iBuilder->SetInsertPoint(segmentLoop);
59    PHINode * segNo = iBuilder->CreatePHI(size_ty, 2, "segNo");
60    segNo->addIncoming(myThreadId, entryBlock);
61    unsigned last_kernel = kernels.size() - 1;
62    Value * alreadyDone = kernels[last_kernel]->getTerminationSignal(instancePtrs[last_kernel]);
63    iBuilder->CreateCondBr(alreadyDone, exitThreadBlock, segmentWait[0]);
64
65    for (unsigned i = 0; i < kernels.size(); i++) {
66        iBuilder->SetInsertPoint(segmentWait[i]);
67        Value * processedSegmentCount = kernels[i]->acquireLogicalSegmentNo(instancePtrs[i]);
68        Value * cond = iBuilder->CreateICmpEQ(segNo, processedSegmentCount);
69        iBuilder->CreateCondBr(cond, segmentLoopBody[i], segmentWait[i]);
70
71        iBuilder->SetInsertPoint(segmentLoopBody[i]);
72        kernels[i]->createDoSegmentCall(instancePtrs[i], segmentBlocks);
73        // Must be the last action, for synchronization.
74        kernels[i]->releaseLogicalSegmentNo(instancePtrs[i], iBuilder->CreateAdd(processedSegmentCount, iBuilder->getSize(1)));
75        if (i == last_kernel) break;
76        iBuilder->CreateBr(segmentWait[i+1]);
77    }
78   
79    segNo->addIncoming(iBuilder->CreateAdd(segNo, ConstantInt::get(size_ty, threadNum)), segmentLoopBody[last_kernel]);
80    Value * endSignal = kernels[last_kernel]->getTerminationSignal(instancePtrs[last_kernel]);
81    iBuilder->CreateCondBr(endSignal, exitThreadBlock, segmentLoop);
82   
83    iBuilder->SetInsertPoint(exitThreadBlock);
84    Value * nullVal = Constant::getNullValue(voidPtrTy);
85    Function * pthreadExitFunc = m->getFunction("pthread_exit");
86    CallInst * exitThread = iBuilder->CreateCall(pthreadExitFunc, {nullVal});
87    exitThread->setDoesNotReturn();
88    iBuilder->CreateRetVoid();
89
90    return threadFunc;
91}
92
93void generateSegmentParallelPipeline(IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels) {
94   
95    unsigned threadNum = codegen::ThreadNum;
96
97    Module * m = iBuilder->getModule();
98
99    Type * const size_ty = iBuilder->getSizeTy();
100    Type * const voidPtrTy = iBuilder->getVoidPtrTy();
101    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
102
103    for (auto k : kernels) k->createInstance();
104
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    for (unsigned i = 0; i < kernels.size(); i++) {
116        structTypes.push_back(kernels[i]->getInstance()->getType());
117    }
118    Type * sharedStructType = StructType::get(m->getContext(), structTypes);
119
120    AllocaInst * sharedStruct = iBuilder->CreateAlloca(sharedStructType);
121    for (unsigned i = 0; i < kernels.size(); i++) {
122        Value * ptr = iBuilder->CreateGEP(sharedStruct, {iBuilder->getInt32(0), iBuilder->getInt32(i)});
123        iBuilder->CreateStore(kernels[i]->getInstance(), ptr);
124    }
125
126    std::vector<Function *> thread_functions;
127    const auto ip = iBuilder->saveIP();
128    for (unsigned i = 0; i < threadNum; i++) {
129        thread_functions.push_back(generateSegmentParallelPipelineThreadFunction("thread"+std::to_string(i), iBuilder, kernels, sharedStructType, i));
130    }
131    iBuilder->restoreIP(ip);
132
133    Function * pthreadCreateFunc = m->getFunction("pthread_create");
134    Function * pthreadJoinFunc = m->getFunction("pthread_join");
135
136    for (unsigned i = 0; i < threadNum; i++) {
137        iBuilder->CreateCall(pthreadCreateFunc, std::vector<Value *>({pthreadsPtrs[i], nullVal, thread_functions[i], iBuilder->CreateBitCast(sharedStruct, int8PtrTy)}));
138    }
139
140    std::vector<Value *> threadIDs;
141    for (unsigned i = 0; i < threadNum; i++) { 
142        threadIDs.push_back(iBuilder->CreateLoad(pthreadsPtrs[i]));
143    }
144   
145    for (unsigned i = 0; i < threadNum; i++) { 
146        iBuilder->CreateCall(pthreadJoinFunc, std::vector<Value *>({threadIDs[i], status}));
147    }
148
149}
150
151void generatePipelineParallel(IDISA::IDISA_Builder * iBuilder, std::vector<KernelBuilder *> kernels) {
152 
153    Module * m = iBuilder->getModule();
154
155    Type * pthreadTy = iBuilder->getSizeTy();     
156    Type * const voidPtrTy = iBuilder->getVoidPtrTy();
157    Type * const int8PtrTy = iBuilder->getInt8PtrTy();
158
159    Type * const pthreadsTy = ArrayType::get(pthreadTy, kernels.size());
160
161    for (auto k : kernels) k->createInstance();
162
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(kernels[i]->getInstance(), 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) {
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    for (auto k : kernels) k->createInstance();
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(kernels[i]->getInstance(), segBlocks);
213        Value * segNo = kernels[i]->acquireLogicalSegmentNo(kernels[i]->getInstance());
214        kernels[i]->releaseLogicalSegmentNo(kernels[i]->getInstance(), iBuilder->CreateAdd(segNo, iBuilder->getSize(1)));
215    }
216    Value * endSignal = kernels.back()->getTerminationSignal(kernels.back()->getInstance());
217    iBuilder->CreateCondBr(endSignal, exitBlock, segmentBlock);
218    iBuilder->SetInsertPoint(exitBlock);
219
220}
Note: See TracBrowser for help on using the repository browser.