Skip to content

Commit 8b0deaf

Browse files
committed
OMP environment variables override values defined in the configuration.
1 parent 70494e0 commit 8b0deaf

7 files changed

Lines changed: 163 additions & 73 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ if (ENABLE_OMP)
464464
# language bindings cant be linked against static libomp
465465
if (UNIX AND NOT APPLE AND (ENABLE_JAVA OR ENABLE_CSHARP OR ENABLE_PYTHON))
466466
set(_copasi_openmp_library_suffixes "${CMAKE_FIND_LIBRARY_SUFFIXES}")
467-
set(CMAKE_FIND_LIBRARY_SUFFIXES ".so")
467+
set(CMAKE_FIND_LIBRARY_SUFFIXES ".so;.so.0")
468468
endif()
469469

470470
if(MSVC)

copasi/CopasiSE/CopasiSE.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ void writeLogo()
12141214
if (NoLogo) return;
12151215

12161216
std::cout << "COPASI "
1217-
<< CVersion::VERSION.getVersion() << COpenMPConfig::Info() << std::endl
1217+
<< CVersion::VERSION.getVersion() << " " << COpenMPConfig::Info() << std::endl
12181218
<< "The use of this software indicates the acceptance of the attached license." << std::endl
12191219
<< "To view the license please use the option: --license" << std::endl
12201220
<< std::endl;

copasi/OpenMP/CContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const omp_sched_t omp_sched_monotonic = (omp_sched_t) 0x80000000;
3434
# define omp_get_max_threads() (1)
3535
# define omp_get_num_threads() (1)
3636
# define omp_get_thread_num() (0)
37+
# define omp_sched_static 1u
38+
# define omp_sched_dynamic 2u
39+
# define omp_sched_guided 3u
40+
# define omp_sched_auto 4u
41+
typedef unsigned int omp_sched_t;
42+
const omp_sched_t omp_sched_monotonic = (omp_sched_t) 0x80000000;
3743
#endif // USE_OMP
3844

3945
template < class Data > class CContext

copasi/OpenMP/COpenMPConfig.cpp

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,70 @@
66
#include "copasi/OpenMP/COpenMPConfig.h"
77
#include "copasi/OpenMP/CContext.h"
88

9-
#ifdef USE_OMP
10-
# include <omp.h>
11-
#else
12-
# define omp_sched_static 1u
13-
# define omp_sched_dynamic 2u
14-
# define omp_sched_guided 3u
15-
# define omp_sched_auto 4u
16-
typedef unsigned int omp_sched_t;
17-
#endif // USE_OMP
9+
// static
10+
const COpenMPConfig::ScheduleStrategyName COpenMPConfig::ScheduleStrategyNames({"static",
11+
"dynamic",
12+
"guided",
13+
"automatic"});
14+
15+
// static
16+
const CEnumAnnotation< omp_sched_t, COpenMPConfig::ScheduleStrategy > COpenMPConfig::ScheduleStrategyOpenMP({omp_sched_static,
17+
omp_sched_dynamic,
18+
omp_sched_guided,
19+
omp_sched_auto});
1820

1921
// static
20-
const COpenMPConfig::ScheduleStrategyName COpenMPConfig::ScheduleStrategyNames({
21-
"static",
22-
"dynamic",
23-
"guided",
24-
"automatic"
25-
});
22+
const COpenMPConfig::MonotonicName COpenMPConfig::MonotonicNames({"nonmonotonic",
23+
"monotonic"});
2624

2725
// static
28-
const CEnumAnnotation< omp_sched_t, COpenMPConfig::ScheduleStrategy > ScheduleStrategyOpenMP({
29-
omp_sched_static,
30-
omp_sched_dynamic,
31-
omp_sched_guided,
32-
omp_sched_auto
33-
});
26+
const CEnumAnnotation< omp_sched_t, COpenMPConfig::Monotonic > COpenMPConfig::MonotonicOpenMP({(omp_sched_t) 0x0, // nonmonotonic
27+
omp_sched_monotonic});
3428

3529
// static
3630
const int COpenMPConfig::MaxNumThreads = omp_get_max_threads();
3731

32+
struct _ScheduleStrategyOpenMP
33+
{
34+
COpenMPConfig::ScheduleStrategy scheduleStrategy = COpenMPConfig::ScheduleStrategy::Static;
35+
COpenMPConfig::Monotonic monotonicity = COpenMPConfig::Monotonic::nonmonotonic;
36+
C_UINT32 chunkSize = 0;
37+
};
38+
39+
_ScheduleStrategyOpenMP getSchedule()
40+
{
41+
std::string OMP_SCHEDULE = COptions::getEnvironmentVariable("OMP_SCHEDULE");
42+
std::string Monotonic = "";
43+
44+
std::string::size_type Pos = OMP_SCHEDULE.find(":");
45+
46+
if (Pos != std::string::npos)
47+
{
48+
Monotonic = OMP_SCHEDULE.substr(0, Pos);
49+
OMP_SCHEDULE = OMP_SCHEDULE.substr(Pos + 1);
50+
51+
#ifndef OMP_HAVE_MONOTONIC
52+
Monotonic = "nonmonotonic";
53+
#endif // OMP_HAVE_MONOTONIC
54+
}
55+
56+
std::string ChunkSize;
57+
Pos = OMP_SCHEDULE.find(",");
58+
59+
if (Pos != std::string::npos)
60+
{
61+
ChunkSize = OMP_SCHEDULE.substr(Pos + 1);
62+
OMP_SCHEDULE = OMP_SCHEDULE.substr(0, Pos);
63+
}
64+
65+
_ScheduleStrategyOpenMP ScheduleStrategyOpenMP;
66+
ScheduleStrategyOpenMP.scheduleStrategy = COpenMPConfig::ScheduleStrategyNames.toEnum(OMP_SCHEDULE);
67+
ScheduleStrategyOpenMP.monotonicity = COpenMPConfig::MonotonicNames.toEnum(Monotonic);
68+
ScheduleStrategyOpenMP.chunkSize = (C_UINT32) ChunkSize.empty() ? -1 : std::stoi(ChunkSize);
69+
70+
return ScheduleStrategyOpenMP;
71+
}
72+
3873
COpenMPConfig::COpenMPConfig(const std::string & name,
3974
const CDataContainer * pParent)
4075
: CCopasiParameterGroup(name, pParent)
@@ -82,11 +117,26 @@ COpenMPConfig & COpenMPConfig::operator=(const COpenMPConfig & rhs)
82117

83118
void COpenMPConfig::apply() const
84119
{
120+
std::string OMP_NUM_THREADS = COptions::getEnvironmentVariable("OMP_NUM_THREADS");
121+
C_UINT32 MaxNumThreads = OMP_NUM_THREADS.empty() ? *mpMaxNumThreads : std::stoi(OMP_NUM_THREADS);
122+
123+
_ScheduleStrategyOpenMP Schedule = getSchedule();
124+
std::string ScheduleStrategy = Schedule.scheduleStrategy == ScheduleStrategy::__SIZE ? *mpScheduleStrategy : ScheduleStrategyNames[Schedule.scheduleStrategy];
125+
std::string Monotonic = Schedule.monotonicity == Monotonic::__SIZE ? *mpMonotonic : MonotonicNames[Schedule.monotonicity];
126+
C_UINT32 ChunkSize = Schedule.chunkSize == (C_UINT32) -1 ? *mpChunkSize : Schedule.chunkSize;
127+
128+
bool IsEnabled = *mpIsEnabled || !OMP_NUM_THREADS.empty() || Schedule.scheduleStrategy != ScheduleStrategy::__SIZE;
129+
85130
#ifdef USE_OMP
86-
if (*mpIsEnabled)
131+
if (IsEnabled)
87132
{
88-
omp_set_num_threads(*mpMaxNumThreads);
89-
omp_set_schedule(ScheduleStrategyOpenMP[ScheduleStrategyNames.toEnum(*mpScheduleStrategy)], 0);
133+
omp_set_num_threads(MaxNumThreads);
134+
omp_sched_t OpenMPStrategy =
135+
ScheduleStrategyOpenMP[ScheduleStrategyNames.toEnum(ScheduleStrategy, ScheduleStrategy::Static)];
136+
omp_sched_t OpenMPMonotonicity =
137+
MonotonicOpenMP[MonotonicNames.toEnum(Monotonic, Monotonic::nonmonotonic)];
138+
139+
omp_set_schedule(omp_sched_t(OpenMPStrategy | OpenMPMonotonicity), ChunkSize);
90140
}
91141
else
92142
{
@@ -146,31 +196,48 @@ bool COpenMPConfig::setScheduleStrategy(const std::string & scheduleStrategy)
146196

147197
void COpenMPConfig::initializeParameter()
148198
{
149-
mpIsEnabled = assertParameter("Enabled", CCopasiParameter::Type::BOOL, false);
150-
mpMaxNumThreads = assertParameter("Max Number Threads", CCopasiParameter::Type::UINT, (C_UINT32) ceil(MaxNumThreads / 2.0));
151-
mpScheduleStrategy = assertParameter("Schedule Strategy", CCopasiParameter::Type::STRING, std::string("static"));
199+
std::string OMP_NUM_THREADS = COptions::getEnvironmentVariable("OMP_NUM_THREADS");
200+
C_UINT32 MaxNumThreads = OMP_NUM_THREADS.empty() ? ceil(MaxNumThreads / 2.0) : std::stoi(OMP_NUM_THREADS);
201+
202+
_ScheduleStrategyOpenMP Schedule = getSchedule();
203+
204+
bool IsEnabled = false || !OMP_NUM_THREADS.empty() || Schedule.scheduleStrategy != ScheduleStrategy::__SIZE;
205+
206+
mpIsEnabled = assertParameter("Enabled", CCopasiParameter::Type::BOOL, IsEnabled);
207+
mpMaxNumThreads = assertParameter("Max Number Threads", CCopasiParameter::Type::UINT, MaxNumThreads);
208+
mpScheduleStrategy = assertParameter("Schedule Strategy", CCopasiParameter::Type::STRING, ScheduleStrategyNames[Schedule.scheduleStrategy != ScheduleStrategy::__SIZE ? Schedule.scheduleStrategy : ScheduleStrategy::Static]);
209+
mpMonotonic = assertParameter("Monotonicity", CCopasiParameter::Type::STRING, MonotonicNames[Schedule.monotonicity != Monotonic::__SIZE ? Schedule.monotonicity : Monotonic::nonmonotonic]);
210+
mpChunkSize = assertParameter("Chunk Size", CCopasiParameter::Type::UINT, Schedule.chunkSize == (C_UINT32) -1 ? 0 : Schedule.chunkSize);
152211

153212
CCopasiParameter * pMaxNumThreadsParameter = getParameter("Max Number Threads");
154-
pMaxNumThreadsParameter->setValidValues(std::vector<std::pair<C_UINT32, C_UINT32>>({{1, MaxNumThreads}}));
213+
pMaxNumThreadsParameter->setValidValues(std::vector< std::pair< C_UINT32, C_UINT32 > >({{1, MaxNumThreads}}));
155214

156215
CCopasiParameter * pScheduleStrategyParameter = getParameter("Schedule Strategy");
157216
pScheduleStrategyParameter->setValidValues(ScheduleStrategyNames);
217+
218+
CCopasiParameter * pMonotonicParameter = getParameter("Monotonicity");
219+
pMonotonicParameter->setValidValues(MonotonicNames);
158220
}
159221

160222
std::string COpenMPConfig::Info()
161223
{
162224
std::ostringstream Info;
163225

164226
#ifdef USE_OMP
165-
Info << " (OpenMP threads: " << omp_get_max_threads() << " schedule: ";
227+
Info << "OpenMP " << COPASI_OMP_VERSION << " (threads: " << omp_get_max_threads() << ", schedule: ";
166228
omp_sched_t Schedule;
167229
int Chunk;
168230
omp_get_schedule(&Schedule, &Chunk);
169231

170232
// Schedule may have the monotonic modifier, we masked it.
171-
Schedule = (omp_sched_t)(Schedule & ~omp_sched_monotonic);
233+
if (Schedule & omp_sched_monotonic)
234+
{
235+
Schedule = (omp_sched_t) (Schedule & ~omp_sched_monotonic);
236+
Info << "monotonic:";
237+
}
238+
172239
Info << ScheduleStrategyNames[ScheduleStrategyOpenMP.toEnum(Schedule)];
173-
Info << "," << Chunk << ")";
240+
Info << ", chunk size: " << Chunk << ")";
174241
#endif // USE_OMP
175242

176243
return Info.str();

copasi/OpenMP/COpenMPConfig.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include "copasi/utilities/CCopasiParameter.h"
9+
#include "copasi/commandline/COptions.h"
910

1011
/**
1112
* COpenMPConfig class.
@@ -26,6 +27,18 @@ class COpenMPConfig : public CCopasiParameterGroup
2627

2728
typedef CEnumAnnotation< std::string, ScheduleStrategy > ScheduleStrategyName;
2829
static const ScheduleStrategyName ScheduleStrategyNames;
30+
static const CEnumAnnotation< omp_sched_t, ScheduleStrategy > ScheduleStrategyOpenMP;
31+
32+
enum struct Monotonic
33+
{
34+
nonmonotonic = 0,
35+
monotonic,
36+
__SIZE
37+
};
38+
39+
typedef CEnumAnnotation< std::string, Monotonic > MonotonicName;
40+
static const MonotonicName MonotonicNames;
41+
static const CEnumAnnotation< omp_sched_t, Monotonic > MonotonicOpenMP;
2942

3043
static const int MaxNumThreads;
3144

@@ -36,7 +49,7 @@ class COpenMPConfig : public CCopasiParameterGroup
3649
* @param const std::string & name (default: MIRIAM Resource)
3750
* @param const CDataContainer * pParent (default: NULL)
3851
*/
39-
COpenMPConfig(const std::string & name = "OpenMP Configuration",
52+
COpenMPConfig(const std::string & name = "Parallel Processing",
4053
const CDataContainer * pParent = NO_PARENT);
4154

4255
/**
@@ -90,7 +103,11 @@ class COpenMPConfig : public CCopasiParameterGroup
90103

91104
bool *mpIsEnabled;
92105

93-
unsigned C_INT32 *mpMaxNumThreads;
106+
C_UINT32 *mpMaxNumThreads;
94107

95108
std::string *mpScheduleStrategy;
109+
110+
std::string *mpMonotonic;
111+
112+
C_UINT32 *mpChunkSize;
96113
};

copasi/UI/AboutDialog.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
// Copyright (C) 2019 - 2024 by Pedro Mendes, Rector and Visitors of the
2-
// University of Virginia, University of Heidelberg, and University
3-
// of Connecticut School of Medicine.
4-
// All rights reserved.
5-
6-
// Copyright (C) 2017 - 2018 by Pedro Mendes, Virginia Tech Intellectual
7-
// Properties, Inc., University of Heidelberg, and University of
8-
// of Connecticut School of Medicine.
9-
// All rights reserved.
10-
11-
// Copyright (C) 2010 - 2016 by Pedro Mendes, Virginia Tech Intellectual
12-
// Properties, Inc., University of Heidelberg, and The University
13-
// of Manchester.
14-
// All rights reserved.
15-
16-
// Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
17-
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
18-
// and The University of Manchester.
19-
// All rights reserved.
20-
21-
// Copyright (C) 2004 - 2007 by Pedro Mendes, Virginia Tech Intellectual
22-
// Properties, Inc. and EML Research, gGmbH.
23-
// All rights reserved.
1+
// Copyright (C) 2019 - 2026 by Pedro Mendes, Rector and Visitors of the
2+
// University of Virginia, University of Heidelberg, and University
3+
// of Connecticut School of Medicine.
4+
// All rights reserved.
5+
6+
// Copyright (C) 2017 - 2018 by Pedro Mendes, Virginia Tech Intellectual
7+
// Properties, Inc., University of Heidelberg, and University of
8+
// of Connecticut School of Medicine.
9+
// All rights reserved.
10+
11+
// Copyright (C) 2010 - 2016 by Pedro Mendes, Virginia Tech Intellectual
12+
// Properties, Inc., University of Heidelberg, and The University
13+
// of Manchester.
14+
// All rights reserved.
15+
16+
// Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
17+
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
18+
// and The University of Manchester.
19+
// All rights reserved.
20+
21+
// Copyright (C) 2004 - 2007 by Pedro Mendes, Virginia Tech Intellectual
22+
// Properties, Inc. and EML Research, gGmbH.
23+
// All rights reserved.
2424

2525
/*!
2626
\file AboutDialog.cpp
@@ -46,7 +46,7 @@
4646
#include <copasi/UI/qtUtilities.h>
4747
#include <copasi/math/CJitCompiler.h>
4848
#include <copasi/utilities/CVersion.h>
49-
#include "copasi/OpenMP/CContext.h"
49+
#include "copasi/OpenMP/COpenMPConfig.h"
5050

5151
#include <copasi/utilities/json.hpp>
5252

@@ -110,34 +110,34 @@ const char *AboutDialog::text =
110110
QString AboutDialog::getDefaultVersionText()
111111
{
112112
QString additionalVersion;
113-
#ifdef COPASI_USE_RAPTOR
113+
#ifdef COPASI_USE_RAPTOR
114114
additionalVersion = QString("<li>raptor %1</li>").arg(COPASI_RAPTOR_VERSION);
115-
#endif
115+
#endif
116116

117-
// add additional version info here.
117+
// add additional version info here.
118118
additionalVersion += QString("<li>json for Modern C++ version %1.%2.%3</li>")
119119
.arg(NLOHMANN_JSON_VERSION_MAJOR)
120120
.arg(NLOHMANN_JSON_VERSION_MINOR)
121121
.arg(NLOHMANN_JSON_VERSION_PATCH);
122122

123123
additionalVersion += QString("<li>statslib 3.4.0</li><li>GCEM 1.18.0</li>");
124124

125-
#ifdef COPASI_USE_QCUSTOMPLOT
125+
#ifdef COPASI_USE_QCUSTOMPLOT
126126
additionalVersion += QString("<li>QCustomPlot %1</li>").arg(QCUSTOMPLOT_VERSION_STR);
127-
#endif
127+
#endif
128128

129-
#ifdef COPASI_USE_CROSSGUID
129+
#ifdef COPASI_USE_CROSSGUID
130130
additionalVersion += QString("<li>CrossGuid</li>");
131-
#else
131+
#else
132132
additionalVersion += QString("<li>stduuid</li>");
133-
#endif
133+
#endif
134134

135-
#ifdef ENABLE_OMP
136-
additionalVersion += QString("<li>OpenMP %1, %2 threads</li>").arg(COPASI_OMP_VERSION).arg(omp_get_num_threads());
137-
#endif
135+
#ifdef ENABLE_OMP
136+
additionalVersion += QString("<li>%1 </li>").arg(FROM_UTF8(COpenMPConfig::Info()));
137+
#endif
138138

139139
return QString(AboutDialog::text)
140-
.arg(FROM_UTF8(CVersion::VERSION.getVersion())) // 1
140+
.arg(FROM_UTF8(CVersion::VERSION.getVersion())) // 1
141141
.arg(QT_VERSION_STR) // 2
142142
.arg(QWT_VERSION_STR) // 3
143143
.arg(COPASI_EXPAT_VERSION) // 4

copasi/commandline/CConfigurationFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ bool CConfigurationFile::elevateChildren()
217217
}
218218

219219
mpOpenMPConfig =
220-
elevate<COpenMPConfig, CCopasiParameterGroup>(getGroup("OpenMP Configuration"));
220+
elevate<COpenMPConfig, CCopasiParameterGroup>(getGroup("Parallel Processing"));
221221

222222
return success;
223223
}
@@ -302,7 +302,7 @@ void CConfigurationFile::initializeParameter()
302302
assertGroup("Check for Updates");
303303

304304
mpDisableJIT = assertParameter("Disable JIT Compilation", CCopasiParameter::Type::BOOL, false);
305-
assertGroup("OpenMP Configuration");
305+
assertGroup("Parallel Processing");
306306

307307
elevateChildren();
308308
}

0 commit comments

Comments
 (0)