Skip to content

Commit 132feb4

Browse files
committed
Added pdlpLog to cupdlp_utils.c and calling it from both cupdlp_solve.c and pdhg.cpp
1 parent 14e0ef2 commit 132feb4

File tree

6 files changed

+39
-19
lines changed

6 files changed

+39
-19
lines changed

check/TestPdlp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ TEST_CASE("hi-pdlp", "[pdlp]") {
338338
// h.setOptionValue("output_flag", dev_run);
339339
REQUIRE(h.readModel(model_file) == HighsStatus::kOk);
340340
h.setOptionValue("solver", kHiPdlpString);
341-
h.setOptionValue("kkt_tolerance", 1e-6); // kkt_tolerance);
341+
h.setOptionValue("kkt_tolerance", kkt_tolerance);
342342

343343
h.setOptionValue("pdlp_scaling_mode", 0);
344344
h.setOptionValue("pdlp_restart_strategy", 0);

highs/pdlp/cupdlp/cupdlp_solver.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,9 @@ cupdlp_retcode PDHG_Solve(const cupdlp_int* has_variables, CUPDLPwork *pdhg) {
897897
timers->nIter = 0;
898898
timers->dSolvingBeg = getTimeStamp();
899899

900+
FILE* pdlp_log_file = fopen("cuPDLP.log", "w");
901+
assert(pdlp_log_file);
902+
900903
// PDHG_Init_Data does nothing!
901904
PDHG_Init_Data(pdhg);
902905

@@ -915,6 +918,7 @@ cupdlp_retcode PDHG_Solve(const cupdlp_int* has_variables, CUPDLPwork *pdhg) {
915918
const int iter_log_between_header = 50;
916919
int iter_log_since_header = iter_log_between_header;
917920
for (timers->nIter = 0; timers->nIter < settings->nIterLim; ++timers->nIter) {
921+
pdlpLog(pdlp_log_file, timers->nIter);
918922
PDHG_Compute_SolvingTime(pdhg);
919923
#if CUPDLP_DUMP_ITERATES_STATS && CUPDLP_DEBUG
920924
PDHG_Dump_Stats(pdhg);
@@ -1150,6 +1154,7 @@ cupdlp_retcode PDHG_Solve(const cupdlp_int* has_variables, CUPDLPwork *pdhg) {
11501154
#endif
11511155

11521156
exit_cleanup:
1157+
fclose(pdlp_log_file);
11531158
return retcode;
11541159
}
11551160

highs/pdlp/cupdlp/cupdlp_utils.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,3 +1781,7 @@ void writeSol(const char *fout, cupdlp_int nCols, cupdlp_int nRows,
17811781
// Close the file
17821782
fclose(fptr);
17831783
}
1784+
1785+
void pdlpLog(FILE* file, const int iter_num) {
1786+
fprintf(file, "%6d\n", iter_num);
1787+
}

highs/pdlp/cupdlp/cupdlp_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ 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 pdlpLog(FILE* file, const int iter_num);
197+
196198
#ifdef __cplusplus
197199
}
198200
#endif

highs/pdlp/hipdlp/pdhg.cc

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#include "pdhg.hpp"
22

3-
#include "linalg.hpp"
4-
#include "restart.hpp"
5-
#include "step.hpp"
6-
// #include "Highs.h"
7-
// #include "highs_interface.hpp"
83
#include <cmath>
94
#include <iostream>
105
#include <random>
116
#include <tuple>
127

8+
#include "linalg.hpp"
9+
#include "pdlp/cupdlp/cupdlp.h" // For pdlpLogging
10+
#include "restart.hpp"
11+
#include "step.hpp"
12+
1313
#define PDHG_CHECK_INTERVAL 40
1414

1515
using namespace std;
@@ -266,7 +266,7 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
266266
Timer solver_timer;
267267
if (original_lp.num_row_ == 0 || original_lp.a_matrix_.start_.size() < 2) {
268268
std::cout << "Empty LP problem." << std::endl;
269-
return;
269+
return solveReturn();
270270
}
271271

272272
HighsSolution solution;
@@ -323,8 +323,7 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
323323

324324
working_params.omega = std::sqrt(step_size.dual_step / step_size.primal_step);
325325
working_params.eta = std::sqrt(step_size.primal_step * step_size.dual_step);
326-
current_eta_ =
327-
working_params.eta; // Initial step size for adaptive strategy
326+
current_eta_ = working_params.eta; // Initial step size for adaptive strategy
328327
std::cout << "Using power method step sizes: eta = " << working_params.eta
329328
<< ", omega = " << working_params.omega << std::endl;
330329

@@ -358,9 +357,13 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
358357

359358
logger_.print_iteration_header();
360359

360+
pdlp_log_file = fopen("HiPDLP.log", "w");
361+
assert(pdlp_log_file);
362+
361363
// --- 2. Main PDHG Loop ---
362364
// A single loop handles max iterations, convergence, and restarts.
363365
for (int iter = 0; iter < params.max_iterations; ++iter) {
366+
pdlpLog(pdlp_log_file, iter);
364367
if (solver_timer.read() > params.time_limit) {
365368
logger_.info("Time limit reached.");
366369
final_iter_count_ = iter;
@@ -381,7 +384,7 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
381384
}
382385
results_.primal_obj = primal_objective;
383386

384-
return; // Exit the function
387+
return solveReturn(); // Exit the function
385388
}
386389

387390
// --- 3. Core PDHG Update Step ---
@@ -412,7 +415,7 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
412415
x = x_avg_;
413416
y = y_avg_;
414417
// scaling_.UnscaleSolution(x, y);
415-
return;
418+
return solveReturn();
416419
}
417420
}
418421

@@ -463,7 +466,7 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
463466
results_ = average_results;
464467
results_.term_code = TerminationStatus::OPTIMAL;
465468
Postsolve(original_lp, lp, x, y, solution);
466-
return;
469+
return solveReturn();
467470
}
468471

469472
bool current_converged = CheckConvergence(
@@ -491,7 +494,7 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
491494
results_ = current_results;
492495
results_.term_code = TerminationStatus::OPTIMAL;
493496
Postsolve(original_lp, lp, x, y, solution);
494-
return;
497+
return solveReturn();
495498
}
496499

497500
// --- 6. Restart Check ---
@@ -551,7 +554,11 @@ void PDLPSolver::Solve(HighsLp& original_lp, const PrimalDualParams& params,
551554
results_.primal_obj = primal_objective;
552555
results_.term_code = TerminationStatus::TIMEOUT;
553556
Postsolve(original_lp, lp, x_avg_, y_avg_, solution);
554-
return;
557+
return solveReturn();
558+
}
559+
560+
void PDLPSolver::solveReturn() {
561+
if (pdlp_log_file) fclose(pdlp_log_file);
555562
}
556563

557564
void PDLPSolver::Initialize(const HighsLp& lp, std::vector<double>& x,
@@ -792,10 +799,10 @@ bool PDLPSolver::CheckConvergence(const HighsLp& lp,
792799
ComputeDualityGap(lp, x, y, lambda);
793800
results.duality_gap = duality_gap;
794801

795-
results.relative_obj_gap = std::abs(duality_gap) / (1.0 + std::abs(cTx) +
796-
std::abs(qTy) +
797-
std::abs(lTlambda_plus) +
798-
std::abs(uTlambda_minus));
802+
results.relative_obj_gap =
803+
std::abs(duality_gap) /
804+
(1.0 + std::abs(cTx) + std::abs(qTy) + std::abs(lTlambda_plus) +
805+
std::abs(uTlambda_minus));
799806

800807
bool primal_feasible = primal_feasibility <= epsilon * (1 + q_norm);
801808
bool dual_feasible = dual_feasibility <= epsilon * (1 + c_norm);

highs/pdlp/hipdlp/pdhg.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "scaling.hpp"
2626
#include "solver_results.hpp"
2727
#include "step.hpp"
28-
// #include "highs_interface.hpp"
2928

3029
// --- Define Macros ---
3130
// Enable or disable GPU usage
@@ -85,6 +84,8 @@ class PDLPSolver {
8584
ScalingParams scaling_params_;
8685
Scaling scaling_;
8786

87+
FILE* pdlp_log_file = nullptr;
88+
8889
// HighsStatus TransformGxLeqToGeq(HighsLp& lp);
8990
void PreprocessLp(const HighsLp& original_lp, HighsLp& processed_lp);
9091
void Postsolve(const HighsLp& original_lp, HighsLp& processed_lp,
@@ -93,6 +94,7 @@ class PDLPSolver {
9394
HighsSolution& solution);
9495

9596
// Helper functions
97+
void solveReturn();
9698
void Initialize(const HighsLp& lp, std::vector<double>& x,
9799
std::vector<double>& y);
98100
bool RunPresolve(const HighsLp& original_lp, Highs& highs,

0 commit comments

Comments
 (0)