diff --git a/internal/cuda_to_hip.h b/internal/cuda_to_hip.h index 04814b7..798eed2 100644 --- a/internal/cuda_to_hip.h +++ b/internal/cuda_to_hip.h @@ -167,7 +167,7 @@ static inline const char *cublasGetStatusName(hipblasStatus_t status) #define cusparseSpMM_bufferSize hipsparseSpMM_bufferSize #define cusparseSpMM_preprocess hipsparseSpMM_preprocess #define CUSPARSE_ORDER_COL HIPSPARSE_ORDER_COL -#define CUSPARSE_SPMM_ALG_DEFAULT HIPSPARSE_SPMM_ALG_DEFAULT +#define CUSPARSE_SPMM_CSR_ALG3 HIPSPARSE_SPMM_CSR_ALG3 #define cusparseCsr2cscEx2 hipsparseCsr2cscEx2 #define cusparseCsr2cscEx2_bufferSize hipsparseCsr2cscEx2_bufferSize diff --git a/internal/internal_types.h b/internal/internal_types.h index b591609..0a8039f 100644 --- a/internal/internal_types.h +++ b/internal/internal_types.h @@ -146,10 +146,6 @@ typedef struct int num_blocks_nnz; double objective_vector_norm; double constraint_bound_norm; - double *constraint_lower_bound_finite_val; - double *constraint_upper_bound_finite_val; - double *variable_lower_bound_finite_val; - double *variable_upper_bound_finite_val; double *initial_primal_solution; double *current_primal_solution; @@ -177,16 +173,13 @@ typedef struct double *variable_rescaling; double constraint_bound_rescaling; double objective_vector_rescaling; - double *primal_slack; double *dual_slack; double rescaling_time_sec; clock_t start_time; double cumulative_time_sec; - double *primal_residual; double absolute_primal_residual; double relative_primal_residual; - double *dual_residual; double absolute_dual_residual; double relative_dual_residual; double primal_objective_value; @@ -201,7 +194,6 @@ typedef struct double dual_ray_certificate_error; termination_reason_t termination_reason; - double *delta_primal_solution; double *delta_dual_solution; double fixed_point_error; double initial_fixed_point_error; diff --git a/internal/reduce.h b/internal/reduce.h index bfed45c..68323c3 100644 --- a/internal/reduce.h +++ b/internal/reduce.h @@ -29,6 +29,11 @@ // The element function of a reduction: DEVICE_FN(i) { return ...; } #define DEVICE_FN(i) [=] __host__ __device__(int i) +__host__ __device__ __forceinline__ double finite_or_zero(double v) +{ + return isfinite(v) ? v : 0.0; +} + // Temporary storage of cub::DeviceReduce, shared by every call. cub rejects a // call that needs more, and captured graphs keep its address, so it is sized // once by planning every reduction. diff --git a/internal/termination.h b/internal/termination.h index 6d3597d..20957f9 100644 --- a/internal/termination.h +++ b/internal/termination.h @@ -31,29 +31,23 @@ extern "C" // Copies the scalar struct to the host and waits. void fetch_device_scalars(pdhg_solver_state_t *state); - // state->delta_primal_solution = to_primal - from_primal, and the same for - // the dual vectors. - void compute_solution_difference(pdhg_solver_state_t *state, - const double *from_primal, - const double *to_primal, - const double *from_dual, - const double *to_dual); - // Residuals and objective values from A x and A^T y already in the state. // Feasibility polishing passes vectors of the original problem where its own // state holds a modified one. - void launch_residual_check(pdhg_solver_state_t *state, - const double *objective_vector, - const double *primal_solution, - const double *constraint_lower_bound_finite, - const double *constraint_upper_bound_finite); - // The reductions of the residual and fixed point checks; see reduce.h for // required_bytes. - void launch_residual_reductions(const pdhg_solver_state_t *state, - size_t *required_bytes, - const double *objective_vector, - const double *primal_solution); + void launch_primal_residual_reductions(const pdhg_solver_state_t *state, + size_t *required_bytes, + const double *objective_vector, + const double *primal_solution, + const double *constraint_lower_bound, + const double *constraint_upper_bound); + void launch_dual_residual_reductions(const pdhg_solver_state_t *state, + size_t *required_bytes, + const double *objective_vector, + const double *primal_solution, + const double *constraint_lower_bound, + const double *constraint_upper_bound); void launch_fixed_point_reductions(const pdhg_solver_state_t *state, size_t *required_bytes); void compute_residual_device(pdhg_solver_state_t *state); diff --git a/internal/utils.h b/internal/utils.h index 94a4dad..9ba4d93 100644 --- a/internal/utils.h +++ b/internal/utils.h @@ -119,8 +119,6 @@ extern "C" void cupdlpx_spmv_ATx(cusparseHandle_t sparse_handle, void *ctx, const double *x, double *y); - void compute_interaction_and_movement(pdhg_solver_state_t *solver_state, double *interaction, double *movement); - bool should_do_adaptive_restart(pdhg_solver_state_t *solver_state, const restart_parameters_t *restart_params, int termination_evaluation_frequency); diff --git a/src/feasibility_polish.cu b/src/feasibility_polish.cu index 2b52142..8797f94 100644 --- a/src/feasibility_polish.cu +++ b/src/feasibility_polish.cu @@ -107,8 +107,17 @@ static void print_initial_feas_polish_info(bool is_primal_polish, const pdhg_par printf("---------------------------------------------------------------------------------------\n"); } +typedef struct +{ + termination_reason_t termination_reason; + int total_count; + double cumulative_time_sec; + double relative_residual; + double objective_value; +} feas_polish_summary_t; + static void -pdhg_feas_polish_final_log(const pdhg_solver_state_t *primal_state, const pdhg_solver_state_t *dual_state, bool verbose) +pdhg_feas_polish_final_log(const feas_polish_summary_t *primal, const feas_polish_summary_t *dual, bool verbose) { if (!verbose) { @@ -116,17 +125,17 @@ pdhg_feas_polish_final_log(const pdhg_solver_state_t *primal_state, const pdhg_s } printf("---------------------------------------------------------------------------------------\n"); printf("Feasibility Polishing Summary\n"); - printf(" Primal Status : %s\n", termination_reason_to_string(primal_state->termination_reason)); - printf(" Primal Iterations : %d\n", primal_state->total_count); - printf(" Primal Time Usage : %.3g sec\n", primal_state->cumulative_time_sec); - printf(" Dual Status : %s\n", termination_reason_to_string(dual_state->termination_reason)); - printf(" Dual Iterations : %d\n", dual_state->total_count); - printf(" Dual Time Usage : %.3g sec\n", dual_state->cumulative_time_sec); - printf(" Primal Residual : %.3e\n", primal_state->relative_primal_residual); - printf(" Dual Residual : %.3e\n", dual_state->relative_dual_residual); + printf(" Primal Status : %s\n", termination_reason_to_string(primal->termination_reason)); + printf(" Primal Iterations : %d\n", primal->total_count); + printf(" Primal Time Usage : %.3g sec\n", primal->cumulative_time_sec); + printf(" Dual Status : %s\n", termination_reason_to_string(dual->termination_reason)); + printf(" Dual Iterations : %d\n", dual->total_count); + printf(" Dual Time Usage : %.3g sec\n", dual->cumulative_time_sec); + printf(" Primal Residual : %.3e\n", primal->relative_residual); + printf(" Dual Residual : %.3e\n", dual->relative_residual); printf(" Primal Dual Gap : %.3e\n", - fabs(primal_state->primal_objective_value - dual_state->dual_objective_value) / - (1.0 + fabs(primal_state->primal_objective_value) + fabs(dual_state->dual_objective_value))); + fabs(primal->objective_value - dual->objective_value) / + (1.0 + fabs(primal->objective_value) + fabs(dual->objective_value))); } static void display_feas_polish_iteration_stats(const pdhg_solver_state_t *state, bool verbose, bool is_primal_polish) @@ -167,20 +176,22 @@ compute_polish_residual_device(pdhg_solver_state_t *state, const pdhg_solver_sta if (is_primal_polish) { cupdlpx_spmv_Ax(state->sparse_handle, state->spmv_ctx, state->pdhg_primal_solution, state->primal_product); - launch_residual_check(state, - ori_state->objective_vector, - state->pdhg_primal_solution, - state->constraint_lower_bound_finite_val, - state->constraint_upper_bound_finite_val); + launch_primal_residual_reductions(state, + NULL, + ori_state->objective_vector, + state->pdhg_primal_solution, + state->constraint_lower_bound, + state->constraint_upper_bound); } else { cupdlpx_spmv_ATx(state->sparse_handle, state->spmv_ctx, state->pdhg_dual_solution, state->dual_product); - launch_residual_check(state, - state->objective_vector, - ori_state->pdhg_primal_solution, - ori_state->constraint_lower_bound_finite_val, - ori_state->constraint_upper_bound_finite_val); + launch_dual_residual_reductions(state, + NULL, + state->objective_vector, + ori_state->pdhg_primal_solution, + ori_state->constraint_lower_bound, + ori_state->constraint_upper_bound); } } @@ -209,21 +220,26 @@ void launch_polish_fixed_point_reductions(const pdhg_solver_state_t *state, size_t *required_bytes, bool is_primal_polish) { - const double *delta = is_primal_polish ? state->delta_primal_solution : state->delta_dual_solution; + const double *from = is_primal_polish ? state->pdhg_primal_solution : state->pdhg_dual_solution; + const double *to = is_primal_polish ? state->reflected_primal_solution : state->reflected_dual_solution; int len = is_primal_polish ? state->num_variables : state->num_constraints; double *out = is_primal_polish ? &state->scalars_d->fixed_point_primal_sq : &state->scalars_d->fixed_point_dual_sq; - reduce_sum(state, required_bytes, len, DEVICE_FN(i) { return delta[i] * delta[i]; }, out); + reduce_sum( + state, + required_bytes, + len, + DEVICE_FN(i) + { + double d = to[i] - from[i]; + return d * d; + }, + out); } // The fixed point error of a polishing iterate: only its own side of the // movement term, without the square root (the restart test compares ratios). void compute_polish_fixed_point_error_device(pdhg_solver_state_t *state, bool is_primal_polish) { - compute_solution_difference(state, - state->pdhg_primal_solution, - state->reflected_primal_solution, - state->pdhg_dual_solution, - state->reflected_dual_solution); launch_polish_fixed_point_reductions(state, NULL, is_primal_polish); } @@ -278,6 +294,12 @@ void feasibility_polish(const pdhg_parameters_t *params, pdhg_solver_state_t *st state->primal_objective_value = primal_state->primal_objective_value; } state->feasibility_iteration += primal_state->total_count - 1; + feas_polish_summary_t primal_summary = {primal_state->termination_reason, + primal_state->total_count, + primal_state->cumulative_time_sec, + primal_state->relative_primal_residual, + primal_state->primal_objective_value}; + primal_feas_polish_state_free(primal_state); // DUAL FEASIBILITY POLISHING pdhg_solver_state_t *dual_state = initialize_dual_feas_polish_state(state); @@ -296,15 +318,19 @@ void feasibility_polish(const pdhg_parameters_t *params, pdhg_solver_state_t *st state->dual_objective_value = dual_state->dual_objective_value; } state->feasibility_iteration += dual_state->total_count - 1; + feas_polish_summary_t dual_summary = {dual_state->termination_reason, + dual_state->total_count, + dual_state->cumulative_time_sec, + dual_state->relative_dual_residual, + dual_state->dual_objective_value}; + dual_feas_polish_state_free(dual_state); state->objective_gap = fabs(state->primal_objective_value - state->dual_objective_value); state->relative_objective_gap = state->objective_gap / (1.0 + fabs(state->primal_objective_value) + fabs(state->dual_objective_value)); // FINAL LOGGING - pdhg_feas_polish_final_log(primal_state, dual_state, params->verbose); - primal_feas_polish_state_free(primal_state); - dual_feas_polish_state_free(dual_state); + pdhg_feas_polish_final_log(&primal_summary, &dual_summary, params->verbose); state->feasibility_polishing_time = (double)(clock() - feasibility_polishing_start_time) / CLOCKS_PER_SEC; return; @@ -499,11 +525,6 @@ static pdhg_solver_state_t *initialize_primal_feas_polish_state(const pdhg_solve ALLOC_ZERO(primal_state->dual_product, num_var * sizeof(double)); ALLOC_ZERO(primal_state->dual_slack, num_var * sizeof(double)); - ALLOC_ZERO(primal_state->primal_slack, num_cons * sizeof(double)); - ALLOC_ZERO(primal_state->dual_residual, num_var * sizeof(double)); - ALLOC_ZERO(primal_state->primal_residual, num_cons * sizeof(double)); - ALLOC_ZERO(primal_state->delta_primal_solution, num_var * sizeof(double)); - ALLOC_ZERO(primal_state->delta_dual_solution, num_cons * sizeof(double)); // RESET SCALAR primal_state->primal_weight_error_sum = 0.0; @@ -528,14 +549,6 @@ static pdhg_solver_state_t *initialize_primal_feas_polish_state(const pdhg_solve primal_state->relative_objective_gap = 0.0; primal_state->objective_gap = 0.0; - primal_state->spmv_ctx = cupdlpx_spmv_ctx_create(primal_state->sparse_handle, - primal_state->constraint_matrix, - primal_state->constraint_matrix_t, - primal_state->pdhg_primal_solution, - primal_state->primal_product, - primal_state->pdhg_dual_solution, - primal_state->dual_product); - return primal_state; } @@ -561,13 +574,7 @@ void primal_feas_polish_state_free(pdhg_solver_state_t *state) SAFE_CUDA_FREE(state->pdhg_dual_solution); SAFE_CUDA_FREE(state->reflected_dual_solution); SAFE_CUDA_FREE(state->primal_product); - SAFE_CUDA_FREE(state->primal_slack); SAFE_CUDA_FREE(state->dual_slack); - SAFE_CUDA_FREE(state->primal_residual); - SAFE_CUDA_FREE(state->dual_residual); - SAFE_CUDA_FREE(state->delta_primal_solution); - SAFE_CUDA_FREE(state->delta_dual_solution); - cupdlpx_spmv_ctx_destroy(state->spmv_ctx); free(state); } @@ -620,11 +627,6 @@ static pdhg_solver_state_t *initialize_dual_feas_polish_state(const pdhg_solver_ CUDA_CHECK(cudaMalloc(&dest, bytes)); \ CUDA_CHECK(cudaMemset(dest, 0, bytes)); - ALLOC_ZERO(dual_state->constraint_lower_bound_finite_val, num_cons * sizeof(double)); - ALLOC_ZERO(dual_state->constraint_upper_bound_finite_val, num_cons * sizeof(double)); - ALLOC_ZERO(dual_state->variable_lower_bound_finite_val, num_var * sizeof(double)); - ALLOC_ZERO(dual_state->variable_upper_bound_finite_val, num_var * sizeof(double)); - // ALLOCATE AND COPY SOLUTION VECTORS ALLOC_AND_COPY_DEV( dual_state->initial_dual_solution, original_state->initial_dual_solution, num_cons * sizeof(double)); @@ -642,11 +644,6 @@ static pdhg_solver_state_t *initialize_dual_feas_polish_state(const pdhg_solver_ ALLOC_ZERO(dual_state->pdhg_primal_solution, num_var * sizeof(double)); ALLOC_ZERO(dual_state->reflected_primal_solution, num_var * sizeof(double)); ALLOC_ZERO(dual_state->primal_product, num_cons * sizeof(double)); - ALLOC_ZERO(dual_state->primal_slack, num_cons * sizeof(double)); - ALLOC_ZERO(dual_state->dual_residual, num_var * sizeof(double)); - ALLOC_ZERO(dual_state->primal_residual, num_cons * sizeof(double)); - ALLOC_ZERO(dual_state->delta_primal_solution, num_var * sizeof(double)); - ALLOC_ZERO(dual_state->delta_dual_solution, num_cons * sizeof(double)); // RESET SCALAR dual_state->primal_weight_error_sum = 0.0; @@ -671,14 +668,6 @@ static pdhg_solver_state_t *initialize_dual_feas_polish_state(const pdhg_solver_ dual_state->relative_objective_gap = 0.0; dual_state->objective_gap = 0.0; - dual_state->spmv_ctx = cupdlpx_spmv_ctx_create(dual_state->sparse_handle, - dual_state->constraint_matrix, - dual_state->constraint_matrix_t, - dual_state->pdhg_primal_solution, - dual_state->primal_product, - dual_state->pdhg_dual_solution, - dual_state->dual_product); - return dual_state; } @@ -697,11 +686,6 @@ void dual_feas_polish_state_free(pdhg_solver_state_t *state) SAFE_CUDA_FREE(state->constraint_upper_bound); SAFE_CUDA_FREE(state->variable_lower_bound); SAFE_CUDA_FREE(state->variable_upper_bound); - SAFE_CUDA_FREE(state->constraint_lower_bound_finite_val); - SAFE_CUDA_FREE(state->constraint_upper_bound_finite_val); - SAFE_CUDA_FREE(state->variable_lower_bound_finite_val); - SAFE_CUDA_FREE(state->variable_upper_bound_finite_val); - SAFE_CUDA_FREE(state->initial_primal_solution); SAFE_CUDA_FREE(state->current_primal_solution); SAFE_CUDA_FREE(state->pdhg_primal_solution); @@ -714,13 +698,7 @@ void dual_feas_polish_state_free(pdhg_solver_state_t *state) SAFE_CUDA_FREE(state->reflected_dual_solution); SAFE_CUDA_FREE(state->primal_product); - SAFE_CUDA_FREE(state->primal_slack); SAFE_CUDA_FREE(state->dual_slack); - SAFE_CUDA_FREE(state->primal_residual); - SAFE_CUDA_FREE(state->dual_residual); - SAFE_CUDA_FREE(state->delta_primal_solution); - SAFE_CUDA_FREE(state->delta_dual_solution); - cupdlpx_spmv_ctx_destroy(state->spmv_ctx); free(state); } diff --git a/src/infeasibility.cu b/src/infeasibility.cu index c10322b..b20649e 100644 --- a/src/infeasibility.cu +++ b/src/infeasibility.cu @@ -166,11 +166,7 @@ void launch_candidate_reductions(const pdhg_solver_state_t *state, size_t *requi const double bound_rescaling = state->constraint_bound_rescaling; const double objective_rescaling = state->objective_vector_rescaling; const double *variable_lower = state->variable_lower_bound, *variable_upper = state->variable_upper_bound; - const double *variable_lower_finite = state->variable_lower_bound_finite_val; - const double *variable_upper_finite = state->variable_upper_bound_finite_val; const double *constraint_lower = state->constraint_lower_bound, *constraint_upper = state->constraint_upper_bound; - const double *constraint_lower_finite = state->constraint_lower_bound_finite_val; - const double *constraint_upper_finite = state->constraint_upper_bound_finite_val; // primal ray d auto d_abs_orig = DEVICE_FN(j) { return fabs(d[j]) / (variable_rescaling[j] * bound_rescaling); }; @@ -189,13 +185,13 @@ void launch_candidate_reductions(const pdhg_solver_state_t *state, size_t *requi // dual ray y and its reduced cost r = -A^T y auto y_abs_orig = DEVICE_FN(i) { return fabs(y[i]) / (constraint_rescaling[i] * objective_rescaling); }; auto con_bounds_term = DEVICE_FN(i) - { return bound_term(y[i], constraint_lower_finite[i], constraint_upper_finite[i]); }; + { return bound_term(y[i], finite_or_zero(constraint_lower[i]), finite_or_zero(constraint_upper[i])); }; auto con_bounds_term_abs = DEVICE_FN(i) - { return bound_term_abs(y[i], constraint_lower_finite[i], constraint_upper_finite[i]); }; - auto var_bounds_term = - DEVICE_FN(j) { return bound_term(-ATy[j], variable_lower_finite[j], variable_upper_finite[j]); }; + { return bound_term_abs(y[i], finite_or_zero(constraint_lower[i]), finite_or_zero(constraint_upper[i])); }; + auto var_bounds_term = DEVICE_FN(j) + { return bound_term(-ATy[j], finite_or_zero(variable_lower[j]), finite_or_zero(variable_upper[j])); }; auto var_bounds_term_abs = DEVICE_FN(j) - { return bound_term_abs(-ATy[j], variable_lower_finite[j], variable_upper_finite[j]); }; + { return bound_term_abs(-ATy[j], finite_or_zero(variable_lower[j]), finite_or_zero(variable_upper[j])); }; auto reduced_cost_violation = DEVICE_FN(j) { return dual_ray_reduced_cost_violation(-ATy[j], isfinite(variable_lower[j]), isfinite(variable_upper[j])); }; auto reduced_cost_violation_orig = DEVICE_FN(j) { return reduced_cost_violation(j) * variable_rescaling[j]; }; @@ -222,7 +218,7 @@ static void spmm_buffer( &HOST_ZERO, out, CUDA_R_64F, - CUSPARSE_SPMM_ALG_DEFAULT, + CUSPARSE_SPMM_CSR_ALG3, &size)); CUDA_CHECK(cudaMalloc(buffer, size > 0 ? size : 16)); } @@ -240,7 +236,7 @@ static void spmm_preprocess( &HOST_ZERO, out, CUDA_R_64F, - CUSPARSE_SPMM_ALG_DEFAULT, + CUSPARSE_SPMM_CSR_ALG3, buffer)); } @@ -256,7 +252,7 @@ spmm(cusparseHandle_t handle, cusparseSpMatDescr_t mat, cusparseDnMatDescr_t in, &HOST_ZERO, out, CUDA_R_64F, - CUSPARSE_SPMM_ALG_DEFAULT, + CUSPARSE_SPMM_CSR_ALG3, buffer)); } diff --git a/src/solver.cu b/src/solver.cu index 0c617cb..3f2cb7a 100644 --- a/src/solver.cu +++ b/src/solver.cu @@ -39,11 +39,6 @@ __global__ void build_transpose_map(const int *__restrict__ A_row_ind, const int *__restrict__ At_col_ind, int nnz, int *__restrict__ A_to_At); -__global__ void fill_finite_bounds_kernel(const double *__restrict__ lower_bound, - const double *__restrict__ upper_bound, - double *__restrict__ lower_bound_finite_val, - double *__restrict__ upper_bound_finite_val, - int num_elements); __global__ void rescale_solution_kernel(double *__restrict__ primal_solution, double *__restrict__ dual_solution, const double *__restrict__ variable_rescaling, @@ -288,6 +283,12 @@ cupdlpx_result_t *optimize(const pdhg_parameters_t *params, const lp_problem_t * display_iteration_stats(state, params); } + if (state->infeas_ctx) + { + infeasibility_context_destroy(state->infeas_ctx); + state->infeas_ctx = NULL; + } + if (params->feasibility_polishing && state->termination_reason != TERMINATION_REASON_DUAL_INFEASIBLE && state->termination_reason != TERMINATION_REASON_PRIMAL_INFEASIBLE) { @@ -500,8 +501,6 @@ static pdhg_solver_state_t *initialize_solver_state(const lp_problem_t *working_ ALLOC_ZERO(state->reflected_primal_solution, var_bytes); ALLOC_ZERO(state->dual_product, var_bytes); ALLOC_ZERO(state->dual_slack, var_bytes); - ALLOC_ZERO(state->dual_residual, var_bytes); - ALLOC_ZERO(state->delta_primal_solution, var_bytes); ALLOC_ZERO(state->delta_dual_product, var_bytes); ALLOC_ZERO(state->initial_dual_solution, con_bytes); @@ -509,8 +508,6 @@ static pdhg_solver_state_t *initialize_solver_state(const lp_problem_t *working_ ALLOC_ZERO(state->pdhg_dual_solution, con_bytes); ALLOC_ZERO(state->reflected_dual_solution, con_bytes); ALLOC_ZERO(state->primal_product, con_bytes); - ALLOC_ZERO(state->primal_slack, con_bytes); - ALLOC_ZERO(state->primal_residual, con_bytes); ALLOC_ZERO(state->delta_dual_solution, con_bytes); if (working_problem->primal_start) @@ -545,23 +542,6 @@ static pdhg_solver_state_t *initialize_solver_state(const lp_problem_t *working_ CUDA_CHECK( cudaMemcpy(state->pdhg_dual_solution, state->initial_dual_solution, con_bytes, cudaMemcpyDeviceToDevice)); - CUDA_CHECK(cudaMalloc(&state->constraint_lower_bound_finite_val, con_bytes)); - CUDA_CHECK(cudaMalloc(&state->constraint_upper_bound_finite_val, con_bytes)); - CUDA_CHECK(cudaMalloc(&state->variable_lower_bound_finite_val, var_bytes)); - CUDA_CHECK(cudaMalloc(&state->variable_upper_bound_finite_val, var_bytes)); - - fill_finite_bounds_kernel<<num_blocks_dual, THREADS_PER_BLOCK>>>(state->constraint_lower_bound, - state->constraint_upper_bound, - state->constraint_lower_bound_finite_val, - state->constraint_upper_bound_finite_val, - n_cons); - - fill_finite_bounds_kernel<<num_blocks_primal, THREADS_PER_BLOCK>>>(state->variable_lower_bound, - state->variable_upper_bound, - state->variable_lower_bound_finite_val, - state->variable_upper_bound_finite_val, - n_vars); - CUDA_CHECK(cudaFree(state->constraint_matrix->row_ind)); state->constraint_matrix->row_ind = NULL; CUDA_CHECK(cudaFree(state->constraint_matrix_t->row_ind)); @@ -721,23 +701,6 @@ __global__ void build_transpose_map(const int *__restrict__ A_row_ind, A_to_At[k] = pos; } -__global__ void fill_finite_bounds_kernel(const double *__restrict__ lb, - const double *__restrict__ ub, - double *__restrict__ lb_finite, - double *__restrict__ ub_finite, - int num_elements) -{ - int i = blockIdx.x * blockDim.x + threadIdx.x; - if (i >= num_elements) - return; - - double Li = lb[i]; - double Ui = ub[i]; - - lb_finite[i] = isfinite(Li) ? Li : 0.0; - ub_finite[i] = isfinite(Ui) ? Ui : 0.0; -} - __global__ void compute_next_primal_solution_kernel(double *__restrict__ current_primal, double *__restrict__ reflected_primal, const double *__restrict__ initial_primal, @@ -966,12 +929,6 @@ void perform_restart(pdhg_solver_state_t *state, const pdhg_parameters_t *params printf("[restart] iter %d: %s\n", state->total_count, state->last_restart_reason); } state->restart_count++; - compute_solution_difference(state, - state->initial_primal_solution, - state->pdhg_primal_solution, - state->initial_dual_solution, - state->pdhg_dual_solution); - launch_restart_reductions(state, NULL); fetch_device_scalars(state); double primal_dist = sqrt(state->scalars_h->restart_primal_sq); @@ -1038,18 +995,27 @@ void perform_restart(pdhg_solver_state_t *state, const pdhg_parameters_t *params void launch_restart_reductions(const pdhg_solver_state_t *state, size_t *required_bytes) { - const double *delta_primal = state->delta_primal_solution, *delta_dual = state->delta_dual_solution; + const double *from_primal = state->initial_primal_solution, *to_primal = state->pdhg_primal_solution; + const double *from_dual = state->initial_dual_solution, *to_dual = state->pdhg_dual_solution; reduce_sum( state, required_bytes, state->num_variables, - DEVICE_FN(j) { return delta_primal[j] * delta_primal[j]; }, + DEVICE_FN(j) + { + double d = to_primal[j] - from_primal[j]; + return d * d; + }, &state->scalars_d->restart_primal_sq); reduce_sum( state, required_bytes, state->num_constraints, - DEVICE_FN(i) { return delta_dual[i] * delta_dual[i]; }, + DEVICE_FN(i) + { + double d = to_dual[i] - from_dual[i]; + return d * d; + }, &state->scalars_d->restart_dual_sq); } @@ -1101,7 +1067,18 @@ static void initialize_step_size_and_primal_weight(pdhg_solver_state_t *state, c static void create_reduction_storage(pdhg_solver_state_t *state) { size_t bytes = 0; - launch_residual_reductions(state, &bytes, state->objective_vector, state->pdhg_primal_solution); + launch_primal_residual_reductions(state, + &bytes, + state->objective_vector, + state->pdhg_primal_solution, + state->constraint_lower_bound, + state->constraint_upper_bound); + launch_dual_residual_reductions(state, + &bytes, + state->objective_vector, + state->pdhg_primal_solution, + state->constraint_lower_bound, + state->constraint_upper_bound); launch_fixed_point_reductions(state, &bytes); launch_restart_reductions(state, &bytes); launch_polish_fixed_point_reductions(state, &bytes, true); @@ -1162,14 +1139,6 @@ void pdhg_solver_state_free(pdhg_solver_state_t *state) CUDA_CHECK(cudaFree(state->constraint_lower_bound)); if (state->constraint_upper_bound) CUDA_CHECK(cudaFree(state->constraint_upper_bound)); - if (state->constraint_lower_bound_finite_val) - CUDA_CHECK(cudaFree(state->constraint_lower_bound_finite_val)); - if (state->constraint_upper_bound_finite_val) - CUDA_CHECK(cudaFree(state->constraint_upper_bound_finite_val)); - if (state->variable_lower_bound_finite_val) - CUDA_CHECK(cudaFree(state->variable_lower_bound_finite_val)); - if (state->variable_upper_bound_finite_val) - CUDA_CHECK(cudaFree(state->variable_upper_bound_finite_val)); if (state->initial_primal_solution) CUDA_CHECK(cudaFree(state->initial_primal_solution)); if (state->current_primal_solution) @@ -1194,16 +1163,8 @@ void pdhg_solver_state_free(pdhg_solver_state_t *state) CUDA_CHECK(cudaFree(state->constraint_rescaling)); if (state->variable_rescaling) CUDA_CHECK(cudaFree(state->variable_rescaling)); - if (state->primal_slack) - CUDA_CHECK(cudaFree(state->primal_slack)); if (state->dual_slack) CUDA_CHECK(cudaFree(state->dual_slack)); - if (state->primal_residual) - CUDA_CHECK(cudaFree(state->primal_residual)); - if (state->dual_residual) - CUDA_CHECK(cudaFree(state->dual_residual)); - if (state->delta_primal_solution) - CUDA_CHECK(cudaFree(state->delta_primal_solution)); if (state->delta_dual_solution) CUDA_CHECK(cudaFree(state->delta_dual_solution)); if (state->delta_dual_product) diff --git a/src/termination.cu b/src/termination.cu index 6e55ffc..688a3a4 100644 --- a/src/termination.cu +++ b/src/termination.cu @@ -24,77 +24,15 @@ limitations under the License. #include #include -__global__ void compute_delta_solution_kernel(const double *__restrict__ initial_primal, - const double *__restrict__ pdhg_primal, - double *__restrict__ delta_primal, - const double *__restrict__ initial_dual, - const double *__restrict__ pdhg_dual, - double *__restrict__ delta_dual, - int n_vars, - int n_cons) +__global__ void compute_delta_dual_solution_kernel(const double *__restrict__ from_dual, + const double *__restrict__ to_dual, + double *__restrict__ delta_dual, + int n_cons) { int i = blockIdx.x * blockDim.x + threadIdx.x; - if (i < n_vars) + if (i < n_cons) { - delta_primal[i] = (pdhg_primal[i] - initial_primal[i]); - } - else if (i < n_vars + n_cons) - { - int idx = i - n_vars; - delta_dual[idx] = (pdhg_dual[idx] - initial_dual[idx]); - } -} - -void compute_solution_difference(pdhg_solver_state_t *state, - const double *from_primal, - const double *to_primal, - const double *from_dual, - const double *to_dual) -{ - compute_delta_solution_kernel<<num_blocks_primal_dual, THREADS_PER_BLOCK, 0, state->stream>>>( - from_primal, - to_primal, - state->delta_primal_solution, - from_dual, - to_dual, - state->delta_dual_solution, - state->num_variables, - state->num_constraints); -} - -__global__ void compute_residual_kernel(double *__restrict__ primal_residual, - const double *__restrict__ primal_product, - const double *__restrict__ constraint_lower_bound, - const double *__restrict__ constraint_upper_bound, - const double *__restrict__ dual_solution, - double *__restrict__ dual_residual, - const double *__restrict__ dual_product, - const double *__restrict__ dual_slack, - const double *__restrict__ objective_vector, - const double *__restrict__ constraint_rescaling, - const double *__restrict__ variable_rescaling, - double *__restrict__ primal_slack, - const double *__restrict__ const_lb_finite, - const double *__restrict__ const_ub_finite, - int num_constraints, - int num_variables) -{ - int i = blockIdx.x * blockDim.x + threadIdx.x; - - if (i < num_constraints) - { - - double clamped_val = fmax(constraint_lower_bound[i], fmin(primal_product[i], constraint_upper_bound[i])); - primal_residual[i] = (primal_product[i] - clamped_val) * constraint_rescaling[i]; - - // the contribution of y_i to the dual objective, against the finite bounds - primal_slack[i] = - fmax(dual_solution[i], 0.0) * const_lb_finite[i] + fmin(dual_solution[i], 0.0) * const_ub_finite[i]; - } - else if (i < num_constraints + num_variables) - { - int idx = i - num_constraints; - dual_residual[idx] = (objective_vector[idx] - dual_product[idx] - dual_slack[idx]) * variable_rescaling[idx]; + delta_dual[i] = (to_dual[i] - from_dual[i]); } } @@ -105,62 +43,88 @@ void fetch_device_scalars(pdhg_solver_state_t *state) CUDA_CHECK(cudaStreamSynchronize(state->stream)); } -void launch_residual_reductions(const pdhg_solver_state_t *state, - size_t *required_bytes, - const double *objective_vector, - const double *primal_solution) +void launch_primal_residual_reductions(const pdhg_solver_state_t *state, + size_t *required_bytes, + const double *objective_vector, + const double *primal_solution, + const double *constraint_lower_bound, + const double *constraint_upper_bound) { device_scalars_t *out = state->scalars_d; const int m = state->num_constraints, n = state->num_variables; - const double *primal_residual = state->primal_residual, *primal_slack = state->primal_slack; - const double *dual_residual = state->dual_residual, *dual_slack = state->dual_slack; + const double *primal_product = state->primal_product, *constraint_rescaling = state->constraint_rescaling; + const double *constraint_lower = constraint_lower_bound, *constraint_upper = constraint_upper_bound; const double *objective = objective_vector, *x = primal_solution; + auto primal_residual = DEVICE_FN(i) + { + double clamped_val = fmax(constraint_lower[i], fmin(primal_product[i], constraint_upper[i])); + return (primal_product[i] - clamped_val) * constraint_rescaling[i]; + }; reduce_sum( - state, required_bytes, m, DEVICE_FN(i) { return primal_residual[i] * primal_residual[i]; }, &out->primal_res_sq); - reduce_max(state, required_bytes, m, DEVICE_FN(i) { return fabs(primal_residual[i]); }, &out->primal_res_max); - reduce_sum(state, required_bytes, m, DEVICE_FN(i) { return primal_slack[i]; }, &out->primal_slack_sum); - reduce_sum(state, required_bytes, n, DEVICE_FN(j) { return dual_residual[j] * dual_residual[j]; }, &out->dual_res_sq); - reduce_max(state, required_bytes, n, DEVICE_FN(j) { return fabs(dual_residual[j]); }, &out->dual_res_max); + state, + required_bytes, + m, + DEVICE_FN(i) + { + double r = primal_residual(i); + return r * r; + }, + &out->primal_res_sq); + reduce_max(state, required_bytes, m, DEVICE_FN(i) { return fabs(primal_residual(i)); }, &out->primal_res_max); reduce_sum(state, required_bytes, n, DEVICE_FN(j) { return objective[j] * x[j]; }, &out->objective_dot); - reduce_sum(state, required_bytes, n, DEVICE_FN(j) { return dual_slack[j] * x[j]; }, &out->dual_slack_dot); } -void launch_residual_check(pdhg_solver_state_t *state, - const double *objective_vector, - const double *primal_solution, - const double *constraint_lower_bound_finite, - const double *constraint_upper_bound_finite) +void launch_dual_residual_reductions(const pdhg_solver_state_t *state, + size_t *required_bytes, + const double *objective_vector, + const double *primal_solution, + const double *constraint_lower_bound, + const double *constraint_upper_bound) { - compute_residual_kernel<<num_blocks_primal_dual, THREADS_PER_BLOCK, 0, state->stream>>>( - state->primal_residual, - state->primal_product, - state->constraint_lower_bound, - state->constraint_upper_bound, - state->pdhg_dual_solution, - state->dual_residual, - state->dual_product, - state->dual_slack, - objective_vector, - state->constraint_rescaling, - state->variable_rescaling, - state->primal_slack, - constraint_lower_bound_finite, - constraint_upper_bound_finite, - state->num_constraints, - state->num_variables); - - launch_residual_reductions(state, NULL, objective_vector, primal_solution); + device_scalars_t *out = state->scalars_d; + const int m = state->num_constraints, n = state->num_variables; + const double *dual_product = state->dual_product, *variable_rescaling = state->variable_rescaling; + const double *constraint_lower = constraint_lower_bound, *constraint_upper = constraint_upper_bound; + const double *y = state->pdhg_dual_solution, *dual_slack = state->dual_slack; + const double *objective = objective_vector, *x = primal_solution; + auto primal_slack = DEVICE_FN(i) + { + return fmax(y[i], 0.0) * finite_or_zero(constraint_lower[i]) + + fmin(y[i], 0.0) * finite_or_zero(constraint_upper[i]); + }; + auto dual_residual = + DEVICE_FN(j) { return (objective[j] - dual_product[j] - dual_slack[j]) * variable_rescaling[j]; }; + reduce_sum(state, required_bytes, m, primal_slack, &out->primal_slack_sum); + reduce_sum( + state, + required_bytes, + n, + DEVICE_FN(j) + { + double r = dual_residual(j); + return r * r; + }, + &out->dual_res_sq); + reduce_max(state, required_bytes, n, DEVICE_FN(j) { return fabs(dual_residual(j)); }, &out->dual_res_max); + reduce_sum(state, required_bytes, n, DEVICE_FN(j) { return dual_slack[j] * x[j]; }, &out->dual_slack_dot); } void compute_residual_device(pdhg_solver_state_t *state) { cupdlpx_spmv_Ax(state->sparse_handle, state->spmv_ctx, state->pdhg_primal_solution, state->primal_product); cupdlpx_spmv_ATx(state->sparse_handle, state->spmv_ctx, state->pdhg_dual_solution, state->dual_product); - launch_residual_check(state, - state->objective_vector, - state->pdhg_primal_solution, - state->constraint_lower_bound_finite_val, - state->constraint_upper_bound_finite_val); + launch_primal_residual_reductions(state, + NULL, + state->objective_vector, + state->pdhg_primal_solution, + state->constraint_lower_bound, + state->constraint_upper_bound); + launch_dual_residual_reductions(state, + NULL, + state->objective_vector, + state->pdhg_primal_solution, + state->constraint_lower_bound, + state->constraint_upper_bound); } // Turns the fetched scalars into the reported residuals and objective values. @@ -197,27 +161,34 @@ void launch_fixed_point_reductions(const pdhg_solver_state_t *state, size_t *req // primal step with A^T times the dual step device_scalars_t *out = state->scalars_d; const int m = state->num_constraints, n = state->num_variables; - const double *delta_primal = state->delta_primal_solution, *delta_dual = state->delta_dual_solution; + const double *from_primal = state->pdhg_primal_solution, *to_primal = state->reflected_primal_solution; + const double *delta_dual = state->delta_dual_solution; const double *delta_dual_product = state->delta_dual_product; + auto delta_primal = DEVICE_FN(j) { return to_primal[j] - from_primal[j]; }; reduce_sum(state, required_bytes, m, DEVICE_FN(i) { return delta_dual[i] * delta_dual[i]; }, &out->fixed_point_dual_sq); reduce_sum( - state, required_bytes, n, DEVICE_FN(j) { return delta_primal[j] * delta_primal[j]; }, &out->fixed_point_primal_sq); + state, + required_bytes, + n, + DEVICE_FN(j) + { + double d = delta_primal(j); + return d * d; + }, + &out->fixed_point_primal_sq); reduce_sum( state, required_bytes, n, - DEVICE_FN(j) { return delta_dual_product[j] * delta_primal[j]; }, + DEVICE_FN(j) { return delta_dual_product[j] * delta_primal(j); }, &out->fixed_point_cross); } // Queues the fixed point error of the current iterate. void compute_fixed_point_error_device(pdhg_solver_state_t *state) { - compute_solution_difference(state, - state->pdhg_primal_solution, - state->reflected_primal_solution, - state->pdhg_dual_solution, - state->reflected_dual_solution); + compute_delta_dual_solution_kernel<<num_blocks_dual, THREADS_PER_BLOCK, 0, state->stream>>>( + state->pdhg_dual_solution, state->reflected_dual_solution, state->delta_dual_solution, state->num_constraints); cupdlpx_spmv_ATx(state->sparse_handle, state->spmv_ctx, state->delta_dual_solution, state->delta_dual_product); diff --git a/src/utils.cu b/src/utils.cu index bcf7e81..8fb57f8 100644 --- a/src/utils.cu +++ b/src/utils.cu @@ -305,26 +305,6 @@ sv_estimator_result_t sv_estimator_run(sv_estimator_ctx_t *ctx, const sv_estimat return result; } -void compute_interaction_and_movement(pdhg_solver_state_t *state, double *interaction, double *movement) -{ - double dual_norm, primal_norm, cross_term; - - CUBLAS_CHECK( - cublasDnrm2_v2_64(state->blas_handle, state->num_constraints, state->delta_dual_solution, 1, &dual_norm)); - CUBLAS_CHECK( - cublasDnrm2_v2_64(state->blas_handle, state->num_variables, state->delta_primal_solution, 1, &primal_norm)); - *movement = 0.5 * (primal_norm * primal_norm * state->primal_weight + dual_norm * dual_norm / state->primal_weight); - - CUBLAS_CHECK(cublasDdot(state->blas_handle, - state->num_variables, - state->dual_product, - 1, - state->delta_primal_solution, - 1, - &cross_term)); - *interaction = fabs(cross_term); -} - const char *termination_reason_to_string(termination_reason_t reason) { switch (reason)