/* * return the number of the highest-numbered block actually written, plus 1. * 0 indicates an error. */ static Devsize writtensize(Device *worm) { Devsize lim = devsize(worm); Iobuf *p; print("devsize(%Z) = %lld\n", worm, (Wideoff)lim); if (!blockok(worm, 0) || !blockok(worm, lim-1)) return 0; delay(5*1000); if (userabort("sanity checks")) return 0; /* find worm's last valid block in case "worm" is an (f)worm */ while (lim > 0) { if (userabort("sizing")) { lim = 0; /* you lose */ break; } --lim; p = getbuf(worm, lim, Brd); if (p != 0) { /* actually read one okay? */ putbuf(p); break; } } print("limit(%Z) = %lld\n", worm, (Wideoff)lim); if (lim <= 0) return 0; return lim + 1; }
/* copy device from src to dest */ static int dodevcopy(void) { Device *from, *to; Iobuf *p; Off a; Devsize lim, tosize; /* * convert config strings into Devices. */ from = iconfig(src); if(f.error || from == nil) { print("bad src device %s\n", src); return -1; } to = iconfig(dest); if(f.error || to == nil) { print("bad dest device %s\n", dest); return -1; } /* * initialise devices, size them, more sanity checking. */ devinit(from); lim = devsize(from); if(lim == 0) panic("no blocks to copy on %Z", from); devinit(to); tosize = devsize(to); if(tosize == 0) panic("no blocks to copy on %Z", to); /* use smaller of the device sizes */ if (tosize < lim) lim = tosize; print("copy %Z to %Z in 8 seconds\n", from, to); delay(8000); if (userabort("preparing to copy")) return -1; print("copying dev: %lld blocks from %Z to %Z\n", (Wideoff)lim, from, to); /* * Copy all blocks, a block at a time. */ for (a = 0; a < lim; a++) { if (userabort("copy")) break; p = getbuf(from, a, Brd); /* * if from is a real WORM device, we'll get errors trying to * read unwritten blocks, but the unwritten blocks need not * be contiguous. */ if (p == 0) { print("%lld not written yet; can't read\n", (Wideoff)a); continue; } if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) { print("out block %lld: write error; bailing", (Wideoff)a); break; } putbuf(p); if(a % 20000 == 0) print("block %lld %T\n", (Wideoff)a, time(nil)); } /* * wrap up: sync target */ print("copied %lld blocks from %Z to %Z\n", (Wideoff)a, from, to); sync("devcopy"); return 0; }
/* copy worm fs from "main"'s inner worm to "output" */ static void dowormcopy(void) { Filsys *f1, *f2; Device *fdev, *from, *to = nil; Iobuf *p; Off a; Devsize lim; /* * convert file system names into Filsyss and Devices. */ f1 = fsstr("main"); if(f1 == nil) panic("main file system missing"); fdev = f1->dev; from = wormof(fdev); /* fake worm special */ if (from->type != Devfworm && from->type != Devcw) { print("main file system is not a worm; copyworm may not do what you want!\n"); print("waiting for 20 seconds...\n"); delay(20000); } f2 = fsstr("output"); if(f2 == nil) { print("no output file system - check only\n\n"); print("reading worm from %Z (worm %Z)\n", fdev, from); } else { to = f2->dev; print("\ncopying worm from %Z (worm %Z) to %Z, starting in 8 seconds\n", fdev, from, to); delay(8000); } if (userabort("preparing to copy")) return; /* * initialise devices, size them, more sanity checking. */ devinit(from); if (0 && fdev != from) { devinit(fdev); print("debugging, sizing %Z first\n", fdev); writtensize(fdev); } lim = writtensize(from); if(lim == 0) panic("no blocks to copy on %Z", from); if (to) { print("reaming %Z in 8 seconds\n", to); delay(8000); if (userabort("preparing to ream & copy")) return; devream(to, 0); devinit(to); print("copying worm: %lld blocks from %Z to %Z\n", (Wideoff)lim, from, to); } /* can't read to's blocks in case to is a real WORM device */ /* * Copy written fs blocks, a block at a time (or just read * if no "output" fs). */ for (a = 0; a < lim; a++) { if (userabort("copy")) break; p = getbuf(from, a, Brd); /* * if from is a real WORM device, we'll get errors trying to * read unwritten blocks, but the unwritten blocks need not * be contiguous. */ if (p == 0) { print("%lld not written yet; can't read\n", (Wideoff)a); continue; } if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) { print("out block %lld: write error; bailing", (Wideoff)a); break; } putbuf(p); if(a % 20000 == 0) print("block %lld %T\n", (Wideoff)a, time(nil)); } /* * wrap up: sync target, loop */ print("copied %lld blocks from %Z to %Z\n", (Wideoff)a, from, to); sync("wormcopy"); delay(2000); print("looping; reset the machine at any time.\n"); for (; ; ) continue; /* await reset */ }
STATIC int primloop(lprec *lp, MYBOOL feasible) { int i, j, k, ok = TRUE; LREAL theta = 0.0; REAL *prow = NULL, *drow = NULL, *pcol = NULL, prevobj, epsvalue; MYBOOL primal = TRUE, minit; MYBOOL pivdynamic, bfpfinal = FALSE; int oldpivrule, oldpivmode, pivrule, Blandswitches, colnr, rownr, lastnr, minitcount = 0; int Rcycle = 0, Ccycle = 0, Ncycle = 0, changedphase = FALSE; int *nzdrow = NULL, *workINT = NULL; /* Add sufficent number of artificial variables to make the problem feasible through the first phase; delete when primal feasibility has been achieved */ lp->Extrap = 0; #ifdef EnablePrimalPhase1 if(!feasible) { #ifdef Paranoia if(!verifyBasis(lp)) report(lp, SEVERE, "primloop: No valid basis for artificial variables\n"); #endif #if 0 /* First check if we can get away with a single artificial variable */ if(lp->equalities == 0) { i = (int) feasibilityOffset(lp, !primal); add_artificial(lp, i); } else #endif /* Otherwise add as many as is necessary to force basic feasibility */ for(i = 1; i <= lp->rows; i++) add_artificial(lp, i); } if(lp->spx_trace) report(lp, DETAILED, "Extrap count = %d\n", lp->Extrap); #endif if(lp->spx_trace) report(lp, DETAILED, "Entered primal simplex algorithm with feasibility %s\n", my_boolstr(feasible)); /* Create work arrays */ #ifdef UseSparseReducedCost allocINT(lp, &nzdrow, lp->sum + 1, FALSE); #endif allocREAL(lp, &prow, lp->sum + 1, TRUE); allocREAL(lp, &drow, lp->sum + 1, TRUE); allocREAL(lp, &pcol, lp->rows + 1, TRUE); /* Refactorize the basis and set status variables */ i = my_if(is_bb_action(lp, ACTION_REBASE), INITSOL_SHIFTZERO, INITSOL_USEZERO); if(((lp->spx_status == SWITCH_TO_PRIMAL) && !lp->justInverted) || (lp->Extrap != 0) || is_bb_action(lp, ACTION_REINVERT)) { simplexPricer(lp, (MYBOOL)!primal); /* Do basis crashing before refactorization, if specified */ invert(lp, (MYBOOL) i, TRUE); } else { if(is_bb_action(lp, ACTION_RECOMPUTE)) recompute_solution(lp, (MYBOOL) i); restartPricer(lp, (MYBOOL)!primal); } lp->bb_action = ACTION_NONE; lp->spx_status = RUNNING; lp->doIterate = FALSE; minit = ITERATE_MAJORMAJOR; prevobj = lp->rhs[0]; oldpivmode = lp->piv_strategy; oldpivrule = get_piv_rule(lp); pivdynamic = ANTICYCLEBLAND && is_piv_mode(lp, PRICE_ADAPTIVE); epsvalue = lp->epspivot; Blandswitches = 0; rownr = 0; colnr = 0; lastnr = 0; lp->rejectpivot[0] = 0; if(feasible) lp->simplex_mode = SIMPLEX_Phase2_PRIMAL; else lp->simplex_mode = SIMPLEX_Phase1_PRIMAL; /* Iterate while we are successful; exit when the model is infeasible/unbounded, or we must terminate due to numeric instability or user-determined reasons */ while(lp->spx_status == RUNNING) { if(lp->spx_trace) if(lastnr > 0) report(lp, NORMAL, "primloop: Objective at iteration %8d is " RESULTVALUEMASK " (%4d: %4d %s- %4d)\n", get_total_iter(lp), lp->rhs[0], rownr, lastnr, my_if(minit == ITERATE_MAJORMAJOR, "<","|"), colnr); pivrule = get_piv_rule(lp); if(pivdynamic && ((pivrule != PRICER_FIRSTINDEX) || (pivrule != oldpivrule)) #if PrimalPivotStickiness==2 && (lp->fixedvars == 0) #elif PrimalPivotStickiness==1 && feasible #endif ) { /* Check if we have a stationary solution */ if((minit == ITERATE_MAJORMAJOR) && !lp->justInverted && (fabs(my_reldiff(lp->rhs[0], prevobj)) < epsvalue)) { Ncycle++; /* Start to monitor for variable cycling if this is the initial stationarity */ if(Ncycle <= 1) { Ccycle = colnr; Rcycle = rownr; } /* Check if we should change pivoting strategy due to stationary variable cycling */ else if((pivrule == oldpivrule) && (((MAX_STALLCOUNT > 1) && (Ncycle > MAX_STALLCOUNT)) || (Ccycle == rownr) || (Rcycle == colnr))) { /* First check if we should give up on Bland's rule and try perturbed bound relaxation instead */ #ifdef EnableStallAntiDegen if((MAX_BLANDSWITCH >= 0) && (Blandswitches >= MAX_BLANDSWITCH)) { lp->spx_status = DEGENERATE; break; } #endif Blandswitches++; lp->piv_strategy = PRICER_FIRSTINDEX; /* There is no advanced normalization for Bland's rule, restart at end */ Ccycle = 0; Rcycle = 0; Ncycle = 0; if(lp->spx_trace) report(lp, DETAILED, "primloop: Detected cycling at iteration %d; changed to FIRST INDEX rule!\n", get_total_iter(lp)); } } #if 0 /* Handle cycling or stationary situations by switching to the dual simplex */ else if((pivrule == oldpivrule) && feasible && (lp->simplex_strategy & SIMPLEX_DYNAMIC)) { lp->spx_status = SWITCH_TO_DUAL; if(lp->total_iter == 0) report(lp, NORMAL, "Start dual simplex for finalization at iteration %8d\n", get_total_iter(lp)); break; } #endif /* Change back to original selection strategy as soon as possible */ else if((minit == ITERATE_MAJORMAJOR) && (pivrule != oldpivrule)) { lp->piv_strategy = oldpivmode; restartPricer(lp, AUTOMATIC); /* Pricer restart following Bland's rule */ Ccycle = 0; Rcycle = 0; Ncycle = 0; if(lp->spx_trace) report(lp, DETAILED, "...returned to original pivot selection rule at iteration %d.\n", get_total_iter(lp)); } } /* Store current LP value for reference at next iteration */ prevobj = lp->rhs[0]; lp->doIterate = FALSE; lp->doInvert = FALSE; /* Find best column to enter the basis */ RetryCol: if(!changedphase) { i = 0; do { if(partial_countBlocks(lp, (MYBOOL) !primal) > 1) partial_blockStep(lp, (MYBOOL) !primal); colnr = colprim(lp, (MYBOOL) (minit == ITERATE_MINORRETRY), drow, nzdrow); i++; } while ((colnr == 0) && (i < partial_countBlocks(lp, (MYBOOL) !primal))); #ifdef FinalOptimalErrorLimitation /* Do additional checking that we have a correct identification of optimality */ if((colnr == 0) && !lp->justInverted) { lp->doInvert = TRUE; i = invert(lp, INITSOL_USEZERO, TRUE); colnr = colprim(lp, FALSE, drow, nzdrow); } #endif } if(colnr > 0) { changedphase = FALSE; fsolve(lp, colnr, pcol, workINT, lp->epsmachine, 1.0, TRUE); /* Solve entering column for Pi */ #ifdef UseRejectionList if(is_anti_degen(lp, ANTIDEGEN_COLUMNCHECK) && !check_degeneracy(lp, pcol, NULL)) { if(lp->rejectpivot[0] < DEF_MAXPIVOTRETRY/3) { i = ++lp->rejectpivot[0]; lp->rejectpivot[i] = colnr; report(lp, DETAILED, "Entering column %d found to be non-improving due to degeneracy!\n", colnr); goto RetryCol; } else { lp->rejectpivot[0] = 0; report(lp, DETAILED, "Gave up trying to find a strictly improving entering column!\n"); } } #endif /* Find the leaving variable that gives the most stringent bound on the entering variable */ theta = drow[colnr]; rownr = rowprim(lp, colnr, &theta, pcol); #if 0 report(lp, NORMAL, "Iteration %d: Enter %d, Leave %d\n", lp->current_iter, colnr, rownr); #endif /* See if we can do a straight artificial<->slack replacement (when "colnr" is a slack) */ if((lp->Extrap != 0) && (rownr == 0) && (colnr <= lp->rows)) rownr = findAnti_artificial(lp, colnr); if(rownr > 0) { lp->rejectpivot[0] = 0; lp->bfp_prepareupdate(lp, rownr, colnr, pcol); } else if(lp->spx_status == UNBOUNDED) { report(lp, DETAILED, "primloop: The model is primal unbounded.\n"); break; } #ifdef UseRejectionList else if(lp->rejectpivot[0] < DEF_MAXPIVOTRETRY) { lp->spx_status = RUNNING; if(lp->justInverted) { lp->rejectpivot[0]++; lp->rejectpivot[lp->rejectpivot[0]] = colnr; report(lp, DETAILED, "...trying to recover via another pivot column!\n"); } else { lp->doInvert = TRUE; invert(lp, INITSOL_USEZERO, TRUE); } goto RetryCol; } #endif else { /* Assume failure if we are still unsuccessful and the model is not unbounded */ if((rownr == 0) && (lp->spx_status == RUNNING)) { report(lp, IMPORTANT, "primloop: Could not find a leaving variable for entering %d (iteration %d)\n", colnr, get_total_iter(lp)); lp->spx_status = NUMFAILURE; } } } #ifdef EnablePrimalPhase1 else if(!feasible || isPhase1(lp)) { if(feasiblePhase1(lp, epsvalue)) { lp->spx_status = RUNNING; if(lp->bb_totalnodes == 0) { report(lp, NORMAL, "Found feasibility by primal simplex at iteration %8d\n", get_total_iter(lp)); if((lp->usermessage != NULL) && (lp->msgmask & MSG_LPFEASIBLE)) lp->usermessage(lp, lp->msghandle, MSG_LPFEASIBLE); } changedphase = FALSE; feasible = TRUE; lp->simplex_mode = SIMPLEX_Phase2_PRIMAL; /* We can do two things now; 1) delete the rows belonging to those variables, since they are redundant, OR 2) drive out the existing artificial variables via pivoting. */ if(lp->Extrap > 0) { #ifdef Phase1EliminateRedundant /* If it is not a MIP model we can try to delete redundant rows */ if((lp->bb_totalnodes == 0) && (MIP_count(lp) == 0)) { while(lp->Extrap > 0) { i = lp->rows; while((i > 0) && (lp->var_basic[i] <= lp->sum-lp->Extrap)) i--; #ifdef Paranoia if(i <= 0) { report(lp, SEVERE, "primloop: Could not find redundant artificial.\n"); break; } #endif /* Obtain column and row indeces */ j = lp->var_basic[i]-lp->rows; k = get_artificialRow(lp, j); /* Delete row before column due to basis "compensation logic" */ if(lp->is_basic[k]) { lp->is_basic[lp->rows+j] = FALSE; del_constraint(lp, k); } else setBasisVar(lp, i, k); del_column(lp, j); lp->Extrap--; } lp->basis_valid = TRUE; } /* Otherwise we drive out the artificials by elimination pivoting */ else { eliminate_artificials(lp, prow); lp->doIterate = FALSE; } #else lp->Extrap = my_flipsign(lp->Extrap); #endif } lp->doInvert = TRUE; prevobj = lp->infinite; } else { lp->spx_status = INFEASIBLE; minit = ITERATE_MAJORMAJOR; if(lp->spx_trace) report(lp, NORMAL, "Model infeasible by primal simplex at iteration %8d\n", get_total_iter(lp)); } } #endif /* Pivot row/col and update the inverse */ if(lp->doIterate) { lastnr = lp->var_basic[rownr]; if(lp->justInverted) minitcount = 0; else if(minitcount > MAX_MINITUPDATES) { recompute_solution(lp, INITSOL_USEZERO); minitcount = 0; } minit = performiteration(lp, rownr, colnr, theta, primal, NULL, NULL, pcol, NULL); if(minit != ITERATE_MAJORMAJOR) minitcount++; if((lp->spx_status == USERABORT) || (lp->spx_status == TIMEOUT)) break; else if(minit == ITERATE_MINORMAJOR) continue; #ifdef UsePrimalReducedCostUpdate /* Do a fast update of the reduced costs in preparation for the next iteration */ if(minit == ITERATE_MAJORMAJOR) update_reducedcosts(lp, primal, lastnr, colnr, pcol, drow); #endif #ifdef EnablePrimalPhase1 /* Detect if an auxiliary variable has left the basis and delete it; if the non-basic variable only changed bound (a "minor iteration"), the basic artificial variable did not leave and there is nothing to do */ if((minit == ITERATE_MAJORMAJOR) && (lastnr > lp->sum - abs(lp->Extrap))) { #ifdef Paranoia if(lp->is_basic[lastnr] || !lp->is_basic[colnr]) report(lp, SEVERE, "primloop: Invalid basis indicator for variable %d at iteration %d\n", lastnr, get_total_iter(lp)); #endif del_column(lp, lastnr-lp->rows); if(lp->Extrap > 0) lp->Extrap--; else lp->Extrap++; if(lp->Extrap == 0) { colnr = 0; prevobj = lp->infinite; changedphase = TRUE; } } #endif } if(lp->spx_status == SWITCH_TO_DUAL) ; else if(!changedphase && lp->bfp_mustrefactorize(lp)) { i = invert(lp, INITSOL_USEZERO, FALSE); #ifdef ResetMinitOnReinvert minit = ITERATE_MAJORMAJOR; #endif if((lp->spx_status == USERABORT) || (lp->spx_status == TIMEOUT)) break; else if(!i) { lp->spx_status = SINGULAR_BASIS; break; } /* Check whether we are still feasible or not... */ if(!isPrimalFeasible(lp, lp->epspivot)) { lp->spx_status = LOSTFEAS; } } userabort(lp, -1); } if (lp->piv_strategy != oldpivmode) lp->piv_strategy = oldpivmode; #ifdef EnablePrimalPhase1 /* Remove any remaining artificial variables (feasible or infeasible model) */ lp->Extrap = abs(lp->Extrap); if(lp->Extrap > 0) { clear_artificials(lp); if(lp->spx_status != OPTIMAL) restore_basis(lp); i = invert(lp, INITSOL_USEZERO, TRUE); } #ifdef Paranoia if(!verifyBasis(lp)) report(lp, SEVERE, "primloop: Invalid basis detected due to internal error\n"); #endif /* Switch to dual phase 1 simplex for MIP models during B&B phases */ if((lp->bb_totalnodes == 0) && (MIP_count(lp) > 0) && ((lp->simplex_strategy & SIMPLEX_Phase1_DUAL) == 0)) { lp->simplex_strategy &= !SIMPLEX_Phase1_PRIMAL; lp->simplex_strategy += SIMPLEX_Phase1_DUAL; } #endif FREE(nzdrow); FREE(drow); FREE(prow); FREE(pcol); return(ok); } /* primloop */