Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
get/set max. thread count in QgsApplication
  • Loading branch information
etiennesky committed May 16, 2014
1 parent 9001c47 commit e879a8b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/app/main.cpp
Expand Up @@ -873,6 +873,10 @@ int main( int argc, char *argv[] )
mySettings.remove( "/qgis/restoreDefaultWindowState" );
}

// set max. thread count
// this should be done in QgsApplication::init() but it doesn't know the settings dir.
QgsApplication::setMaxThreads( QSettings().value( "/qgis/max_threads", -1 ).toInt() );

QgisApp *qgis = new QgisApp( mypSplash, myRestorePlugins ); // "QgisApp" used to find canonical instance
qgis->setObjectName( "QgisApp" );

Expand Down
15 changes: 8 additions & 7 deletions src/app/qgsoptions.cpp
Expand Up @@ -548,10 +548,10 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
chkUseRenderCaching->setChecked( settings.value( "/qgis/enable_render_caching", false ).toBool() );
chkParallelRendering->setChecked( settings.value( "/qgis/parallel_rendering", false ).toBool() );
spinMapUpdateInterval->setValue( settings.value( "/qgis/map_update_interval", 250 ).toInt() );
chkMaxCores->setChecked( settings.value( "/qgis/max_cores", 0 ).toInt() != 0 );
spinMaxCores->setEnabled( chkMaxCores->isChecked() );
spinMaxCores->setRange( 1, QThread::idealThreadCount() );
spinMaxCores->setValue( settings.value( "/qgis/max_cores", QThread::idealThreadCount() ).toInt() );
chkMaxThreads->setChecked( QgsApplication::maxThreads() != -1 );
spinMaxThreads->setEnabled( chkMaxThreads->isChecked() );
spinMaxThreads->setRange( 1, QThread::idealThreadCount() );
spinMaxThreads->setValue( QgsApplication::maxThreads() );

// Default simplify drawing configuration
mSimplifyDrawingGroupBox->setChecked( settings.value( "/qgis/simplifyDrawingHints", ( int )QgsVectorSimplifyMethod::GeometrySimplification ).toInt() != QgsVectorSimplifyMethod::NoSimplification );
Expand Down Expand Up @@ -1076,10 +1076,11 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/enable_anti_aliasing", chkAntiAliasing->isChecked() );
settings.setValue( "/qgis/enable_render_caching", chkUseRenderCaching->isChecked() );
settings.setValue( "/qgis/parallel_rendering", chkParallelRendering->isChecked() );
if ( chkMaxCores->isChecked() )
settings.setValue( "/qgis/max_cores", int( spinMaxCores->value() ) );
if ( chkMaxThreads->isChecked() )
QgsApplication::setMaxThreads( spinMaxThreads->value() );
else
settings.remove( "/qgis/max_cores" );
QgsApplication::setMaxThreads( -1 );

settings.setValue( "/qgis/map_update_interval", spinMapUpdateInterval->value() );
settings.setValue( "/qgis/legendDoubleClickAction", cmbLegendDoubleClickAction->currentIndex() );
bool legendLayersCapitalise = settings.value( "/qgis/capitaliseLayerName", false ).toBool();
Expand Down
28 changes: 28 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -29,6 +29,7 @@
#include <QSettings>
#include <QIcon>
#include <QPixmap>
#include <QThreadPool>

#ifndef Q_WS_WIN
#include <netinet/in.h>
Expand Down Expand Up @@ -61,6 +62,7 @@ QString ABISYM( QgsApplication::mCfgIntDir );
#endif
QString ABISYM( QgsApplication::mBuildOutputPath );
QStringList ABISYM( QgsApplication::mGdalSkipList );
int ABISYM( QgsApplication::mMaxThreads );

/*!
\class QgsApplication
Expand Down Expand Up @@ -181,6 +183,11 @@ void QgsApplication::init( QString customConfigPath )

// allow Qt to search for Qt plugins (e.g. sqldrivers) in our plugin directory
QCoreApplication::addLibraryPath( pluginPath() );

// set max. thread count to -1
// this should be read from QSettings but we don't know where they are at this point
// so we read actual value in main.cpp
ABISYM( mMaxThreads ) = -1;
}

QgsApplication::~QgsApplication()
Expand Down Expand Up @@ -1016,4 +1023,25 @@ bool QgsApplication::createDB( QString *errorMessage )
return true;
}

void QgsApplication::setMaxThreads( int maxThreads )
{
qDebug("max threads: %d",maxThreads);

// make sure value is between 1 and #cores, if not set to -1 (use #cores)
// 0 could be used to disable any parallel processing
if ( maxThreads < 1 || maxThreads > QThread::idealThreadCount() )
maxThreads = -1;

// save value
ABISYM( mMaxThreads ) = maxThreads;
QSettings().setValue( "/qgis/max_threads", maxThreads );

// if -1 use #cores
if ( maxThreads == -1 )
maxThreads = QThread::idealThreadCount();

// set max thread count in QThreadPool
QThreadPool::globalInstance()->setMaxThreadCount( maxThreads );
qDebug( "set QThreadPool max thread count to %d", QThreadPool::globalInstance()->maxThreadCount() );
}

11 changes: 11 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -279,6 +279,14 @@ class CORE_EXPORT QgsApplication : public QApplication
* @note added in 2.0 */
static void applyGdalSkippedDrivers();

/** Get maximum concurrent thread count
* @note added in 2.4 */
static int maxThreads() { return ABISYM( mMaxThreads ); }
/** Set maximum concurrent thread count
* @note must be between 1 and #cores, -1 means use all available cores
* @note added in 2.4 */
static void setMaxThreads( int maxThreads );

#ifdef ANDROID
//dummy method to workaround sip generation issue issue
bool x11EventFilter( XEvent * event )
Expand Down Expand Up @@ -320,6 +328,9 @@ class CORE_EXPORT QgsApplication : public QApplication
* @see skipGdalDriver, restoreGdalDriver
* @note added in 2.0 */
static QStringList ABISYM( mGdalSkipList );
/**
* @note added in 2.4 */
static int ABISYM( mMaxThreads );
};

#endif
12 changes: 1 addition & 11 deletions src/core/qgsmaprendererjob.cpp
Expand Up @@ -732,13 +732,7 @@ void QgsMapRendererParallelJob::start()

mLayerJobs = prepareJobs( 0, mLabelingEngine );

// set max thread count
QSettings settings;
int max_cores = settings.value( "/qgis/max_cores", 0 ).toInt();
if ( max_cores <= 0 || max_cores > QThread::idealThreadCount() )
max_cores = QThread::idealThreadCount();
QThreadPool::globalInstance()->setMaxThreadCount( max_cores );
qDebug( "set max thread count to %d", QThreadPool::globalInstance()->maxThreadCount() );
qDebug( "QThreadPool max thread count is %d", QThreadPool::globalInstance()->maxThreadCount() );

// start async job

Expand Down Expand Up @@ -838,10 +832,6 @@ QImage QgsMapRendererParallelJob::renderedImage()

void QgsMapRendererParallelJob::renderLayersFinished()
{
// restore max thread count
QThreadPool::globalInstance()->setMaxThreadCount( QThread::idealThreadCount() );
qDebug( "restored max thread count to ideal (%d)", QThreadPool::globalInstance()->maxThreadCount() );

Q_ASSERT( mStatus == RenderingLayers );

// compose final image
Expand Down
8 changes: 4 additions & 4 deletions src/ui/qgsoptionsbase.ui
Expand Up @@ -1645,14 +1645,14 @@
</widget>
</item>
<item>
<widget class="QCheckBox" name="chkMaxCores">
<widget class="QCheckBox" name="chkMaxThreads">
<property name="text">
<string>Max cores to use:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinMaxCores"/>
<widget class="QSpinBox" name="spinMaxThreads"/>
</item>
<item>
<spacer name="horizontalSpacer_41">
Expand Down Expand Up @@ -4633,9 +4633,9 @@
</hints>
</connection>
<connection>
<sender>chkMaxCores</sender>
<sender>chkMaxThreads</sender>
<signal>toggled(bool)</signal>
<receiver>spinMaxCores</receiver>
<receiver>spinMaxThreads</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
Expand Down

0 comments on commit e879a8b

Please sign in to comment.