-
Notifications
You must be signed in to change notification settings - Fork 127
Run FJ heuristics before and during presolve #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aliceb-nv
wants to merge
26
commits into
NVIDIA:main
Choose a base branch
from
aliceb-nv:pre-presolve-heuristics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+679
−87
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
49030ad
squash changes
aliceb-nv 5881d35
fix style
aliceb-nv 8a3684c
ai review comments
aliceb-nv e019653
Merge branch 'main' into pre-presolve-heuristics
aliceb-nv 9c89092
review comments, fix crash
aliceb-nv d5f7dde
Merge branch 'pre-presolve-heuristics' of https://github.com/aliceb-n…
aliceb-nv 62a53ef
Merge branch 'main' into pre-presolve-heuristics
aliceb-nv a4a195f
add GIL locks to handle worker threads invoking python callbacks
aliceb-nv ca34f77
Merge branch 'pre-presolve-heuristics' of https://github.com/aliceb-n…
aliceb-nv f349b5b
fix missing nogil
aliceb-nv 506d046
fix python callbacks
aliceb-nv 33230b6
fix attempt
aliceb-nv 71e8c9a
operate on a problem copy for early GPUFJ
aliceb-nv 9d7055e
fix reported bound in user callback
aliceb-nv 7b9451b
fix build
aliceb-nv d0c5a42
fix thrust build + more timer checks
aliceb-nv d559b34
Merge branch 'main' into fix-thrust-build
aliceb-nv c5d0bd5
Merge branch 'fix-thrust-build' into pre-presolve-heuristics
aliceb-nv 67240f5
review comment
aliceb-nv a15424f
fix thrust solve
aliceb-nv dee5d4d
Merge branch 'fix-thrust-build' into pre-presolve-heuristics
aliceb-nv 7b3d40c
fix handle being destroyed before other gpu structures in early heuri…
aliceb-nv a5122ad
Merge branch 'main' into pre-presolve-heuristics
aliceb-nv 68e07bb
fix build
aliceb-nv 352cd15
fix cudaSetDevice bug
aliceb-nv e4b8166
fix empty tests
aliceb-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* clang-format off */ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| /* clang-format on */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <mip_heuristics/problem/problem.cuh> | ||
| #include <mip_heuristics/solution/solution.cuh> | ||
|
|
||
| #include <cuopt/linear_programming/mip/solver_settings.hpp> | ||
|
|
||
| #include <utilities/logger.hpp> | ||
|
|
||
| #include <thrust/fill.h> | ||
|
|
||
| #include <chrono> | ||
| #include <functional> | ||
| #include <limits> | ||
| #include <vector> | ||
|
|
||
| namespace cuopt::linear_programming::detail { | ||
|
|
||
| template <typename f_t> | ||
| using early_incumbent_callback_t = | ||
| std::function<void(f_t solver_obj, f_t user_obj, const std::vector<f_t>& assignment)>; | ||
|
|
||
| // CRTP base for early heuristics that run on the original (or papilo-presolved) problem | ||
| // during presolve to find incumbents as early as possible. | ||
| // Derived classes implement start() and stop(). | ||
| template <typename i_t, typename f_t, typename Derived> | ||
| class early_heuristic_t { | ||
| public: | ||
| early_heuristic_t(const optimization_problem_t<i_t, f_t>& op_problem, | ||
| const typename mip_solver_settings_t<i_t, f_t>::tolerances_t& tolerances, | ||
| early_incumbent_callback_t<f_t> incumbent_callback) | ||
| : incumbent_callback_(std::move(incumbent_callback)) | ||
| { | ||
| RAFT_CUDA_TRY(cudaGetDevice(&device_id_)); | ||
|
|
||
| // Build and preprocess on the original handle, then copy onto our own handle | ||
| // so the derived solver can run on a dedicated stream (prevents graph capture conflicts). | ||
| problem_t<i_t, f_t> temp_problem(op_problem, tolerances, false); | ||
| temp_problem.preprocess_problem(); | ||
| temp_problem.handle_ptr->sync_stream(); | ||
| problem_ptr_ = std::make_unique<problem_t<i_t, f_t>>(temp_problem, &handle_); | ||
|
|
||
| solution_ptr_ = std::make_unique<solution_t<i_t, f_t>>(*problem_ptr_); | ||
| thrust::fill(handle_.get_thrust_policy(), | ||
| solution_ptr_->assignment.begin(), | ||
| solution_ptr_->assignment.end(), | ||
| f_t{0}); | ||
| solution_ptr_->clamp_within_bounds(); | ||
| } | ||
|
|
||
| bool solution_found() const { return solution_found_; } | ||
| f_t get_best_objective() const { return best_objective_; } | ||
| void set_best_objective(f_t obj) { best_objective_ = obj; } | ||
| const std::vector<f_t>& get_best_assignment() const { return best_assignment_; } | ||
|
|
||
| protected: | ||
| ~early_heuristic_t() = default; | ||
|
|
||
| // NOT thread-safe. solver_obj is in solver-space (always minimization). | ||
| // Uses a private CUDA stream to avoid racing with the FJ solver's stream. | ||
| void try_update_best(f_t solver_obj, const std::vector<f_t>& assignment) | ||
| { | ||
| if (solver_obj >= best_objective_) { return; } | ||
| best_objective_ = solver_obj; | ||
|
|
||
| RAFT_CUDA_TRY(cudaSetDevice(device_id_)); | ||
| auto stream = handle_.get_stream(); | ||
| rmm::device_uvector<f_t> d_assignment(assignment.size(), stream); | ||
| raft::copy(d_assignment.data(), assignment.data(), assignment.size(), stream); | ||
| problem_ptr_->post_process_assignment(d_assignment, true, stream); | ||
| auto user_assignment = cuopt::host_copy(d_assignment, stream); | ||
|
|
||
| best_assignment_ = user_assignment; | ||
| solution_found_ = true; | ||
| f_t user_obj = problem_ptr_->get_user_obj_from_solver_obj(solver_obj); | ||
| double elapsed = | ||
| std::chrono::duration<double>(std::chrono::steady_clock::now() - start_time_).count(); | ||
| CUOPT_LOG_INFO("Early heuristics (%s) lowered the primal bound. Objective %g. Time %.2f", | ||
| Derived::name(), | ||
| user_obj, | ||
| elapsed); | ||
| if (incumbent_callback_) { incumbent_callback_(solver_obj, user_obj, user_assignment); } | ||
| } | ||
|
|
||
| int device_id_{0}; | ||
|
|
||
| // handle_ must be declared before problem_ptr_/solution_ptr_ so it outlives them | ||
| // (C++ destroys members in reverse declaration order) | ||
| raft::handle_t handle_; | ||
|
|
||
| std::unique_ptr<problem_t<i_t, f_t>> problem_ptr_; | ||
| std::unique_ptr<solution_t<i_t, f_t>> solution_ptr_; | ||
|
|
||
| bool solution_found_{false}; | ||
| f_t best_objective_{std::numeric_limits<f_t>::infinity()}; | ||
| std::vector<f_t> best_assignment_; | ||
|
|
||
| early_incumbent_callback_t<f_t> incumbent_callback_; | ||
| std::chrono::steady_clock::time_point start_time_; | ||
| }; | ||
|
|
||
| } // namespace cuopt::linear_programming::detail |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.