Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow changing number of threads through the algorithm settings panel
  • Loading branch information
alexbruy authored and nyalldawson committed Apr 24, 2023
1 parent b2b68b5 commit 0f2d2bc
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 27 deletions.
26 changes: 26 additions & 0 deletions python/core/auto_generated/processing/qgsprocessingcontext.sip.in
Expand Up @@ -608,6 +608,32 @@ for all temporary files created during algorithm execution.

.. seealso:: :py:func:`temporaryFolder`

.. versionadded:: 3.32
%End

int numberOfThreads() const;
%Docstring
Returns the (optional) number of threads to use when running algorithms.

If set, this overrides the standard global Processing number of threads setting.
Note that if algorithm implementation does not support multhreaded execution, this
setting will be ignored.

.. seealso:: :py:func:`setNumberOfThreads`

.. versionadded:: 3.32
%End

void setNumberOfThreads( int threads );
%Docstring
Sets the (optional) number of ``threads`` to use when running algorithms.

If set, this overrides the standard global Processing number of threads setting.
Note that if algorithm implementation does not support multhreaded execution, this
setting will be ignored.

.. seealso:: :py:func:`numberOfThreads`

.. versionadded:: 3.32
%End

Expand Down
Expand Up @@ -11,6 +11,7 @@




class QgsProcessingAlgorithmDialogBase : QDialog, QgsProcessingParametersGenerator, QgsProcessingContextGenerator
{
%Docstring(signature="appended")
Expand Down
10 changes: 10 additions & 0 deletions src/core/processing/qgsprocessingcontext.cpp
Expand Up @@ -157,6 +157,16 @@ void QgsProcessingContext::setTemporaryFolder( const QString &folder )
mTemporaryFolderOverride = folder;
}

int QgsProcessingContext::numberOfThreads() const
{
return mThreadsToUse;
}

void QgsProcessingContext::setNumberOfThreads( int threads )
{
mThreadsToUse = threads;
}

QVariantMap QgsProcessingContext::exportToMap() const
{
QVariantMap res;
Expand Down
26 changes: 26 additions & 0 deletions src/core/processing/qgsprocessingcontext.h
Expand Up @@ -101,6 +101,7 @@ class CORE_EXPORT QgsProcessingContext
mAreaUnit = other.mAreaUnit;
mLogLevel = other.mLogLevel;
mTemporaryFolderOverride = other.mTemporaryFolderOverride;
mThreadsToUse = other.mThreadsToUse;
}

/**
Expand Down Expand Up @@ -680,6 +681,30 @@ class CORE_EXPORT QgsProcessingContext
*/
void setTemporaryFolder( const QString &folder );

/**
* Returns the (optional) number of threads to use when running algorithms.
*
* If set, this overrides the standard global Processing number of threads setting.
* Note that if algorithm implementation does not support multhreaded execution, this
* setting will be ignored.
*
* \see setNumberOfThreads()
* \since QGIS 3.32
*/
int numberOfThreads() const;

/**
* Sets the (optional) number of \a threads to use when running algorithms.
*
* If set, this overrides the standard global Processing number of threads setting.
* Note that if algorithm implementation does not support multhreaded execution, this
* setting will be ignored.
*
* \see numberOfThreads()
* \since QGIS 3.32
*/
void setNumberOfThreads( int threads );

/**
* Exports the context's settings to a variant map.
*
Expand Down Expand Up @@ -737,6 +762,7 @@ class CORE_EXPORT QgsProcessingContext
LogLevel mLogLevel = DefaultLevel;

QString mTemporaryFolderOverride;
int mThreadsToUse = QThread::idealThreadCount();

#ifdef SIP_RUN
QgsProcessingContext( const QgsProcessingContext &other );
Expand Down
11 changes: 11 additions & 0 deletions src/gui/processing/qgsprocessingalgorithmdialogbase.cpp
Expand Up @@ -160,6 +160,7 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par
mDistanceUnits = mContextOptionsWidget->distanceUnit();
mAreaUnits = mContextOptionsWidget->areaUnit();
mTemporaryFolderOverride = mContextOptionsWidget->temporaryFolder();
mNumberOfThreads = mContextOptionsWidget->numberOfThreads();
} );
}
}
Expand Down Expand Up @@ -869,6 +870,7 @@ void QgsProcessingAlgorithmDialogBase::applyContextOverrides( QgsProcessingConte
context->setDistanceUnit( mDistanceUnits );
context->setAreaUnit( mAreaUnits );
context->setTemporaryFolder( mTemporaryFolderOverride );
context->setNumberOfThreads( mNumberOfThreads );
}
}

Expand Down Expand Up @@ -1010,10 +1012,13 @@ QgsProcessingContextOptionsWidget::QgsProcessingContextOptionsWidget( QWidget *p
mAreaUnitsCombo->addItem( title, QVariant::fromValue( unit ) );
}

mThreadsSpinBox->setRange( 1, QThread::idealThreadCount() );

connect( mComboInvalidFeatureFiltering, qOverload< int >( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
connect( mDistanceUnitsCombo, qOverload< int >( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
connect( mAreaUnitsCombo, qOverload< int >( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
connect( mTemporaryFolderWidget, &QgsFileWidget::fileChanged, this, &QgsPanelWidget::widgetChanged );
connect( mThreadsSpinBox, qOverload< int >( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
}

void QgsProcessingContextOptionsWidget::setFromContext( const QgsProcessingContext *context )
Expand All @@ -1022,6 +1027,7 @@ void QgsProcessingContextOptionsWidget::setFromContext( const QgsProcessingConte
whileBlocking( mDistanceUnitsCombo )->setCurrentIndex( mDistanceUnitsCombo->findData( QVariant::fromValue( context->distanceUnit() ) ) );
whileBlocking( mAreaUnitsCombo )->setCurrentIndex( mAreaUnitsCombo->findData( QVariant::fromValue( context->areaUnit() ) ) );
whileBlocking( mTemporaryFolderWidget )->setFilePath( context->temporaryFolder() );
whileBlocking( mThreadsSpinBox )->setValue( context->numberOfThreads() );
}

QgsFeatureRequest::InvalidGeometryCheck QgsProcessingContextOptionsWidget::invalidGeometryCheck() const
Expand All @@ -1044,4 +1050,9 @@ QString QgsProcessingContextOptionsWidget::temporaryFolder()
return mTemporaryFolderWidget->filePath();
}

int QgsProcessingContextOptionsWidget::numberOfThreads() const
{
return mThreadsSpinBox->value();
}

///@endcond
7 changes: 7 additions & 0 deletions src/gui/processing/qgsprocessingalgorithmdialogbase.h
Expand Up @@ -25,6 +25,8 @@
#include "qgsprocessingfeedback.h"
#include "qgsprocessingwidgetwrapper.h"

#include <QThread>

///@cond NOT_STABLE

class QgsProcessingAlgorithm;
Expand Down Expand Up @@ -475,6 +477,7 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, public QgsPr
Qgis::DistanceUnit mDistanceUnits = Qgis::DistanceUnit::Unknown;
Qgis::AreaUnit mAreaUnits = Qgis::AreaUnit::Unknown;
QString mTemporaryFolderOverride;
int mNumberOfThreads = QThread::idealThreadCount();

QString formatHelp( QgsProcessingAlgorithm *algorithm );
void scrollToBottomOfLog();
Expand Down Expand Up @@ -564,6 +567,10 @@ class GUI_EXPORT QgsProcessingContextOptionsWidget : public QgsPanelWidget, priv
*/
QString temporaryFolder();

/**
* Returns the number of threads to use selected in the widget.
*/
int numberOfThreads() const;
};

#endif
Expand Down
69 changes: 42 additions & 27 deletions src/ui/processing/qgsprocessingcontextoptionsbase.ui
Expand Up @@ -47,6 +47,39 @@
</rect>
</property>
<layout class="QGridLayout" name="gridLayout" columnstretch="0,0">
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Invalid feature filtering</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Environment Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Temporary folder</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QgsFileWidget" name="mTemporaryFolderWidget" native="true"/>
</item>
</layout>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Number of threads to use</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="mComboInvalidFeatureFiltering">
<property name="toolTip">
Expand Down Expand Up @@ -83,7 +116,10 @@
</layout>
</widget>
</item>
<item row="4" column="0">
<item row="5" column="1">
<widget class="QgsSpinBox" name="mThreadsSpinBox"/>
</item>
<item row="6" column="0" colspan="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -96,32 +132,6 @@
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Invalid feature filtering</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Environment Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Temporary folder</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QgsFileWidget" name="mTemporaryFolderWidget" native="true"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
Expand All @@ -147,6 +157,11 @@
<header>qgsfilewidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsSpinBox</class>
<extends>QSpinBox</extends>
<header>qgsspinbox.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
Expand Down

0 comments on commit 0f2d2bc

Please sign in to comment.