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

Last change on this file since 5772 was 5755, checked in by nmedfort, 19 months ago

Bug fixes and simplified MultiBlockKernel? logic

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