Ignore:
Timestamp:
Apr 19, 2017, 1:41:55 PM (2 years ago)
Author:
cameron
Message:

Speed-up file output using new temporary files; unlinking old files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/kernels/stdout_kernel.cpp

    r5402 r5415  
    7474    BasicBlock * setTerminationOnFailure = CreateBasicBlock("setTerminationOnFailure");
    7575    BasicBlock * fileSinkInitExit = CreateBasicBlock("fileSinkInitExit");
    76     Value * handle = iBuilder->CreateFOpenCall(getScalarField("fileName"), iBuilder->CreateGlobalStringPtr("w"));
    77     setScalarField("IOstreamPtr", handle);
    78     Value * failure = iBuilder->CreateICmpEQ(iBuilder->CreatePtrToInt(handle, iBuilder->getSizeTy()), iBuilder->getSize(0));
     76    Value * fileName = getScalarField("fileName");
     77    Value * fileNameLength = iBuilder->CreateStrlenCall(fileName);
     78    // Make a temporary file name template with the characters "XXXXXX" appended
     79    // as required by mkstemp.
     80    Constant * suffixPlusNullLength = iBuilder->getSize(7);
     81    Value * tmpFileNamePtr = iBuilder->CreatePointerCast(iBuilder->CreateMalloc(iBuilder->CreateAdd(fileNameLength, suffixPlusNullLength)), iBuilder->getInt8PtrTy());
     82    setScalarField("tmpFileName", tmpFileNamePtr);
     83    iBuilder->CreateMemCpy(tmpFileNamePtr, fileName, fileNameLength, 1);
     84#ifdef BACKUP_OLDFILE
     85    iBuilder->CreateMemCpy(iBuilder->CreateGEP(tmpFileNamePtr, fileNameLength), iBuilder->CreateGlobalStringPtr(".saved"), suffixPlusNullLength, 1);
     86    iBuilder->CreateRenameCall(fileName, tmpFileNamePtr);
     87#else
     88    iBuilder->CreateUnlinkCall(fileName);
     89#endif
     90    iBuilder->CreateMemCpy(iBuilder->CreateGEP(tmpFileNamePtr, fileNameLength), iBuilder->CreateGlobalStringPtr("XXXXXX"), suffixPlusNullLength, 1);
     91    Value * fileDes = iBuilder->CreateMkstempCall(tmpFileNamePtr);
     92    setScalarField("fileDes", fileDes);
     93    Value * failure = iBuilder->CreateICmpEQ(fileDes, iBuilder->getInt32(-1));
    7994    iBuilder->CreateCondBr(failure, setTerminationOnFailure, fileSinkInitExit);
    8095    iBuilder->SetInsertPoint(setTerminationOnFailure);
     
    93108    Constant * itemBytes = iBuilder->getSize(mCodeUnitWidth/8);
    94109
    95     Value * IOstreamPtr = getScalarField("IOstreamPtr");
     110    Value * fileDes = getScalarField("fileDes");
    96111    Value * available = getAvailableItemCount("codeUnitBuffer");
    97112    Value * processed = getProcessedItemCount("codeUnitBuffer");
     
    110125    Value * bytePtr = iBuilder->CreatePointerCast(getInputStreamBlockPtr("codeUnitBuffer", iBuilder->getInt32(0)), i8PtrTy);
    111126    bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
    112 
    113     iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
    114 
     127    iBuilder->CreateWriteCall(fileDes, bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes));
    115128   
    116129    processed = iBuilder->CreateAdd(processed, itemsToDo);
     
    129142        bytePtr = iBuilder->CreateGEP(bytePtr, byteOffset);
    130143        itemsToDo = iBuilder->CreateSub(available, processed);
    131         iBuilder->CreateFWriteCall(bytePtr, itemsToDo, itemBytes, IOstreamPtr);
     144        iBuilder->CreateWriteCall(fileDes, bytePtr, iBuilder->CreateMul(itemsToDo, itemBytes));
    132145        processed = iBuilder->CreateAdd(processed, itemsToDo);
    133146        setProcessedItemCount("codeUnitBuffer", available);
     
    138151
    139152    iBuilder->SetInsertPoint(closeFile);
    140     iBuilder->CreateFCloseCall(IOstreamPtr);
     153    iBuilder->CreateCloseCall(fileDes);
     154    Value * newFileNamePtr = getScalarField("fileName");
     155    Value * tmpFileNamePtr = getScalarField("tmpFileName");
     156    iBuilder->CreateRenameCall(tmpFileNamePtr, newFileNamePtr);
     157    iBuilder->CreateFree(tmpFileNamePtr);
     158   
    141159    iBuilder->CreateBr(fileOutExit);
    142160
     
    146164FileSink::FileSink(IDISA::IDISA_Builder * iBuilder, unsigned codeUnitWidth)
    147165: SegmentOrientedKernel(iBuilder, "filesink", {Binding{iBuilder->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {},
    148                 {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getFILEptrTy(), "IOstreamPtr"}})
     166                {Binding{iBuilder->getInt8PtrTy(), "fileName"}}, {}, {Binding{iBuilder->getInt8PtrTy(), "tmpFileName"}, Binding{iBuilder->getInt32Ty(), "fileDes"}})
    149167, mCodeUnitWidth(codeUnitWidth) {
    150168}
Note: See TracChangeset for help on using the changeset viewer.