From 0d1c13469c8b286ff5010adeea6c81e3650d94e8 Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Sun, 13 Sep 2026 02:26:47 -0400 Subject: [PATCH 1/9] Feat: add an active-set step-size boost with rollback on divergence --- CMakeLists.txt | 1 + README.md | 1 + include/cuhsdlp_types.h | 17 ++ internal/active_set_boost.h | 58 ++++ internal/cone_projector.h | 1 + internal/display.h | 10 + internal/internal_types.h | 46 ++- internal/matrix_formats.h | 7 + internal/power_iteration.h | 43 ++- internal/utils.h | 9 + src/active_set_boost.cu | 560 +++++++++++++++++++++++++++++++++++ src/cli.c | 21 +- src/cone_projector.cu | 15 + src/cuhsdlp.c | 10 + src/preconditioner.cu | 27 +- src/result.cu | 3 + src/setup.cu | 241 ++++----------- src/solver.cu | 202 +++++++++---- src/utils/display.cu | 66 +++++ src/utils/matrix_formats.c | 47 +++ src/utils/power_iteration.cu | 522 ++++++++++++++++++++++++-------- src/utils/utils.cu | 66 +++++ 22 files changed, 1594 insertions(+), 379 deletions(-) create mode 100644 internal/active_set_boost.h create mode 100644 src/active_set_boost.cu diff --git a/CMakeLists.txt b/CMakeLists.txt index 59127c2..fb0ec08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,6 +141,7 @@ set(CORE_SOURCES src/solver.cu src/spmv_backend.cu src/termination_checker.cu + src/active_set_boost.cu src/utils/display.cu src/utils/power_iteration.cu src/utils/utils.cu) diff --git a/README.md b/README.md index 0880410..b53f8db 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ After building the project, the `./build/cuhsdlp` binary can be invoked from the | Option | Type | Description | Default | | :--- | :--- | :--- | :--- | | `--step_size_factor` | `float` | Step-size scaling factor in (0, 1). | `0.998` | +| `--no_active_set_step_size_boost` | `flag` | Disable active-set step-size boosting. | enabled | | `--singular_value_max_iter` | `int` | Max power iterations for σ_max(A). | `5000` | | `--singular_value_tol` | `float` | Power-iteration tolerance. | `1e-4` | | `--cone_newton_max_iter` | `int` | Max Newton iterations for cone projection. | `100` | diff --git a/include/cuhsdlp_types.h b/include/cuhsdlp_types.h index ef1a682..3bf1de0 100644 --- a/include/cuhsdlp_types.h +++ b/include/cuhsdlp_types.h @@ -108,6 +108,19 @@ extern "C" double necessary_reduction_for_restart; } restart_parameters_t; + typedef struct + { + bool step_size_boost; + int window_iterations; + double relative_active_set_tol; + double safety_factor; + double min_raise_ratio; + double reestimate_change_ratio; + double divergence_ceiling_ratio; + double divergence_margin; + int max_reverts; + } active_set_parameters_t; + typedef struct { double eps_optimal_relative; @@ -132,6 +145,7 @@ extern "C" cone_parameters_t cone_params; termination_criteria_t termination_criteria; restart_parameters_t restart_params; + active_set_parameters_t active_set_params; } pdhg_parameters_t; typedef struct @@ -171,6 +185,9 @@ extern "C" double final_step_size_primal; double final_step_size_dual; double final_step_size_homogeneous; + int active_set_raise_count; + int active_set_revert_count; + int active_set_power_iteration_count; } cuhsdlp_result_t; typedef enum diff --git a/internal/active_set_boost.h b/internal/active_set_boost.h new file mode 100644 index 0000000..7dcdbc7 --- /dev/null +++ b/internal/active_set_boost.h @@ -0,0 +1,58 @@ +/* +Copyright 2026 Haihao Lu + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#pragma once + +#include "internal_types.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + static inline bool is_active_set_boost_enabled( + const pdhg_parameters_t *params) + { + return params->active_set_params.step_size_boost && + params->active_set_params.max_reverts > 0; + } + + void initialize_active_set( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params); + + void active_set_free(active_set_state_t *active_set); + + void update_active_set_masks( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params); + + bool should_revert_active_set_boost( + const pdhg_solver_state_t *state, + const pdhg_parameters_t *params); + + void try_raise_active_set_step_size( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params); + + void revert_active_set_boost( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params); + +#ifdef __cplusplus +} +#endif diff --git a/internal/cone_projector.h b/internal/cone_projector.h index f1066a5..89cc03a 100644 --- a/internal/cone_projector.h +++ b/internal/cone_projector.h @@ -47,6 +47,7 @@ void update_dual_box_cone_projection( double *current_dual, double *reflected_dual, double *evaluation_dual, + double *constraint_dual_projection_input, const double *initial_dual, const double *gradient, const double *step_size_d, diff --git a/internal/display.h b/internal/display.h index 7095539..1642a66 100644 --- a/internal/display.h +++ b/internal/display.h @@ -33,6 +33,16 @@ extern "C" void display_iteration_stats(const pdhg_solver_state_t *state, bool verbose, bool is_restart); + void display_active_set_boost_raise( + const pdhg_parameters_t *params, + double previous_step_size, + double updated_step_size, + double masked_constraint_norm); + + void display_active_set_boost_revert( + const pdhg_solver_state_t *state, + const pdhg_parameters_t *params); + void pdhg_final_log( const cuhsdlp_result_t *result, const pdhg_parameters_t *params); diff --git a/internal/internal_types.h b/internal/internal_types.h index 563dc55..c95813e 100644 --- a/internal/internal_types.h +++ b/internal/internal_types.h @@ -59,6 +59,7 @@ typedef struct { double step_size_dual; double step_size_homogeneous; double step_size_base; + double step_size; int total_count; int inner_count; int restart_count; @@ -109,6 +110,43 @@ typedef struct { void *buffer; } spmv_plan_t; +typedef struct sv_estimator_ctx sv_estimator_ctx_t; + +typedef struct { + double *primal_solution_d; + double *dual_solution_d; + double *hsd_scalars_d; + double *constraint_cone_scale_d; + double primal_weight; + pid_state_t pid; +} active_set_anchor_t; + +typedef struct { + int variables_added; + int variables_removed; + int constraints_added; + int constraints_removed; +} active_set_mask_changes_t; + +typedef struct { + double *constraint_dual_projection_input_d; + int *variable_last_free_iter_d; + int *constraint_last_binding_iter_d; + bool *free_variable_mask_d; + bool *binding_constraint_mask_d; + active_set_mask_changes_t *mask_changes_d; + sv_estimator_ctx_t *sv_estimator; + int free_variable_count; + int binding_constraint_count; + int mask_changes_since_estimate; + bool raise_checked; + double step_size_ceiling; + active_set_anchor_t anchor; + int raise_count; + int revert_count; + int power_iteration_count; +} active_set_state_t; + typedef struct { int num_variables; int num_constraints; @@ -151,7 +189,6 @@ typedef struct { cusparseDnVecDescr_t spmv_delta_dual_solution; cusparseDnVecDescr_t spmv_primal_product; cusparseDnVecDescr_t spmv_dual_product; - cusparseDnVecDescr_t spmv_power_iteration_normalized_vector; spmv_plan_t matA_spmv_plan; spmv_plan_t matAt_spmv_plan; void *cub_reduce_workspace_d; @@ -205,12 +242,6 @@ typedef struct { double *hsd_dual_scale_current_d; double *hsd_dual_scale_pdhg_d; double *hsd_dual_scale_reflected_d; - - double *power_iteration_normalized_vector_d; - double *power_iteration_vector_norm_scalar_d; - double *power_iter_sigma_squared_mp; - double *power_iter_residual_norm_mp; - double *primal_residual_d; double *dual_residual_d; double *primal_infeasibility_stationarity_residual_d; @@ -221,6 +252,7 @@ typedef struct { box_newton_state_t primal_box_cone_state; solver_control_t control; + active_set_state_t active_set; double *pdhg_reduce_d; double *pdhg_primal_norm_d; diff --git a/internal/matrix_formats.h b/internal/matrix_formats.h index b3a8846..fc9c584 100644 --- a/internal/matrix_formats.h +++ b/internal/matrix_formats.h @@ -56,6 +56,13 @@ extern "C" int **row_ptr, int **col_ind, double **vals, int *nnz_out); + void transpose_csr_and_build_map( + const lp_problem_t *problem, + int **transpose_row_ptr, + int **transpose_col_ind, + double **transpose_val, + int **transpose_map); + #ifdef __cplusplus } #endif diff --git a/internal/power_iteration.h b/internal/power_iteration.h index 33801d8..81d972b 100644 --- a/internal/power_iteration.h +++ b/internal/power_iteration.h @@ -17,15 +17,52 @@ limitations under the License. #pragma once #include "internal_types.h" +#include #ifdef __cplusplus extern "C" { #endif - double estimate_maximum_singular_value( - pdhg_solver_state_t *state, - const power_iteration_parameters_t *power_iter_params); + typedef enum + { + SV_ESTIMATOR_CONVERGED = 0, + SV_ESTIMATOR_EARLY_EXIT = 1, + SV_ESTIMATOR_MAX_ITER = 2, + SV_ESTIMATOR_DEGENERATE = 3, + } sv_estimator_status_t; + + typedef struct + { + int max_iter; + double tol; + const bool *row_mask_d; + const bool *column_mask_d; + double early_exit_singular_value_threshold; + } sv_estimator_opts_t; + + typedef struct + { + sv_estimator_status_t status; + double max_singular_value; + int iterations; + } sv_estimator_result_t; + + sv_estimator_ctx_t *sv_estimator_create( + cusparseHandle_t sparse_handle, + cublasHandle_t blas_handle, + cudaStream_t stream, + curandGenerator_t curand_gen, + const cu_sparse_matrix_csr_t *matrix, + const cu_sparse_matrix_csr_t *transpose, + int num_rows, + int num_columns); + + sv_estimator_result_t sv_estimator_run( + sv_estimator_ctx_t *ctx, + const sv_estimator_opts_t *opts); + + void sv_estimator_free(sv_estimator_ctx_t *ctx); #ifdef __cplusplus } diff --git a/internal/utils.h b/internal/utils.h index 67dd588..7e54c06 100644 --- a/internal/utils.h +++ b/internal/utils.h @@ -52,6 +52,15 @@ extern "C" void *safe_calloc(size_t num, size_t size); void *safe_realloc(void *ptr, size_t new_size); + void allocate_zeroed_device_buffer(void **dst, size_t bytes); + + void allocate_and_copy_to_device( + void **dst, + const void *src, + size_t bytes); + + void allocate_cub_reduce_workspace(pdhg_solver_state_t *state); + void reduce_device_sum( pdhg_solver_state_t *state, const double *input, diff --git a/src/active_set_boost.cu b/src/active_set_boost.cu new file mode 100644 index 0000000..bd8dbb3 --- /dev/null +++ b/src/active_set_boost.cu @@ -0,0 +1,560 @@ +/* +Copyright 2026 Haihao Lu + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#include "active_set_boost.h" +#include "cuda_macros.h" +#include "display.h" +#include "power_iteration.h" +#include "termination_checker.h" +#include "utils.h" +#include + +static bool is_active_set_boost_available( + const pdhg_solver_state_t *state, + const pdhg_parameters_t *params); +__global__ void update_binding_constraint_mask_kernel( + const double *__restrict__ constraint_dual_projection_input, + const double *constraint_cone_scale, + const double *__restrict__ constraint_lower_bound, + const double *__restrict__ constraint_upper_bound, + int *__restrict__ constraint_last_binding_iter, + bool *__restrict__ binding_constraint_mask, + active_set_mask_changes_t *mask_changes, + double relative_active_set_tol, + int current_iter, + int window_start_iter, + int num_constraints); +__global__ void update_free_variable_mask_kernel( + const double *__restrict__ pdhg_reduced_cost, + const double *__restrict__ objective_vector, + const double *primal_scale_pdhg, + const double *__restrict__ variable_lower_bound, + const double *__restrict__ variable_upper_bound, + int *__restrict__ variable_last_free_iter, + bool *__restrict__ free_variable_mask, + active_set_mask_changes_t *mask_changes, + double relative_active_set_tol, + int current_iter, + int window_start_iter, + int num_variables); +static bool is_raise_possible( + double target_step_size, + double current_step_size, + double min_raise_ratio); +static sv_estimator_result_t estimate_masked_constraint_norm( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params); +static void save_active_set_anchor(pdhg_solver_state_t *state); +static void restore_active_set_anchor(pdhg_solver_state_t *state); + +void initialize_active_set( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + if (!is_active_set_boost_enabled(params)) + { + return; + } + + active_set_state_t *active_set = &state->active_set; + const size_t num_variables = (size_t)state->num_variables; + const size_t num_constraints = (size_t)state->num_constraints; + allocate_zeroed_device_buffer( + (void **)&active_set->constraint_dual_projection_input_d, + state->con_bytes); + allocate_zeroed_device_buffer( + (void **)&active_set->variable_last_free_iter_d, + num_variables * sizeof(int)); + allocate_zeroed_device_buffer( + (void **)&active_set->constraint_last_binding_iter_d, + num_constraints * sizeof(int)); + allocate_zeroed_device_buffer( + (void **)&active_set->free_variable_mask_d, + num_variables * sizeof(bool)); + allocate_zeroed_device_buffer( + (void **)&active_set->binding_constraint_mask_d, + num_constraints * sizeof(bool)); + CUDA_CHECK(cudaMemset(active_set->free_variable_mask_d, 1, num_variables * sizeof(bool))); + CUDA_CHECK(cudaMemset(active_set->binding_constraint_mask_d, 1, num_constraints * sizeof(bool))); + allocate_zeroed_device_buffer( + (void **)&active_set->mask_changes_d, + sizeof(active_set_mask_changes_t)); + allocate_zeroed_device_buffer( + (void **)&active_set->anchor.primal_solution_d, + state->var_bytes); + allocate_zeroed_device_buffer( + (void **)&active_set->anchor.dual_solution_d, + state->con_bytes); + allocate_zeroed_device_buffer( + (void **)&active_set->anchor.hsd_scalars_d, + HSD_SCALARS_COUNT * sizeof(double)); + allocate_zeroed_device_buffer( + (void **)&active_set->anchor.constraint_cone_scale_d, + sizeof(double)); + active_set->free_variable_count = state->num_variables; + active_set->binding_constraint_count = state->num_constraints; + active_set->step_size_ceiling = INFINITY; +} + +void active_set_free(active_set_state_t *active_set) +{ + sv_estimator_free(active_set->sv_estimator); + if (active_set->constraint_dual_projection_input_d) + CUDA_CHECK(cudaFree(active_set->constraint_dual_projection_input_d)); + if (active_set->variable_last_free_iter_d) + CUDA_CHECK(cudaFree(active_set->variable_last_free_iter_d)); + if (active_set->constraint_last_binding_iter_d) + CUDA_CHECK(cudaFree(active_set->constraint_last_binding_iter_d)); + if (active_set->free_variable_mask_d) + CUDA_CHECK(cudaFree(active_set->free_variable_mask_d)); + if (active_set->binding_constraint_mask_d) + CUDA_CHECK(cudaFree(active_set->binding_constraint_mask_d)); + if (active_set->mask_changes_d) + CUDA_CHECK(cudaFree(active_set->mask_changes_d)); + if (active_set->anchor.primal_solution_d) + CUDA_CHECK(cudaFree(active_set->anchor.primal_solution_d)); + if (active_set->anchor.dual_solution_d) + CUDA_CHECK(cudaFree(active_set->anchor.dual_solution_d)); + if (active_set->anchor.hsd_scalars_d) + CUDA_CHECK(cudaFree(active_set->anchor.hsd_scalars_d)); + if (active_set->anchor.constraint_cone_scale_d) + CUDA_CHECK(cudaFree(active_set->anchor.constraint_cone_scale_d)); + + *active_set = {}; +} + +void update_active_set_masks( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + if (!is_active_set_boost_available(state, params)) + { + return; + } + + active_set_state_t *active_set = &state->active_set; + const int current_iter = state->control.total_count; + const int window_start_iter = + current_iter - params->active_set_params.window_iterations; + + CUDA_CHECK(cudaMemsetAsync( + active_set->mask_changes_d, + 0, + sizeof(active_set_mask_changes_t), + state->stream_A)); + update_binding_constraint_mask_kernel<<< + state->num_blocks_dual, + THREADS_PER_BLOCK, + 0, + state->stream_A>>>( + active_set->constraint_dual_projection_input_d, + state->dual_box_cone_state.scalar_d, + state->constraint_lower_bound_d, + state->constraint_upper_bound_d, + active_set->constraint_last_binding_iter_d, + active_set->binding_constraint_mask_d, + active_set->mask_changes_d, + params->active_set_params.relative_active_set_tol, + current_iter, + window_start_iter, + state->num_constraints); + update_free_variable_mask_kernel<<< + state->num_blocks_primal, + THREADS_PER_BLOCK, + 0, + state->stream_A>>>( + state->pdhg_reduced_cost_d, + state->objective_vector_d, + state->hsd_primal_scale_pdhg_d, + state->variable_lower_bound_d, + state->variable_upper_bound_d, + active_set->variable_last_free_iter_d, + active_set->free_variable_mask_d, + active_set->mask_changes_d, + params->active_set_params.relative_active_set_tol, + current_iter, + window_start_iter, + state->num_variables); + active_set_mask_changes_t mask_changes = {}; + CUDA_CHECK(cudaMemcpyAsync( + &mask_changes, + active_set->mask_changes_d, + sizeof(mask_changes), + cudaMemcpyDeviceToHost, + state->stream_A)); + CUDA_CHECK(cudaStreamSynchronize(state->stream_A)); + + active_set->free_variable_count += + mask_changes.variables_added - mask_changes.variables_removed; + active_set->binding_constraint_count += + mask_changes.constraints_added - mask_changes.constraints_removed; + active_set->mask_changes_since_estimate += + mask_changes.variables_added + mask_changes.variables_removed + + mask_changes.constraints_added + mask_changes.constraints_removed; +} + +static bool is_active_set_boost_available( + const pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + return is_active_set_boost_enabled(params) && + state->active_set.revert_count < params->active_set_params.max_reverts; +} + +__global__ void update_binding_constraint_mask_kernel( + const double *__restrict__ constraint_dual_projection_input, + const double *constraint_cone_scale, + const double *__restrict__ constraint_lower_bound, + const double *__restrict__ constraint_upper_bound, + int *__restrict__ constraint_last_binding_iter, + bool *__restrict__ binding_constraint_mask, + active_set_mask_changes_t *mask_changes, + double relative_active_set_tol, + int current_iter, + int window_start_iter, + int num_constraints) +{ + const int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i >= num_constraints) + { + return; + } + + const double lower = constraint_lower_bound[i]; + const double upper = constraint_upper_bound[i]; + const double cone_scale = *constraint_cone_scale; + bool binding = lower == upper || !(cone_scale > 0.0); + if (!binding) + { + const double dual_projection_input = + constraint_dual_projection_input[i] / cone_scale; + bool safely_inside = true; + if (isfinite(upper)) + { + const double margin = + relative_active_set_tol * + fmax(1.0, fmax(fabs(dual_projection_input), fabs(upper))); + safely_inside = + safely_inside && (upper + dual_projection_input > margin); + } + if (isfinite(lower)) + { + const double margin = + relative_active_set_tol * + fmax(1.0, fmax(fabs(dual_projection_input), fabs(lower))); + safely_inside = + safely_inside && (-lower - dual_projection_input > margin); + } + binding = !safely_inside; + } + if (binding) + { + constraint_last_binding_iter[i] = current_iter; + } + const bool retained = + constraint_last_binding_iter[i] >= window_start_iter; + if (retained != binding_constraint_mask[i]) + { + binding_constraint_mask[i] = retained; + atomicAdd( + retained ? &mask_changes->constraints_added + : &mask_changes->constraints_removed, + 1); + } +} + +__global__ void update_free_variable_mask_kernel( + const double *__restrict__ pdhg_reduced_cost, + const double *__restrict__ objective_vector, + const double *primal_scale_pdhg, + const double *__restrict__ variable_lower_bound, + const double *__restrict__ variable_upper_bound, + int *__restrict__ variable_last_free_iter, + bool *__restrict__ free_variable_mask, + active_set_mask_changes_t *mask_changes, + double relative_active_set_tol, + int current_iter, + int window_start_iter, + int num_variables) +{ + const int j = blockIdx.x * blockDim.x + threadIdx.x; + if (j >= num_variables) + { + return; + } + + const double lower = variable_lower_bound[j]; + const double upper = variable_upper_bound[j]; + const double reduced_cost_tolerance = + relative_active_set_tol * + fmax(1.0, fabs(objective_vector[j])) * + (*primal_scale_pdhg); + const double reduced_cost = pdhg_reduced_cost[j]; + const bool fixed_at_zero = lower == upper && lower == 0.0; + const bool clamped_at_lower = + lower == 0.0 && reduced_cost > reduced_cost_tolerance; + const bool clamped_at_upper = + upper == 0.0 && reduced_cost < -reduced_cost_tolerance; + const bool clamped_at_zero = + fixed_at_zero || clamped_at_lower || clamped_at_upper; + if (!clamped_at_zero) + { + variable_last_free_iter[j] = current_iter; + } + const bool retained = + variable_last_free_iter[j] >= window_start_iter; + if (retained != free_variable_mask[j]) + { + free_variable_mask[j] = retained; + atomicAdd( + retained ? &mask_changes->variables_added + : &mask_changes->variables_removed, + 1); + } +} + +bool should_revert_active_set_boost( + const pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + if (state->control.step_size <= state->control.step_size_base) + { + return false; + } + const termination_reason_t reason = state->control.termination_reason; + if (reason == TERMINATION_REASON_NUMERICAL_FAILURE) + { + return true; + } + if (reason != TERMINATION_REASON_UNSPECIFIED && + reason != TERMINATION_REASON_TIME_LIMIT && + reason != TERMINATION_REASON_ITERATION_LIMIT) + { + return false; + } + const double fixed_point_error = *state->control.fixed_point_error_mp; + if (!isfinite(state->metrics_h->relative_primal_residual) || + !isfinite(state->metrics_h->relative_dual_residual) || + !isfinite(state->metrics_h->relative_objective_gap) || + !isfinite(fixed_point_error)) + { + return true; + } + const double initial_fixed_point_error = state->control.initial_fixed_point_error; + return initial_fixed_point_error > 0.0 && + fixed_point_error > + (1.0 + params->active_set_params.divergence_margin) * + initial_fixed_point_error; +} + +void try_raise_active_set_step_size( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + if (!is_active_set_boost_available(state, params)) + { + return; + } + + active_set_state_t *active_set = &state->active_set; + if (active_set->free_variable_count == state->num_variables && + active_set->binding_constraint_count == state->num_constraints && + active_set->mask_changes_since_estimate == 0) + { + return; + } + const bool masks_changed_enough = + active_set->mask_changes_since_estimate > 0 && + active_set->mask_changes_since_estimate >= + params->active_set_params.reestimate_change_ratio * + (active_set->free_variable_count + + active_set->binding_constraint_count); + if (active_set->raise_checked && !masks_changed_enough) + { + return; + } + + const double step_size_base = state->control.step_size_base; + const double previous_step_size = state->control.step_size; + const double min_raise_ratio = params->active_set_params.min_raise_ratio; + if (!is_raise_possible( + fmax(step_size_base, active_set->step_size_ceiling), + previous_step_size, + min_raise_ratio)) + { + active_set->raise_checked = true; + active_set->mask_changes_since_estimate = 0; + return; + } + + const sv_estimator_result_t estimate = + estimate_masked_constraint_norm(state, params); + if (estimate.status != SV_ESTIMATOR_EARLY_EXIT && + estimate.status != SV_ESTIMATOR_CONVERGED) + { + return; + } + active_set->raise_checked = true; + active_set->mask_changes_since_estimate = 0; + if (estimate.status == SV_ESTIMATOR_EARLY_EXIT) + { + return; + } + + const double target_step_size = + fmax(step_size_base, + fmin(params->active_set_params.safety_factor / + estimate.max_singular_value, + active_set->step_size_ceiling)); + if (!is_raise_possible(target_step_size, previous_step_size, min_raise_ratio)) + { + return; + } + state->control.step_size = target_step_size; + active_set->raise_count++; + save_active_set_anchor(state); + display_active_set_boost_raise( + params, previous_step_size, target_step_size, + estimate.max_singular_value); +} + +static bool is_raise_possible( + double target_step_size, + double current_step_size, + double min_raise_ratio) +{ + return target_step_size > current_step_size && + target_step_size >= min_raise_ratio * current_step_size; +} + +static sv_estimator_result_t estimate_masked_constraint_norm( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + active_set_state_t *active_set = &state->active_set; + if (!active_set->sv_estimator) + { + active_set->sv_estimator = sv_estimator_create( + state->sparse_handle_A, + state->blas_handle_A, + state->stream_A, + state->curand_gen, + state->constraint_matrix, + state->constraint_matrix_t, + state->num_constraints, + state->num_variables); + } + + sv_estimator_opts_t opts = {}; + opts.max_iter = params->power_iter_params.max_iter; + opts.tol = params->power_iter_params.tol; + opts.row_mask_d = active_set->binding_constraint_mask_d; + opts.column_mask_d = active_set->free_variable_mask_d; + opts.early_exit_singular_value_threshold = + params->active_set_params.safety_factor / + (params->active_set_params.min_raise_ratio * state->control.step_size); + + const sv_estimator_result_t estimate = + sv_estimator_run(active_set->sv_estimator, &opts); + active_set->power_iteration_count += estimate.iterations; + return estimate; +} + +static void save_active_set_anchor(pdhg_solver_state_t *state) +{ + active_set_state_t *active_set = &state->active_set; + CUDA_CHECK(cudaMemcpyAsync( + active_set->anchor.primal_solution_d, + state->initial_primal_solution_d, + state->var_bytes, + cudaMemcpyDeviceToDevice, + state->stream_A)); + CUDA_CHECK(cudaMemcpyAsync( + active_set->anchor.dual_solution_d, + state->initial_dual_solution_d, + state->con_bytes, + cudaMemcpyDeviceToDevice, + state->stream_A)); + CUDA_CHECK(cudaMemcpyAsync( + active_set->anchor.hsd_scalars_d, + state->hsd_scalars_d, + HSD_SCALARS_COUNT * sizeof(double), + cudaMemcpyDeviceToDevice, + state->stream_A)); + CUDA_CHECK(cudaMemcpyAsync( + active_set->anchor.constraint_cone_scale_d, + state->dual_box_cone_state.scalar_d, + sizeof(double), + cudaMemcpyDeviceToDevice, + state->stream_A)); + active_set->anchor.primal_weight = state->control.primal_weight; + active_set->anchor.pid = state->control.pid; +} + +void revert_active_set_boost( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + active_set_state_t *active_set = &state->active_set; + active_set->step_size_ceiling = + params->active_set_params.divergence_ceiling_ratio * state->control.step_size; + state->control.step_size = state->control.step_size_base; + restore_active_set_anchor(state); + active_set->raise_checked = false; + active_set->revert_count++; +} + +static void restore_active_set_anchor(pdhg_solver_state_t *state) +{ + active_set_state_t *active_set = &state->active_set; + double *primal_destinations[] = { + state->initial_primal_solution_d, + state->current_primal_solution_d, + state->pdhg_primal_solution_d}; + double *dual_destinations[] = { + state->initial_dual_solution_d, + state->current_dual_solution_d, + state->pdhg_dual_solution_d}; + for (int i = 0; i < 3; ++i) + { + CUDA_CHECK(cudaMemcpyAsync( + primal_destinations[i], + active_set->anchor.primal_solution_d, + state->var_bytes, + cudaMemcpyDeviceToDevice, + state->stream_A)); + CUDA_CHECK(cudaMemcpyAsync( + dual_destinations[i], + active_set->anchor.dual_solution_d, + state->con_bytes, + cudaMemcpyDeviceToDevice, + state->stream_A)); + } + CUDA_CHECK(cudaMemcpyAsync( + state->hsd_scalars_d, + active_set->anchor.hsd_scalars_d, + HSD_SCALARS_COUNT * sizeof(double), + cudaMemcpyDeviceToDevice, + state->stream_A)); + CUDA_CHECK(cudaMemcpyAsync( + state->dual_box_cone_state.scalar_d, + active_set->anchor.constraint_cone_scale_d, + sizeof(double), + cudaMemcpyDeviceToDevice, + state->stream_A)); + state->control.primal_weight = active_set->anchor.primal_weight; + state->control.pid = active_set->anchor.pid; +} diff --git a/src/cli.c b/src/cli.c index ffa7624..ad93b1a 100644 --- a/src/cli.c +++ b/src/cli.c @@ -176,6 +176,7 @@ void print_usage(const char *prog_name) fprintf(stderr, " --no_bound_obj_rescaling Disable bound objective rescaling.\n"); fprintf(stderr, "\n Solver algorithm:\n"); fprintf(stderr, " --step_size_factor Step size scaling factor in (0, 1) (default: 0.998).\n"); + fprintf(stderr, " --no_active_set_step_size_boost Disable active-set step-size boosting (default: enabled).\n"); fprintf(stderr, " --singular_value_max_iter Max iterations for singular value estimation (default: 5000).\n"); fprintf(stderr, " --singular_value_tol Tolerance for singular value estimation (default: 1e-4).\n"); fprintf(stderr, " --cone_newton_max_iter Max Newton iterations for cone projection (default: 100).\n"); @@ -206,6 +207,7 @@ int main(int argc, char *argv[]) {"pock_chambolle_alpha", required_argument, 0, 1004}, {"no_bound_obj_rescaling", no_argument, 0, 1005}, {"step_size_factor", required_argument, 0, 1006}, + {"no_active_set_step_size_boost", no_argument, 0, 1007}, {"singular_value_max_iter", required_argument, 0, 1008}, {"singular_value_tol", required_argument, 0, 1009}, {"cone_newton_max_iter", required_argument, 0, 1010}, @@ -214,10 +216,10 @@ int main(int argc, char *argv[]) {"iter_limit", required_argument, 0, 1013}, {"eps_opt", required_argument, 0, 1014}, {"eps_feas", required_argument, 0, 1015}, - {"eps_farkas_homogeneous", required_argument, 0, 1019}, - {"eps_farkas_displacement", required_argument, 0, 1016}, - {"eval_freq", required_argument, 0, 1017}, - {"opt_norm", required_argument, 0, 1018}, + {"eps_farkas_homogeneous", required_argument, 0, 1016}, + {"eps_farkas_displacement", required_argument, 0, 1017}, + {"eval_freq", required_argument, 0, 1018}, + {"opt_norm", required_argument, 0, 1019}, {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0}}; @@ -256,6 +258,9 @@ int main(int argc, char *argv[]) ¶ms.step_size_params.step_size_factor)) return 1; break; + case 1007: + params.active_set_params.step_size_boost = false; + break; case 1008: if (!parse_int_arg("--singular_value_max_iter", optarg, ¶ms.power_iter_params.max_iter)) @@ -296,22 +301,22 @@ int main(int argc, char *argv[]) ¶ms.termination_criteria.eps_feasible_relative)) return 1; break; - case 1019: + case 1016: if (!parse_double_arg("--eps_farkas_homogeneous", optarg, ¶ms.termination_criteria.eps_farkas_homogeneous)) return 1; break; - case 1016: + case 1017: if (!parse_double_arg("--eps_farkas_displacement", optarg, ¶ms.termination_criteria.eps_farkas_displacement)) return 1; break; - case 1017: + case 1018: if (!parse_int_arg("--eval_freq", optarg, ¶ms.termination_evaluation_frequency)) return 1; break; - case 1018: + case 1019: { const char *norm_str = optarg; if (strcmp(norm_str, "l2") == 0) diff --git a/src/cone_projector.cu b/src/cone_projector.cu index d5ce98a..7f1f92a 100644 --- a/src/cone_projector.cu +++ b/src/cone_projector.cu @@ -38,6 +38,7 @@ __global__ void update_dual_box_cone_projection_cooperative_kernel( double *__restrict__ current_dual, double *__restrict__ reflected_dual, double *__restrict__ evaluation_dual, + double *__restrict__ constraint_dual_projection_input, const double *__restrict__ initial_dual, const double *__restrict__ gradient, const double *step_size_d, @@ -57,6 +58,7 @@ __global__ void update_dual_box_cone_projection_single_block_kernel( double *__restrict__ current_dual, double *__restrict__ reflected_dual, double *__restrict__ evaluation_dual, + double *__restrict__ constraint_dual_projection_input, const double *__restrict__ initial_dual, const double *__restrict__ gradient, const double *step_size_d, @@ -177,6 +179,7 @@ void update_dual_box_cone_projection( double *current_dual, double *reflected_dual, double *evaluation_dual, + double *constraint_dual_projection_input, const double *initial_dual, const double *gradient, const double *step_size_d, @@ -200,6 +203,7 @@ void update_dual_box_cone_projection( current_dual, reflected_dual, evaluation_dual, + constraint_dual_projection_input, initial_dual, gradient, step_size_d, @@ -237,6 +241,7 @@ void update_dual_box_cone_projection( current_dual, reflected_dual, evaluation_dual, + constraint_dual_projection_input, initial_dual, gradient, step_size_d, @@ -379,6 +384,7 @@ __global__ void update_dual_box_cone_projection_cooperative_kernel( double *__restrict__ current_dual, double *__restrict__ reflected_dual, double *__restrict__ evaluation_dual, + double *__restrict__ constraint_dual_projection_input, const double *__restrict__ initial_dual, const double *__restrict__ gradient, const double *step_size_d, @@ -502,7 +508,11 @@ __global__ void update_dual_box_cone_projection_cooperative_kernel( const double projected = unprojected + fmin(fmax(-unprojected, box_lower), box_upper); if (is_major) + { evaluation_dual[i] = projected; + if (constraint_dual_projection_input) + constraint_dual_projection_input[i] = unprojected; + } const double reflected = 2.0 * projected - current; reflected_dual[i] = reflected; const double relaxed = @@ -533,6 +543,7 @@ __global__ void update_dual_box_cone_projection_single_block_kernel( double *__restrict__ current_dual, double *__restrict__ reflected_dual, double *__restrict__ evaluation_dual, + double *__restrict__ constraint_dual_projection_input, const double *__restrict__ initial_dual, const double *__restrict__ gradient, const double *step_size_d, @@ -629,7 +640,11 @@ __global__ void update_dual_box_cone_projection_single_block_kernel( const double projected = unprojected + fmin(fmax(-unprojected, box_lower), box_upper); if (is_major) + { evaluation_dual[i] = projected; + if (constraint_dual_projection_input) + constraint_dual_projection_input[i] = unprojected; + } const double reflected = 2.0 * projected - current; reflected_dual[i] = reflected; const double relaxed = diff --git a/src/cuhsdlp.c b/src/cuhsdlp.c index e43a54c..f0733d8 100644 --- a/src/cuhsdlp.c +++ b/src/cuhsdlp.c @@ -209,6 +209,16 @@ void set_default_parameters(pdhg_parameters_t *params) params->restart_params.artificial_restart_threshold = 0.36; params->restart_params.sufficient_reduction_for_restart = 0.2; params->restart_params.necessary_reduction_for_restart = 0.5; + + params->active_set_params.step_size_boost = true; + params->active_set_params.window_iterations = 10000; + params->active_set_params.relative_active_set_tol = 1e-8; + params->active_set_params.safety_factor = 0.9; + params->active_set_params.min_raise_ratio = 1.1; + params->active_set_params.reestimate_change_ratio = 0.01; + params->active_set_params.divergence_ceiling_ratio = 0.7; + params->active_set_params.divergence_margin = 0.05; + params->active_set_params.max_reverts = 2; } cuhsdlp_result_t *solve_lp_problem( diff --git a/src/preconditioner.cu b/src/preconditioner.cu index 96a8cdd..c298e1a 100644 --- a/src/preconditioner.cu +++ b/src/preconditioner.cu @@ -617,9 +617,30 @@ void rescale_problem( initialize_spmv_plans(state); compute_lp_problem_norms_device(state, optimality_norm, &state->rescaled_objective_norm, &state->rescaled_bound_norm); - state->rescaled_constraint_norm = estimate_maximum_singular_value(state, power_iter_params); - state->rescaling_time_sec = - (double)(clock() - start_rescaling) / CLOCKS_PER_SEC; + if (state->constraint_matrix->num_nonzeros == 0) + { + state->rescaled_constraint_norm = 0.0; + } + else + { + sv_estimator_opts_t opts = {}; + opts.max_iter = power_iter_params->max_iter; + opts.tol = power_iter_params->tol; + sv_estimator_ctx_t *estimator = sv_estimator_create( + state->sparse_handle_A, + state->blas_handle_A, + state->stream_A, + state->curand_gen, + state->constraint_matrix, + state->constraint_matrix_t, + state->num_constraints, + state->num_variables); + const sv_estimator_result_t estimate = + sv_estimator_run(estimator, &opts); + sv_estimator_free(estimator); + state->rescaled_constraint_norm = estimate.max_singular_value; + } + state->rescaling_time_sec = (double)(clock() - start_rescaling) / CLOCKS_PER_SEC; if (constraint_rescaling) CUDA_CHECK(cudaFree(constraint_rescaling)); diff --git a/src/result.cu b/src/result.cu index 77f7901..3ae0a24 100644 --- a/src/result.cu +++ b/src/result.cu @@ -133,6 +133,9 @@ cuhsdlp_result_t *create_result_from_state( results->termination_reason = state->control.termination_reason; results->restart_count = state->control.restart_count; + results->active_set_raise_count = state->active_set.raise_count; + results->active_set_revert_count = state->active_set.revert_count; + results->active_set_power_iteration_count = state->active_set.power_iteration_count; results->homogeneous_scale = state->metrics_h->pdhg_primal_scale; results->final_step_size_primal = state->control.step_size_primal; results->final_step_size_dual = state->control.step_size_dual; diff --git a/src/setup.cu b/src/setup.cu index 4e253d6..e30a466 100644 --- a/src/setup.cu +++ b/src/setup.cu @@ -15,19 +15,19 @@ limitations under the License. */ #include "setup.h" +#include "active_set_boost.h" #include "cone_projector.h" #include "cuhsdlp.h" #include "display.h" +#include "matrix_formats.h" #include "preconditioner.h" #include "spmv_backend.h" #include "termination_checker.h" #include "utils.h" -#include #include #include #include #include -#include static constexpr int COOP_NEWTON_THRESHOLD = 4 * THREADS_PER_BLOCK; @@ -37,12 +37,6 @@ __global__ void build_row_ind_kernel( int *__restrict__ row_ind); __global__ void init_hsd_scalars_kernel(double *hsd_scalars_d); static void param_error(const char *requirement, double value); -static void transpose_csr_and_build_map( - const lp_problem_t *problem, - int **transpose_row_ptr, - int **transpose_col_ind, - double **transpose_val, - int **transpose_map); static void create_csr_descriptor( cusparseSpMatDescr_t *descriptor, int num_rows, @@ -53,20 +47,11 @@ static void create_dense_vector_descriptor( int length, double *values); static void setup_sparse_resources(pdhg_solver_state_t *state); -static void allocate_and_copy_to_device( - void **dst, - const void *src, - size_t bytes); -static void allocate_zeroed_device_buffer( - void **dst, - size_t bytes); -static void allocate_cub_reduce_workspace(pdhg_solver_state_t *state); static void upload_problem_data( pdhg_solver_state_t *state, const lp_problem_t *working_problem); static void allocate_solver_work_vectors(pdhg_solver_state_t *state); static void initialize_homogeneous_scalars(pdhg_solver_state_t *state); -static void initialize_power_iteration_buffers(pdhg_solver_state_t *state); static void allocate_host_staging_buffers(pdhg_solver_state_t *state); static void allocate_device_scalars(pdhg_solver_state_t *state); static void apply_warm_starts( @@ -175,6 +160,52 @@ void check_params_validity(const pdhg_parameters_t *params) param_error("cone_newton_tol must be finite and > 0", params->cone_params.tol); } + if (params->active_set_params.window_iterations < 1) + { + param_error("active_set window_iterations must be >= 1", + params->active_set_params.window_iterations); + } + if (!(params->active_set_params.relative_active_set_tol >= 0.0 && + isfinite(params->active_set_params.relative_active_set_tol))) + { + param_error("active_set relative_active_set_tol must be finite and >= 0", + params->active_set_params.relative_active_set_tol); + } + if (!(params->active_set_params.safety_factor > 0.0 && + params->active_set_params.safety_factor < 1.0)) + { + param_error("active_set safety_factor must be in (0, 1)", + params->active_set_params.safety_factor); + } + if (!(params->active_set_params.min_raise_ratio >= 1.0 && + isfinite(params->active_set_params.min_raise_ratio))) + { + param_error("active_set min_raise_ratio must be finite and >= 1", + params->active_set_params.min_raise_ratio); + } + if (!(params->active_set_params.reestimate_change_ratio >= 0.0 && + isfinite(params->active_set_params.reestimate_change_ratio))) + { + param_error("active_set reestimate_change_ratio must be finite and >= 0", + params->active_set_params.reestimate_change_ratio); + } + if (!(params->active_set_params.divergence_ceiling_ratio > 0.0 && + params->active_set_params.divergence_ceiling_ratio <= 1.0)) + { + param_error("active_set divergence_ceiling_ratio must be in (0, 1]", + params->active_set_params.divergence_ceiling_ratio); + } + if (!(params->active_set_params.divergence_margin >= 0.0 && + isfinite(params->active_set_params.divergence_margin))) + { + param_error("active_set divergence_margin must be finite and >= 0", + params->active_set_params.divergence_margin); + } + if (params->active_set_params.max_reverts < 0) + { + param_error("active_set max_reverts must be >= 0", + params->active_set_params.max_reverts); + } if (!(params->termination_criteria.eps_optimal_relative > 0.0 && isfinite(params->termination_criteria.eps_optimal_relative))) { @@ -286,7 +317,7 @@ pdhg_solver_state_t *initialize_solver_state( allocate_solver_work_vectors(state); allocate_host_staging_buffers(state); initialize_homogeneous_scalars(state); - initialize_power_iteration_buffers(state); + initialize_active_set(state, params); allocate_device_scalars(state); initialize_newton_structure(state, working_problem); initialize_newton_workspaces(state); @@ -322,52 +353,6 @@ pdhg_solver_state_t *initialize_solver_state( return state; } -static void transpose_csr_and_build_map( - const lp_problem_t *problem, - int **transpose_row_ptr, - int **transpose_col_ind, - double **transpose_val, - int **transpose_map) -{ - const int num_rows = problem->num_constraints; - const int num_cols = problem->num_variables; - const int nnz = problem->constraint_matrix_num_nonzeros; - const int *row_ptr = problem->constraint_matrix_row_pointers; - const int *col_ind = problem->constraint_matrix_col_indices; - const double *val = problem->constraint_matrix_values; - const size_t alloc_nnz = (nnz > 0) ? (size_t)nnz : 1; - const size_t alloc_cols = (num_cols > 0) ? (size_t)num_cols : 1; - - *transpose_row_ptr = (int *)safe_calloc((size_t)(num_cols + 1), sizeof(int)); - *transpose_col_ind = (int *)safe_malloc(alloc_nnz * sizeof(int)); - *transpose_val = (double *)safe_malloc(alloc_nnz * sizeof(double)); - *transpose_map = (int *)safe_malloc(alloc_nnz * sizeof(int)); - - for (int k = 0; k < nnz; ++k) - (*transpose_row_ptr)[col_ind[k] + 1]++; - - for (int j = 0; j < num_cols; ++j) - (*transpose_row_ptr)[j + 1] += (*transpose_row_ptr)[j]; - - int *next = (int *)safe_malloc(alloc_cols * sizeof(int)); - if (num_cols > 0) - memcpy(next, *transpose_row_ptr, (size_t)num_cols * sizeof(int)); - - for (int i = 0; i < num_rows; ++i) - { - for (int k = row_ptr[i]; k < row_ptr[i + 1]; ++k) - { - int j = col_ind[k]; - int pos = next[j]++; - (*transpose_col_ind)[pos] = i; - (*transpose_val)[pos] = val[k]; - (*transpose_map)[k] = pos; - } - } - - free(next); -} - static void create_csr_descriptor( cusparseSpMatDescr_t *descriptor, int num_rows, @@ -442,10 +427,6 @@ static void setup_sparse_resources(pdhg_solver_state_t *state) &state->spmv_dual_product, state->num_variables, state->dual_product_d); - create_dense_vector_descriptor( - &state->spmv_power_iteration_normalized_vector, - state->num_constraints, - state->power_iteration_normalized_vector_d); } void initialize_spmv_plans(pdhg_solver_state_t *state) @@ -464,74 +445,6 @@ void initialize_spmv_plans(pdhg_solver_state_t *state) &state->matAt_spmv_plan); } -static void allocate_and_copy_to_device( - void **dst, - const void *src, - size_t bytes) -{ - const size_t allocation_bytes = (bytes > 0) ? bytes : 1; - CUDA_CHECK(cudaMalloc(dst, allocation_bytes)); - if (bytes > 0) - { - CUDA_CHECK(cudaMemcpy( - *dst, - src, - bytes, - cudaMemcpyHostToDevice)); - } -} - -static void allocate_zeroed_device_buffer( - void **dst, - size_t bytes) -{ - const size_t allocation_bytes = (bytes > 0) ? bytes : 1; - CUDA_CHECK(cudaMalloc(dst, allocation_bytes)); - CUDA_CHECK(cudaMemset(*dst, 0, allocation_bytes)); -} - -static void allocate_cub_reduce_workspace(pdhg_solver_state_t *state) -{ - const int max_reduction_length = - state->num_variables + state->num_constraints; - size_t sum_bytes = 0; - size_t max_bytes = 0; - size_t abs_max_bytes = 0; - double *null_values = NULL; - auto absolute_values = - thrust::make_transform_iterator( - null_values, - absolute_value_transform_t{}); - - CUDA_CHECK(cub::DeviceReduce::Sum( - NULL, - sum_bytes, - null_values, - null_values, - max_reduction_length)); - CUDA_CHECK(cub::DeviceReduce::Max( - NULL, - max_bytes, - null_values, - null_values, - max_reduction_length)); - CUDA_CHECK(cub::DeviceReduce::Max( - NULL, - abs_max_bytes, - absolute_values, - null_values, - max_reduction_length)); - - size_t workspace_bytes = - (sum_bytes > max_bytes) ? sum_bytes : max_bytes; - if (abs_max_bytes > workspace_bytes) - workspace_bytes = abs_max_bytes; - state->cub_reduce_workspace_bytes = workspace_bytes; - CUDA_CHECK(cudaMalloc( - &state->cub_reduce_workspace_d, - state->cub_reduce_workspace_bytes)); -} - static void upload_problem_data( pdhg_solver_state_t *state, const lp_problem_t *working_problem) @@ -613,17 +526,13 @@ static void allocate_solver_work_vectors(pdhg_solver_state_t *state) { const size_t var_bytes = state->var_bytes; const size_t con_bytes = state->con_bytes; - const size_t padded_constraint_count = - ((size_t)state->num_constraints + 1) & ~(size_t)1; allocate_zeroed_device_buffer((void **)&state->initial_primal_solution_d, var_bytes); allocate_zeroed_device_buffer((void **)&state->current_primal_solution_d, var_bytes); allocate_zeroed_device_buffer((void **)&state->pdhg_primal_solution_d, var_bytes); allocate_zeroed_device_buffer((void **)&state->reflected_primal_solution_d, var_bytes); allocate_zeroed_device_buffer((void **)&state->delta_primal_solution_d, var_bytes); - allocate_zeroed_device_buffer( - (void **)&state->primal_product_d, - padded_constraint_count * sizeof(double)); + allocate_zeroed_device_buffer((void **)&state->primal_product_d, con_bytes); allocate_zeroed_device_buffer( (void **)&state->variable_reduction_entries_d, var_bytes); @@ -658,18 +567,6 @@ static void initialize_homogeneous_scalars(pdhg_solver_state_t *state) state->metrics_h->pdhg_dual_scale = 1.0; } -static void initialize_power_iteration_buffers(pdhg_solver_state_t *state) -{ - const size_t vector_bytes = - (state->con_bytes > 0) - ? state->con_bytes - : sizeof(double); - CUDA_CHECK(cudaMalloc( - &state->power_iteration_normalized_vector_d, - vector_bytes)); - CUDA_CHECK(cudaMalloc(&state->power_iteration_vector_norm_scalar_d, sizeof(double))); -} - static void allocate_host_staging_buffers(pdhg_solver_state_t *state) { CUDA_CHECK(cudaMallocHost(&state->metrics_h, sizeof(pdhg_metrics_t))); @@ -682,8 +579,6 @@ static void allocate_host_staging_buffers(pdhg_solver_state_t *state) &state->objective_norm_workspace_mp, sizeof(double), cudaHostAllocMapped)); - CUDA_CHECK(cudaHostAlloc(&state->power_iter_sigma_squared_mp, sizeof(double), cudaHostAllocMapped)); - CUDA_CHECK(cudaHostAlloc(&state->power_iter_residual_norm_mp, sizeof(double), cudaHostAllocMapped)); CUDA_CHECK(cudaHostAlloc(&state->control.fixed_point_error_mp, sizeof(double), cudaHostAllocMapped)); } @@ -699,8 +594,9 @@ static void allocate_device_scalars(pdhg_solver_state_t *state) state->hsd_dual_scale_pdhg_d = state->hsd_scalars_d + HSD_DUAL_SCALE_PDHG; state->hsd_dual_scale_reflected_d = state->hsd_scalars_d + HSD_DUAL_SCALE_REFLECTED; - CUDA_CHECK(cudaMalloc(&state->pdhg_reduce_d, PDHG_COUNT * sizeof(double))); - CUDA_CHECK(cudaMemset(state->pdhg_reduce_d, 0, PDHG_COUNT * sizeof(double))); + allocate_zeroed_device_buffer( + (void **)&state->pdhg_reduce_d, + PDHG_COUNT * sizeof(double)); state->pdhg_primal_norm_d = state->pdhg_reduce_d + PDHG_PRIMAL_NORM; state->pdhg_dual_norm_d = state->pdhg_reduce_d + PDHG_DUAL_NORM; state->objective_dot_primal_d = @@ -708,10 +604,9 @@ static void allocate_device_scalars(pdhg_solver_state_t *state) state->fixed_point_operator_cross_d = state->pdhg_reduce_d + PDHG_OPERATOR_CROSS_TERM; - CUDA_CHECK(cudaMalloc(&state->termination_reductions_d, - sizeof(termination_reductions_t))); - CUDA_CHECK(cudaMemset(state->termination_reductions_d, 0, - sizeof(termination_reductions_t))); + allocate_zeroed_device_buffer( + (void **)&state->termination_reductions_d, + sizeof(termination_reductions_t)); CUDA_CHECK(cudaMalloc(&state->metrics_d, sizeof(pdhg_metrics_t))); CUDA_CHECK(cudaMalloc(&state->hsd_inverse_scale_d, sizeof(double))); @@ -832,22 +727,6 @@ static void initialize_newton_workspaces(pdhg_solver_state_t *state) static void release_preprocessing_resources( pdhg_solver_state_t *state) { - if (state->spmv_power_iteration_normalized_vector) - { - CUSPARSE_CHECK(cusparseDestroyDnVec( - state->spmv_power_iteration_normalized_vector)); - state->spmv_power_iteration_normalized_vector = NULL; - } - CUDA_CHECK(cudaFree(state->power_iteration_normalized_vector_d)); - state->power_iteration_normalized_vector_d = NULL; - CUDA_CHECK(cudaFree( - state->power_iteration_vector_norm_scalar_d)); - state->power_iteration_vector_norm_scalar_d = NULL; - - CUDA_CHECK(cudaFreeHost(state->power_iter_sigma_squared_mp)); - state->power_iter_sigma_squared_mp = NULL; - CUDA_CHECK(cudaFreeHost(state->power_iter_residual_norm_mp)); - state->power_iter_residual_norm_mp = NULL; CUDA_CHECK(cudaFreeHost(state->bound_norm_workspace_mp)); state->bound_norm_workspace_mp = NULL; CUDA_CHECK(cudaFreeHost(state->objective_norm_workspace_mp)); @@ -906,6 +785,7 @@ void pdhg_solver_state_free(pdhg_solver_state_t *state) state->graphExec = NULL; } + active_set_free(&state->active_set); free_sparse_resources(state); free_solver_device_buffers(state); free_device_sparse_matrix(state->constraint_matrix); @@ -958,8 +838,6 @@ static void free_sparse_resources(pdhg_solver_state_t *state) CUSPARSE_CHECK(cusparseDestroyDnVec(state->spmv_primal_product)); if (state->spmv_dual_product) CUSPARSE_CHECK(cusparseDestroyDnVec(state->spmv_dual_product)); - if (state->spmv_power_iteration_normalized_vector) - CUSPARSE_CHECK(cusparseDestroyDnVec(state->spmv_power_iteration_normalized_vector)); } static void free_device_sparse_matrix(cu_sparse_matrix_csr_t *matrix) @@ -1024,9 +902,6 @@ static void free_solver_device_buffers(pdhg_solver_state_t *state) FREE_DEVICE_BUFFER(state->hsd_scalars_d); FREE_DEVICE_BUFFER(state->hsd_inverse_scale_d); - FREE_DEVICE_BUFFER(state->power_iteration_normalized_vector_d); - FREE_DEVICE_BUFFER(state->power_iteration_vector_norm_scalar_d); - FREE_DEVICE_BUFFER(state->primal_residual_d); FREE_DEVICE_BUFFER(state->dual_residual_d); FREE_DEVICE_BUFFER(state->primal_infeasibility_stationarity_residual_d); @@ -1058,8 +933,6 @@ static void free_solver_device_buffers(pdhg_solver_state_t *state) FREE_HOST_BUFFER(state->metrics_h); FREE_HOST_BUFFER(state->bound_norm_workspace_mp); FREE_HOST_BUFFER(state->objective_norm_workspace_mp); - FREE_HOST_BUFFER(state->power_iter_sigma_squared_mp); - FREE_HOST_BUFFER(state->power_iter_residual_norm_mp); FREE_HOST_BUFFER(state->control.fixed_point_error_mp); #undef FREE_HOST_BUFFER diff --git a/src/solver.cu b/src/solver.cu index ae7e4e5..13ef6a8 100644 --- a/src/solver.cu +++ b/src/solver.cu @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include "active_set_boost.h" #include "cone_projector.h" #include "cuhsdlp.h" #include "display.h" @@ -50,15 +51,26 @@ __global__ void compute_fixed_point_error_kernel( double step_size_dual, double step_size_homogeneous); __global__ void restart_hsd_scalars_kernel( - double *primal_scale_initial, double *primal_scale_current, double *primal_scale_pdhg, double *primal_scale_reflected, - double *dual_scale_initial, double *dual_scale_current, double *dual_scale_pdhg, double *dual_scale_reflected, + double *primal_scale_initial, + double *primal_scale_current, + double *primal_scale_pdhg, + double *primal_scale_reflected, + double *dual_scale_initial, + double *dual_scale_current, + double *dual_scale_pdhg, + double *dual_scale_reflected, double *constraint_cone_scale_d, double cone_warm_start_scale); __global__ void scale_hsd_scalars_kernel( double *primal_scale_pdhg, double *dual_scale_pdhg, double inv_scale); -static void compute_fixed_point_error(pdhg_solver_state_t *state); +static void compute_fixed_point_error( + pdhg_solver_state_t *state, + const step_size_parameters_t *step_size_params); +static void perform_active_set_boost_revert( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params); static void hsd_run_iteration_step( pdhg_solver_state_t *state, const pdhg_parameters_t *params, @@ -67,6 +79,9 @@ static void hsd_run_iteration_step( static void initialize_step_size_and_primal_weight( pdhg_solver_state_t *state, const pdhg_parameters_t *params); +static void compute_step_sizes( + pdhg_solver_state_t *state, + const step_size_parameters_t *step_size_params); static void hsd_update_dual_solution( pdhg_solver_state_t *state, const pdhg_parameters_t *params, @@ -85,7 +100,7 @@ static bool should_do_adaptive_restart( const pdhg_parameters_t *params); static void perform_restart( pdhg_solver_state_t *state, - const step_size_parameters_t *step_size_params); + const pdhg_parameters_t *params); static void update_primal_weight_via_restart_pid( pdhg_solver_state_t *state, const step_size_parameters_t *step_size_params); @@ -133,21 +148,22 @@ cuhsdlp_result_t *optimize( initialize_step_size_and_primal_weight(state, params); state->control.start_time = clock(); - bool do_restart = false; + bool restarted = false; - while (state->control.total_count < params->termination_criteria.iteration_limit) + while (state->control.termination_reason == TERMINATION_REASON_UNSPECIFIED && + state->control.total_count < params->termination_criteria.iteration_limit) { sync_inner_count_to_gpu(state); sync_step_sizes_to_gpu(state); hsd_run_iteration_step(state, params, 1, true); - if (do_restart) + if (restarted) { - compute_fixed_point_error(state); + compute_fixed_point_error(state, ¶ms->step_size_params); CUDA_CHECK(cudaStreamSynchronize(state->stream_A)); state->control.initial_fixed_point_error = *state->control.fixed_point_error_mp; - do_restart = false; + restarted = false; } if (!state->graph_created) @@ -170,29 +186,41 @@ cuhsdlp_result_t *optimize( } CUDA_CHECK(cudaGraphLaunch(state->graphExec, state->stream_A)); - compute_fixed_point_error(state); + compute_fixed_point_error(state, ¶ms->step_size_params); compute_termination_metrics(state, params); state->control.inner_count += params->termination_evaluation_frequency; state->control.total_count += params->termination_evaluation_frequency; check_termination_criteria(state, ¶ms->termination_criteria); - if (state->control.termination_reason != TERMINATION_REASON_UNSPECIFIED) + const bool terminated = + state->control.termination_reason != TERMINATION_REASON_UNSPECIFIED; + + if (!terminated) { - display_iteration_stats(state, params->verbose, false); - break; + update_active_set_masks(state, params); } - do_restart = should_do_adaptive_restart(state, params); - if (do_restart) + if (should_revert_active_set_boost(state, params)) { - state->control.restart_count++; - display_iteration_stats(state, params->verbose, true); - perform_restart(state, ¶ms->step_size_params); + perform_active_set_boost_revert(state, params); + restarted = true; + continue; } - else + + if (terminated) { display_iteration_stats(state, params->verbose, false); + break; } + + if (should_do_adaptive_restart(state, params)) + { + perform_restart(state, params); + restarted = true; + continue; + } + + display_iteration_stats(state, params->verbose, false); } if (state->control.termination_reason == TERMINATION_REASON_UNSPECIFIED) @@ -371,31 +399,19 @@ static void sync_step_sizes_to_gpu(pdhg_solver_state_t *state) state->stream_A)); } -static void compute_step_sizes( - pdhg_solver_state_t *state, - const step_size_parameters_t *step_size_params) -{ - double omega = state->control.primal_weight; - double lambda = state->control.step_size_base; - double factor = step_size_params->step_size_factor; - - state->control.step_size_primal = lambda / omega; - state->control.step_size_dual = omega * lambda; - state->control.step_size_homogeneous = 0.99 * factor / state->control.step_size_dual; -} - static void initialize_step_size_and_primal_weight( pdhg_solver_state_t *state, const pdhg_parameters_t *params) { - double sigma = state->rescaled_constraint_norm; - double factor = params->step_size_params.step_size_factor; + double constraint_norm = state->rescaled_constraint_norm; - if (sigma == 0.0) + if (constraint_norm == 0.0) { - sigma = 1.0; + constraint_norm = 1.0; } - state->control.step_size_base = factor / sigma; + state->control.step_size_base = + params->step_size_params.step_size_factor / constraint_norm; + state->control.step_size = state->control.step_size_base; if (params->scaling_params.bound_objective_rescaling) { @@ -412,6 +428,19 @@ static void initialize_step_size_and_primal_weight( compute_step_sizes(state, ¶ms->step_size_params); } +static void compute_step_sizes( + pdhg_solver_state_t *state, + const step_size_parameters_t *step_size_params) +{ + state->control.step_size_primal = + state->control.step_size / state->control.primal_weight; + state->control.step_size_dual = + state->control.primal_weight * state->control.step_size; + state->control.step_size_homogeneous = + 0.99 * step_size_params->step_size_factor / + state->control.step_size_dual; +} + static void hsd_run_iteration_step( pdhg_solver_state_t *state, const pdhg_parameters_t *params, @@ -449,6 +478,7 @@ static void hsd_update_dual_solution( state->current_dual_solution_d, state->reflected_dual_solution_d, state->pdhg_dual_solution_d, + state->active_set.constraint_dual_projection_input_d, state->initial_dual_solution_d, state->primal_product_d, state->control.step_size_dual_d, @@ -509,7 +539,9 @@ static void update_hsd_primal_block( state->stream_A); } -static void compute_fixed_point_error(pdhg_solver_state_t *state) +static void compute_fixed_point_error( + pdhg_solver_state_t *state, + const step_size_parameters_t *step_size_params) { compute_delta_kernel<<< state->num_blocks_primal_dual, @@ -559,6 +591,13 @@ static void compute_fixed_point_error(pdhg_solver_state_t *state) 1, state->fixed_point_operator_cross_d)); + const double metric_step_size_primal = + state->control.step_size_base / state->control.primal_weight; + const double metric_step_size_dual = + state->control.primal_weight * state->control.step_size_base; + const double metric_step_size_homogeneous = + 0.99 * step_size_params->step_size_factor / metric_step_size_dual; + compute_fixed_point_error_kernel<<<1, 1, 0, state->stream_A>>>( state->control.fixed_point_error_mp, state->pdhg_primal_norm_d, @@ -568,20 +607,41 @@ static void compute_fixed_point_error(pdhg_solver_state_t *state) state->hsd_primal_scale_reflected_d, state->hsd_dual_scale_pdhg_d, state->hsd_dual_scale_reflected_d, - state->control.step_size_primal, - state->control.step_size_dual, - state->control.step_size_homogeneous); + metric_step_size_primal, + metric_step_size_dual, + metric_step_size_homogeneous); CUDA_CHECK(cudaEventRecord(state->event_A, state->stream_A)); CUDA_CHECK(cudaStreamWaitEvent(state->stream_At, state->event_A, 0)); } +static void perform_active_set_boost_revert( + pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + state->control.restart_count++; + revert_active_set_boost(state, params); + state->control.inner_count = 0; + state->control.last_trial_fixed_point_error = INFINITY; + + compute_step_sizes(state, ¶ms->step_size_params); + compute_termination_metrics(state, params); + display_iteration_stats(state, params->verbose, true); + display_active_set_boost_revert(state, params); + state->control.termination_reason = TERMINATION_REASON_UNSPECIFIED; + check_termination_criteria(state, ¶ms->termination_criteria); +} + static bool should_do_adaptive_restart( pdhg_solver_state_t *state, const pdhg_parameters_t *params) { const restart_parameters_t *restart_params = ¶ms->restart_params; - int termination_evaluation_frequency = params->termination_evaluation_frequency; + const int termination_evaluation_frequency = + params->termination_evaluation_frequency; + const double fixed_point_error = *state->control.fixed_point_error_mp; + const double initial_fixed_point_error = + state->control.initial_fixed_point_error; bool do_restart = false; if (state->control.total_count == termination_evaluation_frequency) @@ -590,30 +650,40 @@ static bool should_do_adaptive_restart( } else if (state->control.total_count > termination_evaluation_frequency) { - if (*state->control.fixed_point_error_mp <= restart_params->sufficient_reduction_for_restart * state->control.initial_fixed_point_error) + if (fixed_point_error <= + restart_params->sufficient_reduction_for_restart * + initial_fixed_point_error) { do_restart = true; } - if (*state->control.fixed_point_error_mp <= restart_params->necessary_reduction_for_restart * state->control.initial_fixed_point_error) + else if ( + fixed_point_error <= + restart_params->necessary_reduction_for_restart * + initial_fixed_point_error && + fixed_point_error > state->control.last_trial_fixed_point_error) { - if (*state->control.fixed_point_error_mp > state->control.last_trial_fixed_point_error) - { - do_restart = true; - } + do_restart = true; } - if (state->control.inner_count >= restart_params->artificial_restart_threshold * state->control.total_count) + else if (state->control.inner_count >= + restart_params->artificial_restart_threshold * + state->control.total_count) { do_restart = true; } } - state->control.last_trial_fixed_point_error = *state->control.fixed_point_error_mp; + state->control.last_trial_fixed_point_error = fixed_point_error; return do_restart; } static void perform_restart( pdhg_solver_state_t *state, - const step_size_parameters_t *step_size_params) + const pdhg_parameters_t *params) { + const step_size_parameters_t *step_size_params = ¶ms->step_size_params; + + state->control.restart_count++; + display_iteration_stats(state, params->verbose, true); + compute_delta_kernel<<< state->num_blocks_primal_dual, THREADS_PER_BLOCK, @@ -643,8 +713,6 @@ static void perform_restart( update_primal_weight_via_restart_pid(state, step_size_params); - compute_step_sizes(state, step_size_params); - double cone_warm_start_scale = apply_gauge_control(state); CUDA_CHECK(cudaMemcpyAsync( @@ -674,13 +742,23 @@ static void perform_restart( state->metrics_h->current_primal_scale = state->metrics_h->pdhg_primal_scale; restart_hsd_scalars_kernel<<<1, 1, 0, state->stream_A>>>( - state->hsd_primal_scale_initial_d, state->hsd_primal_scale_current_d, state->hsd_primal_scale_pdhg_d, state->hsd_primal_scale_reflected_d, - state->hsd_dual_scale_initial_d, state->hsd_dual_scale_current_d, state->hsd_dual_scale_pdhg_d, state->hsd_dual_scale_reflected_d, + state->hsd_primal_scale_initial_d, + state->hsd_primal_scale_current_d, + state->hsd_primal_scale_pdhg_d, + state->hsd_primal_scale_reflected_d, + state->hsd_dual_scale_initial_d, + state->hsd_dual_scale_current_d, + state->hsd_dual_scale_pdhg_d, + state->hsd_dual_scale_reflected_d, state->dual_box_cone_state.scalar_d, cone_warm_start_scale); state->control.inner_count = 0; state->control.last_trial_fixed_point_error = INFINITY; + + try_raise_active_set_step_size(state, params); + + compute_step_sizes(state, step_size_params); } static void update_primal_weight_via_restart_pid( @@ -810,12 +888,20 @@ __global__ void compute_fixed_point_error_kernel( dual_movement + delta_primal_scale * delta_primal_scale / step_size_homogeneous; double interaction = -2.0 * (delta_primal_scale * delta_constraint_offset + cross_term); - fixed_point_error_d[0] = sqrt(fmax(0.0, movement + interaction)); + const double squared_error = movement + interaction; + fixed_point_error_d[0] = + isfinite(squared_error) ? sqrt(fmax(0.0, squared_error)) : INFINITY; } __global__ void restart_hsd_scalars_kernel( - double *primal_scale_initial, double *primal_scale_current, double *primal_scale_pdhg, double *primal_scale_reflected, - double *dual_scale_initial, double *dual_scale_current, double *dual_scale_pdhg, double *dual_scale_reflected, + double *primal_scale_initial, + double *primal_scale_current, + double *primal_scale_pdhg, + double *primal_scale_reflected, + double *dual_scale_initial, + double *dual_scale_current, + double *dual_scale_pdhg, + double *dual_scale_reflected, double *constraint_cone_scale_d, double cone_warm_start_scale) { diff --git a/src/utils/display.cu b/src/utils/display.cu index 18cf954..7f21efa 100644 --- a/src/utils/display.cu +++ b/src/utils/display.cu @@ -15,6 +15,7 @@ limitations under the License. */ #include "display.h" +#include "active_set_boost.h" #include "cuhsdlp.h" #include "spmv_backend.h" #include "termination_checker.h" @@ -156,6 +157,33 @@ void print_initial_info( PRINT_DIFF_DBL("cone_newton_tol", params->cone_params.tol, default_params.cone_params.tol); + PRINT_DIFF_BOOL("active_set_step_size_boost", + params->active_set_params.step_size_boost, + default_params.active_set_params.step_size_boost); + PRINT_DIFF_INT("active_set_window_iterations", + params->active_set_params.window_iterations, + default_params.active_set_params.window_iterations); + PRINT_DIFF_DBL("active_set_relative_tol", + params->active_set_params.relative_active_set_tol, + default_params.active_set_params.relative_active_set_tol); + PRINT_DIFF_DBL("active_set_safety_factor", + params->active_set_params.safety_factor, + default_params.active_set_params.safety_factor); + PRINT_DIFF_DBL("active_set_min_raise_ratio", + params->active_set_params.min_raise_ratio, + default_params.active_set_params.min_raise_ratio); + PRINT_DIFF_DBL("active_set_reestimate_change_ratio", + params->active_set_params.reestimate_change_ratio, + default_params.active_set_params.reestimate_change_ratio); + PRINT_DIFF_DBL("active_set_divergence_ceiling_ratio", + params->active_set_params.divergence_ceiling_ratio, + default_params.active_set_params.divergence_ceiling_ratio); + PRINT_DIFF_DBL("active_set_divergence_margin", + params->active_set_params.divergence_margin, + default_params.active_set_params.divergence_margin); + PRINT_DIFF_INT("active_set_max_reverts", + params->active_set_params.max_reverts, + default_params.active_set_params.max_reverts); } #undef PRINT_DIFF_INT @@ -243,6 +271,36 @@ void display_iteration_stats( } } +void display_active_set_boost_raise( + const pdhg_parameters_t *params, + double previous_step_size, + double updated_step_size, + double masked_constraint_norm) +{ + if (!params->verbose) + { + return; + } + printf(" active-set boost: step size %.3e -> %.3e (masked norm %.3e)\n", + previous_step_size, + updated_step_size, + masked_constraint_norm); +} + +void display_active_set_boost_revert( + const pdhg_solver_state_t *state, + const pdhg_parameters_t *params) +{ + if (!params->verbose) + { + return; + } + printf(" active-set boost: boosted epoch diverged, step size reverted " + "(%d of %d)\n", + state->active_set.revert_count, + params->active_set_params.max_reverts); +} + void pdhg_final_log( const cuhsdlp_result_t *result, const pdhg_parameters_t *params) @@ -267,6 +325,14 @@ void pdhg_final_log( printf(" Objective gap : %.3e\n", result->relative_objective_gap); printf(" Primal infeas : %.3e\n", result->relative_primal_residual); printf(" Dual infeas : %.3e\n", result->relative_dual_residual); + if (is_active_set_boost_enabled(params) && result->total_count > 0) + { + printf(" Active-set boost : %d raises, %d reverts, " + "%d power iterations\n", + result->active_set_raise_count, + result->active_set_revert_count, + result->active_set_power_iteration_count); + } if (result->termination_reason == TERMINATION_REASON_PRIMAL_INFEASIBLE || result->termination_reason == TERMINATION_REASON_DUAL_INFEASIBLE) { diff --git a/src/utils/matrix_formats.c b/src/utils/matrix_formats.c index af2b438..8b402e1 100644 --- a/src/utils/matrix_formats.c +++ b/src/utils/matrix_formats.c @@ -18,6 +18,7 @@ limitations under the License. #include "utils.h" #include #include +#include typedef struct { @@ -350,6 +351,52 @@ matrix_format_status_t canonicalize_csr(const matrix_desc_t *desc, int **row_ptr return MATRIX_FORMAT_OK; } +void transpose_csr_and_build_map( + const lp_problem_t *problem, + int **transpose_row_ptr, + int **transpose_col_ind, + double **transpose_val, + int **transpose_map) +{ + const int num_rows = problem->num_constraints; + const int num_cols = problem->num_variables; + const int nnz = problem->constraint_matrix_num_nonzeros; + const int *row_ptr = problem->constraint_matrix_row_pointers; + const int *col_ind = problem->constraint_matrix_col_indices; + const double *val = problem->constraint_matrix_values; + const size_t alloc_nnz = nnz > 0 ? (size_t)nnz : 1; + const size_t alloc_cols = num_cols > 0 ? (size_t)num_cols : 1; + + *transpose_row_ptr = (int *)safe_calloc((size_t)(num_cols + 1), sizeof(int)); + *transpose_col_ind = (int *)safe_malloc(alloc_nnz * sizeof(int)); + *transpose_val = (double *)safe_malloc(alloc_nnz * sizeof(double)); + *transpose_map = (int *)safe_malloc(alloc_nnz * sizeof(int)); + + for (int k = 0; k < nnz; ++k) + (*transpose_row_ptr)[col_ind[k] + 1]++; + + for (int j = 0; j < num_cols; ++j) + (*transpose_row_ptr)[j + 1] += (*transpose_row_ptr)[j]; + + int *next = (int *)safe_malloc(alloc_cols * sizeof(int)); + if (num_cols > 0) + memcpy(next, *transpose_row_ptr, (size_t)num_cols * sizeof(int)); + + for (int i = 0; i < num_rows; ++i) + { + for (int k = row_ptr[i]; k < row_ptr[i + 1]; ++k) + { + const int j = col_ind[k]; + const int pos = next[j]++; + (*transpose_col_ind)[pos] = i; + (*transpose_val)[pos] = val[k]; + (*transpose_map)[k] = pos; + } + } + + free(next); +} + static int compare_coo_entries(const void *lhs, const void *rhs) { const CooEntry *a = (const CooEntry *)lhs; diff --git a/src/utils/power_iteration.cu b/src/utils/power_iteration.cu index 3e7aada..bb901ba 100644 --- a/src/utils/power_iteration.cu +++ b/src/utils/power_iteration.cu @@ -19,140 +19,232 @@ limitations under the License. #include "utils.h" #include -__global__ void normalize_power_iteration_vector_kernel( +struct sv_estimator_ctx +{ + cusparseHandle_t sparse_handle; + cublasHandle_t blas_handle; + cudaStream_t stream; + curandGenerator_t curand_gen; + cusparseSpMatDescr_t matrix; + cusparseSpMatDescr_t transpose; + spmv_plan_t matrix_plan; + spmv_plan_t transpose_plan; + int num_rows; + int num_columns; + int num_blocks_row; + int num_blocks_column; + double *eigenvector_d; + double *next_eigenvector_d; + double *transpose_product_d; + double *vector_norm_d; + cusparseDnVecDescr_t spmv_eigenvector; + cusparseDnVecDescr_t spmv_next_eigenvector; + cusparseDnVecDescr_t spmv_transpose_product; + double *sigma_squared_mp; + double *residual_norm_mp; + bool has_warm_start; +}; + +static __global__ void apply_mask_kernel( + double *__restrict__ vector_d, + const bool *__restrict__ mask_d, + int n); +static __global__ void seed_zero_warm_start_entries_kernel( + double *__restrict__ vector_d, + const bool *__restrict__ mask_d, + double perturbation_amplitude, + int n); +static void run_power_iteration_step( + sv_estimator_ctx_t *ctx, + const sv_estimator_opts_t *opts, + bool evaluate_residual); +static __global__ void normalize_power_iteration_vector_kernel( double *normalized_vector_d, const double *vector_d, const double *vector_norm_d, - int n) -{ - int i = blockIdx.x * blockDim.x + threadIdx.x; - if (i >= n) - return; - - double vector_norm = *vector_norm_d; - normalized_vector_d[i] = (vector_norm > 0.0) ? (vector_d[i] / vector_norm) : 0.0; -} - -__global__ void compute_power_iteration_residual_kernel( + int n); +static __global__ void compute_power_iteration_residual_kernel( double *residual_d, const double *vector_d, const double *normalized_vector_d, const double *sigma_squared_d, - int n) -{ - int i = blockIdx.x * blockDim.x + threadIdx.x; - if (i >= n) - return; - - double sigma_squared = *sigma_squared_d; - residual_d[i] = vector_d[i] - sigma_squared * normalized_vector_d[i]; -} + int n); +static sv_estimator_status_t classify_power_iteration_outcome( + const sv_estimator_ctx_t *ctx, + const sv_estimator_opts_t *opts); -static void run_power_iteration_step( - pdhg_solver_state_t *state, - int m, - bool evaluate_residual) +sv_estimator_ctx_t *sv_estimator_create( + cusparseHandle_t sparse_handle, + cublasHandle_t blas_handle, + cudaStream_t stream, + curandGenerator_t curand_gen, + const cu_sparse_matrix_csr_t *matrix, + const cu_sparse_matrix_csr_t *transpose, + int num_rows, + int num_columns) { - double *power_iteration_vector_d = state->primal_product_d; - double *power_iteration_normalized_vector_d = state->power_iteration_normalized_vector_d; + sv_estimator_ctx_t *ctx = + (sv_estimator_ctx_t *)safe_calloc(1, sizeof(sv_estimator_ctx_t)); + ctx->sparse_handle = sparse_handle; + ctx->blas_handle = blas_handle; + ctx->stream = stream; + ctx->curand_gen = curand_gen; + ctx->num_rows = num_rows; + ctx->num_columns = num_columns; + ctx->num_blocks_row = + (num_rows + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK; + ctx->num_blocks_column = + (num_columns + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK; - CUBLAS_CHECK(cublasDnrm2_v2_64( - state->blas_handle_A, - m, - power_iteration_vector_d, - 1, - state->power_iteration_vector_norm_scalar_d)); + CUSPARSE_CHECK(cusparseCreateCsr( + &ctx->matrix, + num_rows, + num_columns, + matrix->num_nonzeros, + matrix->row_ptr, + matrix->col_ind, + matrix->val, + CUSPARSE_INDEX_32I, + CUSPARSE_INDEX_32I, + CUSPARSE_INDEX_BASE_ZERO, + CUDA_R_64F)); + CUSPARSE_CHECK(cusparseCreateCsr( + &ctx->transpose, + num_columns, + num_rows, + transpose->num_nonzeros, + transpose->row_ptr, + transpose->col_ind, + transpose->val, + CUSPARSE_INDEX_32I, + CUSPARSE_INDEX_32I, + CUSPARSE_INDEX_BASE_ZERO, + CUDA_R_64F)); - normalize_power_iteration_vector_kernel<<< - state->num_blocks_dual, - THREADS_PER_BLOCK, - 0, - state->stream_A>>>( - power_iteration_normalized_vector_d, - power_iteration_vector_d, - state->power_iteration_vector_norm_scalar_d, - m); - - execute_spmv( - state->sparse_handle_A, - state->spmv_power_iteration_normalized_vector, - state->spmv_dual_product, - &state->matAt_spmv_plan); + allocate_zeroed_device_buffer( + (void **)&ctx->eigenvector_d, + (((size_t)num_rows + 1) & ~(size_t)1) * sizeof(double)); + allocate_zeroed_device_buffer( + (void **)&ctx->next_eigenvector_d, + (size_t)num_rows * sizeof(double)); + allocate_zeroed_device_buffer( + (void **)&ctx->transpose_product_d, + (size_t)num_columns * sizeof(double)); + allocate_zeroed_device_buffer((void **)&ctx->vector_norm_d, sizeof(double)); + CUDA_CHECK(cudaHostAlloc(&ctx->sigma_squared_mp, sizeof(double), cudaHostAllocMapped)); + CUDA_CHECK(cudaHostAlloc(&ctx->residual_norm_mp, sizeof(double), cudaHostAllocMapped)); + CUSPARSE_CHECK(cusparseCreateDnVec( + &ctx->spmv_eigenvector, num_rows, ctx->eigenvector_d, CUDA_R_64F)); + CUSPARSE_CHECK(cusparseCreateDnVec( + &ctx->spmv_next_eigenvector, num_rows, ctx->next_eigenvector_d, CUDA_R_64F)); + CUSPARSE_CHECK(cusparseCreateDnVec( + &ctx->spmv_transpose_product, num_columns, ctx->transpose_product_d, CUDA_R_64F)); + create_spmv_plan( + sparse_handle, ctx->transpose, ctx->spmv_next_eigenvector, + ctx->spmv_transpose_product, &ctx->transpose_plan); + create_spmv_plan( + sparse_handle, ctx->matrix, ctx->spmv_transpose_product, + ctx->spmv_eigenvector, &ctx->matrix_plan); + return ctx; +} - execute_spmv( - state->sparse_handle_A, - state->spmv_dual_product, - state->spmv_primal_product, - &state->matA_spmv_plan); +sv_estimator_result_t sv_estimator_run( + sv_estimator_ctx_t *ctx, + const sv_estimator_opts_t *opts) +{ + sv_estimator_result_t result = {}; + result.status = SV_ESTIMATOR_DEGENERATE; - CUBLAS_CHECK(cublasDdot( - state->blas_handle_A, - m, - power_iteration_normalized_vector_d, - 1, - power_iteration_vector_d, - 1, - state->power_iter_sigma_squared_mp)); + if (ctx->num_rows == 0 || ctx->num_columns == 0) + { + return result; + } - if (evaluate_residual) + bool random_start = !ctx->has_warm_start; + if (!random_start && opts->row_mask_d) { - compute_power_iteration_residual_kernel<<< - state->num_blocks_dual, + apply_mask_kernel<<< + ctx->num_blocks_row, THREADS_PER_BLOCK, 0, - state->stream_A>>>( - power_iteration_normalized_vector_d, - power_iteration_vector_d, - power_iteration_normalized_vector_d, - state->power_iter_sigma_squared_mp, - m); + ctx->stream>>>(ctx->eigenvector_d, opts->row_mask_d, ctx->num_rows); CUBLAS_CHECK(cublasDnrm2_v2_64( - state->blas_handle_A, - m, - power_iteration_normalized_vector_d, + ctx->blas_handle, + ctx->num_rows, + ctx->eigenvector_d, 1, - state->power_iter_residual_norm_mp)); - } -} + ctx->vector_norm_d)); + double warm_start_norm = 0.0; + CUDA_CHECK(cudaMemcpyAsync( + &warm_start_norm, + ctx->vector_norm_d, + sizeof(double), + cudaMemcpyDeviceToHost, + ctx->stream)); + CUDA_CHECK(cudaStreamSynchronize(ctx->stream)); -double estimate_maximum_singular_value( - pdhg_solver_state_t *state, - const power_iteration_parameters_t *power_iter_params) -{ - const int window = 5; - int m = state->num_constraints; - - if (m == 0) - return 0.0; - - size_t curand_count = ((size_t)m + 1) & ~(size_t)1; - CURAND_CHECK(curandGenerateNormalDouble( - state->curand_gen, - state->primal_product_d, - curand_count, - 0.0, - 1.0)); + if (isfinite(warm_start_norm) && warm_start_norm > 0.0) + { + const double perturbation_amplitude = + 1e-3 * warm_start_norm / sqrt((double)ctx->num_rows); + seed_zero_warm_start_entries_kernel<<< + ctx->num_blocks_row, + THREADS_PER_BLOCK, + 0, + ctx->stream>>>( + ctx->eigenvector_d, + opts->row_mask_d, + perturbation_amplitude, + ctx->num_rows); + } + else + { + random_start = true; + } + } + if (random_start) + { + const size_t curand_sample_count = + ((size_t)ctx->num_rows + 1) & ~(size_t)1; + CURAND_CHECK(curandGenerateNormalDouble( + ctx->curand_gen, + ctx->eigenvector_d, + curand_sample_count, + 0.0, + 1.0)); + if (opts->row_mask_d) + { + apply_mask_kernel<<< + ctx->num_blocks_row, + THREADS_PER_BLOCK, + 0, + ctx->stream>>>( + ctx->eigenvector_d, + opts->row_mask_d, + ctx->num_rows); + } + } - *state->power_iter_sigma_squared_mp = 1.0; - *state->power_iter_residual_norm_mp = INFINITY; + result.status = SV_ESTIMATOR_MAX_ITER; - const int num_windows = power_iter_params->max_iter / window; - const int remainder = power_iter_params->max_iter % window; + const int evaluation_interval = 5; + const int num_full_batches = opts->max_iter / evaluation_interval; + const int remaining_iterations = opts->max_iter % evaluation_interval; - if (num_windows > 0) + if (num_full_batches > 0) { cudaGraph_t graph = NULL; cudaGraphExec_t graph_exec = NULL; CUDA_CHECK(cudaStreamBeginCapture( - state->stream_A, + ctx->stream, cudaStreamCaptureModeGlobal)); - for (int k = 0; k < window - 1; ++k) + for (int k = 0; k < evaluation_interval - 1; ++k) { - run_power_iteration_step(state, m, false); + run_power_iteration_step(ctx, opts, false); } - run_power_iteration_step(state, m, true); - CUDA_CHECK(cudaStreamEndCapture(state->stream_A, &graph)); + run_power_iteration_step(ctx, opts, true); + CUDA_CHECK(cudaStreamEndCapture(ctx->stream, &graph)); CUDA_CHECK(cudaGraphInstantiate( &graph_exec, graph, @@ -161,14 +253,15 @@ double estimate_maximum_singular_value( 0)); CUDA_CHECK(cudaGraphDestroy(graph)); - for (int j = 0; j < num_windows; ++j) + for (int batch = 0; batch < num_full_batches; ++batch) { CUDA_CHECK(cudaGraphLaunch( graph_exec, - state->stream_A)); - CUDA_CHECK(cudaStreamSynchronize(state->stream_A)); - if (*state->power_iter_residual_norm_mp < - power_iter_params->tol * fmin(1.0, *state->power_iter_sigma_squared_mp)) + ctx->stream)); + CUDA_CHECK(cudaStreamSynchronize(ctx->stream)); + result.iterations += evaluation_interval; + result.status = classify_power_iteration_outcome(ctx, opts); + if (result.status != SV_ESTIMATOR_MAX_ITER) { break; } @@ -177,17 +270,214 @@ double estimate_maximum_singular_value( CUDA_CHECK(cudaGraphExecDestroy(graph_exec)); } - if (remainder > 0 && - *state->power_iter_residual_norm_mp >= - power_iter_params->tol * fmin(1.0, *state->power_iter_sigma_squared_mp)) + if (result.status == SV_ESTIMATOR_MAX_ITER && remaining_iterations > 0) { - for (int k = 0; k < remainder - 1; ++k) + for (int k = 0; k < remaining_iterations - 1; ++k) { - run_power_iteration_step(state, m, false); + run_power_iteration_step(ctx, opts, false); } - run_power_iteration_step(state, m, true); - CUDA_CHECK(cudaStreamSynchronize(state->stream_A)); + run_power_iteration_step(ctx, opts, true); + CUDA_CHECK(cudaStreamSynchronize(ctx->stream)); + result.iterations += remaining_iterations; + result.status = classify_power_iteration_outcome(ctx, opts); } - return sqrt(*state->power_iter_sigma_squared_mp); + if (result.status != SV_ESTIMATOR_DEGENERATE) + { + result.max_singular_value = sqrt(*ctx->sigma_squared_mp); + } + ctx->has_warm_start = result.status != SV_ESTIMATOR_DEGENERATE; + return result; +} + +static __global__ void apply_mask_kernel( + double *__restrict__ vector_d, + const bool *__restrict__ mask_d, + int n) +{ + int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i >= n) + return; + if (!mask_d[i]) + vector_d[i] = 0.0; +} + +static __global__ void seed_zero_warm_start_entries_kernel( + double *__restrict__ vector_d, + const bool *__restrict__ mask_d, + double perturbation_amplitude, + int n) +{ + int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i >= n) + return; + if (!mask_d[i] || vector_d[i] != 0.0) + return; + + unsigned long long hash = + (unsigned long long)i + 0x9E3779B97F4A7C15ULL; + hash = (hash ^ (hash >> 30)) * 0xBF58476D1CE4E5B9ULL; + hash = (hash ^ (hash >> 27)) * 0x94D049BB133111EBULL; + hash = hash ^ (hash >> 31); + const double uniform_sample = + (double)(hash >> 11) * (1.0 / 9007199254740992.0); + vector_d[i] = perturbation_amplitude * (uniform_sample - 0.5); +} + +static void run_power_iteration_step( + sv_estimator_ctx_t *ctx, + const sv_estimator_opts_t *opts, + bool evaluate_residual) +{ + const int num_rows = ctx->num_rows; + const int num_columns = ctx->num_columns; + + CUBLAS_CHECK(cublasDnrm2_v2_64( + ctx->blas_handle, + num_rows, + ctx->eigenvector_d, + 1, + ctx->vector_norm_d)); + normalize_power_iteration_vector_kernel<<< + ctx->num_blocks_row, + THREADS_PER_BLOCK, + 0, + ctx->stream>>>( + ctx->next_eigenvector_d, + ctx->eigenvector_d, + ctx->vector_norm_d, + num_rows); + execute_spmv( + ctx->sparse_handle, + ctx->spmv_next_eigenvector, + ctx->spmv_transpose_product, + &ctx->transpose_plan); + if (opts->column_mask_d) + { + apply_mask_kernel<<< + ctx->num_blocks_column, + THREADS_PER_BLOCK, + 0, + ctx->stream>>>( + ctx->transpose_product_d, + opts->column_mask_d, + num_columns); + } + execute_spmv( + ctx->sparse_handle, + ctx->spmv_transpose_product, + ctx->spmv_eigenvector, + &ctx->matrix_plan); + if (opts->row_mask_d) + { + apply_mask_kernel<<< + ctx->num_blocks_row, + THREADS_PER_BLOCK, + 0, + ctx->stream>>>(ctx->eigenvector_d, opts->row_mask_d, num_rows); + } + CUBLAS_CHECK(cublasDdot( + ctx->blas_handle, + num_rows, + ctx->next_eigenvector_d, + 1, + ctx->eigenvector_d, + 1, + ctx->sigma_squared_mp)); + if (evaluate_residual) + { + compute_power_iteration_residual_kernel<<< + ctx->num_blocks_row, + THREADS_PER_BLOCK, + 0, + ctx->stream>>>( + ctx->next_eigenvector_d, + ctx->eigenvector_d, + ctx->next_eigenvector_d, + ctx->sigma_squared_mp, + num_rows); + CUBLAS_CHECK(cublasDnrm2_v2_64( + ctx->blas_handle, + num_rows, + ctx->next_eigenvector_d, + 1, + ctx->residual_norm_mp)); + } +} + +static __global__ void normalize_power_iteration_vector_kernel( + double *normalized_vector_d, + const double *vector_d, + const double *vector_norm_d, + int n) +{ + int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i >= n) + return; + + double vector_norm = *vector_norm_d; + normalized_vector_d[i] = (vector_norm > 0.0) ? (vector_d[i] / vector_norm) : 0.0; +} + +static __global__ void compute_power_iteration_residual_kernel( + double *residual_d, + const double *vector_d, + const double *normalized_vector_d, + const double *sigma_squared_d, + int n) +{ + int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i >= n) + return; + + double sigma_squared = *sigma_squared_d; + residual_d[i] = vector_d[i] - sigma_squared * normalized_vector_d[i]; +} + +static sv_estimator_status_t classify_power_iteration_outcome( + const sv_estimator_ctx_t *ctx, + const sv_estimator_opts_t *opts) +{ + const double sigma_squared = *ctx->sigma_squared_mp; + if (!(sigma_squared > 0.0) || !isfinite(sigma_squared)) + { + return SV_ESTIMATOR_DEGENERATE; + } + const double residual_norm = *ctx->residual_norm_mp; + if (!isfinite(residual_norm)) + { + return SV_ESTIMATOR_DEGENERATE; + } + if (opts->early_exit_singular_value_threshold > 0.0 && + sqrt(sigma_squared) >= opts->early_exit_singular_value_threshold) + { + return SV_ESTIMATOR_EARLY_EXIT; + } + if (residual_norm < opts->tol * fmin(1.0, sigma_squared)) + { + return SV_ESTIMATOR_CONVERGED; + } + return SV_ESTIMATOR_MAX_ITER; +} + +void sv_estimator_free(sv_estimator_ctx_t *ctx) +{ + if (!ctx) + { + return; + } + destroy_spmv_plan(&ctx->transpose_plan); + destroy_spmv_plan(&ctx->matrix_plan); + CUSPARSE_CHECK(cusparseDestroySpMat(ctx->transpose)); + CUSPARSE_CHECK(cusparseDestroySpMat(ctx->matrix)); + CUSPARSE_CHECK(cusparseDestroyDnVec(ctx->spmv_eigenvector)); + CUSPARSE_CHECK(cusparseDestroyDnVec(ctx->spmv_next_eigenvector)); + CUSPARSE_CHECK(cusparseDestroyDnVec(ctx->spmv_transpose_product)); + CUDA_CHECK(cudaFree(ctx->eigenvector_d)); + CUDA_CHECK(cudaFree(ctx->next_eigenvector_d)); + CUDA_CHECK(cudaFree(ctx->transpose_product_d)); + CUDA_CHECK(cudaFree(ctx->vector_norm_d)); + CUDA_CHECK(cudaFreeHost(ctx->sigma_squared_mp)); + CUDA_CHECK(cudaFreeHost(ctx->residual_norm_mp)); + free(ctx); } diff --git a/src/utils/utils.cu b/src/utils/utils.cu index 467429c..4309528 100644 --- a/src/utils/utils.cu +++ b/src/utils/utils.cu @@ -237,6 +237,72 @@ void *safe_realloc(void *ptr, size_t new_size) return tmp; } +void allocate_zeroed_device_buffer(void **dst, size_t bytes) +{ + const size_t allocation_bytes = bytes > 0 ? bytes : 1; + CUDA_CHECK(cudaMalloc(dst, allocation_bytes)); + CUDA_CHECK(cudaMemset(*dst, 0, allocation_bytes)); +} + +void allocate_and_copy_to_device( + void **dst, + const void *src, + size_t bytes) +{ + const size_t allocation_bytes = bytes > 0 ? bytes : 1; + CUDA_CHECK(cudaMalloc(dst, allocation_bytes)); + if (bytes > 0) + { + CUDA_CHECK(cudaMemcpy( + *dst, + src, + bytes, + cudaMemcpyHostToDevice)); + } +} + +void allocate_cub_reduce_workspace(pdhg_solver_state_t *state) +{ + const int max_reduction_length = + state->num_variables + state->num_constraints; + size_t sum_bytes = 0; + size_t max_bytes = 0; + size_t abs_max_bytes = 0; + double *null_values = NULL; + auto absolute_values = + thrust::make_transform_iterator( + null_values, + absolute_value_transform_t{}); + + CUDA_CHECK(cub::DeviceReduce::Sum( + NULL, + sum_bytes, + null_values, + null_values, + max_reduction_length)); + CUDA_CHECK(cub::DeviceReduce::Max( + NULL, + max_bytes, + null_values, + null_values, + max_reduction_length)); + CUDA_CHECK(cub::DeviceReduce::Max( + NULL, + abs_max_bytes, + absolute_values, + null_values, + max_reduction_length)); + + size_t workspace_bytes = + sum_bytes > max_bytes ? sum_bytes : max_bytes; + if (abs_max_bytes > workspace_bytes) + workspace_bytes = abs_max_bytes; + state->cub_reduce_workspace_bytes = workspace_bytes; + CUDA_CHECK(cudaMalloc( + &state->cub_reduce_workspace_d, + state->cub_reduce_workspace_bytes)); +} + void compute_lp_problem_norms( const lp_problem_t *problem, norm_type_t optimality_norm, From 649e1455ce4dde4fdb3374bf37ed6f36c1ea22a4 Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Sun, 13 Sep 2026 13:10:50 -0400 Subject: [PATCH 2/9] Fix: classify active-set columns from the primal projection input --- internal/cone_projector.h | 1 + internal/internal_types.h | 1 + src/active_set_boost.cu | 28 +++++++++++++++++++++------- src/cone_projector.cu | 11 +++++++++++ src/solver.cu | 1 + 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/internal/cone_projector.h b/internal/cone_projector.h index 89cc03a..be3269b 100644 --- a/internal/cone_projector.h +++ b/internal/cone_projector.h @@ -68,6 +68,7 @@ void update_primal_box_cone_resolvent( double *current_primal, double *reflected_primal, double *evaluation_primal, + double *variable_primal_projection_input, const double *initial_primal, const double *gradient, const double *objective_d, diff --git a/internal/internal_types.h b/internal/internal_types.h index c95813e..b181d8f 100644 --- a/internal/internal_types.h +++ b/internal/internal_types.h @@ -130,6 +130,7 @@ typedef struct { typedef struct { double *constraint_dual_projection_input_d; + double *variable_primal_projection_input_d; int *variable_last_free_iter_d; int *constraint_last_binding_iter_d; bool *free_variable_mask_d; diff --git a/src/active_set_boost.cu b/src/active_set_boost.cu index bd8dbb3..05d8a86 100644 --- a/src/active_set_boost.cu +++ b/src/active_set_boost.cu @@ -38,7 +38,7 @@ __global__ void update_binding_constraint_mask_kernel( int window_start_iter, int num_constraints); __global__ void update_free_variable_mask_kernel( - const double *__restrict__ pdhg_reduced_cost, + const double *__restrict__ variable_primal_projection_input, const double *__restrict__ objective_vector, const double *primal_scale_pdhg, const double *__restrict__ variable_lower_bound, @@ -47,6 +47,7 @@ __global__ void update_free_variable_mask_kernel( bool *__restrict__ free_variable_mask, active_set_mask_changes_t *mask_changes, double relative_active_set_tol, + double step_size_primal, int current_iter, int window_start_iter, int num_variables); @@ -75,6 +76,9 @@ void initialize_active_set( allocate_zeroed_device_buffer( (void **)&active_set->constraint_dual_projection_input_d, state->con_bytes); + allocate_zeroed_device_buffer( + (void **)&active_set->variable_primal_projection_input_d, + state->var_bytes); allocate_zeroed_device_buffer( (void **)&active_set->variable_last_free_iter_d, num_variables * sizeof(int)); @@ -114,6 +118,8 @@ void active_set_free(active_set_state_t *active_set) sv_estimator_free(active_set->sv_estimator); if (active_set->constraint_dual_projection_input_d) CUDA_CHECK(cudaFree(active_set->constraint_dual_projection_input_d)); + if (active_set->variable_primal_projection_input_d) + CUDA_CHECK(cudaFree(active_set->variable_primal_projection_input_d)); if (active_set->variable_last_free_iter_d) CUDA_CHECK(cudaFree(active_set->variable_last_free_iter_d)); if (active_set->constraint_last_binding_iter_d) @@ -176,7 +182,7 @@ void update_active_set_masks( THREADS_PER_BLOCK, 0, state->stream_A>>>( - state->pdhg_reduced_cost_d, + active_set->variable_primal_projection_input_d, state->objective_vector_d, state->hsd_primal_scale_pdhg_d, state->variable_lower_bound_d, @@ -185,6 +191,7 @@ void update_active_set_masks( active_set->free_variable_mask_d, active_set->mask_changes_d, params->active_set_params.relative_active_set_tol, + state->control.step_size_primal, current_iter, window_start_iter, state->num_variables); @@ -277,7 +284,7 @@ __global__ void update_binding_constraint_mask_kernel( } __global__ void update_free_variable_mask_kernel( - const double *__restrict__ pdhg_reduced_cost, + const double *__restrict__ variable_primal_projection_input, const double *__restrict__ objective_vector, const double *primal_scale_pdhg, const double *__restrict__ variable_lower_bound, @@ -286,6 +293,7 @@ __global__ void update_free_variable_mask_kernel( bool *__restrict__ free_variable_mask, active_set_mask_changes_t *mask_changes, double relative_active_set_tol, + double step_size_primal, int current_iter, int window_start_iter, int num_variables) @@ -298,16 +306,22 @@ __global__ void update_free_variable_mask_kernel( const double lower = variable_lower_bound[j]; const double upper = variable_upper_bound[j]; - const double reduced_cost_tolerance = + const double projection_residual_tolerance = relative_active_set_tol * fmax(1.0, fabs(objective_vector[j])) * (*primal_scale_pdhg); - const double reduced_cost = pdhg_reduced_cost[j]; + const double projection_input_margin = + step_size_primal * projection_residual_tolerance; + const double projection_input = variable_primal_projection_input[j]; const bool fixed_at_zero = lower == upper && lower == 0.0; const bool clamped_at_lower = - lower == 0.0 && reduced_cost > reduced_cost_tolerance; + isfinite(projection_input) && + lower == 0.0 && + projection_input < -projection_input_margin; const bool clamped_at_upper = - upper == 0.0 && reduced_cost < -reduced_cost_tolerance; + isfinite(projection_input) && + upper == 0.0 && + projection_input > projection_input_margin; const bool clamped_at_zero = fixed_at_zero || clamped_at_lower || clamped_at_upper; if (!clamped_at_zero) diff --git a/src/cone_projector.cu b/src/cone_projector.cu index 7f1f92a..58561a5 100644 --- a/src/cone_projector.cu +++ b/src/cone_projector.cu @@ -78,6 +78,7 @@ __global__ void update_primal_box_cone_resolvent_cooperative_kernel( double *__restrict__ current_primal, double *__restrict__ reflected_primal, double *__restrict__ evaluation_primal, + double *__restrict__ variable_primal_projection_input, const double *__restrict__ initial_primal, const double *__restrict__ gradient, const double *__restrict__ objective_d, @@ -99,6 +100,7 @@ __global__ void update_primal_box_cone_resolvent_single_block_kernel( double *__restrict__ current_primal, double *__restrict__ reflected_primal, double *__restrict__ evaluation_primal, + double *__restrict__ variable_primal_projection_input, const double *__restrict__ initial_primal, const double *__restrict__ gradient, const double *__restrict__ objective_d, @@ -265,6 +267,7 @@ void update_primal_box_cone_resolvent( double *current_primal, double *reflected_primal, double *evaluation_primal, + double *variable_primal_projection_input, const double *initial_primal, const double *gradient, const double *objective_d, @@ -290,6 +293,7 @@ void update_primal_box_cone_resolvent( current_primal, reflected_primal, evaluation_primal, + variable_primal_projection_input, initial_primal, gradient, objective_d, @@ -329,6 +333,7 @@ void update_primal_box_cone_resolvent( current_primal, reflected_primal, evaluation_primal, + variable_primal_projection_input, initial_primal, gradient, objective_d, @@ -675,6 +680,7 @@ __global__ void update_primal_box_cone_resolvent_cooperative_kernel( double *__restrict__ current_primal, double *__restrict__ reflected_primal, double *__restrict__ evaluation_primal, + double *__restrict__ variable_primal_projection_input, const double *__restrict__ initial_primal, const double *__restrict__ gradient, const double *__restrict__ objective_d, @@ -818,6 +824,8 @@ __global__ void update_primal_box_cone_resolvent_cooperative_kernel( { evaluation_primal[i] = projected; reflected_primal[i] = reflected; + if (variable_primal_projection_input) + variable_primal_projection_input[i] = shifted; } const double relaxed = reflection_coefficient * reflected + @@ -850,6 +858,7 @@ __global__ void update_primal_box_cone_resolvent_single_block_kernel( double *__restrict__ current_primal, double *__restrict__ reflected_primal, double *__restrict__ evaluation_primal, + double *__restrict__ variable_primal_projection_input, const double *__restrict__ initial_primal, const double *__restrict__ gradient, const double *__restrict__ objective_d, @@ -964,6 +973,8 @@ __global__ void update_primal_box_cone_resolvent_single_block_kernel( { evaluation_primal[i] = projected; reflected_primal[i] = reflected; + if (variable_primal_projection_input) + variable_primal_projection_input[i] = shifted; } const double relaxed = reflection_coefficient * reflected + diff --git a/src/solver.cu b/src/solver.cu index 13ef6a8..7d13797 100644 --- a/src/solver.cu +++ b/src/solver.cu @@ -522,6 +522,7 @@ static void update_hsd_primal_block( state->current_primal_solution_d, state->reflected_primal_solution_d, state->pdhg_primal_solution_d, + state->active_set.variable_primal_projection_input_d, state->initial_primal_solution_d, state->dual_product_d, state->objective_vector_d, From 34ec92fae8a1597a7f1cd40a4283671b936bd210 Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Sun, 13 Sep 2026 20:27:56 -0400 Subject: [PATCH 3/9] Refactor: clarify active-set predicates, step-size map, and estimator names --- include/cuhsdlp_types.h | 2 +- internal/active_set_boost.h | 2 +- src/active_set_boost.cu | 238 +++++++++++++++++++++-------------- src/cuhsdlp.c | 2 +- src/setup.cu | 8 +- src/solver.cu | 69 ++++++---- src/utils/display.cu | 4 +- src/utils/power_iteration.cu | 93 +++++++------- 8 files changed, 247 insertions(+), 171 deletions(-) diff --git a/include/cuhsdlp_types.h b/include/cuhsdlp_types.h index 3bf1de0..9b4382d 100644 --- a/include/cuhsdlp_types.h +++ b/include/cuhsdlp_types.h @@ -112,7 +112,7 @@ extern "C" { bool step_size_boost; int window_iterations; - double relative_active_set_tol; + double relative_tol; double safety_factor; double min_raise_ratio; double reestimate_change_ratio; diff --git a/internal/active_set_boost.h b/internal/active_set_boost.h index 7dcdbc7..9749470 100644 --- a/internal/active_set_boost.h +++ b/internal/active_set_boost.h @@ -49,7 +49,7 @@ extern "C" pdhg_solver_state_t *state, const pdhg_parameters_t *params); - void revert_active_set_boost( + void revert_active_set_step_size( pdhg_solver_state_t *state, const pdhg_parameters_t *params); diff --git a/src/active_set_boost.cu b/src/active_set_boost.cu index 05d8a86..1e5c4e9 100644 --- a/src/active_set_boost.cu +++ b/src/active_set_boost.cu @@ -22,7 +22,7 @@ limitations under the License. #include "utils.h" #include -static bool is_active_set_boost_available( +static bool has_active_set_revert_budget( const pdhg_solver_state_t *state, const pdhg_parameters_t *params); __global__ void update_binding_constraint_mask_kernel( @@ -33,7 +33,7 @@ __global__ void update_binding_constraint_mask_kernel( int *__restrict__ constraint_last_binding_iter, bool *__restrict__ binding_constraint_mask, active_set_mask_changes_t *mask_changes, - double relative_active_set_tol, + double relative_tol, int current_iter, int window_start_iter, int num_constraints); @@ -46,15 +46,29 @@ __global__ void update_free_variable_mask_kernel( int *__restrict__ variable_last_free_iter, bool *__restrict__ free_variable_mask, active_set_mask_changes_t *mask_changes, - double relative_active_set_tol, + double relative_tol, double step_size_primal, int current_iter, int window_start_iter, int num_variables); +static __device__ __forceinline__ bool is_safely_between_bounds( + double dual_projection_input, + double lower, + double upper, + double relative_tol); +static __device__ __forceinline__ void update_window_mask_entry( + bool observed_in_mask, + int current_iter, + int window_start_iter, + int *last_observed_iter, + bool *mask, + int *entries_added, + int *entries_removed); static bool is_raise_possible( double target_step_size, double current_step_size, double min_raise_ratio); +static void mark_raise_checked(active_set_state_t *active_set); static sv_estimator_result_t estimate_masked_constraint_norm( pdhg_solver_state_t *state, const pdhg_parameters_t *params); @@ -91,8 +105,6 @@ void initialize_active_set( allocate_zeroed_device_buffer( (void **)&active_set->binding_constraint_mask_d, num_constraints * sizeof(bool)); - CUDA_CHECK(cudaMemset(active_set->free_variable_mask_d, 1, num_variables * sizeof(bool))); - CUDA_CHECK(cudaMemset(active_set->binding_constraint_mask_d, 1, num_constraints * sizeof(bool))); allocate_zeroed_device_buffer( (void **)&active_set->mask_changes_d, sizeof(active_set_mask_changes_t)); @@ -108,6 +120,9 @@ void initialize_active_set( allocate_zeroed_device_buffer( (void **)&active_set->anchor.constraint_cone_scale_d, sizeof(double)); + + CUDA_CHECK(cudaMemset(active_set->free_variable_mask_d, 1, num_variables * sizeof(bool))); + CUDA_CHECK(cudaMemset(active_set->binding_constraint_mask_d, 1, num_constraints * sizeof(bool))); active_set->free_variable_count = state->num_variables; active_set->binding_constraint_count = state->num_constraints; active_set->step_size_ceiling = INFINITY; @@ -146,7 +161,7 @@ void update_active_set_masks( pdhg_solver_state_t *state, const pdhg_parameters_t *params) { - if (!is_active_set_boost_available(state, params)) + if (!has_active_set_revert_budget(state, params)) { return; } @@ -173,7 +188,7 @@ void update_active_set_masks( active_set->constraint_last_binding_iter_d, active_set->binding_constraint_mask_d, active_set->mask_changes_d, - params->active_set_params.relative_active_set_tol, + params->active_set_params.relative_tol, current_iter, window_start_iter, state->num_constraints); @@ -190,7 +205,7 @@ void update_active_set_masks( active_set->variable_last_free_iter_d, active_set->free_variable_mask_d, active_set->mask_changes_d, - params->active_set_params.relative_active_set_tol, + params->active_set_params.relative_tol, state->control.step_size_primal, current_iter, window_start_iter, @@ -213,7 +228,7 @@ void update_active_set_masks( mask_changes.constraints_added + mask_changes.constraints_removed; } -static bool is_active_set_boost_available( +static bool has_active_set_revert_budget( const pdhg_solver_state_t *state, const pdhg_parameters_t *params) { @@ -229,7 +244,7 @@ __global__ void update_binding_constraint_mask_kernel( int *__restrict__ constraint_last_binding_iter, bool *__restrict__ binding_constraint_mask, active_set_mask_changes_t *mask_changes, - double relative_active_set_tol, + double relative_tol, int current_iter, int window_start_iter, int num_constraints) @@ -243,44 +258,22 @@ __global__ void update_binding_constraint_mask_kernel( const double lower = constraint_lower_bound[i]; const double upper = constraint_upper_bound[i]; const double cone_scale = *constraint_cone_scale; - bool binding = lower == upper || !(cone_scale > 0.0); - if (!binding) - { - const double dual_projection_input = - constraint_dual_projection_input[i] / cone_scale; - bool safely_inside = true; - if (isfinite(upper)) - { - const double margin = - relative_active_set_tol * - fmax(1.0, fmax(fabs(dual_projection_input), fabs(upper))); - safely_inside = - safely_inside && (upper + dual_projection_input > margin); - } - if (isfinite(lower)) - { - const double margin = - relative_active_set_tol * - fmax(1.0, fmax(fabs(dual_projection_input), fabs(lower))); - safely_inside = - safely_inside && (-lower - dual_projection_input > margin); - } - binding = !safely_inside; - } - if (binding) - { - constraint_last_binding_iter[i] = current_iter; - } - const bool retained = - constraint_last_binding_iter[i] >= window_start_iter; - if (retained != binding_constraint_mask[i]) - { - binding_constraint_mask[i] = retained; - atomicAdd( - retained ? &mask_changes->constraints_added - : &mask_changes->constraints_removed, - 1); - } + const bool is_binding = + lower == upper || + !(cone_scale > 0.0) || + !is_safely_between_bounds( + constraint_dual_projection_input[i] / cone_scale, + lower, + upper, + relative_tol); + update_window_mask_entry( + is_binding, + current_iter, + window_start_iter, + &constraint_last_binding_iter[i], + &binding_constraint_mask[i], + &mask_changes->constraints_added, + &mask_changes->constraints_removed); } __global__ void update_free_variable_mask_kernel( @@ -292,7 +285,7 @@ __global__ void update_free_variable_mask_kernel( int *__restrict__ variable_last_free_iter, bool *__restrict__ free_variable_mask, active_set_mask_changes_t *mask_changes, - double relative_active_set_tol, + double relative_tol, double step_size_primal, int current_iter, int window_start_iter, @@ -307,36 +300,71 @@ __global__ void update_free_variable_mask_kernel( const double lower = variable_lower_bound[j]; const double upper = variable_upper_bound[j]; const double projection_residual_tolerance = - relative_active_set_tol * + relative_tol * fmax(1.0, fabs(objective_vector[j])) * (*primal_scale_pdhg); const double projection_input_margin = step_size_primal * projection_residual_tolerance; const double projection_input = variable_primal_projection_input[j]; - const bool fixed_at_zero = lower == upper && lower == 0.0; + const bool input_is_finite = isfinite(projection_input); + const bool fixed_at_zero = lower == 0.0 && upper == 0.0; const bool clamped_at_lower = - isfinite(projection_input) && lower == 0.0 && + input_is_finite && projection_input < -projection_input_margin; const bool clamped_at_upper = - isfinite(projection_input) && upper == 0.0 && + input_is_finite && projection_input > projection_input_margin; - const bool clamped_at_zero = - fixed_at_zero || clamped_at_lower || clamped_at_upper; - if (!clamped_at_zero) + const bool is_free = + !(fixed_at_zero || clamped_at_lower || clamped_at_upper); + update_window_mask_entry( + is_free, + current_iter, + window_start_iter, + &variable_last_free_iter[j], + &free_variable_mask[j], + &mask_changes->variables_added, + &mask_changes->variables_removed); +} + +static __device__ __forceinline__ bool is_safely_between_bounds( + double dual_projection_input, + double lower, + double upper, + double relative_tol) +{ + const bool below_upper = + !isfinite(upper) || + upper + dual_projection_input > + relative_tol * + fmax(1.0, fmax(fabs(dual_projection_input), fabs(upper))); + const bool above_lower = + !isfinite(lower) || + -lower - dual_projection_input > + relative_tol * + fmax(1.0, fmax(fabs(dual_projection_input), fabs(lower))); + return below_upper && above_lower; +} + +static __device__ __forceinline__ void update_window_mask_entry( + bool observed_in_mask, + int current_iter, + int window_start_iter, + int *last_observed_iter, + bool *mask, + int *entries_added, + int *entries_removed) +{ + if (observed_in_mask) { - variable_last_free_iter[j] = current_iter; + *last_observed_iter = current_iter; } - const bool retained = - variable_last_free_iter[j] >= window_start_iter; - if (retained != free_variable_mask[j]) + const bool retained = *last_observed_iter >= window_start_iter; + if (retained != *mask) { - free_variable_mask[j] = retained; - atomicAdd( - retained ? &mask_changes->variables_added - : &mask_changes->variables_removed, - 1); + *mask = retained; + atomicAdd(retained ? entries_added : entries_removed, 1); } } @@ -344,7 +372,9 @@ bool should_revert_active_set_boost( const pdhg_solver_state_t *state, const pdhg_parameters_t *params) { - if (state->control.step_size <= state->control.step_size_base) + const bool is_boosted = + state->control.step_size > state->control.step_size_base; + if (!is_boosted) { return false; } @@ -353,17 +383,21 @@ bool should_revert_active_set_boost( { return true; } - if (reason != TERMINATION_REASON_UNSPECIFIED && - reason != TERMINATION_REASON_TIME_LIMIT && - reason != TERMINATION_REASON_ITERATION_LIMIT) + const bool is_uncertified = + reason == TERMINATION_REASON_UNSPECIFIED || + reason == TERMINATION_REASON_TIME_LIMIT || + reason == TERMINATION_REASON_ITERATION_LIMIT; + if (!is_uncertified) { return false; } const double fixed_point_error = *state->control.fixed_point_error_mp; - if (!isfinite(state->metrics_h->relative_primal_residual) || - !isfinite(state->metrics_h->relative_dual_residual) || - !isfinite(state->metrics_h->relative_objective_gap) || - !isfinite(fixed_point_error)) + const bool metrics_are_finite = + isfinite(state->metrics_h->relative_primal_residual) && + isfinite(state->metrics_h->relative_dual_residual) && + isfinite(state->metrics_h->relative_objective_gap) && + isfinite(fixed_point_error); + if (!metrics_are_finite) { return true; } @@ -378,15 +412,24 @@ void try_raise_active_set_step_size( pdhg_solver_state_t *state, const pdhg_parameters_t *params) { - if (!is_active_set_boost_available(state, params)) + if (!has_active_set_revert_budget(state, params)) { return; } active_set_state_t *active_set = &state->active_set; - if (active_set->free_variable_count == state->num_variables && + const bool a_mask_is_empty = + active_set->free_variable_count == 0 || + active_set->binding_constraint_count == 0; + if (a_mask_is_empty) + { + return; + } + const bool masks_are_full_and_unchanged = + active_set->free_variable_count == state->num_variables && active_set->binding_constraint_count == state->num_constraints && - active_set->mask_changes_since_estimate == 0) + active_set->mask_changes_since_estimate == 0; + if (masks_are_full_and_unchanged) { return; } @@ -402,38 +445,40 @@ void try_raise_active_set_step_size( } const double step_size_base = state->control.step_size_base; - const double previous_step_size = state->control.step_size; + const double current_step_size = state->control.step_size; const double min_raise_ratio = params->active_set_params.min_raise_ratio; + const double largest_allowed_step_size = + fmax(step_size_base, active_set->step_size_ceiling); if (!is_raise_possible( - fmax(step_size_base, active_set->step_size_ceiling), - previous_step_size, + largest_allowed_step_size, + current_step_size, min_raise_ratio)) { - active_set->raise_checked = true; - active_set->mask_changes_since_estimate = 0; + mark_raise_checked(active_set); return; } const sv_estimator_result_t estimate = estimate_masked_constraint_norm(state, params); - if (estimate.status != SV_ESTIMATOR_EARLY_EXIT && - estimate.status != SV_ESTIMATOR_CONVERGED) + const bool estimate_is_trusted = + estimate.status == SV_ESTIMATOR_CONVERGED || + estimate.status == SV_ESTIMATOR_EARLY_EXIT; + if (!estimate_is_trusted) { return; } - active_set->raise_checked = true; - active_set->mask_changes_since_estimate = 0; + mark_raise_checked(active_set); if (estimate.status == SV_ESTIMATOR_EARLY_EXIT) { return; } const double target_step_size = - fmax(step_size_base, - fmin(params->active_set_params.safety_factor / - estimate.max_singular_value, - active_set->step_size_ceiling)); - if (!is_raise_possible(target_step_size, previous_step_size, min_raise_ratio)) + fmin(fmax(step_size_base, + params->active_set_params.safety_factor / + estimate.max_singular_value), + largest_allowed_step_size); + if (!is_raise_possible(target_step_size, current_step_size, min_raise_ratio)) { return; } @@ -441,7 +486,7 @@ void try_raise_active_set_step_size( active_set->raise_count++; save_active_set_anchor(state); display_active_set_boost_raise( - params, previous_step_size, target_step_size, + params, current_step_size, target_step_size, estimate.max_singular_value); } @@ -454,6 +499,12 @@ static bool is_raise_possible( target_step_size >= min_raise_ratio * current_step_size; } +static void mark_raise_checked(active_set_state_t *active_set) +{ + active_set->raise_checked = true; + active_set->mask_changes_since_estimate = 0; +} + static sv_estimator_result_t estimate_masked_constraint_norm( pdhg_solver_state_t *state, const pdhg_parameters_t *params) @@ -518,13 +569,14 @@ static void save_active_set_anchor(pdhg_solver_state_t *state) active_set->anchor.pid = state->control.pid; } -void revert_active_set_boost( +void revert_active_set_step_size( pdhg_solver_state_t *state, const pdhg_parameters_t *params) { active_set_state_t *active_set = &state->active_set; + const double failed_step_size = state->control.step_size; active_set->step_size_ceiling = - params->active_set_params.divergence_ceiling_ratio * state->control.step_size; + params->active_set_params.divergence_ceiling_ratio * failed_step_size; state->control.step_size = state->control.step_size_base; restore_active_set_anchor(state); active_set->raise_checked = false; diff --git a/src/cuhsdlp.c b/src/cuhsdlp.c index f0733d8..075d6dc 100644 --- a/src/cuhsdlp.c +++ b/src/cuhsdlp.c @@ -212,7 +212,7 @@ void set_default_parameters(pdhg_parameters_t *params) params->active_set_params.step_size_boost = true; params->active_set_params.window_iterations = 10000; - params->active_set_params.relative_active_set_tol = 1e-8; + params->active_set_params.relative_tol = 1e-8; params->active_set_params.safety_factor = 0.9; params->active_set_params.min_raise_ratio = 1.1; params->active_set_params.reestimate_change_ratio = 0.01; diff --git a/src/setup.cu b/src/setup.cu index e30a466..afb5a02 100644 --- a/src/setup.cu +++ b/src/setup.cu @@ -165,11 +165,11 @@ void check_params_validity(const pdhg_parameters_t *params) param_error("active_set window_iterations must be >= 1", params->active_set_params.window_iterations); } - if (!(params->active_set_params.relative_active_set_tol >= 0.0 && - isfinite(params->active_set_params.relative_active_set_tol))) + if (!(params->active_set_params.relative_tol >= 0.0 && + isfinite(params->active_set_params.relative_tol))) { - param_error("active_set relative_active_set_tol must be finite and >= 0", - params->active_set_params.relative_active_set_tol); + param_error("active_set relative_tol must be finite and >= 0", + params->active_set_params.relative_tol); } if (!(params->active_set_params.safety_factor > 0.0 && params->active_set_params.safety_factor < 1.0)) diff --git a/src/solver.cu b/src/solver.cu index 7d13797..628077b 100644 --- a/src/solver.cu +++ b/src/solver.cu @@ -80,8 +80,12 @@ static void initialize_step_size_and_primal_weight( pdhg_solver_state_t *state, const pdhg_parameters_t *params); static void compute_step_sizes( - pdhg_solver_state_t *state, - const step_size_parameters_t *step_size_params); + double step_size, + double primal_weight, + const step_size_parameters_t *step_size_params, + double *step_size_primal, + double *step_size_dual, + double *step_size_homogeneous); static void hsd_update_dual_solution( pdhg_solver_state_t *state, const pdhg_parameters_t *params, @@ -425,20 +429,26 @@ static void initialize_step_size_and_primal_weight( initialize_pid_state(&state->control.pid); - compute_step_sizes(state, ¶ms->step_size_params); + compute_step_sizes( + state->control.step_size, + state->control.primal_weight, + ¶ms->step_size_params, + &state->control.step_size_primal, + &state->control.step_size_dual, + &state->control.step_size_homogeneous); } static void compute_step_sizes( - pdhg_solver_state_t *state, - const step_size_parameters_t *step_size_params) + double step_size, + double primal_weight, + const step_size_parameters_t *step_size_params, + double *step_size_primal, + double *step_size_dual, + double *step_size_homogeneous) { - state->control.step_size_primal = - state->control.step_size / state->control.primal_weight; - state->control.step_size_dual = - state->control.primal_weight * state->control.step_size; - state->control.step_size_homogeneous = - 0.99 * step_size_params->step_size_factor / - state->control.step_size_dual; + *step_size_primal = step_size / primal_weight; + *step_size_dual = primal_weight * step_size; + *step_size_homogeneous = 0.99 * step_size_params->step_size_factor / *step_size_dual; } static void hsd_run_iteration_step( @@ -592,12 +602,16 @@ static void compute_fixed_point_error( 1, state->fixed_point_operator_cross_d)); - const double metric_step_size_primal = - state->control.step_size_base / state->control.primal_weight; - const double metric_step_size_dual = - state->control.primal_weight * state->control.step_size_base; - const double metric_step_size_homogeneous = - 0.99 * step_size_params->step_size_factor / metric_step_size_dual; + double metric_step_size_primal = 0.0; + double metric_step_size_dual = 0.0; + double metric_step_size_homogeneous = 0.0; + compute_step_sizes( + state->control.step_size_base, + state->control.primal_weight, + step_size_params, + &metric_step_size_primal, + &metric_step_size_dual, + &metric_step_size_homogeneous); compute_fixed_point_error_kernel<<<1, 1, 0, state->stream_A>>>( state->control.fixed_point_error_mp, @@ -620,12 +634,19 @@ static void perform_active_set_boost_revert( pdhg_solver_state_t *state, const pdhg_parameters_t *params) { + revert_active_set_step_size(state, params); + state->control.restart_count++; - revert_active_set_boost(state, params); state->control.inner_count = 0; state->control.last_trial_fixed_point_error = INFINITY; - compute_step_sizes(state, ¶ms->step_size_params); + compute_step_sizes( + state->control.step_size, + state->control.primal_weight, + ¶ms->step_size_params, + &state->control.step_size_primal, + &state->control.step_size_dual, + &state->control.step_size_homogeneous); compute_termination_metrics(state, params); display_iteration_stats(state, params->verbose, true); display_active_set_boost_revert(state, params); @@ -759,7 +780,13 @@ static void perform_restart( try_raise_active_set_step_size(state, params); - compute_step_sizes(state, step_size_params); + compute_step_sizes( + state->control.step_size, + state->control.primal_weight, + step_size_params, + &state->control.step_size_primal, + &state->control.step_size_dual, + &state->control.step_size_homogeneous); } static void update_primal_weight_via_restart_pid( diff --git a/src/utils/display.cu b/src/utils/display.cu index 7f21efa..b512de5 100644 --- a/src/utils/display.cu +++ b/src/utils/display.cu @@ -164,8 +164,8 @@ void print_initial_info( params->active_set_params.window_iterations, default_params.active_set_params.window_iterations); PRINT_DIFF_DBL("active_set_relative_tol", - params->active_set_params.relative_active_set_tol, - default_params.active_set_params.relative_active_set_tol); + params->active_set_params.relative_tol, + default_params.active_set_params.relative_tol); PRINT_DIFF_DBL("active_set_safety_factor", params->active_set_params.safety_factor, default_params.active_set_params.safety_factor); diff --git a/src/utils/power_iteration.cu b/src/utils/power_iteration.cu index bb901ba..981bf98 100644 --- a/src/utils/power_iteration.cu +++ b/src/utils/power_iteration.cu @@ -33,12 +33,12 @@ struct sv_estimator_ctx int num_columns; int num_blocks_row; int num_blocks_column; - double *eigenvector_d; - double *next_eigenvector_d; + double *iterate_d; + double *normalized_iterate_d; double *transpose_product_d; - double *vector_norm_d; - cusparseDnVecDescr_t spmv_eigenvector; - cusparseDnVecDescr_t spmv_next_eigenvector; + double *iterate_norm_d; + cusparseDnVecDescr_t spmv_iterate; + cusparseDnVecDescr_t spmv_normalized_iterate; cusparseDnVecDescr_t spmv_transpose_product; double *sigma_squared_mp; double *residual_norm_mp; @@ -63,10 +63,9 @@ static __global__ void normalize_power_iteration_vector_kernel( const double *vector_d, const double *vector_norm_d, int n); -static __global__ void compute_power_iteration_residual_kernel( - double *residual_d, - const double *vector_d, - const double *normalized_vector_d, +static __global__ void replace_normalized_iterate_with_residual_kernel( + double *normalized_iterate_d, + const double *iterate_d, const double *sigma_squared_d, int n); static sv_estimator_status_t classify_power_iteration_outcome( @@ -122,29 +121,29 @@ sv_estimator_ctx_t *sv_estimator_create( CUDA_R_64F)); allocate_zeroed_device_buffer( - (void **)&ctx->eigenvector_d, + (void **)&ctx->iterate_d, (((size_t)num_rows + 1) & ~(size_t)1) * sizeof(double)); allocate_zeroed_device_buffer( - (void **)&ctx->next_eigenvector_d, + (void **)&ctx->normalized_iterate_d, (size_t)num_rows * sizeof(double)); allocate_zeroed_device_buffer( (void **)&ctx->transpose_product_d, (size_t)num_columns * sizeof(double)); - allocate_zeroed_device_buffer((void **)&ctx->vector_norm_d, sizeof(double)); + allocate_zeroed_device_buffer((void **)&ctx->iterate_norm_d, sizeof(double)); CUDA_CHECK(cudaHostAlloc(&ctx->sigma_squared_mp, sizeof(double), cudaHostAllocMapped)); CUDA_CHECK(cudaHostAlloc(&ctx->residual_norm_mp, sizeof(double), cudaHostAllocMapped)); CUSPARSE_CHECK(cusparseCreateDnVec( - &ctx->spmv_eigenvector, num_rows, ctx->eigenvector_d, CUDA_R_64F)); + &ctx->spmv_iterate, num_rows, ctx->iterate_d, CUDA_R_64F)); CUSPARSE_CHECK(cusparseCreateDnVec( - &ctx->spmv_next_eigenvector, num_rows, ctx->next_eigenvector_d, CUDA_R_64F)); + &ctx->spmv_normalized_iterate, num_rows, ctx->normalized_iterate_d, CUDA_R_64F)); CUSPARSE_CHECK(cusparseCreateDnVec( &ctx->spmv_transpose_product, num_columns, ctx->transpose_product_d, CUDA_R_64F)); create_spmv_plan( - sparse_handle, ctx->transpose, ctx->spmv_next_eigenvector, + sparse_handle, ctx->transpose, ctx->spmv_normalized_iterate, ctx->spmv_transpose_product, &ctx->transpose_plan); create_spmv_plan( sparse_handle, ctx->matrix, ctx->spmv_transpose_product, - ctx->spmv_eigenvector, &ctx->matrix_plan); + ctx->spmv_iterate, &ctx->matrix_plan); return ctx; } @@ -167,18 +166,18 @@ sv_estimator_result_t sv_estimator_run( ctx->num_blocks_row, THREADS_PER_BLOCK, 0, - ctx->stream>>>(ctx->eigenvector_d, opts->row_mask_d, ctx->num_rows); + ctx->stream>>>(ctx->iterate_d, opts->row_mask_d, ctx->num_rows); CUBLAS_CHECK(cublasDnrm2_v2_64( ctx->blas_handle, ctx->num_rows, - ctx->eigenvector_d, + ctx->iterate_d, 1, - ctx->vector_norm_d)); + ctx->iterate_norm_d)); double warm_start_norm = 0.0; CUDA_CHECK(cudaMemcpyAsync( &warm_start_norm, - ctx->vector_norm_d, + ctx->iterate_norm_d, sizeof(double), cudaMemcpyDeviceToHost, ctx->stream)); @@ -193,7 +192,7 @@ sv_estimator_result_t sv_estimator_run( THREADS_PER_BLOCK, 0, ctx->stream>>>( - ctx->eigenvector_d, + ctx->iterate_d, opts->row_mask_d, perturbation_amplitude, ctx->num_rows); @@ -209,7 +208,7 @@ sv_estimator_result_t sv_estimator_run( ((size_t)ctx->num_rows + 1) & ~(size_t)1; CURAND_CHECK(curandGenerateNormalDouble( ctx->curand_gen, - ctx->eigenvector_d, + ctx->iterate_d, curand_sample_count, 0.0, 1.0)); @@ -220,7 +219,7 @@ sv_estimator_result_t sv_estimator_run( THREADS_PER_BLOCK, 0, ctx->stream>>>( - ctx->eigenvector_d, + ctx->iterate_d, opts->row_mask_d, ctx->num_rows); } @@ -335,21 +334,21 @@ static void run_power_iteration_step( CUBLAS_CHECK(cublasDnrm2_v2_64( ctx->blas_handle, num_rows, - ctx->eigenvector_d, + ctx->iterate_d, 1, - ctx->vector_norm_d)); + ctx->iterate_norm_d)); normalize_power_iteration_vector_kernel<<< ctx->num_blocks_row, THREADS_PER_BLOCK, 0, ctx->stream>>>( - ctx->next_eigenvector_d, - ctx->eigenvector_d, - ctx->vector_norm_d, + ctx->normalized_iterate_d, + ctx->iterate_d, + ctx->iterate_norm_d, num_rows); execute_spmv( ctx->sparse_handle, - ctx->spmv_next_eigenvector, + ctx->spmv_normalized_iterate, ctx->spmv_transpose_product, &ctx->transpose_plan); if (opts->column_mask_d) @@ -366,7 +365,7 @@ static void run_power_iteration_step( execute_spmv( ctx->sparse_handle, ctx->spmv_transpose_product, - ctx->spmv_eigenvector, + ctx->spmv_iterate, &ctx->matrix_plan); if (opts->row_mask_d) { @@ -374,32 +373,31 @@ static void run_power_iteration_step( ctx->num_blocks_row, THREADS_PER_BLOCK, 0, - ctx->stream>>>(ctx->eigenvector_d, opts->row_mask_d, num_rows); + ctx->stream>>>(ctx->iterate_d, opts->row_mask_d, num_rows); } CUBLAS_CHECK(cublasDdot( ctx->blas_handle, num_rows, - ctx->next_eigenvector_d, + ctx->normalized_iterate_d, 1, - ctx->eigenvector_d, + ctx->iterate_d, 1, ctx->sigma_squared_mp)); if (evaluate_residual) { - compute_power_iteration_residual_kernel<<< + replace_normalized_iterate_with_residual_kernel<<< ctx->num_blocks_row, THREADS_PER_BLOCK, 0, ctx->stream>>>( - ctx->next_eigenvector_d, - ctx->eigenvector_d, - ctx->next_eigenvector_d, + ctx->normalized_iterate_d, + ctx->iterate_d, ctx->sigma_squared_mp, num_rows); CUBLAS_CHECK(cublasDnrm2_v2_64( ctx->blas_handle, num_rows, - ctx->next_eigenvector_d, + ctx->normalized_iterate_d, 1, ctx->residual_norm_mp)); } @@ -419,10 +417,9 @@ static __global__ void normalize_power_iteration_vector_kernel( normalized_vector_d[i] = (vector_norm > 0.0) ? (vector_d[i] / vector_norm) : 0.0; } -static __global__ void compute_power_iteration_residual_kernel( - double *residual_d, - const double *vector_d, - const double *normalized_vector_d, +static __global__ void replace_normalized_iterate_with_residual_kernel( + double *normalized_iterate_d, + const double *iterate_d, const double *sigma_squared_d, int n) { @@ -431,7 +428,7 @@ static __global__ void compute_power_iteration_residual_kernel( return; double sigma_squared = *sigma_squared_d; - residual_d[i] = vector_d[i] - sigma_squared * normalized_vector_d[i]; + normalized_iterate_d[i] = iterate_d[i] - sigma_squared * normalized_iterate_d[i]; } static sv_estimator_status_t classify_power_iteration_outcome( @@ -470,13 +467,13 @@ void sv_estimator_free(sv_estimator_ctx_t *ctx) destroy_spmv_plan(&ctx->matrix_plan); CUSPARSE_CHECK(cusparseDestroySpMat(ctx->transpose)); CUSPARSE_CHECK(cusparseDestroySpMat(ctx->matrix)); - CUSPARSE_CHECK(cusparseDestroyDnVec(ctx->spmv_eigenvector)); - CUSPARSE_CHECK(cusparseDestroyDnVec(ctx->spmv_next_eigenvector)); + CUSPARSE_CHECK(cusparseDestroyDnVec(ctx->spmv_iterate)); + CUSPARSE_CHECK(cusparseDestroyDnVec(ctx->spmv_normalized_iterate)); CUSPARSE_CHECK(cusparseDestroyDnVec(ctx->spmv_transpose_product)); - CUDA_CHECK(cudaFree(ctx->eigenvector_d)); - CUDA_CHECK(cudaFree(ctx->next_eigenvector_d)); + CUDA_CHECK(cudaFree(ctx->iterate_d)); + CUDA_CHECK(cudaFree(ctx->normalized_iterate_d)); CUDA_CHECK(cudaFree(ctx->transpose_product_d)); - CUDA_CHECK(cudaFree(ctx->vector_norm_d)); + CUDA_CHECK(cudaFree(ctx->iterate_norm_d)); CUDA_CHECK(cudaFreeHost(ctx->sigma_squared_mp)); CUDA_CHECK(cudaFreeHost(ctx->residual_norm_mp)); free(ctx); From ee2796325f08ab16796fcfd1f0e4fbe64ac0ab09 Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Sun, 13 Sep 2026 20:45:31 -0400 Subject: [PATCH 4/9] Refactor: organize step-control and utility modules --- CMakeLists.txt | 8 ++++---- src/{ => step_control}/active_set_boost.cu | 0 src/{utils => step_control}/pid_controller.c | 0 src/{utils => step_control}/power_iteration.cu | 0 src/{ => utils}/mps_parser.c | 0 5 files changed, 4 insertions(+), 4 deletions(-) rename src/{ => step_control}/active_set_boost.cu (100%) rename src/{utils => step_control}/pid_controller.c (100%) rename src/{utils => step_control}/power_iteration.cu (100%) rename src/{ => utils}/mps_parser.c (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb0ec08..03d8700 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,10 +130,8 @@ FetchContent_MakeAvailable(pslp) # ----------------------------------------------------------------------------- set(CORE_SOURCES src/cuhsdlp.c - src/mps_parser.c src/presolver.c src/utils/matrix_formats.c - src/utils/pid_controller.c src/cone_projector.cu src/preconditioner.cu src/result.cu @@ -141,9 +139,11 @@ set(CORE_SOURCES src/solver.cu src/spmv_backend.cu src/termination_checker.cu - src/active_set_boost.cu + src/step_control/active_set_boost.cu + src/step_control/pid_controller.c + src/step_control/power_iteration.cu src/utils/display.cu - src/utils/power_iteration.cu + src/utils/mps_parser.c src/utils/utils.cu) # Set common include directories for the core libraries diff --git a/src/active_set_boost.cu b/src/step_control/active_set_boost.cu similarity index 100% rename from src/active_set_boost.cu rename to src/step_control/active_set_boost.cu diff --git a/src/utils/pid_controller.c b/src/step_control/pid_controller.c similarity index 100% rename from src/utils/pid_controller.c rename to src/step_control/pid_controller.c diff --git a/src/utils/power_iteration.cu b/src/step_control/power_iteration.cu similarity index 100% rename from src/utils/power_iteration.cu rename to src/step_control/power_iteration.cu diff --git a/src/mps_parser.c b/src/utils/mps_parser.c similarity index 100% rename from src/mps_parser.c rename to src/utils/mps_parser.c From 51ab802b95585e8356a4008f75c40d79666e8ca0 Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Wed, 16 Sep 2026 22:36:38 -0400 Subject: [PATCH 5/9] Perf: collect active-set mask changes once per restart --- src/step_control/active_set_boost.cu | 50 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/step_control/active_set_boost.cu b/src/step_control/active_set_boost.cu index 1e5c4e9..5c4bb12 100644 --- a/src/step_control/active_set_boost.cu +++ b/src/step_control/active_set_boost.cu @@ -64,6 +64,7 @@ static __device__ __forceinline__ void update_window_mask_entry( bool *mask, int *entries_added, int *entries_removed); +static void collect_active_set_mask_changes(pdhg_solver_state_t *state); static bool is_raise_possible( double target_step_size, double current_step_size, @@ -171,11 +172,6 @@ void update_active_set_masks( const int window_start_iter = current_iter - params->active_set_params.window_iterations; - CUDA_CHECK(cudaMemsetAsync( - active_set->mask_changes_d, - 0, - sizeof(active_set_mask_changes_t), - state->stream_A)); update_binding_constraint_mask_kernel<<< state->num_blocks_dual, THREADS_PER_BLOCK, @@ -210,22 +206,6 @@ void update_active_set_masks( current_iter, window_start_iter, state->num_variables); - active_set_mask_changes_t mask_changes = {}; - CUDA_CHECK(cudaMemcpyAsync( - &mask_changes, - active_set->mask_changes_d, - sizeof(mask_changes), - cudaMemcpyDeviceToHost, - state->stream_A)); - CUDA_CHECK(cudaStreamSynchronize(state->stream_A)); - - active_set->free_variable_count += - mask_changes.variables_added - mask_changes.variables_removed; - active_set->binding_constraint_count += - mask_changes.constraints_added - mask_changes.constraints_removed; - active_set->mask_changes_since_estimate += - mask_changes.variables_added + mask_changes.variables_removed + - mask_changes.constraints_added + mask_changes.constraints_removed; } static bool has_active_set_revert_budget( @@ -417,6 +397,8 @@ void try_raise_active_set_step_size( return; } + collect_active_set_mask_changes(state); + active_set_state_t *active_set = &state->active_set; const bool a_mask_is_empty = active_set->free_variable_count == 0 || @@ -490,6 +472,32 @@ void try_raise_active_set_step_size( estimate.max_singular_value); } +static void collect_active_set_mask_changes(pdhg_solver_state_t *state) +{ + active_set_state_t *active_set = &state->active_set; + active_set_mask_changes_t mask_changes = {}; + CUDA_CHECK(cudaMemcpyAsync( + &mask_changes, + active_set->mask_changes_d, + sizeof(mask_changes), + cudaMemcpyDeviceToHost, + state->stream_A)); + CUDA_CHECK(cudaMemsetAsync( + active_set->mask_changes_d, + 0, + sizeof(mask_changes), + state->stream_A)); + CUDA_CHECK(cudaStreamSynchronize(state->stream_A)); + + active_set->free_variable_count += + mask_changes.variables_added - mask_changes.variables_removed; + active_set->binding_constraint_count += + mask_changes.constraints_added - mask_changes.constraints_removed; + active_set->mask_changes_since_estimate += + mask_changes.variables_added + mask_changes.variables_removed + + mask_changes.constraints_added + mask_changes.constraints_removed; +} + static bool is_raise_possible( double target_step_size, double current_step_size, From 598fe76b2b3733cc5d8b3fd5a9f63312f217e774 Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Thu, 17 Sep 2026 00:25:15 -0400 Subject: [PATCH 6/9] Refactor: tidy active-set initialization and raise guards --- src/step_control/active_set_boost.cu | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/step_control/active_set_boost.cu b/src/step_control/active_set_boost.cu index 5c4bb12..218f319 100644 --- a/src/step_control/active_set_boost.cu +++ b/src/step_control/active_set_boost.cu @@ -122,8 +122,14 @@ void initialize_active_set( (void **)&active_set->anchor.constraint_cone_scale_d, sizeof(double)); - CUDA_CHECK(cudaMemset(active_set->free_variable_mask_d, 1, num_variables * sizeof(bool))); - CUDA_CHECK(cudaMemset(active_set->binding_constraint_mask_d, 1, num_constraints * sizeof(bool))); + CUDA_CHECK(cudaMemset( + active_set->free_variable_mask_d, + 1, + num_variables * sizeof(bool))); + CUDA_CHECK(cudaMemset( + active_set->binding_constraint_mask_d, + 1, + num_constraints * sizeof(bool))); active_set->free_variable_count = state->num_variables; active_set->binding_constraint_count = state->num_constraints; active_set->step_size_ceiling = INFINITY; @@ -381,7 +387,8 @@ bool should_revert_active_set_boost( { return true; } - const double initial_fixed_point_error = state->control.initial_fixed_point_error; + const double initial_fixed_point_error = + state->control.initial_fixed_point_error; return initial_fixed_point_error > 0.0 && fixed_point_error > (1.0 + params->active_set_params.divergence_margin) * @@ -400,10 +407,10 @@ void try_raise_active_set_step_size( collect_active_set_mask_changes(state); active_set_state_t *active_set = &state->active_set; - const bool a_mask_is_empty = + const bool either_mask_is_empty = active_set->free_variable_count == 0 || active_set->binding_constraint_count == 0; - if (a_mask_is_empty) + if (either_mask_is_empty) { return; } From e2b73a2dc2a86b7b581b59b4ef6babfb20ef6c07 Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Thu, 17 Sep 2026 00:32:36 -0400 Subject: [PATCH 7/9] Fix: skip repeated degenerate active-set norm estimates --- src/step_control/active_set_boost.cu | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/step_control/active_set_boost.cu b/src/step_control/active_set_boost.cu index 218f319..00e24cb 100644 --- a/src/step_control/active_set_boost.cu +++ b/src/step_control/active_set_boost.cu @@ -449,15 +449,12 @@ void try_raise_active_set_step_size( const sv_estimator_result_t estimate = estimate_masked_constraint_norm(state, params); - const bool estimate_is_trusted = - estimate.status == SV_ESTIMATOR_CONVERGED || - estimate.status == SV_ESTIMATOR_EARLY_EXIT; - if (!estimate_is_trusted) + if (estimate.status == SV_ESTIMATOR_MAX_ITER) { return; } mark_raise_checked(active_set); - if (estimate.status == SV_ESTIMATOR_EARLY_EXIT) + if (estimate.status != SV_ESTIMATOR_CONVERGED) { return; } From a3f154576d6ca39f953452e347f0a18938b1fa24 Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Thu, 17 Sep 2026 00:36:19 -0400 Subject: [PATCH 8/9] Perf: skip unused Rayleigh quotient computations in power iteration --- src/step_control/power_iteration.cu | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/step_control/power_iteration.cu b/src/step_control/power_iteration.cu index 981bf98..60d5102 100644 --- a/src/step_control/power_iteration.cu +++ b/src/step_control/power_iteration.cu @@ -375,16 +375,16 @@ static void run_power_iteration_step( 0, ctx->stream>>>(ctx->iterate_d, opts->row_mask_d, num_rows); } - CUBLAS_CHECK(cublasDdot( - ctx->blas_handle, - num_rows, - ctx->normalized_iterate_d, - 1, - ctx->iterate_d, - 1, - ctx->sigma_squared_mp)); if (evaluate_residual) { + CUBLAS_CHECK(cublasDdot( + ctx->blas_handle, + num_rows, + ctx->normalized_iterate_d, + 1, + ctx->iterate_d, + 1, + ctx->sigma_squared_mp)); replace_normalized_iterate_with_residual_kernel<<< ctx->num_blocks_row, THREADS_PER_BLOCK, From c2c91a9b2fc0f20ac616bc8bcdbbabe41e83e0db Mon Sep 17 00:00:00 2001 From: Bo Tang Date: Thu, 17 Sep 2026 00:40:49 -0400 Subject: [PATCH 9/9] Refactor: make active-set interval checks use the actual point --- src/step_control/active_set_boost.cu | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/step_control/active_set_boost.cu b/src/step_control/active_set_boost.cu index 00e24cb..739b30e 100644 --- a/src/step_control/active_set_boost.cu +++ b/src/step_control/active_set_boost.cu @@ -52,7 +52,7 @@ __global__ void update_free_variable_mask_kernel( int window_start_iter, int num_variables); static __device__ __forceinline__ bool is_safely_between_bounds( - double dual_projection_input, + double value, double lower, double upper, double relative_tol); @@ -248,7 +248,7 @@ __global__ void update_binding_constraint_mask_kernel( lower == upper || !(cone_scale > 0.0) || !is_safely_between_bounds( - constraint_dual_projection_input[i] / cone_scale, + -(constraint_dual_projection_input[i] / cone_scale), lower, upper, relative_tol); @@ -315,21 +315,21 @@ __global__ void update_free_variable_mask_kernel( } static __device__ __forceinline__ bool is_safely_between_bounds( - double dual_projection_input, + double value, double lower, double upper, double relative_tol) { const bool below_upper = !isfinite(upper) || - upper + dual_projection_input > + upper - value > relative_tol * - fmax(1.0, fmax(fabs(dual_projection_input), fabs(upper))); + fmax(1.0, fmax(fabs(value), fabs(upper))); const bool above_lower = !isfinite(lower) || - -lower - dual_projection_input > + value - lower > relative_tol * - fmax(1.0, fmax(fabs(dual_projection_input), fabs(lower))); + fmax(1.0, fmax(fabs(value), fabs(lower))); return below_upper && above_lower; }