Changeset 5456


Ignore:
Timestamp:
May 19, 2017, 9:39:14 AM (2 years ago)
Author:
cameron
Message:

Add kernel CPU cycle counting for segment-parallel-pipeline mode.

Location:
icGREP/icgrep-devel/icgrep
Files:
3 edited

Legend:

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

    r5454 r5456  
    3434const std::string Kernel::BUFFER_PTR_SUFFIX = "_bufferPtr";
    3535const std::string Kernel::CONSUMER_SUFFIX = "_consumerLocks";
     36const std::string Kernel::CYCLECOUNT_SCALAR = "CPUcycles";
    3637
    3738unsigned Kernel::addScalar(Type * const type, const std::string & name) {
     
    181182    }
    182183
     184    // We compile in a 64-bit CPU cycle counter into every kernel.   It will remain unused
     185    // in normal execution, but when codegen::EnableCycleCounter is specified, pipelines
     186    // will be able to add instrumentation to cached modules without recompilation.
     187    addScalar(idb->getInt64Ty(), CYCLECOUNT_SCALAR);
     188   
    183189    mKernelStateType = StructType::create(idb->getContext(), mKernelFields, getName());
    184190}
  • icGREP/icgrep-devel/icgrep/kernels/kernel.h

    r5454 r5456  
    4444    static const std::string BUFFER_PTR_SUFFIX;
    4545    static const std::string CONSUMER_SUFFIX;
     46public:
     47    static const std::string CYCLECOUNT_SCALAR;
    4648
    4749public:
  • icGREP/icgrep-devel/icgrep/toolchain/pipeline.cpp

    r5446 r5456  
    9494    StreamSetBufferMap<Value *> consumedPos;
    9595
     96    Value * cycleCountStart = nullptr;
     97    Value * cycleCountEnd = nullptr;
     98    if (codegen::EnableCycleCounter) {
     99        cycleCountStart = iBuilder->CreateReadCycleCounter();
     100    }
     101
    96102    for (unsigned k = 0; k < n; ++k) {
    97103
     
    163169            }
    164170        }
     171        if (codegen::EnableCycleCounter) {
     172            cycleCountEnd = iBuilder->CreateReadCycleCounter();
     173            //Value * counterPtr = iBuilder->CreateGEP(mCycleCounts, {iBuilder->getInt32(0), iBuilder->getInt32(k)});
     174            Value * counterPtr = iBuilder->getScalarFieldPtr(Kernel::CYCLECOUNT_SCALAR);
     175            iBuilder->CreateStore(iBuilder->CreateAdd(iBuilder->CreateLoad(counterPtr), iBuilder->CreateSub(cycleCountEnd, cycleCountStart)), counterPtr);
     176            cycleCountStart = cycleCountEnd;
     177        }
     178       
    165179        iBuilder->releaseLogicalSegmentNo(nextSegNo);
    166180    }
     
    248262        iBuilder->CreatePThreadJoinCall(threadId, status);
    249263    }
     264   
     265    if (codegen::EnableCycleCounter) {
     266        for (unsigned k = 0; k < kernels.size(); k++) {
     267            auto & kernel = kernels[k];
     268            iBuilder->setKernel(kernel);
     269            const auto & inputs = kernel->getStreamInputs();
     270            const auto & outputs = kernel->getStreamOutputs();
     271            Value * items = nullptr;
     272            if (inputs.empty()) {
     273                items = iBuilder->getProducedItemCount(outputs[0].name);
     274            } else {
     275                items = iBuilder->getProcessedItemCount(inputs[0].name);
     276            }
     277            Value * fItems = iBuilder->CreateUIToFP(items, iBuilder->getDoubleTy());
     278            Value * cycles = iBuilder->CreateLoad(iBuilder->getScalarFieldPtr(Kernel::CYCLECOUNT_SCALAR));
     279            Value * fCycles = iBuilder->CreateUIToFP(cycles, iBuilder->getDoubleTy());
     280            std::string formatString = kernel->getName() + ": %7.2e items processed; %7.2e CPU cycles,  %6.2f cycles per item.\n";
     281            Value * stringPtr = iBuilder->CreatePointerCast(iBuilder->GetString(formatString), iBuilder->getInt8PtrTy());
     282            iBuilder->CreateCall(iBuilder->GetDprintf(), {iBuilder->getInt32(2), stringPtr, fItems, fCycles, iBuilder->CreateFDiv(fCycles, fItems)});
     283        }
     284    }
     285   
    250286}
    251287
     
    459495    BasicBlock * entryBlock = iBuilder->GetInsertBlock();
    460496    Function * main = entryBlock->getParent();
    461     Value * mCycleCounts = nullptr;
    462     if (codegen::EnableCycleCounter) {
    463         ArrayType * cycleCountArray = ArrayType::get(iBuilder->getInt64Ty(), kernels.size());
    464         mCycleCounts = iBuilder->CreateAlloca(ArrayType::get(iBuilder->getInt64Ty(), kernels.size()));
    465         iBuilder->CreateStore(Constant::getNullValue(cycleCountArray), mCycleCounts);
    466     }
    467497
    468498    // Create the basic blocks for the loop.
     
    524554        if (codegen::EnableCycleCounter) {
    525555            cycleCountEnd = iBuilder->CreateReadCycleCounter();
    526             Value * counterPtr = iBuilder->CreateGEP(mCycleCounts, {iBuilder->getInt32(0), iBuilder->getInt32(k)});
     556            //Value * counterPtr = iBuilder->CreateGEP(mCycleCounts, {iBuilder->getInt32(0), iBuilder->getInt32(k)});
     557            Value * counterPtr = iBuilder->getScalarFieldPtr(Kernel::CYCLECOUNT_SCALAR);
    527558            iBuilder->CreateStore(iBuilder->CreateAdd(iBuilder->CreateLoad(counterPtr), iBuilder->CreateSub(cycleCountEnd, cycleCountStart)), counterPtr);
    528559            cycleCountStart = cycleCountEnd;
    529560        }
    530 
    531561
    532562        Value * const segNo = iBuilder->acquireLogicalSegmentNo();
     
    563593            }
    564594            Value * fItems = iBuilder->CreateUIToFP(items, iBuilder->getDoubleTy());
    565             Value * cycles = iBuilder->CreateLoad(iBuilder->CreateGEP(mCycleCounts, {iBuilder->getInt32(0), iBuilder->getInt32(k)}));
     595            Value * cycles = iBuilder->CreateLoad(iBuilder->getScalarFieldPtr(Kernel::CYCLECOUNT_SCALAR));
    566596            Value * fCycles = iBuilder->CreateUIToFP(cycles, iBuilder->getDoubleTy());
    567597            std::string formatString = kernel->getName() + ": %7.2e items processed; %7.2e CPU cycles,  %6.2f cycles per item.\n";
Note: See TracChangeset for help on using the changeset viewer.