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

Last change on this file since 5822 was 5793, checked in by nmedfort, 17 months ago

Bug fix for pipeline: it was terminating too early when there was insufficient output space to process all of the input for a kernel.

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