2012-08-29 18:43:37 +00:00
|
|
|
#include "ThreadGroup.h"
|
2012-08-31 18:58:51 +00:00
|
|
|
#include "BC7Compressor.h"
|
2012-08-29 18:43:37 +00:00
|
|
|
|
2012-08-29 21:57:04 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-08-29 18:43:37 +00:00
|
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
#include <boost/thread/barrier.hpp>
|
2012-08-31 21:33:54 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
#include <boost/thread/condition_variable.hpp>
|
2012-08-29 18:43:37 +00:00
|
|
|
|
|
|
|
CmpThread::CmpThread()
|
2012-08-31 21:33:54 +00:00
|
|
|
: m_ParentCounter(NULL)
|
|
|
|
, m_ParentCounterLock(NULL)
|
|
|
|
, m_FinishCV(NULL)
|
|
|
|
, m_Barrier(NULL)
|
2012-08-29 18:43:37 +00:00
|
|
|
, m_Width(0)
|
|
|
|
, m_Height(0)
|
|
|
|
, m_CmpFunc(NULL)
|
|
|
|
, m_OutBuf(NULL)
|
|
|
|
, m_InBuf(NULL)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void CmpThread::operator()() {
|
2012-08-31 21:33:54 +00:00
|
|
|
if(!m_Barrier || !m_CmpFunc || !m_OutBuf || !m_InBuf
|
|
|
|
|| !m_ParentCounter || !m_ParentCounterLock
|
|
|
|
|| !m_FinishCV
|
|
|
|
) {
|
2012-08-29 18:43:37 +00:00
|
|
|
fprintf(stderr, "Incorrect thread initialization.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:33:54 +00:00
|
|
|
// Wait for all threads to be ready...
|
2012-08-29 18:43:37 +00:00
|
|
|
m_Barrier->wait();
|
2012-08-31 18:58:51 +00:00
|
|
|
|
|
|
|
(*m_CmpFunc)(m_InBuf, m_OutBuf, m_Width, m_Height);
|
2012-08-31 21:33:54 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
boost::lock_guard<boost::mutex> lock(*m_ParentCounterLock);
|
|
|
|
(*m_ParentCounter)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_FinishCV->notify_one();
|
2012-08-29 18:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-31 18:58:51 +00:00
|
|
|
ThreadGroup::ThreadGroup( int numThreads, const ImageFile &image, CompressionFunc func, unsigned char *outBuf )
|
2012-08-29 18:43:37 +00:00
|
|
|
: m_Barrier(new boost::barrier(numThreads))
|
2012-08-31 21:33:54 +00:00
|
|
|
, m_FinishMutex(new boost::mutex())
|
|
|
|
, m_FinishCV(new boost::condition_variable())
|
2012-08-29 18:43:37 +00:00
|
|
|
, m_NumThreads(numThreads)
|
|
|
|
, m_ActiveThreads(0)
|
2012-08-31 18:58:51 +00:00
|
|
|
, m_Func(func)
|
|
|
|
, m_Image(image)
|
|
|
|
, m_OutBuf(outBuf)
|
|
|
|
{
|
2012-08-31 21:33:54 +00:00
|
|
|
for(int i = 0; i < kMaxNumThreads; i++) {
|
|
|
|
// Thread synchronization primitives
|
|
|
|
m_Threads[i].m_ParentCounterLock = m_FinishMutex;
|
|
|
|
m_Threads[i].m_FinishCV = m_FinishCV;
|
|
|
|
m_Threads[i].m_ParentCounter = &m_ThreadsFinished;
|
2012-08-31 18:58:51 +00:00
|
|
|
m_Threads[i].m_Barrier = m_Barrier;
|
2012-08-31 21:33:54 +00:00
|
|
|
}
|
2012-08-31 18:58:51 +00:00
|
|
|
}
|
2012-08-29 18:43:37 +00:00
|
|
|
|
|
|
|
ThreadGroup::~ThreadGroup() {
|
|
|
|
delete m_Barrier;
|
2012-09-01 03:22:05 +00:00
|
|
|
delete m_FinishMutex;
|
|
|
|
delete m_FinishCV;
|
2012-08-29 18:43:37 +00:00
|
|
|
}
|
2012-08-31 21:33:54 +00:00
|
|
|
|
2012-08-31 18:58:51 +00:00
|
|
|
unsigned int ThreadGroup::GetCompressedBlockSize() {
|
|
|
|
if(m_Func == BC7C::CompressImageBC7) return 16;
|
2012-09-13 21:43:58 +00:00
|
|
|
#ifdef HAS_SSE_41
|
2012-08-31 18:58:51 +00:00
|
|
|
if(m_Func == BC7C::CompressImageBC7SIMD) return 16;
|
2012-09-13 21:43:58 +00:00
|
|
|
#endif
|
2012-08-31 18:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ThreadGroup::GetUncompressedBlockSize() {
|
|
|
|
if(m_Func == BC7C::CompressImageBC7) return 64;
|
2012-09-13 21:43:58 +00:00
|
|
|
#ifdef HAS_SSE_41
|
2012-08-31 18:58:51 +00:00
|
|
|
if(m_Func == BC7C::CompressImageBC7SIMD) return 64;
|
2012-09-13 21:43:58 +00:00
|
|
|
#endif
|
2012-08-31 18:58:51 +00:00
|
|
|
}
|
2012-08-29 18:43:37 +00:00
|
|
|
|
|
|
|
void ThreadGroup::Start() {
|
|
|
|
|
2012-08-30 18:00:18 +00:00
|
|
|
// Have we already activated the thread group?
|
|
|
|
if(m_ActiveThreads > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-31 18:58:51 +00:00
|
|
|
// Make sure that the image dimensions are multiples of 4
|
|
|
|
assert((m_Image.GetWidth() & 3) == 0);
|
|
|
|
assert((m_Image.GetHeight() & 3) == 0);
|
|
|
|
|
|
|
|
// We can assume that the image data is in block stream order
|
|
|
|
// so, the size of the data given to each thread will be (nb*4)x4
|
|
|
|
int numBlocks = (m_Image.GetWidth() * m_Image.GetHeight()) / 16;
|
|
|
|
|
|
|
|
int blocksProcessed = 0;
|
|
|
|
int blocksPerThread = (numBlocks/m_NumThreads) + ((numBlocks % m_NumThreads)? 1 : 0);
|
|
|
|
|
2012-08-31 21:33:54 +00:00
|
|
|
// Currently no threads are finished...
|
|
|
|
m_ThreadsFinished = 0;
|
2012-08-29 18:43:37 +00:00
|
|
|
for(int i = 0; i < m_NumThreads; i++) {
|
|
|
|
|
|
|
|
if(m_ActiveThreads >= kMaxNumThreads)
|
|
|
|
break;
|
|
|
|
|
2012-08-31 18:58:51 +00:00
|
|
|
int numBlocksThisThread = blocksPerThread;
|
|
|
|
if(blocksProcessed + numBlocksThisThread > numBlocks) {
|
|
|
|
numBlocksThisThread = numBlocks - blocksProcessed;
|
|
|
|
}
|
|
|
|
|
2012-08-29 18:43:37 +00:00
|
|
|
CmpThread &t = m_Threads[m_ActiveThreads];
|
2012-08-31 18:58:51 +00:00
|
|
|
t.m_Height = 4;
|
|
|
|
t.m_Width = numBlocksThisThread * 4;
|
|
|
|
t.m_CmpFunc = m_Func;
|
|
|
|
t.m_OutBuf = m_OutBuf + (blocksProcessed * GetCompressedBlockSize());
|
|
|
|
t.m_InBuf = m_Image.GetPixels() + (blocksProcessed * GetUncompressedBlockSize());
|
|
|
|
|
|
|
|
blocksProcessed += numBlocksThisThread;
|
|
|
|
|
2012-08-29 18:43:37 +00:00
|
|
|
m_ThreadHandles[m_ActiveThreads] = new boost::thread(t);
|
|
|
|
|
|
|
|
m_ActiveThreads++;
|
|
|
|
}
|
|
|
|
|
2012-08-30 18:00:18 +00:00
|
|
|
m_StopWatch.Reset();
|
|
|
|
m_StopWatch.Start();
|
2012-08-29 18:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadGroup::Join() {
|
|
|
|
|
2012-08-31 21:33:54 +00:00
|
|
|
boost::unique_lock<boost::mutex> lock(*m_FinishMutex);
|
|
|
|
while(m_ThreadsFinished != m_ActiveThreads) {
|
|
|
|
m_FinishCV->wait(lock);
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:45:55 +00:00
|
|
|
m_StopWatch.Stop();
|
|
|
|
|
2012-08-29 18:43:37 +00:00
|
|
|
for(int i = 0; i < m_ActiveThreads; i++) {
|
|
|
|
m_ThreadHandles[i]->join();
|
|
|
|
delete m_ThreadHandles[i];
|
|
|
|
}
|
|
|
|
|
2012-08-30 18:00:18 +00:00
|
|
|
// Reset active number of threads...
|
2012-08-29 18:43:37 +00:00
|
|
|
m_ActiveThreads = 0;
|
|
|
|
}
|