Skip to content

Commit ceec5a4

Browse files
committed
Refactored debugPdlp logging
1 parent 01fe286 commit ceec5a4

File tree

14 files changed

+85
-43
lines changed

14 files changed

+85
-43
lines changed

highs/pdlp/CupdlpWrapper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,12 @@ HighsStatus solveLpCupdlp(const HighsOptions& options, HighsTimer& timer,
206206
highs_info.pdlp_iteration_count = pdlp_num_iter;
207207

208208
// Print final solution using debugPdlpFinalSolutionLog
209-
debugPdlpFinalSolutionLog(w->debug_pdlp_log_file, highs_solution.col_value.data(), lp.num_col_, highs_solution.row_dual.data(), lp.num_row_);
210-
fclose(w->debug_pdlp_log_file);
209+
debugPdlpFinalSolutionLog(w->debug_pdlp_log_file_,
210+
highs_solution.col_value.data(),
211+
lp.num_col_,
212+
highs_solution.row_dual.data(),
213+
lp.num_row_);
214+
fclose(w->debug_pdlp_log_file_);
211215
model_status = HighsModelStatus::kUnknown;
212216
highs_solution.value_valid = value_valid;
213217
highs_solution.dual_valid = dual_valid;

highs/pdlp/cupdlp/cupdlp_defs.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,13 @@ struct CUPDLP_TIMERS {
400400
#endif
401401
};
402402

403+
struct DebugPdlpData {
404+
double ax_norm;
405+
double aty_norm;
406+
double ax_average_norm;
407+
double aty_average_norm;
408+
};
409+
403410
struct CUPDLP_WORK {
404411
CUPDLPproblem *problem;
405412
CUPDLPsettings *settings;
@@ -424,7 +431,8 @@ struct CUPDLP_WORK {
424431
// cusparseDnVecDescr_t vecbuffer;
425432
cublasHandle_t cublashandle;
426433
#endif
427-
FILE* debug_pdlp_log_file;
434+
FILE* debug_pdlp_log_file_;
435+
struct DebugPdlpData debug_pdlp_data_;
428436
};
429437

430438
#ifdef __cplusplus

highs/pdlp/cupdlp/cupdlp_restart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ PDHG_restart_choice PDHG_Check_Restart_GPU(CUPDLPwork *work) {
2727
cupdlp_float muAverage = PDHG_Restart_Score_GPU(
2828
work->stepsize->dBeta, work->resobj->dPrimalFeasAverage,
2929
work->resobj->dDualFeasAverage, work->resobj->dDualityGapAverage);
30-
debugPdlpRestartLog(work->debug_pdlp_log_file, timers->nIter, muCurrent, muAverage);
30+
debugPdlpRestartLog(work->debug_pdlp_log_file_, timers->nIter, muCurrent, muAverage);
3131
cupdlp_float muCandidate = 0.0;
3232
PDHG_restart_choice restart_choice = PDHG_RESTART_TO_AVERAGE;
3333
if (muCurrent < muAverage) {

highs/pdlp/cupdlp/cupdlp_solver.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,8 @@ cupdlp_retcode PDHG_Solve(const cupdlp_int* has_variables, CUPDLPwork *pdhg) {
895895
timers->nIter = 0;
896896
timers->dSolvingBeg = getTimeStamp();
897897

898-
pdhg->debug_pdlp_log_file = fopen("cuPDLP.log", "w");
899-
assert(pdhg->debug_pdlp_log_file);
898+
pdhg->debug_pdlp_log_file_ = fopen("cuPDLP.log", "w");
899+
assert(pdhg->debug_pdlp_log_file_);
900900

901901
// PDHG_Init_Data does nothing!
902902
PDHG_Init_Data(pdhg);
@@ -915,8 +915,10 @@ cupdlp_retcode PDHG_Solve(const cupdlp_int* has_variables, CUPDLPwork *pdhg) {
915915
// iter_log_since_header so that an initial header is printed
916916
const int iter_log_between_header = 50;
917917
int iter_log_since_header = iter_log_between_header;
918+
debugPdlpIterHeaderLog(pdhg->debug_pdlp_log_file_);
919+
debugPdlpDataInitialise(&pdhg->debug_pdlp_data_);
918920
for (timers->nIter = 0; timers->nIter < settings->nIterLim; ++timers->nIter) {
919-
debugPdlpIterLog(pdhg->debug_pdlp_log_file, timers->nIter, pdhg->stepsize->dBeta);
921+
debugPdlpIterLog(pdhg->debug_pdlp_log_file_, timers->nIter, &pdhg->debug_pdlp_data_, pdhg->stepsize->dBeta);
920922
PDHG_Compute_SolvingTime(pdhg);
921923
#if CUPDLP_DUMP_ITERATES_STATS && CUPDLP_DEBUG
922924
PDHG_Dump_Stats(pdhg);
@@ -1152,7 +1154,7 @@ cupdlp_retcode PDHG_Solve(const cupdlp_int* has_variables, CUPDLPwork *pdhg) {
11521154
#endif
11531155

11541156
exit_cleanup:
1155-
//fclose(pdhg->debug_pdlp_log_file);
1157+
//fclose(pdhg->debug_pdlp_log_file_);
11561158
return retcode;
11571159
}
11581160

highs/pdlp/cupdlp/cupdlp_step.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void PDHG_primalGradientStep(CUPDLPwork *work, CUPDLPvec *xUpdate,
2121
// print norm of A'y
2222
double aty_norm = 0.0;
2323
cupdlp_twoNorm(work, problem->nCols, ATy->data, &aty_norm);
24-
debugPdlpAtyNormLog(work->debug_pdlp_log_file, aty_norm);
24+
work->debug_pdlp_data_.aty_norm = aty_norm;
2525

2626
#if !defined(CUPDLP_CPU) && USE_KERNELS
2727
cupdlp_pgrad_cuda(xUpdate->data, x->data, problem->cost,
@@ -200,7 +200,7 @@ void PDHG_Update_Iterate_Constant_Step_Size(CUPDLPwork *pdhg) {
200200
double ax_norm = 0.0;
201201
cupdlp_twoNorm(pdhg, problem->nRows, ax->data,
202202
&ax_norm);
203-
debugPdlpAxNormLog(pdhg->debug_pdlp_log_file, ax_norm);
203+
pdhg->debug_pdlp_data_.ax_norm = ax_norm;
204204
ATy(pdhg, aty, y);
205205

206206
// x^{k+1} = proj_{X}(x^k - dPrimalStep * (c - A'y^k))
@@ -412,7 +412,7 @@ void PDHG_Compute_Average_Iterate(CUPDLPwork *work) {
412412

413413
cupdlp_float ax_norm = 0.0;
414414
cupdlp_twoNormSquared(work, lp->nCols, iterates->axAverage->data, &ax_norm);
415-
debugPdlpAxavgNormLog(work->debug_pdlp_log_file, ax_norm);
415+
work->debug_pdlp_data_.ax_norm = ax_norm;
416416
}
417417

418418
void PDHG_Update_Average(CUPDLPwork *work) {

highs/pdlp/cupdlp/cupdlp_utils.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,20 +1782,28 @@ void writeSol(const char *fout, cupdlp_int nCols, cupdlp_int nRows,
17821782
fclose(fptr);
17831783
}
17841784

1785-
void debugPdlpIterLog(FILE* file, const int iter_num, const double beta) {
1786-
fprintf(file, "Iter %6d, beta = %g\n", iter_num, beta);
1785+
void debugPdlpIterHeaderLog(FILE* file) {
1786+
fprintf(file, " Iter ||Ax|| ||Aty|| ||Ax_Avg|| ||Aty_Avg|| beta\n");
17871787
}
17881788

1789-
void debugPdlpAxNormLog(FILE* file, const double ax_norm) {
1790-
fprintf(file, "||Ax|| = %g\n", ax_norm);
1789+
void debugPdlpDataInitialise(struct DebugPdlpData* debug_pdlp) {
1790+
debug_pdlp->ax_norm = 0;
1791+
debug_pdlp->aty_norm = 0;
1792+
debug_pdlp->ax_average_norm = 0;
1793+
debug_pdlp->aty_average_norm = 0;
17911794
}
17921795

1793-
void debugPdlpAtyNormLog(FILE* file, const double aty_norm) {
1794-
fprintf(file, "||A^Ty|| = %g\n", aty_norm);
1796+
void debugPdlpIterLog(FILE* file, const int iter_num, const struct DebugPdlpData* debug_pdlp, const double beta) {
1797+
fprintf(file, "%6d %11.4g %11.4g %11.4g %11.4g %11.4g\n",
1798+
iter_num,
1799+
debug_pdlp->ax_norm,
1800+
debug_pdlp->aty_norm,
1801+
debug_pdlp->ax_average_norm,
1802+
debug_pdlp->aty_average_norm,
1803+
beta);
17951804
}
17961805

1797-
void debugPdlpAxavgNormLog(FILE* file, const double ax_avg_norm) {
1798-
fprintf(file, "||Ax_avg|| = %g\n", ax_avg_norm);
1806+
void debugPdlpFeasOptLog(FILE* file) {
17991807
}
18001808

18011809
void debugPdlpRestartLog(FILE* file, const int iter_num, const double current_score, const double average_score) {
@@ -1817,4 +1825,4 @@ void debugPdlpFinalSolutionLog(FILE* file, const double* x, int nCols, const dou
18171825
for (int i = 0; i < nRows; ++i) {
18181826
fprintf(file, "y[%d]=%g\n", i, y[i]);
18191827
}
1820-
}
1828+
}

highs/pdlp/cupdlp/cupdlp_utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ void writeSol(const char *fout, cupdlp_int nCols, cupdlp_int nRows,
193193
cupdlp_float *col_value, cupdlp_float *col_dual,
194194
cupdlp_float *row_value, cupdlp_float *row_dual);
195195

196-
void debugPdlpIterLog(FILE* file, const int iter_num, const double beta);
197-
void debugPdlpAxNormLog(FILE* file, const double ax_norm);
198-
void debugPdlpAtyNormLog(FILE* file, const double aty_norm);
199-
void debugPdlpAxavgNormLog(FILE* file, const double ax_avg_norm);
196+
void debugPdlpIterHeaderLog(FILE* file);
197+
void debugPdlpDataInitialise(struct DebugPdlpData* debug_pdlp);
198+
void debugPdlpIterLog(FILE* file, const int iter_num, const struct DebugPdlpData* debug_pdlp, const double beta);
199+
void debugPdlpFeasOptLog(FILE* file);
200200
void debugPdlpRestartLog(FILE* file, const int iter_num, const double current_score, const double average_score);
201201
void debugPdlpRestarScoretLog(FILE* file, const double weight_squared, const double primal_feas, const double dual_feas, const double obj_gap);
202202
void debugPdlpFinalSolutionLog(FILE* file, const double* x, int nCols, const double* y, int nRows);

highs/pdlp/hipdlp/pdhg.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ void PDLPSolver::solve(std::vector<double>& x, std::vector<double>& y) {
312312
const double op_norm_sq = PowerMethod();
313313
step_.passLp(&lp_);
314314
step_.passLogOptions(&params_.log_options_);
315-
step_.passDebugLogFile(debug_pdlp_log_file_);
315+
step_.passDebugPdlpLogFile(debug_pdlp_log_file_);
316+
step_.passDebugPdlpData(&debug_pdlp_data_);
316317
StepSizeConfig step_size =
317318
step_.InitializeStepSizesPowerMethod(lp, op_norm_sq);
318319
const double fixed_eta = 0.99 / sqrt(op_norm_sq);
@@ -334,7 +335,8 @@ void PDLPSolver::solve(std::vector<double>& x, std::vector<double>& y) {
334335

335336
// --- 1. Initialization ---
336337
restart_scheme_.passLogOptions(&working_params.log_options_);
337-
restart_scheme_.passDebugLogFile(debug_pdlp_log_file_);
338+
restart_scheme_.passDebugPdlpLogFile(debug_pdlp_log_file_);
339+
restart_scheme_.passDebugPdlpData(&debug_pdlp_data_);
338340
Initialize(lp, x, y); // Sets initial x, y and results_
339341
restart_scheme_.passParams(&working_params);
340342
restart_scheme_.Initialize(results_);
@@ -364,8 +366,11 @@ void PDLPSolver::solve(std::vector<double>& x, std::vector<double>& y) {
364366
logger_.print_iteration_header();
365367

366368
// --- 2. Main PDHG Loop ---
369+
debugPdlpIterHeaderLog(debug_pdlp_log_file_);
370+
debugPdlpDataInitialise(&debug_pdlp_data_);
371+
367372
for (int iter = 0; iter < params_.max_iterations; ++iter) {
368-
debugPdlpIterLog(debug_pdlp_log_file_, iter, restart_scheme_.getBeta());
373+
debugPdlpIterLog(debug_pdlp_log_file_, iter, &debug_pdlp_data_, restart_scheme_.getBeta());
369374

370375
// Check time limit
371376
if (solver_timer.read() > params_.time_limit) {
@@ -475,11 +480,9 @@ void PDLPSolver::solve(std::vector<double>& x, std::vector<double>& y) {
475480
linalg::ATy(lp, y_current_, ATy_current);
476481
}
477482

478-
// Print Ax norm for debugging
479-
double ax_norm = linalg::vector_norm(Ax_current);
480-
double axavg_norm = linalg::vector_norm(Ax_avg);
481-
debugPdlpAxNormLog(debug_pdlp_log_file_, ax_norm);
482-
debugPdlpAxavgNormLog(debug_pdlp_log_file_, axavg_norm);
483+
// Record Ax norm for debugging
484+
debug_pdlp_data_.ax_norm = linalg::vector_norm(Ax_current);
485+
debug_pdlp_data_.ax_average_norm = linalg::vector_norm(Ax_avg);
483486

484487
// --- 5. Core PDHG Update Step ---
485488
bool step_success = true;

highs/pdlp/hipdlp/pdhg.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class PDLPSolver {
5151
int getIterationCount() const { return final_iter_count_; }
5252
void logSummary();
5353

54-
void solveReturn();
55-
FILE* debug_pdlp_log_file_ = nullptr;
54+
void solveReturn();
55+
FILE* debug_pdlp_log_file_ = nullptr;
56+
DebugPdlpData debug_pdlp_data_;
5657

5758
private:
5859
// Problem data

highs/pdlp/hipdlp/restart.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ double RestartScheme::ComputeRestartScore(const SolverResults& results) {
4242
double dual_feas_sq = results.dual_feasibility * results.dual_feasibility;
4343
double gap_sq = results.duality_gap * results.duality_gap;
4444

45-
//debugPdlpRestarScoretLog(debug_log_file_, weight_squared, results.primal_feasibility, results.dual_feasibility, results.duality_gap);
45+
//debugPdlpRestarScoretLog(debug_pdlp_log_file_, weight_squared, results.primal_feasibility, results.dual_feasibility, results.duality_gap);
4646

4747
return std::sqrt(weight_squared * primal_feas_sq +
4848
dual_feas_sq / weight_squared + gap_sq);
@@ -76,7 +76,7 @@ RestartInfo RestartScheme::Check(int current_iter,
7676
case RestartStrategy::ADAPTIVE_RESTART: {
7777
double current_score = ComputeRestartScore(current_results);
7878
double average_score = ComputeRestartScore(average_results);
79-
debugPdlpRestartLog(debug_log_file_, current_iter, current_score,
79+
debugPdlpRestartLog(debug_pdlp_log_file_, current_iter, current_score,
8080
average_score);
8181

8282
// Choose the best candidate (current vs. average) based on the score

0 commit comments

Comments
 (0)