source: icGREP/icgrep-devel/icgrep/kernels/stdout_kernel.cpp @ 5718

Last change on this file since 5718 was 5706, checked in by nmedfort, 21 months ago

First stage of MultiBlockKernel? and pipeline restructuring

File size: 4.9 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 "stdout_kernel.h"
6#include <llvm/IR/Module.h>
7#include <kernels/kernel_builder.h>
8#include <kernels/streamset.h>
9
10namespace llvm { class Type; }
11
12using namespace llvm;
13using namespace parabix;
14
15namespace kernel {
16
17void StdOutKernel::generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> & iBuilder, llvm::Value * const /* numOfStrides */) {
18    Value * codeUnitBuffer = iBuilder->getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0));
19    codeUnitBuffer = iBuilder->CreatePointerCast(codeUnitBuffer, iBuilder->getInt8PtrTy());
20    Value * bytesToDo = mAvailableItemCount[0];
21    if (LLVM_UNLIKELY(mCodeUnitWidth > 8)) {
22        bytesToDo = iBuilder->CreateMul(bytesToDo, iBuilder->getSize(mCodeUnitWidth / 8));
23    } else if (LLVM_UNLIKELY(mCodeUnitWidth < 8)) {
24        bytesToDo = iBuilder->CreateUDiv(bytesToDo, iBuilder->getSize(8 / mCodeUnitWidth));
25    }
26    iBuilder->CreateWriteCall(iBuilder->getInt32(1), codeUnitBuffer, bytesToDo);
27}
28
29StdOutKernel::StdOutKernel(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, unsigned codeUnitWidth)
30: MultiBlockKernel("stdout", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {}, {}, {}, {})
31, mCodeUnitWidth(codeUnitWidth) {
32    setNoTerminateAttribute(true);
33    // setKernelStride(getpagesize());
34}
35
36void FileSink::generateInitializeMethod(const std::unique_ptr<kernel::KernelBuilder> & iBuilder) {
37    BasicBlock * setTerminationOnFailure = iBuilder->CreateBasicBlock("setTerminationOnFailure");
38    BasicBlock * fileSinkInitExit = iBuilder->CreateBasicBlock("fileSinkInitExit");
39    Value * fileName = iBuilder->getScalarField("fileName");
40    Value * fileNameLength = iBuilder->CreateStrlenCall(fileName);
41    // Make a temporary file name template with the characters "XXXXXX" appended
42    // as required by mkstemp.
43    Constant * suffixPlusNullLength = iBuilder->getSize(7);
44    Value * tmpFileNamePtr = iBuilder->CreatePointerCast(iBuilder->CreateMalloc(iBuilder->CreateAdd(fileNameLength, suffixPlusNullLength)), iBuilder->getInt8PtrTy());
45    iBuilder->setScalarField("tmpFileName", tmpFileNamePtr);
46    iBuilder->CreateMemCpy(tmpFileNamePtr, fileName, fileNameLength, 1);
47#ifdef BACKUP_OLDFILE
48    iBuilder->CreateMemCpy(iBuilder->CreateGEP(tmpFileNamePtr, fileNameLength), iBuilder->GetString(".saved"), suffixPlusNullLength, 1);
49    iBuilder->CreateRenameCall(fileName, tmpFileNamePtr);
50#else
51    iBuilder->CreateUnlinkCall(fileName);
52#endif
53    iBuilder->CreateMemCpy(iBuilder->CreateGEP(tmpFileNamePtr, fileNameLength), iBuilder->GetString("XXXXXX"), suffixPlusNullLength, 1);
54    Value * fileDes = iBuilder->CreateMkstempCall(tmpFileNamePtr);
55    iBuilder->setScalarField("fileDes", fileDes);
56    Value * failure = iBuilder->CreateICmpEQ(fileDes, iBuilder->getInt32(-1));
57    iBuilder->CreateCondBr(failure, setTerminationOnFailure, fileSinkInitExit);
58    iBuilder->SetInsertPoint(setTerminationOnFailure);
59    iBuilder->setTerminationSignal();
60    iBuilder->CreateBr(fileSinkInitExit);
61    iBuilder->SetInsertPoint(fileSinkInitExit);
62}
63
64void FileSink::generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> & iBuilder, Value * const /* numOfStrides */) {
65    BasicBlock * const closeFile = iBuilder->CreateBasicBlock("closeFile");
66    BasicBlock * const fileOutExit = iBuilder->CreateBasicBlock("fileOutExit");
67
68    Value * const fileDes = iBuilder->getScalarField("fileDes");
69    Value * const codeUnitBuffer = iBuilder->CreatePointerCast(getStreamSetInputBufferPtr(0), iBuilder->getInt8PtrTy());
70    Value * bytesToDo = mAvailableItemCount[0];
71    if (LLVM_UNLIKELY(mCodeUnitWidth > 8)) {
72        bytesToDo = iBuilder->CreateMul(bytesToDo, iBuilder->getSize(mCodeUnitWidth / 8));
73    } else if (LLVM_UNLIKELY(mCodeUnitWidth < 8)) {
74        bytesToDo = iBuilder->CreateUDiv(bytesToDo, iBuilder->getSize(8 / mCodeUnitWidth));
75    }   
76    iBuilder->CreateWriteCall(fileDes, codeUnitBuffer, bytesToDo);
77    iBuilder->CreateUnlikelyCondBr(mIsFinal, closeFile, fileOutExit);
78
79    iBuilder->SetInsertPoint(closeFile);   
80    iBuilder->CreateCloseCall(fileDes);
81    Value * newFileNamePtr = iBuilder->getScalarField("fileName");
82    Value * tmpFileNamePtr = iBuilder->getScalarField("tmpFileName");
83    iBuilder->CreateRenameCall(tmpFileNamePtr, newFileNamePtr);
84    iBuilder->CreateFree(tmpFileNamePtr);   
85    iBuilder->CreateBr(fileOutExit);
86   
87    iBuilder->SetInsertPoint(fileOutExit);
88}
89
90FileSink::FileSink(const std::unique_ptr<kernel::KernelBuilder> & iBuilder, unsigned codeUnitWidth)
91: MultiBlockKernel("filesink" + std::to_string(codeUnitWidth),
92{Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}},
93{},
94{Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getInt8PtrTy(), "tmpFileName"}, Binding{iBuilder->getInt32Ty(), "fileDes"}})
95, mCodeUnitWidth(codeUnitWidth) {
96    // setKernelStride(getpagesize());
97}
98
99}
100
101
102
103
Note: See TracBrowser for help on using the repository browser.