--- trunk/gadget/hooke.cc 2015/07/23 19:00:38 11 +++ trunk/gadget/hooke.cc 2015/10/13 11:29:01 16 @@ -139,12 +139,12 @@ #include "ecosystem.h" #include "global.h" -#ifndef NO_OPENMP +#ifdef _OPENMP #include "omp.h" #endif extern Ecosystem* EcoSystem; -#ifndef NO_OPENMP +#ifdef _OPENMP extern Ecosystem** EcoSystems; #endif @@ -156,17 +156,9 @@ DoubleVector z(point); minf = prevbest; -// for (int k=0;kSimulateAndUpdate(z); -// cout << i <<"-z["<< param[i]<<"]:" <= nvars) paral_tokens = nvars - i; -//cout << "i:" << i << endl; - omp_set_dynamic(0); - omp_set_nested(1); -#pragma omp parallel for num_threads(paral_tokens) private(k) +#pragma omp parallel for num_threads(paral_tokens) private(k) //parallelize the parameters (numThr/2) for (j = 0; j < paral_tokens; ++j) { -// cout << "thr_for:" << omp_get_num_threads()<SimulateAndUpdate(v1); -// cout << "->" << j << endl; } - #pragma omp section + #pragma omp section { -// cout << "->" << j+paral_tokens << endl; storage[j+paral_tokens].ftmp = EcoSystems[j+paral_tokens]->SimulateAndUpdate(v2); } } -//cout << "-----" << endl; -// cout <scaleVariables(); -#ifndef NO_OPENMP +#ifdef _OPENMP int numThr = omp_get_max_threads ( ); - for (i = 0; i < numThr; i++) + for (i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread EcoSystems[i]->scaleVariables(); #endif EcoSystem->getOptScaledValues(x); @@ -337,7 +312,6 @@ bestx[i] = x[i]; trialx[i] = x[i]; param[i] = i; - //FIXME rand_r delta[i] = ((2 * (rand() % 2)) - 1) * rho; //JMB - randomise the sign } @@ -360,7 +334,7 @@ while (1) { if (isZero(bestf)) { -#ifdef NO_OPENMP +#ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif handle.logMessage(LOGINFO, "Error in Hooke & Jeeves optimisation after", iters, "function evaluations, f(x) = 0"); @@ -371,7 +345,6 @@ /* randomize the order of the parameters once in a while */ rchange = 0; while (rchange < nvars) { - //FIXME rand_r rnumber = rand() % nvars; rcheck = 1; for (i = 0; i < rchange; i++) @@ -386,8 +359,8 @@ /* find best new point, one coord at a time */ for (i = 0; i < nvars; i++) trialx[i] = x[i]; -#ifndef NO_OPENMP - newf = this->bestNearbyOMP(delta, trialx, bestf, param); +#ifdef _OPENMP + newf = this->bestNearbyRepro(delta, trialx, bestf, param); if (newf == -1) { handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); @@ -398,10 +371,9 @@ #endif /* if too many function evaluations occur, terminate the algorithm */ -#ifdef NO_OPENMP +#ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif -// cout << "newf:" << newf << "-" << iters< hookeiter) { handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); @@ -469,7 +441,7 @@ /* only move forward if this is really an improvement */ oldf = newf; newf = EcoSystem->SimulateAndUpdate(trialx); -#ifndef NO_OPENMP +#ifdef _OPENMP iters++; #endif if ((isEqual(newf, oldf)) || (newf > oldf)) { @@ -482,8 +454,8 @@ for (i = 0; i < nvars; i++) x[i] = trialx[i]; -#ifndef NO_OPENMP - newf = this->bestNearbyOMP(delta, trialx, bestf, param); +#ifdef _OPENMP + newf = this->bestNearbyRepro(delta, trialx, bestf, param); if (newf == -1) { handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); @@ -496,10 +468,9 @@ break; /* if too many function evaluations occur, terminate the algorithm */ -#ifdef NO_OPENMP +#ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif -// cout << "newf:" << newf << "-" << iters< hookeiter) { handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); @@ -514,9 +485,9 @@ EcoSystem->storeVariables(score, bestx); return; } - } + } // while (newf < bestf) -#ifdef NO_OPENMP +#ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif if (newf < bestf) { @@ -552,11 +523,10 @@ } } -#ifdef GADGET_OPENMP - //TODO doc -double OptInfoHooke::bestNearbyOMP2(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { -//cout << "beastNearbyINI" << endl; - double minf;//, ftmp; +/* Functions to perform the parallelization of the algorithm of HJ with OpenMP*/ +#ifdef SPECULATIVE +double OptInfoHooke::bestNearbySpec(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { + double minf; int i, j, k, ii; DoubleVector z(point); int bestId = 0; @@ -569,7 +539,6 @@ minf = prevbest; - int paral_tokens, numThr, nvars = point.Size(); numThr = omp_get_max_threads ( ); @@ -580,21 +549,15 @@ return -1; } + omp_set_dynamic(0); + omp_set_nested(1); //permit the nested parallelization for (ii=0; ii< paral_tokens; ii++) { i = 0; while ( i < nvars) { - // for (int k=0;k= nvars) paral_tokens = nvars - i; - //cout << "i:" << i << endl; - omp_set_dynamic(0); - omp_set_nested(1); #pragma omp parallel for num_threads(paral_tokens) private(k) for (j = 0; j < paral_tokens; ++j) { - // cout << "thr_for:" << omp_get_num_threads()<SimulateAndUpdate(v1); - // cout << "->" << j << endl; } #pragma omp section { - // cout << "->" << j+paral_tokens << endl; storage[j+paral_tokens].ftmp = EcoSystems[j+paral_tokens]->SimulateAndUpdate(v2); } } - //cout << "-----" << endl; - // cout <scaleVariables(); - #ifndef NO_OPENMP int numThr = omp_get_max_threads ( ); - for (i = 0; i < numThr; i++) + for (i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread EcoSystems[i]->scaleVariables(); - #endif EcoSystem->getOptScaledValues(x); EcoSystem->getOptLowerBounds(lowerb); EcoSystem->getOptUpperBounds(upperb); @@ -709,7 +656,6 @@ bestx[i] = x[i]; trialx[i] = x[i]; param[i] = i; - //FIXME rand_r delta[i] = ((2 * (rand() % 2)) - 1) * rho; //JMB - randomise the sign } @@ -732,7 +678,7 @@ while (1) { if (isZero(bestf)) { - #ifdef NO_OPENMP + #ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif handle.logMessage(LOGINFO, "Error in Hooke & Jeeves optimisation after", iters, "function evaluations, f(x) = 0"); @@ -743,7 +689,6 @@ /* randomize the order of the parameters once in a while */ rchange = 0; while (rchange < nvars) { - //FIXME rand_r rnumber = rand() % nvars; rcheck = 1; for (i = 0; i < rchange; i++) @@ -758,8 +703,8 @@ /* find best new point, one coord at a time */ for (i = 0; i < nvars; i++) trialx[i] = x[i]; - #ifndef NO_OPENMP - newf = this->bestNearbyOMP2(delta, trialx, bestf, param); + #ifdef _OPENMP + newf = this->bestNearbySpec(delta, trialx, bestf, param); if (newf == -1) { handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); @@ -770,10 +715,9 @@ #endif /* if too many function evaluations occur, terminate the algorithm */ - #ifdef NO_OPENMP + #ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif - // cout << "newf:" << newf << "-" << iters< hookeiter) { handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); @@ -786,7 +730,6 @@ for (i = 0; i < nvars; i++) bestx[i] = trialx[i] * init[i]; EcoSystem->storeVariables(score, bestx); -// EcoSystem->writeOptValuesOMP(); return; } @@ -842,7 +785,7 @@ /* only move forward if this is really an improvement */ oldf = newf; newf = EcoSystem->SimulateAndUpdate(trialx); - #ifndef NO_OPENMP + #ifdef _OPENMP iters++; #endif if ((isEqual(newf, oldf)) || (newf > oldf)) { @@ -855,8 +798,8 @@ for (i = 0; i < nvars; i++) x[i] = trialx[i]; - #ifndef NO_OPENMP - newf = this->bestNearbyOMP2(delta, trialx, bestf, param); + #ifdef _OPENMP + newf = this->bestNearbySpec(delta, trialx, bestf, param); if (newf == -1) { handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); handle.logMessage(LOGINFO, "\nThe number of threads must be a multiple of 2\n"); @@ -869,10 +812,9 @@ break; /* if too many function evaluations occur, terminate the algorithm */ - #ifdef NO_OPENMP + #ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif - // cout << "newf:" << newf << "-" << iters< hookeiter) { handle.logMessage(LOGINFO, "\nStopping Hooke & Jeeves optimisation algorithm\n"); handle.logMessage(LOGINFO, "The optimisation stopped after", iters, "function evaluations"); @@ -885,12 +827,11 @@ for (i = 0; i < nvars; i++) bestx[i] = trialx[i] * init[i]; EcoSystem->storeVariables(score, bestx); -// EcoSystem->writeOptValuesOMP(); return; } } - #ifdef NO_OPENMP + #ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif if (newf < bestf) { @@ -916,7 +857,6 @@ score = bestf; handle.logMessage(LOGINFO, "\nHooke & Jeeves finished with a likelihood score of", score); EcoSystem->storeVariables(bestf, bestx); -// EcoSystem->writeOptValuesOMP(); return; }