--- trunk/gadget/hooke.cc 2015/07/30 12:36:46 14 +++ 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 @@ -178,13 +178,13 @@ } /* given a point, look for a better one nearby, one coord at a time */ -#ifndef NO_OPENMP +#ifdef _OPENMP /* * function bestBeraby parallelized with OpenMP * · 2 threads per coord to parallelize the calculation of +delta/-delta * · parallelize the calculation of the best nearby of the coord */ -double OptInfoHooke::bestNearbyOMP(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { +double OptInfoHooke::bestNearbyRepro(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { double minf;//, ftmp; int i, j, k; DoubleVector z(point); @@ -209,11 +209,11 @@ return -1; } + omp_set_dynamic(0); + omp_set_nested(1); //permit the nested parallelization while ( i < nvars) { if ((i + paral_tokens -1) >= nvars) paral_tokens = nvars - i; - omp_set_dynamic(0); - omp_set_nested(1); //permit the nested parallelization #pragma omp parallel for num_threads(paral_tokens) private(k) //parallelize the parameters (numThr/2) for (j = 0; j < paral_tokens; ++j) { storage[j].z = z; @@ -260,7 +260,7 @@ } } } - + delete[] storage; for (i = 0; i < nvars; ++i) point[i] = z[i]; return minf; @@ -289,7 +289,7 @@ IntVector trapped(nvars, 0); EcoSystem->scaleVariables(); -#ifndef NO_OPENMP +#ifdef _OPENMP int numThr = omp_get_max_threads ( ); for (i = 0; i < numThr; i++) // scale the variables for the ecosystem of every thread EcoSystems[i]->scaleVariables(); @@ -334,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"); @@ -359,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"); @@ -371,7 +371,7 @@ #endif /* if too many function evaluations occur, terminate the algorithm */ -#ifdef NO_OPENMP +#ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif if (iters > hookeiter) { @@ -441,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)) { @@ -454,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"); @@ -468,7 +468,7 @@ break; /* if too many function evaluations occur, terminate the algorithm */ -#ifdef NO_OPENMP +#ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif if (iters > hookeiter) { @@ -485,9 +485,9 @@ EcoSystem->storeVariables(score, bestx); return; } - } + } // while (newf < bestf) -#ifdef NO_OPENMP +#ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif if (newf < bestf) { @@ -524,8 +524,8 @@ } /* Functions to perform the parallelization of the algorithm of HJ with OpenMP*/ -#ifdef GADGET_OPENMP -double OptInfoHooke::bestNearbyOMP2(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { +#ifdef SPECULATIVE +double OptInfoHooke::bestNearbySpec(DoubleVector& delta, DoubleVector& point, double prevbest, IntVector& param) { double minf; int i, j, k, ii; DoubleVector z(point); @@ -549,13 +549,13 @@ 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) { if ((i + paral_tokens -1) >= nvars) paral_tokens = nvars - i; - 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) { storage[j].z = z; @@ -678,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"); @@ -703,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"); @@ -715,7 +715,7 @@ #endif /* if too many function evaluations occur, terminate the algorithm */ - #ifdef NO_OPENMP + #ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif if (iters > hookeiter) { @@ -785,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)) { @@ -798,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"); @@ -812,7 +812,7 @@ break; /* if too many function evaluations occur, terminate the algorithm */ - #ifdef NO_OPENMP + #ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif if (iters > hookeiter) { @@ -831,7 +831,7 @@ } } - #ifdef NO_OPENMP + #ifndef _OPENMP iters = EcoSystem->getFuncEval() - offset; #endif if (newf < bestf) {