From 54cb951f710cfdad7c3e469dee0b572503f00aa6 Mon Sep 17 00:00:00 2001 From: Pavel Krajcevski Date: Fri, 21 Sep 2012 18:43:35 -0400 Subject: [PATCH] Add command line options to invoke worker queue. --- CLTool/src/clunix.cpp | 14 ++++++++++++++ Core/src/StopWatch.h | 5 +++++ Core/src/TexComp.cpp | 4 ++++ Core/src/WorkerQueue.cpp | 16 ++++++++++++++-- Core/src/WorkerQueue.h | 9 ++++++++- 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/CLTool/src/clunix.cpp b/CLTool/src/clunix.cpp index 2328ded..b4ae4f1 100644 --- a/CLTool/src/clunix.cpp +++ b/CLTool/src/clunix.cpp @@ -18,6 +18,7 @@ int main(int argc, char **argv) { exit(1); } + int numJobs = 0; int quality = 50; int numThreads = 1; int numCompressions = 1; @@ -69,6 +70,18 @@ int main(int argc, char **argv) { knowArg = true; } + if(strcmp(argv[fileArg], "-j") == 0) { + fileArg++; + + if(fileArg == argc || (numJobs = atoi(argv[fileArg])) < 0) { + PrintUsage(); + exit(1); + } + + fileArg++; + knowArg = true; + } + } while(knowArg); if(fileArg == argc) { @@ -88,6 +101,7 @@ int main(int argc, char **argv) { settings.iNumThreads = numThreads; settings.iQuality = quality; settings.iNumCompressions = numCompressions; + settings.iJobSize = numJobs; CompressedImage *ci = img->Compress(settings); if(NULL == ci) { diff --git a/Core/src/StopWatch.h b/Core/src/StopWatch.h index 7c65d25..d3ab97e 100755 --- a/Core/src/StopWatch.h +++ b/Core/src/StopWatch.h @@ -1,3 +1,6 @@ +#ifndef __TEXCOMP_STOP_WATCH_H__ +#define __TEXCOMP_STOP_WATCH_H__ + //-------------------------------------------------------------------------------------- // Copyright 2011 Intel Corporation // All Rights Reserved @@ -41,3 +44,5 @@ public: private: StopWatchImpl *impl; }; + +#endif // __TEXCOMP_STOP_WATCH_H__ diff --git a/Core/src/TexComp.cpp b/Core/src/TexComp.cpp index ce79d3c..9f7f43a 100644 --- a/Core/src/TexComp.cpp +++ b/Core/src/TexComp.cpp @@ -135,6 +135,7 @@ static double CompressImageWithWorkerQueue( unsigned char *outBuf ) { WorkerQueue wq ( + settings.iNumCompressions, settings.iNumThreads, settings.iJobSize, imgData, @@ -144,6 +145,9 @@ static double CompressImageWithWorkerQueue( ); wq.Run(); + + return wq.GetStopWatch().TimeInMilliseconds() / + double(settings.iNumCompressions); } bool CompressImageData( diff --git a/Core/src/WorkerQueue.cpp b/Core/src/WorkerQueue.cpp index cdb22a7..ae7ae6a 100644 --- a/Core/src/WorkerQueue.cpp +++ b/Core/src/WorkerQueue.cpp @@ -59,6 +59,7 @@ void WorkerThread::operator()() { } WorkerQueue::WorkerQueue( + uint32 numCompressions, uint32 numThreads, uint32 jobSize, const uint8 *inBuf, @@ -66,7 +67,9 @@ WorkerQueue::WorkerQueue( CompressionFunc func, uint8 *outBuf ) - : m_NumThreads(numThreads) + : m_NumCompressions(0) + , m_TotalNumCompressions(max(uint32(1), numCompressions)) + , m_NumThreads(numThreads) , m_ActiveThreads(0) , m_JobSize(max(uint32(1), jobSize)) , m_InBufSz(inBufSz) @@ -74,7 +77,7 @@ WorkerQueue::WorkerQueue( , m_OutBuf(outBuf) , m_CompressionFunc(func) { - clamp(m_NumThreads, uint32(1), kMaxNumWorkerThreads); + clamp(m_NumThreads, uint32(1), uint32(kMaxNumWorkerThreads)); #ifndef NDEBUG if(m_InBufSz % 64) { @@ -93,11 +96,16 @@ void WorkerQueue::Run() { m_ActiveThreads++; } + m_StopWatch.Reset(); + m_StopWatch.Start(); + // Wait for them to finish... while(m_ActiveThreads > 0) { m_CV.wait(lock); } + m_StopWatch.Stop(); + // Join them all together.. for(int i = 0; i < m_NumThreads; i++) { m_ThreadHandles[i]->join(); @@ -141,6 +149,10 @@ WorkerThread::EAction WorkerQueue::AcceptThreadData(uint32 threadIdx) { // Make sure the next block is updated. m_NextBlock += blocksProcessed; + if(m_NextBlock == totalBlocks && ++m_NumCompressions < m_TotalNumCompressions) { + m_NextBlock = 0; + } + return WorkerThread::eAction_DoWork; } diff --git a/Core/src/WorkerQueue.h b/Core/src/WorkerQueue.h index 648719d..3280ad1 100644 --- a/Core/src/WorkerQueue.h +++ b/Core/src/WorkerQueue.h @@ -12,6 +12,7 @@ namespace boost { #include "TexComp.h" #include #include +#include "StopWatch.h" struct WorkerThread { friend class WorkerQueue; @@ -36,6 +37,7 @@ class WorkerQueue { friend class WorkerThread; public: WorkerQueue( + uint32 numCompressions, uint32 numThreads, uint32 jobSize, const uint8 *inBuf, @@ -48,9 +50,12 @@ class WorkerQueue { // Runs the workers void Run(); + const StopWatch &GetStopWatch() const { return m_StopWatch; } private: + uint32 m_NumCompressions; + const uint32 m_TotalNumCompressions; uint32 m_NumThreads; uint32 m_ActiveThreads; uint32 m_JobSize; @@ -62,7 +67,7 @@ class WorkerQueue { boost::mutex m_Mutex; uint32 m_NextBlock; - static const uint32 kMaxNumWorkerThreads = 256; + static const int kMaxNumWorkerThreads = 256; uint32 m_Offsets[kMaxNumWorkerThreads]; uint32 m_NumBlocks[kMaxNumWorkerThreads]; @@ -75,6 +80,8 @@ class WorkerQueue { const CompressionFunc m_CompressionFunc; CompressionFunc GetCompressionFunc() const { return m_CompressionFunc; } + StopWatch m_StopWatch; + WorkerThread::EAction AcceptThreadData(uint32 threadIdx); void NotifyWorkerFinished(); };