Enables setting thread count via command line

Allows users to set the maximum number of threads used for parallel processing via a command-line argument. The thread count can also be set via preferences. Command line takes precedence.
This commit is contained in:
Magne Sjaastad committed 2025-08-13 13:00:49 +02:00
1 parent c854166be6
commit 41870a2c6d
8 files changed
+82 -9

No files matched your search

+3
View File
@@ -191,6 +191,9 @@ int main( int argc, char* argv[] )
}
}
#endif
// Check if max thread count is set either from command line or preferences
app->setThreadCount();
exitCode = QCoreApplication::instance()->exec();
}
catch ( std::exception& exep )
@@ -363,6 +363,37 @@ RimCommandRouter* RiaApplication::commandRouter()
return m_commandRouter.get();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaApplication::setThreadCount() const
{
std::optional<int> threadCount = std::nullopt;
QString source;
if ( m_threadCountFromCommandLine.has_value() )
{
threadCount = m_threadCountFromCommandLine.value();
source = "Command Line";
}
else
{
auto threadCountPrefs = RiaPreferencesSystem::current()->threadCount();
if ( threadCountPrefs.has_value() )
{
threadCount = threadCountPrefs.value();
source = "Preferences";
}
}
if ( threadCount.has_value() )
{
RiaLogging::info( QString( "Setting number of threads to %1 from %2" ).arg( threadCount.value() ).arg( source ) );
RiaOpenMPTools::setMaxThreads( threadCount.value() );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -1597,12 +1628,6 @@ void RiaApplication::initialize()
caf::SelectionManager::instance()->setPdmRootObject( project() );
initializeDataLoadController();
const auto& [enabled, maxThreads] = RiaPreferencesSystem::current()->maximumNumberOfThreads();
if ( enabled )
{
RiaOpenMPTools::setMaxThreads( maxThreads );
}
}
//--------------------------------------------------------------------------------------------------
@@ -121,6 +121,8 @@ public:
RimProject* project();
RimCommandRouter* commandRouter();
void setThreadCount() const;
void createMockModel();
void createResultsMockModel();
void createLargeResultsMockModel();
@@ -261,6 +263,8 @@ protected:
bool m_runningWorkerProcess;
std::optional<int> m_threadCountFromCommandLine;
private:
static RiaApplication* s_riaApplication;
QPointer<RiaOsduConnector> m_osduConnector;
@@ -142,6 +142,23 @@ RiaApplication::ApplicationStatus RiaConsoleApplication::handleArguments( gsl::n
return RiaApplication::ApplicationStatus::EXIT_COMPLETED;
}
if ( cvf::Option o = progOpt->option( "threadcount" ) )
{
if ( o.valueCount() == 1 )
{
auto value = o.value( 0 ).toInt();
if ( value > 0 )
{
m_threadCountFromCommandLine = value;
}
else
{
RiaLogging::error( "Error: Invalid value for --threadcount. Must be a positive integer." );
return RiaApplication::ApplicationStatus::EXIT_WITH_ERROR;
}
}
}
// Code generation
// -----------------
if ( cvf::Option o = progOpt->option( "generate" ) )
@@ -478,6 +478,23 @@ RiaApplication::ApplicationStatus RiaGuiApplication::handleArguments( gsl::not_n
return RiaApplication::ApplicationStatus::EXIT_COMPLETED;
}
if ( cvf::Option o = progOpt->option( "threadcount" ) )
{
if ( o.valueCount() == 1 )
{
auto value = o.value( 0 ).toInt();
if ( value > 0 )
{
m_threadCountFromCommandLine = value;
}
else
{
RiaLogging::error( "Error: Invalid value for --threadcount. Must be a positive integer." );
return RiaApplication::ApplicationStatus::EXIT_WITH_ERROR;
}
}
}
// Code generation
// -----------------
if ( cvf::Option o = progOpt->option( "generate" ) )
@@ -255,11 +255,16 @@ bool RiaPreferencesSystem::useMultiThreadingForSummaryImport() const
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<bool, int> RiaPreferencesSystem::maximumNumberOfThreads() const
std::optional<int> RiaPreferencesSystem::threadCount() const
{
const auto& [enabled, text] = m_maximumNumberOfThreads();
return { enabled, text.toInt() };
if ( !enabled || text.isEmpty() )
{
return std::nullopt; // No limit set
}
return text.toInt();
}
//--------------------------------------------------------------------------------------------------
@@ -60,7 +60,7 @@ public:
bool useImprovedSummaryImport() const;
bool useMultiThreadingForSummaryImport() const;
std::pair<bool, int> maximumNumberOfThreads() const;
std::optional<int> threadCount() const;
bool logToFile() const;
@@ -74,6 +74,8 @@ bool RiaArgumentParser::parseArguments( cvf::ProgramOptions* progOpt )
progOpt->registerOption( "server", "[<portnumber>]", "Launch as a GRPC server. Default port is 50051", cvf::ProgramOptions::SINGLE_VALUE );
progOpt->registerOption( "portnumberfile", "[<filename>]", "Write the port number to this file.", cvf::ProgramOptions::SINGLE_VALUE );
progOpt->registerOption( "threadcount", "<threadcount>", "Set number of threads for parallel processing.\n", cvf::ProgramOptions::SINGLE_VALUE );
progOpt->registerOption( "startdir", "<folder>", "Set startup directory.\n", cvf::ProgramOptions::SINGLE_VALUE );
progOpt->registerOption( "summaryplot",