Skip to content

Commit 892185a

Browse files
committed
All population based methods are parallelized and ready for testing.
1 parent a871c39 commit 892185a

51 files changed

Lines changed: 462 additions & 313 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

copasi/OpenMP/CMethodContext.h

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,58 @@
66
#ifndef COPASI_CMethodContext
77
#define COPASI_CMethodContext
88

9-
#include "copasi/OpenMP/CPointerMathContext.h"
10-
#include "copasi/utilities/CCopasiMethod.h"
9+
#include "copasi/OpenMP/CProblemContext.h"
1110

12-
typedef CPointerMathContext< CMathContainer > CMethodContext;
11+
template < class Method >
12+
class CMethodContext : public CPointerMathContext< Method >
13+
{
14+
public:
15+
typedef CPointerMathContext< Method > Base;
16+
17+
CMethodContext() = delete;
18+
19+
CMethodContext(const CMethodContext & src) = delete;
20+
21+
CMethodContext(const bool & parallel, CDataContainer * pParent);
22+
23+
~CMethodContext();
24+
25+
/**
26+
* Set the pointer to problem used for calculations
27+
* @param CProblemContext * pContext
28+
*/
29+
template < class Problem >
30+
bool setProblemContext(CProblemContext< Problem > & Context);
31+
};
32+
33+
template < class Method >
34+
CMethodContext< Method >::CMethodContext(const bool & parallel, CDataContainer * pParent)
35+
: Base(parallel, pParent)
36+
{}
37+
38+
template < class Method >
39+
CMethodContext< Method >::~CMethodContext()
40+
{}
41+
42+
template < class Method >
43+
template < class Problem >
44+
bool CMethodContext< Method >::setProblemContext(CProblemContext< Problem > & Context)
45+
{
46+
bool success = false;
47+
48+
if (Base::master() != NULL)
49+
{
50+
success = true;
51+
52+
success &= Base::master()->setProblem(Context.master());
53+
54+
if (Base::size() > 1)
55+
#pragma omp parallel for reduce(&: success)
56+
for (size_t i = 0; i < Base::size(); ++i)
57+
success &= Base::threadData()[i]->setProblem(Context.threadData()[i]);
58+
}
59+
60+
return success;
61+
}
1362

1463
#endif // COPASI_CMethodContext

copasi/OpenMP/CProblemContext.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
#include "copasi/OpenMP/CPointerMathContext.h"
1010
#include "copasi/utilities/CCopasiProblem.h"
1111

12-
typedef CPointerMathContext< CCopasiProblem > CProblemContext;
12+
template < class Problem >
13+
class CProblemContext : public CPointerMathContext< Problem >
14+
{
15+
public:
16+
typedef CPointerMathContext< Problem > Base;
17+
18+
CProblemContext() = delete;
19+
20+
CProblemContext(const CProblemContext & src) = delete;
21+
22+
CProblemContext(const bool & parallel, CDataContainer * pParent);
23+
24+
~CProblemContext();
25+
};
26+
27+
template < class Problem >
28+
CProblemContext< Problem >::CProblemContext(const bool & parallel, CDataContainer * pParent)
29+
: Base(parallel, pParent)
30+
{}
31+
32+
template < class Problem >
33+
CProblemContext< Problem >::~CProblemContext()
34+
{}
1335

1436
#endif // COPASI_CProblemContext

copasi/lyap/CLyapMethod.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ CLyapMethod::~CLyapMethod()
9595
* This method is used by CLyap
9696
* @param "CLyapProblem *" problem
9797
*/
98-
void CLyapMethod::setProblem(CLyapProblem * problem)
99-
{mpProblem = problem;}
98+
bool CLyapMethod::setProblem(CCopasiProblem * pProblem)
99+
{
100+
mpProblem = dynamic_cast< CLyapProblem * >(pProblem);
101+
102+
return mpProblem != nullptr;
103+
}
100104

101105
/**
102106
* This instructs the method to calculate a a time step of deltaT

copasi/lyap/CLyapMethod.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class CLyapMethod : public CCopasiMethod
120120
* This method is used by CLyap
121121
* @param "CLyapProblem *" problem
122122
*/
123-
void setProblem(CLyapProblem * problem);
123+
virtual bool setProblem(CCopasiProblem * pProblem) override;
124124

125125
/**
126126
* This instructs the method to calculate a time step of deltaT

copasi/moieties/CMoietiesMethod.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ bool CMoietiesMethod::process()
5757
return true;
5858
}
5959

60-
void CMoietiesMethod::setProblem(CMoietiesProblem * pProblem)
60+
bool CMoietiesMethod::setProblem(CCopasiProblem * pProblem)
6161
{
62-
mpProblem = pProblem;
62+
mpProblem = dynamic_cast< CMoietiesProblem * >(pProblem);
63+
64+
return mpProblem != nullptr;
6365
}

copasi/moieties/CMoietiesMethod.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class CMoietiesMethod : public CCopasiMethod
6767
* Set the problem
6868
* @param CMoietiesProblem * pProblem
6969
*/
70-
void setProblem(CMoietiesProblem * pProblem);
70+
virtual bool setProblem(CCopasiProblem * pProblem) override;
7171

7272
// Attributes
7373
protected:

copasi/optimization/COptMethod.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ COptMethod::COptMethod(const CDataContainer * pParent,
5959
}
6060

6161
COptMethod::COptMethod(const COptMethod & src,
62-
const CDataContainer * pParent)
62+
const CDataContainer * pParent,
63+
const bool & parallel)
6364
: CCopasiMethod(src, pParent)
6465
, mpParentTask(src.mpParentTask)
65-
, mParallel(src.mParallel)
66+
, mParallel(parallel)
6667
, mMathContext(src.mParallel)
6768
, mProblemContext(src.mParallel, this)
6869
, mLogVerbosity(src.mLogVerbosity)
@@ -104,10 +105,12 @@ void COptMethod::reflect(COptProblem * pProblem, const C_FLOAT64 & bestValue, C_
104105
}
105106
*/
106107

107-
void COptMethod::setProblem(COptProblem * pProblem)
108+
bool COptMethod::setProblem(CCopasiProblem * pProblem)
108109
{
109-
mProblemContext.setMaster(pProblem);
110+
mProblemContext.setMaster(dynamic_cast< COptProblem * >(pProblem));
110111
mProblemContext.setMathContext(mMathContext);
112+
113+
return mProblemContext.master() != nullptr;
111114
}
112115

113116
//virtual C_INT32 COptMethod::Optimise(C_FLOAT64 (*func) (void))

copasi/optimization/COptMethod.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
#include <string>
4444

45-
#include "copasi/OpenMP/CPointerMathContext.h"
45+
#include "copasi/OpenMP/CProblemContext.h"
4646
#include "copasi/utilities/CCopasiMethod.h"
4747
#include "copasi/optimization/COptLog.h"
4848

@@ -51,7 +51,6 @@ class COptItem;
5151
class COptTask;
5252
template < class CType > class CVector;
5353

54-
typedef CPointerMathContext< COptProblem > COptProblemContext;
5554

5655
// YOHE: this is an abstract class that contains many virtual functions
5756
// without definitions
@@ -92,7 +91,7 @@ class COptMethod : public CCopasiMethod
9291
/**
9392
* A thread specific problem
9493
*/
95-
COptProblemContext mProblemContext;
94+
CProblemContext< COptProblem > mProblemContext;
9695

9796
/**
9897
* Define the current verbosity for the log
@@ -130,7 +129,8 @@ class COptMethod : public CCopasiMethod
130129
* @param const CDataContainer * pParent (default: NULL)
131130
*/
132131
COptMethod(const COptMethod & src,
133-
const CDataContainer * pParent);
132+
const CDataContainer * pParent,
133+
const bool & parallel);
134134

135135
/**
136136
* Destructor
@@ -149,7 +149,7 @@ class COptMethod : public CCopasiMethod
149149
* Set the problem to be optimized
150150
* @param "COptProblem *" problem
151151
*/
152-
void setProblem(COptProblem * problem);
152+
virtual bool setProblem(CCopasiProblem * pProblem) override;
153153

154154
/**
155155
* Initialize arrays and pointer.

copasi/optimization/COptMethodCoranaWalk.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ COptMethodCoranaWalk::COptMethodCoranaWalk(const CDataContainer * pParent,
6060

6161
COptMethodCoranaWalk::COptMethodCoranaWalk(const COptMethodCoranaWalk & src,
6262
const CDataContainer * pParent)
63-
: COptMethod(src, pParent)
63+
: COptMethod(src, pParent, false)
6464
, mTemperature(src.mTemperature)
6565
, mhIterations(C_INVALID_INDEX)
6666
, mIterations(src.mIterations)
@@ -102,7 +102,7 @@ bool COptMethodCoranaWalk::optimise()
102102
C_FLOAT64 xc, p, c, New, minstep;
103103
bool processing;
104104

105-
const std::vector< COptItem * > & OptItemList = mProblemContext.master()->getOptItemList(true);
105+
const std::vector< COptItem * > & OptItemList = mProblemContext.active()->getOptItemList(true);
106106

107107
// set the minimum step size as being the average of step sizes
108108
// or 100*DBL_EPSILON if average is zero
@@ -190,7 +190,7 @@ bool COptMethodCoranaWalk::optimise()
190190
signalStop();
191191

192192
// Check all functional constraints
193-
if (!mProblemContext.master()->checkFunctionalConstraints())
193+
if (!mProblemContext.active()->checkFunctionalConstraints())
194194
{
195195
// Undo since not accepted
196196
OptItem.setItemValue(mCurrent[h], COptItem::CheckPolicyFlag::None);
@@ -253,10 +253,10 @@ bool COptMethodCoranaWalk::optimise()
253253

254254
if (processing)
255255
{
256-
mCurrent = mProblemContext.master()->getSolutionVariables(true);
256+
mCurrent = mProblemContext.active()->getSolutionVariables(true);
257257

258258
for (a = 0; a < mVariableSize; a++)
259-
mProblemContext.master()->getOptItemList(true)[a]->setItemValue(mCurrent[a], COptItem::CheckPolicyFlag::None);
259+
mProblemContext.active()->getOptItemList(true)[a]->setItemValue(mCurrent[a], COptItem::CheckPolicyFlag::None);
260260

261261
mCurrentValue = getBestValue();
262262
}
@@ -304,7 +304,7 @@ bool COptMethodCoranaWalk::initialize()
304304
mProcessReport.addItem("Iterations",
305305
mCurrentIteration, &mIterations);
306306

307-
mVariableSize = mProblemContext.master()->getOptItemList(true).size();
307+
mVariableSize = mProblemContext.active()->getOptItemList(true).size();
308308

309309
mCurrent.resize(mVariableSize);
310310
mStep.resize(mVariableSize);

copasi/optimization/COptMethodDE.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ COptMethodDE::COptMethodDE(const CDataContainer * pParent,
5252
}
5353

5454
COptMethodDE::COptMethodDE(const COptMethodDE & src,
55-
const CDataContainer * pParent)
56-
: COptPopulationMethod(src, pParent)
55+
const CDataContainer * pParent,
56+
const bool & parallel)
57+
: COptPopulationMethod(src, pParent, parallel)
5758
, mpPermutation(NULL)
5859
, mMutationVariance(0.1)
5960
, mStopAfterStalledGenerations(0)
@@ -70,12 +71,13 @@ bool COptMethodDE::replicate()
7071
size_t i, j;
7172
size_t a, b, c;
7273

73-
const std::vector< COptItem * > & OptItemList = mProblemContext.master()->getOptItemList(true);
7474
mpPermutation->shuffle();
7575

7676
#pragma omp parallel for schedule(runtime)
7777
for (i = mPopulationSize; i < 2 * mPopulationSize; i++)
7878
{
79+
const std::vector< COptItem * > & OptItemList = mProblemContext.active()->getOptItemList(true);
80+
7981
// MUTATION a, b, c in [0, mPopulationSize - 1]
8082
a = mpPermutation->pick();
8183
// b is guaranteed to be different from a
@@ -100,17 +102,20 @@ bool COptMethodDE::replicate()
100102
#pragma omp parallel for schedule(runtime)
101103
for (i = 2 * mPopulationSize; i < 3 * mPopulationSize; i++)
102104
{
105+
const std::vector< COptItem * > & OptItemList = mProblemContext.active()->getOptItemList(true);
106+
CRandom * pRandom = mRandomContext.active();
107+
103108
for (j = 0; j < mVariableSize; j++)
104109
{
105110
COptItem & OptItem = *OptItemList[j];
106111
C_FLOAT64 & mut = (*mIndividuals[i])[j];
107112

108-
size_t r = mRandomContext.master()->getRandomU(mPopulationSize - 1);
113+
size_t r = pRandom->getRandomU(mPopulationSize - 1);
109114

110115
if (r < 0.6 * mPopulationSize)
111116
{
112117
mut = (*mIndividuals[i - mPopulationSize])[j] *
113-
mRandomContext.master()->getRandomNormal(1, mMutationVariance);
118+
pRandom->getRandomNormal(1, mMutationVariance);
114119
}
115120
else
116121
mut = (*mIndividuals[i - 2 * mPopulationSize])[j];

0 commit comments

Comments
 (0)