Skip to content

Commit 0f2d2bc

Browse files
alexbruynyalldawson
authored andcommittedApr 24, 2023
allow changing number of threads through the algorithm settings panel
1 parent b2b68b5 commit 0f2d2bc

File tree

7 files changed

+123
-27
lines changed

7 files changed

+123
-27
lines changed
 

‎python/core/auto_generated/processing/qgsprocessingcontext.sip.in

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,32 @@ for all temporary files created during algorithm execution.
608608

609609
.. seealso:: :py:func:`temporaryFolder`
610610

611+
.. versionadded:: 3.32
612+
%End
613+
614+
int numberOfThreads() const;
615+
%Docstring
616+
Returns the (optional) number of threads to use when running algorithms.
617+
618+
If set, this overrides the standard global Processing number of threads setting.
619+
Note that if algorithm implementation does not support multhreaded execution, this
620+
setting will be ignored.
621+
622+
.. seealso:: :py:func:`setNumberOfThreads`
623+
624+
.. versionadded:: 3.32
625+
%End
626+
627+
void setNumberOfThreads( int threads );
628+
%Docstring
629+
Sets the (optional) number of ``threads`` to use when running algorithms.
630+
631+
If set, this overrides the standard global Processing number of threads setting.
632+
Note that if algorithm implementation does not support multhreaded execution, this
633+
setting will be ignored.
634+
635+
.. seealso:: :py:func:`numberOfThreads`
636+
611637
.. versionadded:: 3.32
612638
%End
613639

‎python/gui/auto_generated/processing/qgsprocessingalgorithmdialogbase.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313

14+
1415
class QgsProcessingAlgorithmDialogBase : QDialog, QgsProcessingParametersGenerator, QgsProcessingContextGenerator
1516
{
1617
%Docstring(signature="appended")

‎src/core/processing/qgsprocessingcontext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ void QgsProcessingContext::setTemporaryFolder( const QString &folder )
157157
mTemporaryFolderOverride = folder;
158158
}
159159

160+
int QgsProcessingContext::numberOfThreads() const
161+
{
162+
return mThreadsToUse;
163+
}
164+
165+
void QgsProcessingContext::setNumberOfThreads( int threads )
166+
{
167+
mThreadsToUse = threads;
168+
}
169+
160170
QVariantMap QgsProcessingContext::exportToMap() const
161171
{
162172
QVariantMap res;

‎src/core/processing/qgsprocessingcontext.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class CORE_EXPORT QgsProcessingContext
101101
mAreaUnit = other.mAreaUnit;
102102
mLogLevel = other.mLogLevel;
103103
mTemporaryFolderOverride = other.mTemporaryFolderOverride;
104+
mThreadsToUse = other.mThreadsToUse;
104105
}
105106

106107
/**
@@ -680,6 +681,30 @@ class CORE_EXPORT QgsProcessingContext
680681
*/
681682
void setTemporaryFolder( const QString &folder );
682683

684+
/**
685+
* Returns the (optional) number of threads to use when running algorithms.
686+
*
687+
* If set, this overrides the standard global Processing number of threads setting.
688+
* Note that if algorithm implementation does not support multhreaded execution, this
689+
* setting will be ignored.
690+
*
691+
* \see setNumberOfThreads()
692+
* \since QGIS 3.32
693+
*/
694+
int numberOfThreads() const;
695+
696+
/**
697+
* Sets the (optional) number of \a threads to use when running algorithms.
698+
*
699+
* If set, this overrides the standard global Processing number of threads setting.
700+
* Note that if algorithm implementation does not support multhreaded execution, this
701+
* setting will be ignored.
702+
*
703+
* \see numberOfThreads()
704+
* \since QGIS 3.32
705+
*/
706+
void setNumberOfThreads( int threads );
707+
683708
/**
684709
* Exports the context's settings to a variant map.
685710
*
@@ -737,6 +762,7 @@ class CORE_EXPORT QgsProcessingContext
737762
LogLevel mLogLevel = DefaultLevel;
738763

739764
QString mTemporaryFolderOverride;
765+
int mThreadsToUse = QThread::idealThreadCount();
740766

741767
#ifdef SIP_RUN
742768
QgsProcessingContext( const QgsProcessingContext &other );

‎src/gui/processing/qgsprocessingalgorithmdialogbase.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ QgsProcessingAlgorithmDialogBase::QgsProcessingAlgorithmDialogBase( QWidget *par
160160
mDistanceUnits = mContextOptionsWidget->distanceUnit();
161161
mAreaUnits = mContextOptionsWidget->areaUnit();
162162
mTemporaryFolderOverride = mContextOptionsWidget->temporaryFolder();
163+
mNumberOfThreads = mContextOptionsWidget->numberOfThreads();
163164
} );
164165
}
165166
}
@@ -869,6 +870,7 @@ void QgsProcessingAlgorithmDialogBase::applyContextOverrides( QgsProcessingConte
869870
context->setDistanceUnit( mDistanceUnits );
870871
context->setAreaUnit( mAreaUnits );
871872
context->setTemporaryFolder( mTemporaryFolderOverride );
873+
context->setNumberOfThreads( mNumberOfThreads );
872874
}
873875
}
874876

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

1015+
mThreadsSpinBox->setRange( 1, QThread::idealThreadCount() );
1016+
10131017
connect( mComboInvalidFeatureFiltering, qOverload< int >( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
10141018
connect( mDistanceUnitsCombo, qOverload< int >( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
10151019
connect( mAreaUnitsCombo, qOverload< int >( &QComboBox::currentIndexChanged ), this, &QgsPanelWidget::widgetChanged );
10161020
connect( mTemporaryFolderWidget, &QgsFileWidget::fileChanged, this, &QgsPanelWidget::widgetChanged );
1021+
connect( mThreadsSpinBox, qOverload< int >( &QSpinBox::valueChanged ), this, &QgsPanelWidget::widgetChanged );
10171022
}
10181023

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

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

1053+
int QgsProcessingContextOptionsWidget::numberOfThreads() const
1054+
{
1055+
return mThreadsSpinBox->value();
1056+
}
1057+
10471058
///@endcond

‎src/gui/processing/qgsprocessingalgorithmdialogbase.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "qgsprocessingfeedback.h"
2626
#include "qgsprocessingwidgetwrapper.h"
2727

28+
#include <QThread>
29+
2830
///@cond NOT_STABLE
2931

3032
class QgsProcessingAlgorithm;
@@ -475,6 +477,7 @@ class GUI_EXPORT QgsProcessingAlgorithmDialogBase : public QDialog, public QgsPr
475477
Qgis::DistanceUnit mDistanceUnits = Qgis::DistanceUnit::Unknown;
476478
Qgis::AreaUnit mAreaUnits = Qgis::AreaUnit::Unknown;
477479
QString mTemporaryFolderOverride;
480+
int mNumberOfThreads = QThread::idealThreadCount();
478481

479482
QString formatHelp( QgsProcessingAlgorithm *algorithm );
480483
void scrollToBottomOfLog();
@@ -564,6 +567,10 @@ class GUI_EXPORT QgsProcessingContextOptionsWidget : public QgsPanelWidget, priv
564567
*/
565568
QString temporaryFolder();
566569

570+
/**
571+
* Returns the number of threads to use selected in the widget.
572+
*/
573+
int numberOfThreads() const;
567574
};
568575

569576
#endif

‎src/ui/processing/qgsprocessingcontextoptionsbase.ui

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,39 @@
4747
</rect>
4848
</property>
4949
<layout class="QGridLayout" name="gridLayout" columnstretch="0,0">
50+
<item row="1" column="0">
51+
<widget class="QLabel" name="label">
52+
<property name="text">
53+
<string>Invalid feature filtering</string>
54+
</property>
55+
</widget>
56+
</item>
57+
<item row="3" column="0" colspan="2">
58+
<widget class="QGroupBox" name="groupBox_2">
59+
<property name="title">
60+
<string>Environment Settings</string>
61+
</property>
62+
<layout class="QGridLayout" name="gridLayout_3">
63+
<item row="0" column="0">
64+
<widget class="QLabel" name="label_4">
65+
<property name="text">
66+
<string>Temporary folder</string>
67+
</property>
68+
</widget>
69+
</item>
70+
<item row="0" column="1">
71+
<widget class="QgsFileWidget" name="mTemporaryFolderWidget" native="true"/>
72+
</item>
73+
</layout>
74+
</widget>
75+
</item>
76+
<item row="5" column="0">
77+
<widget class="QLabel" name="label_5">
78+
<property name="text">
79+
<string>Number of threads to use</string>
80+
</property>
81+
</widget>
82+
</item>
5083
<item row="1" column="1">
5184
<widget class="QComboBox" name="mComboInvalidFeatureFiltering">
5285
<property name="toolTip">
@@ -83,7 +116,10 @@
83116
</layout>
84117
</widget>
85118
</item>
86-
<item row="4" column="0">
119+
<item row="5" column="1">
120+
<widget class="QgsSpinBox" name="mThreadsSpinBox"/>
121+
</item>
122+
<item row="6" column="0" colspan="2">
87123
<spacer name="verticalSpacer">
88124
<property name="orientation">
89125
<enum>Qt::Vertical</enum>
@@ -96,32 +132,6 @@
96132
</property>
97133
</spacer>
98134
</item>
99-
<item row="1" column="0">
100-
<widget class="QLabel" name="label">
101-
<property name="text">
102-
<string>Invalid feature filtering</string>
103-
</property>
104-
</widget>
105-
</item>
106-
<item row="3" column="0" colspan="2">
107-
<widget class="QGroupBox" name="groupBox_2">
108-
<property name="title">
109-
<string>Environment Settings</string>
110-
</property>
111-
<layout class="QGridLayout" name="gridLayout_3">
112-
<item row="0" column="0">
113-
<widget class="QLabel" name="label_4">
114-
<property name="text">
115-
<string>Temporary folder</string>
116-
</property>
117-
</widget>
118-
</item>
119-
<item row="0" column="1">
120-
<widget class="QgsFileWidget" name="mTemporaryFolderWidget" native="true"/>
121-
</item>
122-
</layout>
123-
</widget>
124-
</item>
125135
</layout>
126136
</widget>
127137
</widget>
@@ -147,6 +157,11 @@
147157
<header>qgsfilewidget.h</header>
148158
<container>1</container>
149159
</customwidget>
160+
<customwidget>
161+
<class>QgsSpinBox</class>
162+
<extends>QSpinBox</extends>
163+
<header>qgsspinbox.h</header>
164+
</customwidget>
150165
</customwidgets>
151166
<resources/>
152167
<connections/>

0 commit comments

Comments
 (0)
Please sign in to comment.