00001
00019 #include "reo.h"
00020
00024
00028
00041 void reoReorderSift( reo_man * p )
00042 {
00043 double CostCurrent;
00044 double CostLimit;
00045 double CostBest;
00046 int BestQ;
00047 int VarCurrent;
00048 int q;
00049 int c;
00050 int v;
00051
00052 assert( p->nSupp > 0 );
00053
00054
00055 if ( p->fMinWidth )
00056 CostCurrent = p->nWidthCur;
00057 else if ( p->fMinApl )
00058 CostCurrent = p->nAplCur;
00059 else
00060 CostCurrent = p->nNodesCur;
00061
00062
00063 CostLimit = 1 + (int)(REO_REORDER_LIMIT * CostCurrent);
00064
00065
00066 for ( c = 0; c < p->nSupp; c++ )
00067 {
00068
00069 VarCurrent = -1;
00070 CostBest = -1.0;
00071 for ( v = 0; v < p->nSupp; v++ )
00072 {
00073 p->pVarCosts[v] = REO_HIGH_VALUE;
00074 if ( !p->pPlanes[v].fSifted )
00075 {
00076
00077
00078 if ( CostBest < p->pPlanes[v].statsNodes )
00079 {
00080
00081 CostBest = p->pPlanes[v].statsNodes;
00082 VarCurrent = v;
00083 }
00084
00085 }
00086 }
00087 assert( VarCurrent != -1 );
00088
00089 p->pPlanes[VarCurrent].fSifted = 1;
00090
00091
00092 p->pVarCosts[VarCurrent] = CostCurrent;
00093
00094
00095 CostBest = CostCurrent;
00096 BestQ = VarCurrent;
00097
00098
00099
00100
00101
00102 if ( VarCurrent < p->nSupp/2 )
00103 {
00104
00105 p->pPlanes[0].statsCostAbove = 0;
00106 for ( v = 1; v <= VarCurrent; v++ )
00107 p->pPlanes[v].statsCostAbove = p->pPlanes[v-1].statsCostAbove + p->pPlanes[v-1].statsCost;
00108
00109 p->pPlanes[p->nSupp].statsCostBelow = 0;
00110 for ( v = p->nSupp - 1; v >= VarCurrent; v-- )
00111 p->pPlanes[v].statsCostBelow = p->pPlanes[v+1].statsCostBelow + p->pPlanes[v+1].statsCost;
00112
00113 assert( CostCurrent == p->pPlanes[VarCurrent].statsCostAbove +
00114 p->pPlanes[VarCurrent].statsCost +
00115 p->pPlanes[VarCurrent].statsCostBelow );
00116
00117
00118 for ( q = VarCurrent-1; q >= 0; q-- )
00119 {
00120 CostCurrent -= reoReorderSwapAdjacentVars( p, q, 1 );
00121
00122 p->pVarCosts[q] = CostCurrent;
00123
00124 p->pPlanes[q].statsCostBelow = p->pPlanes[q+1].statsCostBelow + p->pPlanes[q+1].statsCost;
00125
00126 if ( CostCurrent >= CostLimit )
00127 break;
00128
00129 if ( p->pPlanes[q].statsCostBelow + (REO_QUAL_PAR-1)*p->pPlanes[q].statsCostAbove/REO_QUAL_PAR >= CostBest )
00130 break;
00131
00132 if ( CostBest > CostCurrent )
00133 {
00134 CostBest = CostCurrent;
00135 BestQ = q;
00136
00137 CostLimit = ddMin( CostLimit, 1 + (int)(REO_REORDER_LIMIT * CostCurrent) );
00138 }
00139
00140
00141
00142
00143 if ( p->fMinWidth || p->fMinApl )
00144 {
00145 if ( p->nNodesCur >= 2 * p->nNodesMaxAlloc )
00146 {
00147
00148 reoResizeStructures( p, 0, p->nNodesCur, 0 );
00149 }
00150 }
00151 }
00152
00153 if ( q == -1 )
00154 q++;
00155
00156
00157
00158 for ( ; q < p->nSupp-1; )
00159 {
00160 CostCurrent -= reoReorderSwapAdjacentVars( p, q, 0 );
00161 q++;
00162
00163 if ( p->pVarCosts[q] != REO_HIGH_VALUE && fabs( p->pVarCosts[q] - CostCurrent ) > REO_COST_EPSILON )
00164 printf("reoReorderSift(): Error! On the backward move, the costs are different.\n");
00165 p->pVarCosts[q] = CostCurrent;
00166
00167 p->pPlanes[q].statsCostAbove = p->pPlanes[q-1].statsCostAbove + p->pPlanes[q-1].statsCost;
00168
00169 if ( q >= BestQ )
00170 {
00171
00172 if ( CostCurrent >= CostLimit )
00173 break;
00174
00175 if ( p->pPlanes[q].statsCostAbove + (REO_QUAL_PAR-1)*p->pPlanes[q].statsCostBelow/REO_QUAL_PAR >= CostBest )
00176 break;
00177 }
00178
00179 if ( CostBest >= CostCurrent )
00180 {
00181 CostBest = CostCurrent;
00182 BestQ = q;
00183
00184 CostLimit = ddMin( CostLimit, 1 + (int)(REO_REORDER_LIMIT * CostCurrent) );
00185 }
00186
00187
00188
00189
00190 if ( p->fMinWidth || p->fMinApl )
00191 {
00192 if ( p->nNodesCur >= 2 * p->nNodesMaxAlloc )
00193 {
00194
00195 reoResizeStructures( p, 0, p->nNodesCur, 0 );
00196 }
00197 }
00198 }
00199
00200 assert( q >= BestQ );
00201 for ( ; q > BestQ; q-- )
00202 {
00203 CostCurrent -= reoReorderSwapAdjacentVars( p, q-1, 1 );
00204
00205 if ( fabs( p->pVarCosts[q-1] - CostCurrent ) > REO_COST_EPSILON )
00206 {
00207 printf("reoReorderSift(): Error! On the return move, the costs are different.\n" );
00208 fflush(stdout);
00209 }
00210 }
00211 }
00212 else
00213 {
00214
00215 p->pPlanes[0].statsCostAbove = 0;
00216 for ( v = 1; v <= VarCurrent; v++ )
00217 p->pPlanes[v].statsCostAbove = p->pPlanes[v-1].statsCostAbove + p->pPlanes[v-1].statsCost;
00218
00219 p->pPlanes[p->nSupp].statsCostBelow = 0;
00220 for ( v = p->nSupp - 1; v >= VarCurrent; v-- )
00221 p->pPlanes[v].statsCostBelow = p->pPlanes[v+1].statsCostBelow + p->pPlanes[v+1].statsCost;
00222
00223 assert( CostCurrent == p->pPlanes[VarCurrent].statsCostAbove +
00224 p->pPlanes[VarCurrent].statsCost +
00225 p->pPlanes[VarCurrent].statsCostBelow );
00226
00227
00228 for ( q = VarCurrent; q < p->nSupp-1; )
00229 {
00230 CostCurrent -= reoReorderSwapAdjacentVars( p, q, 0 );
00231 q++;
00232 p->pVarCosts[q] = CostCurrent;
00233
00234 p->pPlanes[q].statsCostAbove = p->pPlanes[q-1].statsCostAbove + p->pPlanes[q-1].statsCost;
00235
00236 if ( CostCurrent >= CostLimit )
00237 break;
00238
00239 if ( p->pPlanes[q].statsCostAbove + (REO_QUAL_PAR-1)*p->pPlanes[q].statsCostBelow/REO_QUAL_PAR >= CostBest )
00240 break;
00241
00242 if ( CostBest > CostCurrent )
00243 {
00244 CostBest = CostCurrent;
00245 BestQ = q;
00246
00247 CostLimit = ddMin( CostLimit, 1 + (int)(REO_REORDER_LIMIT * CostCurrent) );
00248 }
00249
00250
00251
00252
00253 if ( p->fMinWidth || p->fMinApl )
00254 {
00255 if ( p->nNodesCur >= 2 * p->nNodesMaxAlloc )
00256 {
00257
00258 reoResizeStructures( p, 0, p->nNodesCur, 0 );
00259 }
00260 }
00261 }
00262
00263
00264 for ( --q; q >= 0; q-- )
00265 {
00266 CostCurrent -= reoReorderSwapAdjacentVars( p, q, 1 );
00267
00268
00269 if ( p->pVarCosts[q] != REO_HIGH_VALUE && fabs( p->pVarCosts[q] - CostCurrent ) > REO_COST_EPSILON )
00270 printf("reoReorderSift(): Error! On the backward move, the costs are different.\n");
00271 p->pVarCosts[q] = CostCurrent;
00272
00273 p->pPlanes[q].statsCostBelow = p->pPlanes[q+1].statsCostBelow + p->pPlanes[q+1].statsCost;
00274
00275 if ( q <= BestQ )
00276 {
00277
00278 if ( CostCurrent >= CostLimit )
00279 break;
00280
00281 if ( p->pPlanes[q].statsCostBelow + (REO_QUAL_PAR-1)*p->pPlanes[q].statsCostAbove/REO_QUAL_PAR >= CostBest )
00282 break;
00283 }
00284
00285 if ( CostBest >= CostCurrent )
00286 {
00287 CostBest = CostCurrent;
00288 BestQ = q;
00289
00290 CostLimit = ddMin( CostLimit, 1 + (int)(REO_REORDER_LIMIT * CostCurrent) );
00291 }
00292
00293
00294
00295
00296 if ( p->fMinWidth || p->fMinApl )
00297 {
00298 if ( p->nNodesCur >= 2 * p->nNodesMaxAlloc )
00299 {
00300
00301 reoResizeStructures( p, 0, p->nNodesCur, 0 );
00302 }
00303 }
00304 }
00305
00306 if ( q == -1 )
00307 q++;
00308
00309
00310 assert( q <= BestQ );
00311 for ( ; q < BestQ; q++ )
00312 {
00313 CostCurrent -= reoReorderSwapAdjacentVars( p, q, 0 );
00314
00315 if ( fabs( p->pVarCosts[q+1] - CostCurrent ) > REO_COST_EPSILON )
00316 {
00317 printf("reoReorderSift(): Error! On the return move, the costs are different.\n" );
00318 fflush(stdout);
00319 }
00320 }
00321 }
00322 assert( fabs( CostBest - CostCurrent ) < REO_COST_EPSILON );
00323
00324
00325 if ( p->fMinWidth )
00326 p->nWidthCur = (int)CostBest;
00327 else if ( p->fMinApl )
00328 p->nAplCur = CostCurrent;
00329 else
00330 p->nNodesCur = (int)CostBest;
00331 }
00332
00333
00334 for ( v = 0; v < p->nSupp; v++ )
00335 p->pPlanes[v].fSifted = 0;
00336 }
00337
00341