Skip to content

Commit

Permalink
Merge pull request #394 from pyswmm/dev_api_threadcount
Browse files Browse the repository at this point in the history
Added simple support for Thread Count setter - partially tested
  • Loading branch information
bemcdonnell authored Jun 19, 2023
2 parents 2e5b21d + d6c4806 commit 033a9f7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/solver/include/toolkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ EXPORT_TOOLKIT int swmm_getSimulationAnalysisSetting(SM_SimOption type, int *val
*/
EXPORT_TOOLKIT int swmm_getSimulationParam(SM_SimSetting type, double *value);

/**
@brief Set Simulation Analysis Setting
@param type Option code (see @ref SM_SimSetting)
@param value Option value
@return Error code
*/
EXPORT_TOOLKIT int swmm_setSimulationParam(SM_SimSetting type, double value);
/**
@brief Gets Object Count
@param type Option code (see @ref SM_ObjectType)
Expand Down
3 changes: 2 additions & 1 deletion src/solver/include/toolkit_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ typedef enum {
SM_QUALERROR = 10, /**< Quality routing error */
SM_HEADTOL = 11, /**< DW routing head tolerance (ft) */
SM_SYSFLOWTOL = 12, /**< Tolerance for steady system flow */
SM_LATFLOWTOL = 13 /**< Tolerance for steady nodal inflow */
SM_LATFLOWTOL = 13, /**< Tolerance for steady nodal inflow */
SM_THREADS = 14 /**< Number of Threads for this process */
} SM_SimSetting;

/// Node property codes
Expand Down
49 changes: 49 additions & 0 deletions src/solver/toolkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
#include "swmm5.h"
#include "toolkit.h"

// Protect against lack of compiler support for OpenMP
#if defined(_OPENMP)
#include <omp.h>
int alt_omp_get_max_threads(void)
{
return omp_get_max_threads();
}
#else
int alt_omp_get_max_threads(void) { return 1;}
#endif

// Function Declarations for API
int massbal_getRoutingTotal(SM_RoutingTotals **routingTot);
Expand Down Expand Up @@ -405,13 +415,52 @@ EXPORT_TOOLKIT int swmm_getSimulationParam(SM_SimSetting type, double *value)
case SM_SYSFLOWTOL: *value = SysFlowTol; break;
// Tolerance for steady nodal inflow
case SM_LATFLOWTOL: *value = LatFlowTol; break;
// Number of Threads (if OpenMP enabled)
case SM_THREADS: *value = NumThreads; break;
// Type not available
default: error_code = ERR_TKAPI_OUTBOUNDS; break;
}
}
return error_code;
}

EXPORT_TOOLKIT int swmm_setSimulationParam(SM_SimSetting type, double value)
///
/// Input: type = SM_SimSetting
/// Simulation Parameter
/// Returns: error code
/// Purpose: Set simulation analysis parameter (limited support)
{
int error_code = 0;

// Check if Open
if(swmm_IsOpenFlag() == FALSE)
{
error_code = ERR_TKAPI_INPUTNOTOPEN;
}
// Check if Simulation is Running
else if(swmm_IsStartedFlag() == TRUE)
{
error_code = ERR_TKAPI_SIM_NRUNNING;
}
else
{
switch(type)
{
case SM_THREADS:
{
// --- adjust number of parallel threads to be used
if ( (int)value <= 0 ) NumThreads = 1;
else NumThreads = MIN((int)value, alt_omp_get_max_threads());
if ( Nobjects[LINK] < 4 * NumThreads ) NumThreads = 1;
break;
}
default: error_code = ERR_TKAPI_OUTBOUNDS; break;
}
}
return error_code;
}

EXPORT_TOOLKIT int swmm_countObjects(SM_ObjectType type, int *count)
///
/// Input: type = object type (Based on SM_ObjectType enum)
Expand Down
11 changes: 11 additions & 0 deletions tests/solver/test_toolkit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ BOOST_FIXTURE_TEST_CASE(project_info_metric, FixtureMetric) {
error = swmm_getSimulationParam(SM_HEADTOL, &value);
BOOST_REQUIRE(error == ERR_NONE);
BOOST_CHECK_EQUAL(value, 0.015);

error = swmm_getSimulationParam(SM_THREADS, &value);
BOOST_REQUIRE(error == ERR_NONE);
BOOST_CHECK_EQUAL(value, 1);

#if defined(_OPENMP)
error = swmm_setSimulationParam(SM_THREADS, 2);
error = swmm_getSimulationParam(SM_THREADS, &value);
BOOST_REQUIRE(error == ERR_NONE);
BOOST_CHECK_EQUAL(value, 2);
#endif
}


Expand Down

0 comments on commit 033a9f7

Please sign in to comment.