188 |
|
|
189 |
/* 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 */ |
190 |
#ifndef NO_OPENMP |
#ifndef NO_OPENMP |
191 |
|
/* |
192 |
|
* function bestBeraby parallelized with OpenMP |
193 |
|
* · 2 threads per coord to parallelize the calculation of +delta/-delta |
194 |
|
* · parallelize the calculation of the best nearby of the coord |
195 |
|
*/ |
196 |
double OptInfoHooke::bestNearbyOMP(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { |
double OptInfoHooke::bestNearbyOMP(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { |
|
//cout << "beastNearbyINI" << endl; |
|
197 |
double minf;//, ftmp; |
double minf;//, ftmp; |
198 |
int i, j, k; |
int i, j, k; |
199 |
DoubleVector z(point); |
DoubleVector z(point); |
219 |
} |
} |
220 |
|
|
221 |
while ( i < nvars) { |
while ( i < nvars) { |
|
// for (int k=0;k<point.Size(); k++) |
|
|
// cout << z[k] << " " ; |
|
|
// cout << endl; |
|
222 |
if ((i + paral_tokens -1) >= nvars) |
if ((i + paral_tokens -1) >= nvars) |
223 |
paral_tokens = nvars - i; |
paral_tokens = nvars - i; |
|
//cout << "i:" << i << endl; |
|
224 |
omp_set_dynamic(0); |
omp_set_dynamic(0); |
225 |
omp_set_nested(1); |
omp_set_nested(1); |
226 |
#pragma omp parallel for num_threads(paral_tokens) private(k) |
#pragma omp parallel for num_threads(paral_tokens) private(k) |
227 |
for (j = 0; j < paral_tokens; ++j) { |
for (j = 0; j < paral_tokens; ++j) { |
|
// cout << "thr_for:" << omp_get_num_threads()<<endl; |
|
|
// cout << "J:" << j << endl; |
|
228 |
storage[j].z = z; |
storage[j].z = z; |
229 |
storage[j].delta = delta; |
storage[j].delta = delta; |
230 |
DoubleVector v1(z); |
DoubleVector v1(z); |
235 |
|
|
236 |
#pragma omp parallel sections num_threads(2) |
#pragma omp parallel sections num_threads(2) |
237 |
{ |
{ |
|
// cout << "thr_sec:" << omp_get_num_threads()<<endl; |
|
238 |
#pragma omp section |
#pragma omp section |
239 |
{ |
{ |
240 |
storage[j].ftmp = EcoSystems[j]->SimulateAndUpdate(v1); |
storage[j].ftmp = EcoSystems[j]->SimulateAndUpdate(v1); |
|
// cout << "->" << j << endl; |
|
241 |
} |
} |
242 |
#pragma omp section |
#pragma omp section |
243 |
{ |
{ |
|
// cout << "->" << j+paral_tokens << endl; |
|
244 |
storage[j+paral_tokens].ftmp = EcoSystems[j+paral_tokens]->SimulateAndUpdate(v2); |
storage[j+paral_tokens].ftmp = EcoSystems[j+paral_tokens]->SimulateAndUpdate(v2); |
245 |
} |
} |
246 |
} |
} |
|
//cout << "-----" << endl; |
|
|
// cout <<i+j<< "-z["<< param[i+j]<<"]:" << (storage[j].z)[param[i+j]] << " - " << storage[j].ftmp << endl; |
|
247 |
|
|
248 |
if (storage[j].ftmp < minf) { |
if (storage[j].ftmp < minf) { |
249 |
storage[j].iters = 1; |
storage[j].iters = 1; |
250 |
storage[j].z[k] = v1[k]; |
storage[j].z[k] = v1[k]; |
|
// storage[j].delta[k] += delta[k]; |
|
|
// minf = ftmp; |
|
251 |
} else { |
} else { |
252 |
storage[j].iters = 2; |
storage[j].iters = 2; |
253 |
storage[j].delta[k] = 0.0 - delta[k]; |
storage[j].delta[k] = 0.0 - delta[k]; |
254 |
if (storage[j+paral_tokens].ftmp < minf) { |
if (storage[j+paral_tokens].ftmp < minf) { |
|
// minf = ftmp; |
|
255 |
storage[j].ftmp = storage[j+paral_tokens].ftmp; |
storage[j].ftmp = storage[j+paral_tokens].ftmp; |
256 |
storage[j].z[k] = v2[k]; |
storage[j].z[k] = v2[k]; |
257 |
} |
} |
|
// else |
|
|
// storage[j].z[k] = point[k]; |
|
258 |
} |
} |
259 |
} |
} |
260 |
|
|
|
// cout << i <<"-z["<< param[i]<<"]:" <<z[param[i]] << " - " ; |
|
261 |
for (j = 0; j < paral_tokens; ++j) { |
for (j = 0; j < paral_tokens; ++j) { |
262 |
i++; |
i++; |
263 |
iters += storage[j].iters; |
iters += storage[j].iters; |
265 |
minf = storage[j].ftmp; |
minf = storage[j].ftmp; |
266 |
z = storage[j].z; |
z = storage[j].z; |
267 |
delta = storage[j].delta; |
delta = storage[j].delta; |
|
// cout << storage[j].ftmp; |
|
268 |
break; |
break; |
269 |
} |
} |
270 |
} |
} |
271 |
} |
} |
272 |
|
|
|
// omp_set_num_threads(numThr); |
|
|
|
|
273 |
for (i = 0; i < nvars; ++i) |
for (i = 0; i < nvars; ++i) |
274 |
point[i] = z[i]; |
point[i] = z[i]; |
275 |
return minf; |
return minf; |
321 |
bestx[i] = x[i]; |
bestx[i] = x[i]; |
322 |
trialx[i] = x[i]; |
trialx[i] = x[i]; |
323 |
param[i] = i; |
param[i] = i; |
|
//FIXME rand_r |
|
324 |
delta[i] = ((2 * (rand() % 2)) - 1) * rho; //JMB - randomise the sign |
delta[i] = ((2 * (rand() % 2)) - 1) * rho; //JMB - randomise the sign |
325 |
} |
} |
326 |
|
|
354 |
/* randomize the order of the parameters once in a while */ |
/* randomize the order of the parameters once in a while */ |
355 |
rchange = 0; |
rchange = 0; |
356 |
while (rchange < nvars) { |
while (rchange < nvars) { |
|
//FIXME rand_r |
|
357 |
rnumber = rand() % nvars; |
rnumber = rand() % nvars; |
358 |
rcheck = 1; |
rcheck = 1; |
359 |
for (i = 0; i < rchange; i++) |
for (i = 0; i < rchange; i++) |
383 |
#ifdef NO_OPENMP |
#ifdef NO_OPENMP |
384 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
385 |
#endif |
#endif |
|
// cout << "newf:" << newf << "-" << iters<<endl; |
|
386 |
if (iters > hookeiter) { |
if (iters > hookeiter) { |
387 |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
388 |
handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); |
handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); |
480 |
#ifdef NO_OPENMP |
#ifdef NO_OPENMP |
481 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
482 |
#endif |
#endif |
|
// cout << "newf:" << newf << "-" << iters<<endl; |
|
483 |
if (iters > hookeiter) { |
if (iters > hookeiter) { |
484 |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
485 |
handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); |
handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); |
532 |
} |
} |
533 |
} |
} |
534 |
|
|
535 |
|
/* Functions to perform the parallelization of the algorithm of HJ with OpenMP*/ |
536 |
#ifdef GADGET_OPENMP |
#ifdef GADGET_OPENMP |
|
//TODO doc |
|
537 |
double OptInfoHooke::bestNearbyOMP2(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { |
double OptInfoHooke::bestNearbyOMP2(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { |
538 |
//cout << "beastNearbyINI" << endl; |
double minf; |
|
double minf;//, ftmp; |
|
539 |
int i, j, k, ii; |
int i, j, k, ii; |
540 |
DoubleVector z(point); |
DoubleVector z(point); |
541 |
int bestId = 0; |
int bestId = 0; |
548 |
|
|
549 |
minf = prevbest; |
minf = prevbest; |
550 |
|
|
|
|
|
551 |
int paral_tokens, numThr, nvars = point.Size(); |
int paral_tokens, numThr, nvars = point.Size(); |
552 |
numThr = omp_get_max_threads ( ); |
numThr = omp_get_max_threads ( ); |
553 |
|
|
561 |
for (ii=0; ii< paral_tokens; ii++) { |
for (ii=0; ii< paral_tokens; ii++) { |
562 |
i = 0; |
i = 0; |
563 |
while ( i < nvars) { |
while ( i < nvars) { |
|
// for (int k=0;k<point.Size(); k++) |
|
|
// cout << z[k] << " " ; |
|
|
// cout << endl; |
|
564 |
if ((i + paral_tokens -1) >= nvars) |
if ((i + paral_tokens -1) >= nvars) |
565 |
paral_tokens = nvars - i; |
paral_tokens = nvars - i; |
|
//cout << "i:" << i << endl; |
|
566 |
omp_set_dynamic(0); |
omp_set_dynamic(0); |
567 |
omp_set_nested(1); |
omp_set_nested(1); |
568 |
#pragma omp parallel for num_threads(paral_tokens) private(k) |
#pragma omp parallel for num_threads(paral_tokens) private(k) |
569 |
for (j = 0; j < paral_tokens; ++j) { |
for (j = 0; j < paral_tokens; ++j) { |
|
// cout << "thr_for:" << omp_get_num_threads()<<endl; |
|
|
// cout << "J:" << j << endl; |
|
570 |
storage[j].z = z; |
storage[j].z = z; |
571 |
storage[j].delta = delta; |
storage[j].delta = delta; |
572 |
DoubleVector v1(z); |
DoubleVector v1(z); |
577 |
|
|
578 |
#pragma omp parallel sections num_threads(2) |
#pragma omp parallel sections num_threads(2) |
579 |
{ |
{ |
|
// cout << "thr_sec:" << omp_get_num_threads()<<endl; |
|
580 |
#pragma omp section |
#pragma omp section |
581 |
{ |
{ |
582 |
storage[j].ftmp = EcoSystems[j]->SimulateAndUpdate(v1); |
storage[j].ftmp = EcoSystems[j]->SimulateAndUpdate(v1); |
|
// cout << "->" << j << endl; |
|
583 |
} |
} |
584 |
#pragma omp section |
#pragma omp section |
585 |
{ |
{ |
|
// cout << "->" << j+paral_tokens << endl; |
|
586 |
storage[j+paral_tokens].ftmp = EcoSystems[j+paral_tokens]->SimulateAndUpdate(v2); |
storage[j+paral_tokens].ftmp = EcoSystems[j+paral_tokens]->SimulateAndUpdate(v2); |
587 |
} |
} |
588 |
} |
} |
|
//cout << "-----" << endl; |
|
|
// cout <<i+j<< "-z["<< param[i+j]<<"]:" << (storage[j].z)[param[i+j]] << " - " << storage[j].ftmp << endl; |
|
|
|
|
589 |
if (storage[j].ftmp < minf) { |
if (storage[j].ftmp < minf) { |
590 |
storage[j].iters = 1; |
storage[j].iters = 1; |
591 |
storage[j].z[k] = v1[k]; |
storage[j].z[k] = v1[k]; |
|
// storage[j].delta[k] += delta[k]; |
|
|
// minf = ftmp; |
|
592 |
} else { |
} else { |
593 |
storage[j].iters = 2; |
storage[j].iters = 2; |
594 |
storage[j].delta[k] = 0.0 - delta[k]; |
storage[j].delta[k] = 0.0 - delta[k]; |
595 |
if (storage[j+paral_tokens].ftmp < minf) { |
if (storage[j+paral_tokens].ftmp < minf) { |
|
// minf = ftmp; |
|
596 |
storage[j].ftmp = storage[j+paral_tokens].ftmp; |
storage[j].ftmp = storage[j+paral_tokens].ftmp; |
597 |
storage[j].z[k] = v2[k]; |
storage[j].z[k] = v2[k]; |
598 |
} |
} |
599 |
else iters += 2; |
else iters += 2; |
|
// storage[j].z[k] = point[k]; |
|
600 |
} |
} |
601 |
} |
} |
602 |
|
|
|
// cout << i <<"-z["<< param[i]<<"]:" <<z[param[i]] << " - " ; |
|
603 |
bestId = 0; |
bestId = 0; |
604 |
for (j = 1; j < paral_tokens; ++j) { |
for (j = 1; j < paral_tokens; ++j) { |
605 |
if (storage[j].ftmp < storage[bestId].ftmp) |
if (storage[j].ftmp < storage[bestId].ftmp) |
610 |
minf = storage[bestId].ftmp; |
minf = storage[bestId].ftmp; |
611 |
z = storage[bestId].z; |
z = storage[bestId].z; |
612 |
delta = storage[bestId].delta; |
delta = storage[bestId].delta; |
|
// cout << storage[j].ftmp; |
|
|
|
|
613 |
} |
} |
614 |
|
|
615 |
i += paral_tokens; |
i += paral_tokens; |
616 |
} |
} |
617 |
} |
} |
618 |
|
|
|
// omp_set_num_threads(numThr); |
|
619 |
delete[] storage; |
delete[] storage; |
620 |
for (i = 0; i < nvars; ++i) |
for (i = 0; i < nvars; ++i) |
621 |
point[i] = z[i]; |
point[i] = z[i]; |
667 |
bestx[i] = x[i]; |
bestx[i] = x[i]; |
668 |
trialx[i] = x[i]; |
trialx[i] = x[i]; |
669 |
param[i] = i; |
param[i] = i; |
|
//FIXME rand_r |
|
670 |
delta[i] = ((2 * (rand() % 2)) - 1) * rho; //JMB - randomise the sign |
delta[i] = ((2 * (rand() % 2)) - 1) * rho; //JMB - randomise the sign |
671 |
} |
} |
672 |
|
|
700 |
/* randomize the order of the parameters once in a while */ |
/* randomize the order of the parameters once in a while */ |
701 |
rchange = 0; |
rchange = 0; |
702 |
while (rchange < nvars) { |
while (rchange < nvars) { |
|
//FIXME rand_r |
|
703 |
rnumber = rand() % nvars; |
rnumber = rand() % nvars; |
704 |
rcheck = 1; |
rcheck = 1; |
705 |
for (i = 0; i < rchange; i++) |
for (i = 0; i < rchange; i++) |
729 |
#ifdef NO_OPENMP |
#ifdef NO_OPENMP |
730 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
731 |
#endif |
#endif |
|
// cout << "newf:" << newf << "-" << iters<<endl; |
|
732 |
if (iters > hookeiter) { |
if (iters > hookeiter) { |
733 |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
734 |
handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); |
handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); |
741 |
for (i = 0; i < nvars; i++) |
for (i = 0; i < nvars; i++) |
742 |
bestx[i] = trialx[i] * init[i]; |
bestx[i] = trialx[i] * init[i]; |
743 |
EcoSystem->storeVariables(score, bestx); |
EcoSystem->storeVariables(score, bestx); |
|
// EcoSystem->writeOptValuesOMP(); |
|
744 |
return; |
return; |
745 |
} |
} |
746 |
|
|
826 |
#ifdef NO_OPENMP |
#ifdef NO_OPENMP |
827 |
iters = EcoSystem->getFuncEval() - offset; |
iters = EcoSystem->getFuncEval() - offset; |
828 |
#endif |
#endif |
|
// cout << "newf:" << newf << "-" << iters<<endl; |
|
829 |
if (iters > hookeiter) { |
if (iters > hookeiter) { |
830 |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); |
831 |
handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); |
handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); |
838 |
for (i = 0; i < nvars; i++) |
for (i = 0; i < nvars; i++) |
839 |
bestx[i] = trialx[i] * init[i]; |
bestx[i] = trialx[i] * init[i]; |
840 |
EcoSystem->storeVariables(score, bestx); |
EcoSystem->storeVariables(score, bestx); |
|
// EcoSystem->writeOptValuesOMP(); |
|
841 |
return; |
return; |
842 |
} |
} |
843 |
} |
} |
868 |
score = bestf; |
score = bestf; |
869 |
handle.logMessage(LOGINFO, "\nHooke & Jeeves finished with a likelihood score of", score); |
handle.logMessage(LOGINFO, "\nHooke & Jeeves finished with a likelihood score of", score); |
870 |
EcoSystem->storeVariables(bestf, bestx); |
EcoSystem->storeVariables(bestf, bestx); |
|
// EcoSystem->writeOptValuesOMP(); |
|
871 |
return; |
return; |
872 |
} |
} |
873 |
|
|