139 |
#include "ecosystem.h" |
#include "ecosystem.h" |
140 |
#include "global.h" |
#include "global.h" |
141 |
|
|
142 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
143 |
#include "omp.h" |
#include "omp.h" |
144 |
#endif |
#endif |
145 |
|
|
146 |
extern Ecosystem* EcoSystem; |
extern Ecosystem* EcoSystem; |
147 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
148 |
extern Ecosystem** EcoSystems; |
extern Ecosystem** EcoSystems; |
149 |
#endif |
#endif |
150 |
|
|
156 |
DoubleVector z(point); |
DoubleVector z(point); |
157 |
|
|
158 |
minf = prevbest; |
minf = prevbest; |
|
// for (int k=0;k<point.Size(); k++) |
|
|
// cout << z[k] ; |
|
|
// cout << endl; |
|
159 |
for (i = 0; i < point.Size(); i++) { |
for (i = 0; i < point.Size(); i++) { |
|
|
|
|
// for (int k=0;k<point.Size(); k++) |
|
|
// cout << z[k] << " " ; |
|
|
//cout << endl; |
|
160 |
z[param[i]] = point[param[i]] + delta[param[i]]; |
z[param[i]] = point[param[i]] + delta[param[i]]; |
161 |
ftmp = EcoSystem->SimulateAndUpdate(z); |
ftmp = EcoSystem->SimulateAndUpdate(z); |
|
// cout << i <<"-z["<< param[i]<<"]:" <<z[param[i]] << " - " << ftmp << endl; |
|
162 |
if (ftmp < minf) { |
if (ftmp < minf) { |
163 |
minf = ftmp; |
minf = ftmp; |
164 |
} else { |
} else { |
170 |
else |
else |
171 |
z[param[i]] = point[param[i]]; |
z[param[i]] = point[param[i]]; |
172 |
} |
} |
|
// cout << i <<"-z["<< param[i]<<"]:" <<z[param[i]] << " - " << ftmp <<" - " << prevbest << endl; |
|
173 |
} |
} |
174 |
|
|
175 |
for (i = 0; i < point.Size(); i++) |
for (i = 0; i < point.Size(); i++) |
178 |
} |
} |
179 |
|
|
180 |
/* given a point, look for a better one nearby, one coord at a time */ |
/* given a point, look for a better one nearby, one coord at a time */ |
181 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
182 |
/* |
/* |
183 |
* function bestBeraby parallelized with OpenMP |
* function bestBeraby parallelized with OpenMP |
184 |
* · 2 threads per coord to parallelize the calculation of +delta/-delta |
* · 2 threads per coord to parallelize the calculation of +delta/-delta |
185 |
* · parallelize the calculation of the best nearby of the coord |
* · parallelize the calculation of the best nearby of the coord |
186 |
*/ |
*/ |
187 |
double OptInfoHooke::bestNearbyOMP(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { |
double OptInfoHooke::bestNearbyRepro(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { |
188 |
double minf;//, ftmp; |
double minf;//, ftmp; |
189 |
int i, j, k; |
int i, j, k; |
190 |
DoubleVector z(point); |
DoubleVector z(point); |
209 |
return -1; |
return -1; |
210 |
} |
} |
211 |
|
|
212 |
|
omp_set_dynamic(0); |
213 |
|
omp_set_nested(1); //permit the nested parallelization |
214 |
while ( i < nvars) { |
while ( i < nvars) { |
215 |
if ((i + paral_tokens -1) >= nvars) |
if ((i + paral_tokens -1) >= nvars) |
216 |
paral_tokens = nvars - i; |
paral_tokens = nvars - i; |
217 |
omp_set_dynamic(0); |
#pragma omp parallel for num_threads(paral_tokens) private(k) //parallelize the parameters (numThr/2) |
|
omp_set_nested(1); |
|
|
#pragma omp parallel for num_threads(paral_tokens) private(k) |
|
218 |
for (j = 0; j < paral_tokens; ++j) { |
for (j = 0; j < paral_tokens; ++j) { |
219 |
storage[j].z = z; |
storage[j].z = z; |
220 |
storage[j].delta = delta; |
storage[j].delta = delta; |
224 |
v1[k] += delta[k]; |
v1[k] += delta[k]; |
225 |
v2[k] -= delta[k]; |
v2[k] -= delta[k]; |
226 |
|
|
227 |
#pragma omp parallel sections num_threads(2) |
#pragma omp parallel sections num_threads(2) //parrallelize the +/- delta simulation for each parameter |
228 |
{ |
{ |
229 |
#pragma omp section |
#pragma omp section |
230 |
{ |
{ |
260 |
} |
} |
261 |
} |
} |
262 |
} |
} |
263 |
|
delete[] storage; |
264 |
for (i = 0; i < nvars; ++i) |
for (i = 0; i < nvars; ++i) |
265 |
point[i] = z[i]; |
point[i] = z[i]; |
266 |
return minf; |
return minf; |
289 |
IntVector trapped(nvars, 0); |
IntVector trapped(nvars, 0); |
290 |
|
|
291 |
EcoSystem->scaleVariables(); |
EcoSystem->scaleVariables(); |
292 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
293 |
int numThr = omp_get_max_threads ( ); |
int numThr = omp_get_max_threads ( ); |
294 |
for (i = 0; i < numThr; i++) |
for (i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread |
295 |
EcoSystems[i]->scaleVariables(); |
EcoSystems[i]->scaleVariables(); |
296 |
#endif |
#endif |
297 |
EcoSystem->getOptScaledValues(x); |
EcoSystem->getOptScaledValues(x); |
334 |
|
|
335 |
while (1) { |
while (1) { |
336 |
if (isZero(bestf)) { |
if (isZero(bestf)) { |
337 |
#ifdef NO_OPENMP |
#ifndef _OPENMP |
338 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
339 |
#endif |
#endif |
340 |
handle.logMessage(LOGINFO, "Error in Hooke & Jeeves optimisation after", iters, "function evaluations, f(x) = 0"); |
handle.logMessage(LOGINFO, "Error in Hooke & Jeeves optimisation after", iters, "function evaluations, f(x) = 0"); |
359 |
/* find best new point, one coord at a time */ |
/* find best new point, one coord at a time */ |
360 |
for (i = 0; i < nvars; i++) |
for (i = 0; i < nvars; i++) |
361 |
trialx[i] = x[i]; |
trialx[i] = x[i]; |
362 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
363 |
newf = this->bestNearbyOMP(delta, trialx, bestf, param); |
newf = this->bestNearbyRepro(delta, trialx, bestf, param); |
364 |
if (newf == -1) { |
if (newf == -1) { |
365 |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
366 |
handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); |
handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); |
371 |
#endif |
#endif |
372 |
/* if too many function evaluations occur, terminate the algorithm */ |
/* if too many function evaluations occur, terminate the algorithm */ |
373 |
|
|
374 |
#ifdef NO_OPENMP |
#ifndef _OPENMP |
375 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
376 |
#endif |
#endif |
377 |
if (iters > hookeiter) { |
if (iters > hookeiter) { |
441 |
/* only move forward if this is really an improvement */ |
/* only move forward if this is really an improvement */ |
442 |
oldf = newf; |
oldf = newf; |
443 |
newf = EcoSystem->SimulateAndUpdate(trialx); |
newf = EcoSystem->SimulateAndUpdate(trialx); |
444 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
445 |
iters++; |
iters++; |
446 |
#endif |
#endif |
447 |
if ((isEqual(newf, oldf)) || (newf > oldf)) { |
if ((isEqual(newf, oldf)) || (newf > oldf)) { |
454 |
for (i = 0; i < nvars; i++) |
for (i = 0; i < nvars; i++) |
455 |
x[i] = trialx[i]; |
x[i] = trialx[i]; |
456 |
|
|
457 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
458 |
newf = this->bestNearbyOMP(delta, trialx, bestf, param); |
newf = this->bestNearbyRepro(delta, trialx, bestf, param); |
459 |
if (newf == -1) { |
if (newf == -1) { |
460 |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
461 |
handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); |
handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); |
468 |
break; |
break; |
469 |
|
|
470 |
/* if too many function evaluations occur, terminate the algorithm */ |
/* if too many function evaluations occur, terminate the algorithm */ |
471 |
#ifdef NO_OPENMP |
#ifndef _OPENMP |
472 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
473 |
#endif |
#endif |
474 |
if (iters > hookeiter) { |
if (iters > hookeiter) { |
485 |
EcoSystem->storeVariables(score, bestx); |
EcoSystem->storeVariables(score, bestx); |
486 |
return; |
return; |
487 |
} |
} |
488 |
} |
} // while (newf < bestf) |
489 |
|
|
490 |
#ifdef NO_OPENMP |
#ifndef _OPENMP |
491 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
492 |
#endif |
#endif |
493 |
if (newf < bestf) { |
if (newf < bestf) { |
524 |
} |
} |
525 |
|
|
526 |
/* Functions to perform the parallelization of the algorithm of HJ with OpenMP*/ |
/* Functions to perform the parallelization of the algorithm of HJ with OpenMP*/ |
527 |
#ifdef GADGET_OPENMP |
#ifdef SPECULATIVE |
528 |
double OptInfoHooke::bestNearbyOMP2(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { |
double OptInfoHooke::bestNearbySpec(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { |
529 |
double minf; |
double minf; |
530 |
int i, j, k, ii; |
int i, j, k, ii; |
531 |
DoubleVector z(point); |
DoubleVector z(point); |
549 |
return -1; |
return -1; |
550 |
} |
} |
551 |
|
|
552 |
|
omp_set_dynamic(0); |
553 |
|
omp_set_nested(1); //permit the nested parallelization |
554 |
for (ii=0; ii< paral_tokens; ii++) { |
for (ii=0; ii< paral_tokens; ii++) { |
555 |
i = 0; |
i = 0; |
556 |
while ( i < nvars) { |
while ( i < nvars) { |
557 |
if ((i + paral_tokens -1) >= nvars) |
if ((i + paral_tokens -1) >= nvars) |
558 |
paral_tokens = nvars - i; |
paral_tokens = nvars - i; |
|
omp_set_dynamic(0); |
|
|
omp_set_nested(1); |
|
559 |
#pragma omp parallel for num_threads(paral_tokens) private(k) |
#pragma omp parallel for num_threads(paral_tokens) private(k) |
560 |
for (j = 0; j < paral_tokens; ++j) { |
for (j = 0; j < paral_tokens; ++j) { |
561 |
storage[j].z = z; |
storage[j].z = z; |
635 |
IntVector trapped(nvars, 0); |
IntVector trapped(nvars, 0); |
636 |
|
|
637 |
EcoSystem->scaleVariables(); |
EcoSystem->scaleVariables(); |
|
#ifndef NO_OPENMP |
|
638 |
int numThr = omp_get_max_threads ( ); |
int numThr = omp_get_max_threads ( ); |
639 |
for (i = 0; i < numThr; i++) |
for (i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread |
640 |
EcoSystems[i]->scaleVariables(); |
EcoSystems[i]->scaleVariables(); |
|
#endif |
|
641 |
EcoSystem->getOptScaledValues(x); |
EcoSystem->getOptScaledValues(x); |
642 |
EcoSystem->getOptLowerBounds(lowerb); |
EcoSystem->getOptLowerBounds(lowerb); |
643 |
EcoSystem->getOptUpperBounds(upperb); |
EcoSystem->getOptUpperBounds(upperb); |
678 |
|
|
679 |
while (1) { |
while (1) { |
680 |
if (isZero(bestf)) { |
if (isZero(bestf)) { |
681 |
#ifdef NO_OPENMP |
#ifndef _OPENMP |
682 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
683 |
#endif |
#endif |
684 |
handle.logMessage(LOGINFO, "Error in Hooke & Jeeves optimisation after", iters, "function evaluations, f(x) = 0"); |
handle.logMessage(LOGINFO, "Error in Hooke & Jeeves optimisation after", iters, "function evaluations, f(x) = 0"); |
703 |
/* find best new point, one coord at a time */ |
/* find best new point, one coord at a time */ |
704 |
for (i = 0; i < nvars; i++) |
for (i = 0; i < nvars; i++) |
705 |
trialx[i] = x[i]; |
trialx[i] = x[i]; |
706 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
707 |
newf = this->bestNearbyOMP2(delta, trialx, bestf, param); |
newf = this->bestNearbySpec(delta, trialx, bestf, param); |
708 |
if (newf == -1) { |
if (newf == -1) { |
709 |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
710 |
handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); |
handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); |
715 |
#endif |
#endif |
716 |
/* if too many function evaluations occur, terminate the algorithm */ |
/* if too many function evaluations occur, terminate the algorithm */ |
717 |
|
|
718 |
#ifdef NO_OPENMP |
#ifndef _OPENMP |
719 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
720 |
#endif |
#endif |
721 |
if (iters > hookeiter) { |
if (iters > hookeiter) { |
785 |
/* only move forward if this is really an improvement */ |
/* only move forward if this is really an improvement */ |
786 |
oldf = newf; |
oldf = newf; |
787 |
newf = EcoSystem->SimulateAndUpdate(trialx); |
newf = EcoSystem->SimulateAndUpdate(trialx); |
788 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
789 |
iters++; |
iters++; |
790 |
#endif |
#endif |
791 |
if ((isEqual(newf, oldf)) || (newf > oldf)) { |
if ((isEqual(newf, oldf)) || (newf > oldf)) { |
798 |
for (i = 0; i < nvars; i++) |
for (i = 0; i < nvars; i++) |
799 |
x[i] = trialx[i]; |
x[i] = trialx[i]; |
800 |
|
|
801 |
#ifndef NO_OPENMP |
#ifdef _OPENMP |
802 |
newf = this->bestNearbyOMP2(delta, trialx, bestf, param); |
newf = this->bestNearbySpec(delta, trialx, bestf, param); |
803 |
if (newf == -1) { |
if (newf == -1) { |
804 |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
805 |
handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); |
handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); |
812 |
break; |
break; |
813 |
|
|
814 |
/* if too many function evaluations occur, terminate the algorithm */ |
/* if too many function evaluations occur, terminate the algorithm */ |
815 |
#ifdef NO_OPENMP |
#ifndef _OPENMP |
816 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
817 |
#endif |
#endif |
818 |
if (iters > hookeiter) { |
if (iters > hookeiter) { |
831 |
} |
} |
832 |
} |
} |
833 |
|
|
834 |
#ifdef NO_OPENMP |
#ifndef _OPENMP |
835 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
836 |
#endif |
#endif |
837 |
if (newf < bestf) { |
if (newf < bestf) { |