Skip to content

Commit

Permalink
Merge branch 'multiCoreOpt' of github.com:MrKepzie/Natron into multiC…
Browse files Browse the repository at this point in the history
…oreOpt
  • Loading branch information
MrKepzie committed Sep 24, 2014
2 parents 045196d + 7a45626 commit be01b4f
Show file tree
Hide file tree
Showing 23 changed files with 1,154 additions and 887 deletions.
2 changes: 1 addition & 1 deletion App/App.pro
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ VERSION = 1.0.0
TEMPLATE = app
CONFIG += app
CONFIG += moc
CONFIG += boost glew opengl qt expat cairo
CONFIG += boost glew opengl qt expat cairo sigar
QT += gui core opengl network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets concurrent

Expand Down
65 changes: 65 additions & 0 deletions Engine/AppManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include <QDebug>
#include <QAbstractSocket>
#include <QCoreApplication>
#include <QThread>

#include <sigar.h>


#include "Global/MemoryInfo.h"
#include "Global/QtCompat.h" // for removeRecursively
Expand Down Expand Up @@ -81,6 +85,11 @@ struct AppManagerPrivate

std::string currentOCIOConfigPath; //< the currentOCIO config path

mutable sigar_t* sigarInfos; // for CPU idle time
QMutex cpuIdleTimeMutex;
U64 cpuIdleTime;
U64 cpuTotalTime;

AppManagerPrivate()
: _appType(AppManager::APP_BACKGROUND)
, _appInstances()
Expand All @@ -103,9 +112,19 @@ struct AppManagerPrivate
,maxCacheFiles(0)
,currentCacheFilesCount(0)
,currentCacheFilesCountMutex()
,sigarInfos(0)
,cpuIdleTimeMutex()
,cpuIdleTime(0)
,cpuTotalTime(0)

{
setMaxCacheFiles();
sigar_open(&sigarInfos);
}

~AppManagerPrivate()
{
sigar_close(sigarInfos);
}

void initProcessInputChannel(const QString & mainProcessServerName);
Expand Down Expand Up @@ -1665,6 +1684,52 @@ AppManager::getOCIOConfigPath() const
return _imp->currentOCIOConfigPath;
}

int
AppManager::evaluateBestNoConcurrentThreads(int currentNoThreads) const
{
sigar_cpu_t cpuInfos;

U64 idleTimeElapsed;
U64 totalTimeElapsed;
{
QMutexLocker l(&_imp->cpuIdleTimeMutex);
sigar_cpu_get(_imp->sigarInfos, &cpuInfos);

idleTimeElapsed = cpuInfos.idle - _imp->cpuIdleTime;
U64 newTotalTime = cpuInfos.user + cpuInfos.sys + cpuInfos.idle;
totalTimeElapsed = newTotalTime - _imp->cpuTotalTime;
_imp->cpuIdleTime = cpuInfos.idle;
_imp->cpuTotalTime = newTotalTime;
}

///This can happen somehow, we don't treat this case
if (totalTimeElapsed == 0) {
return currentNoThreads;
}

double activityPercent = 1. - idleTimeElapsed / (double)totalTimeElapsed;
int ret;

if (activityPercent < 1.) {
ret = activityPercent > 0. ? currentNoThreads / activityPercent : currentNoThreads;
} else {
ret = currentNoThreads - 1;
}
return std::min(QThread::idealThreadCount(), ret);
}

void
AppManager::resetCPUIdleTime()
{
sigar_cpu_t cpuInfos;

QMutexLocker l(&_imp->cpuIdleTimeMutex);
sigar_cpu_get(_imp->sigarInfos, &cpuInfos);
_imp->cpuIdleTime = cpuInfos.idle;
_imp->cpuTotalTime = cpuInfos.user + cpuInfos.sys + cpuInfos.idle;

}

namespace Natron {
void
errorDialog(const std::string & title,
Expand Down
13 changes: 13 additions & 0 deletions Engine/AppManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ class AppManager
///Non MT-safe!
const std::string& getOCIOConfigPath() const;

/**
* @brief This functions aims to calculate the best number of parallel renders (threads)
* that should be concurrent to attain 100% CPU activity. This internally uses CPU idle
* time via the sigar library.
* This is to be used once a render is finished
**/
int evaluateBestNoConcurrentThreads(int currentNoThreads) const;

/**
* @brief Reset the CPU Idle time tracked, this is to be called when starting a new render
**/
void resetCPUIdleTime();

public slots:

///Closes the application not saving any projects.
Expand Down
4 changes: 2 additions & 2 deletions Engine/EffectInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3146,9 +3146,9 @@ OutputEffectInstance::~OutputEffectInstance()
}

void
OutputEffectInstance::renderCurrentFrame()
OutputEffectInstance::renderCurrentFrame(bool abortPrevious)
{
_scheduler->renderCurrentFrame();
_scheduler->renderCurrentFrame(abortPrevious);
}

bool
Expand Down
2 changes: 1 addition & 1 deletion Engine/EffectInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ class OutputEffectInstance

void notifyRenderFinished();

void renderCurrentFrame();
void renderCurrentFrame(bool abortPrevious = true);

bool ifInfiniteclipRectToProjectDefault(RectD* rod) const;

Expand Down
2 changes: 1 addition & 1 deletion Engine/Engine.pro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TARGET = Engine
TEMPLATE = lib
CONFIG += staticlib
CONFIG += moc
CONFIG += boost qt expat cairo
CONFIG += boost qt expat cairo sigar
QT += core network
greaterThan(QT_MAJOR_VERSION, 4): QT += concurrent
QT -= gui
Expand Down
4 changes: 2 additions & 2 deletions Engine/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ Bitmap::minimalNonMarkedRects(const RectI & roi) const

#endif // NATRON_BITMAP_DISABLE_OPTIMIZATION
# ifdef DEBUG
qDebug() << "render " << ret.size() << " rectangles";
/*qDebug() << "render " << ret.size() << " rectangles";
for (std::list<RectI>::const_iterator it = ret.begin(); it != ret.end(); ++it) {
qDebug() << "rect: " << "x1= " << it->x1 << " , x2= " << it->x2 << " , y1= " << it->y1 << " , y2= " << it->y2;
}
}*/
# endif // DEBUG
return ret;
} // minimalNonMarkedRects
Expand Down
7 changes: 7 additions & 0 deletions Engine/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ class Image
return dataSize() + _bitmap.getBounds().area();
}


///Overriden from BufferableObject
virtual std::size_t sizeInRAM() const OVERRIDE FINAL
{
return size();
}

unsigned int getMipMapLevel() const
{
return this->_key._mipMapLevel;
Expand Down
4 changes: 3 additions & 1 deletion Engine/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,9 @@ Node::makePreviewImage(SequenceTime time,
unsigned int* buf)
{
assert(_imp->knobsInitialized);

if (!_imp->liveInstance) {
return;
}
{
QMutexLocker locker(&_imp->mustQuitPreviewMutex);
if (_imp->mustQuitPreview) {
Expand Down
Loading

0 comments on commit be01b4f

Please sign in to comment.