Skip to content

Commit e0dc603

Browse files
authored
Several fixes needed to compile cuOpt with LLVM (#121)
- add std:: prefix to min() and max(); - add missing headers; - avoid calling std::optional::value() on device code since it can throw exceptions; - add "template" keyword to help parsing; - use a constexpr static method to define init_data(), to avoid using a not-yet-finished classes. Authors: - Vitor Sessak (https://github.com/vitor1001) Approvers: - Hugo Linsenmaier (https://github.com/hlinsen) URL: #121
1 parent 161b8c2 commit e0dc603

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+153
-138
lines changed

cpp/cuopt_cli.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ std::string param_name_to_arg_name(const std::string& input)
177177
int main(int argc, char* argv[])
178178
{
179179
// Get the version string from the version_config.hpp file
180-
const auto version_string = std::string("cuOpt ") + std::to_string(CUOPT_VERSION_MAJOR) + "." +
181-
std::to_string(CUOPT_VERSION_MINOR) + "." +
182-
std::to_string(CUOPT_VERSION_PATCH);
180+
const std::string version_string = std::string("cuOpt ") + std::to_string(CUOPT_VERSION_MAJOR) +
181+
"." + std::to_string(CUOPT_VERSION_MINOR) + "." +
182+
std::to_string(CUOPT_VERSION_PATCH);
183183

184184
// Create the argument parser
185185
argparse::ArgumentParser program("cuopt_cli", version_string);

cpp/include/cuopt/linear_programming/mip/solver_settings.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#pragma once
1919

20+
#include <vector>
21+
2022
#include <cuopt/linear_programming/constants.h>
2123
#include <cuopt/linear_programming/utilities/internals.hpp>
2224

cpp/libmps_parser/src/utilities/error.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
#pragma once
1818

19+
#include <string>
20+
1921
#include <stdarg.h>
2022
#include <stdexcept>
2123

cpp/src/linear_programming/pdlp_constants.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@
2020
#include <raft/util/cuda_utils.cuh>
2121

2222
namespace cuopt::linear_programming::detail {
23-
constexpr int block_size = 128;
23+
inline constexpr int block_size = 128;
2424

2525
// When using APIs that handle variable stride sizes these are used to express that we assume that
2626
// the data accessed has a contigous layout in memory for both solutions
2727
// {
28-
constexpr int primal_stride = 1;
29-
constexpr int dual_stride = 1;
28+
inline constexpr int primal_stride = 1;
29+
inline constexpr int dual_stride = 1;
3030
// }
3131

3232
// #define PDLP_DEBUG_MODE
3333

3434
// Value used to determine what we see as too small (the value) or too large (1/value) values when
3535
// computing the new primal weight during the restart.
3636
template <typename f_t>
37-
constexpr f_t safe_guard_for_extreme_values_in_primal_weight_computation = 1.0e-10;
37+
inline constexpr f_t safe_guard_for_extreme_values_in_primal_weight_computation = 1.0e-10;
3838
// }
3939

4040
// used to detect divergence in the movement as should trigger a numerical_error
4141
template <typename f_t>
42-
constexpr f_t divergent_movement = f_t{};
42+
inline constexpr f_t divergent_movement = f_t{};
4343

4444
template <>
45-
constexpr float divergent_movement<float> = 1.0e20f;
45+
inline constexpr float divergent_movement<float> = 1.0e20f;
4646

4747
template <>
48-
constexpr double divergent_movement<double> = 1.0e100;
48+
inline constexpr double divergent_movement<double> = 1.0e100;
4949

5050
// }
5151

cpp/src/linear_programming/utils.cuh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ struct relative_residual_t {
398398

399399
// Used for best primal so far, count how many constraints are violated
400400
if (abs_.has_value() && nb_violated_constraints_.has_value()) {
401-
if (residual >= abs_.value() + rel_ * rhs) atomicAdd(nb_violated_constraints_.value(), 1);
401+
if (residual >= *abs_ + rel_ * rhs) atomicAdd(*nb_violated_constraints_, 1);
402402
}
403403
return residual - rel_ * rhs;
404404
}

cpp/src/math_optimization/solution_reader.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
#include "solution_reader.hpp"
1919

20+
#include <algorithm>
2021
#include <fstream>
2122
#include <optional>
2223
#include <regex>
2324
#include <sstream>
2425
#include <stdexcept>
2526
#include <string>
27+
#include <unordered_map>
28+
2629
namespace cuopt::linear_programming {
2730

2831
/**

cpp/src/mip/diversity/diversity_manager.cu

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool diversity_manager_t<i_t, f_t>::regenerate_solutions()
7373
const i_t min_size = 2;
7474
while (population.current_size() <= min_size && (current_step == 0 || counter < 5)) {
7575
CUOPT_LOG_DEBUG("Trying to regenerate solution, pop size %d\n", population.current_size());
76-
time_limit = min(time_limit, timer.remaining_time());
76+
time_limit = std::min(time_limit, timer.remaining_time());
7777
ls.fj.randomize_weights(problem_ptr->handle_ptr);
7878
population.add_solution(generate_solution(time_limit));
7979
if (timer.check_time_limit()) { return false; }
@@ -92,18 +92,18 @@ std::vector<solution_t<i_t, f_t>> diversity_manager_t<i_t, f_t>::generate_more_s
9292
{
9393
std::vector<solution_t<i_t, f_t>> solutions;
9494
timer_t total_time_to_generate = timer_t(timer.remaining_time() / 5.);
95-
f_t time_limit = min(60., total_time_to_generate.remaining_time());
96-
f_t ls_limit = min(5., timer.remaining_time() / 20.);
95+
f_t time_limit = std::min(60., total_time_to_generate.remaining_time());
96+
f_t ls_limit = std::min(5., timer.remaining_time() / 20.);
9797
const i_t n_sols_to_generate = 2;
9898
for (i_t i = 0; i < n_sols_to_generate; ++i) {
9999
CUOPT_LOG_DEBUG("Trying to generate more solutions");
100-
time_limit = min(time_limit, timer.remaining_time());
100+
time_limit = std::min(time_limit, timer.remaining_time());
101101
ls.fj.randomize_weights(problem_ptr->handle_ptr);
102102
auto sol = generate_solution(time_limit);
103103
population.run_solution_callbacks(sol);
104104
solutions.emplace_back(solution_t<i_t, f_t>(sol));
105105
if (total_time_to_generate.check_time_limit()) { return solutions; }
106-
timer_t timer(min(ls_limit, timer.remaining_time()));
106+
timer_t timer(std::min(ls_limit, timer.remaining_time()));
107107
ls.run_local_search(sol, population.weights, timer);
108108
population.run_solution_callbacks(sol);
109109
solutions.emplace_back(std::move(sol));
@@ -185,7 +185,7 @@ void diversity_manager_t<i_t, f_t>::generate_initial_solutions()
185185
// solution if we can generate faster generate up to 10 sols
186186
const f_t generation_time_limit = 0.6 * timer.get_time_limit();
187187
const f_t max_island_gen_time = 600;
188-
f_t total_island_gen_time = min(generation_time_limit, max_island_gen_time);
188+
f_t total_island_gen_time = std::min(generation_time_limit, max_island_gen_time);
189189
timer_t gen_timer(total_island_gen_time);
190190
f_t sol_time_limit = gen_timer.remaining_time();
191191
for (i_t i = 0; i < maximum_island_size; ++i) {
@@ -267,7 +267,7 @@ void diversity_manager_t<i_t, f_t>::generate_quick_feasible_solution()
267267
{
268268
solution_t<i_t, f_t> solution(*problem_ptr);
269269
// min 1 second, max 10 seconds
270-
const f_t generate_fast_solution_time = min(10., max(1., timer.remaining_time() / 20.));
270+
const f_t generate_fast_solution_time = std::min(10., std::max(1., timer.remaining_time() / 20.));
271271
timer_t sol_timer(generate_fast_solution_time);
272272
// do very short LP run to get somewhere close to the optimal point
273273
ls.generate_fast_solution(solution, sol_timer);
@@ -308,7 +308,7 @@ solution_t<i_t, f_t> diversity_manager_t<i_t, f_t>::run_solver()
308308
const f_t time_limit = timer.remaining_time();
309309
constexpr f_t time_ratio_on_init_lp = 0.1;
310310
constexpr f_t max_time_on_lp = 30;
311-
const f_t lp_time_limit = min(max_time_on_lp, time_limit * time_ratio_on_init_lp);
311+
const f_t lp_time_limit = std::min(max_time_on_lp, time_limit * time_ratio_on_init_lp);
312312

313313
// to automatically compute the solving time on scope exit
314314
auto timer_raii_guard =
@@ -334,7 +334,8 @@ solution_t<i_t, f_t> diversity_manager_t<i_t, f_t>::run_solver()
334334
generate_quick_feasible_solution();
335335
constexpr f_t time_ratio_of_probing_cache = 0.10;
336336
constexpr f_t max_time_on_probing = 60;
337-
f_t time_for_probing_cache = min(max_time_on_probing, time_limit * time_ratio_of_probing_cache);
337+
f_t time_for_probing_cache =
338+
std::min(max_time_on_probing, time_limit * time_ratio_of_probing_cache);
338339
timer_t probing_timer{time_for_probing_cache};
339340
if (check_b_b_preemption()) { return population.best_feasible(); }
340341
compute_probing_cache(ls.constraint_prop.bounds_update, *problem_ptr, probing_timer);
@@ -544,7 +545,7 @@ diversity_manager_t<i_t, f_t>::recombine_and_local_search(solution_t<i_t, f_t>&
544545
cuopt_assert(population.test_invariant(), "");
545546
cuopt_assert(lp_offspring.test_number_all_integer(), "All must be integers before LP");
546547
f_t lp_run_time = offspring.get_feasible() ? 3. : 1.;
547-
lp_run_time = min(lp_run_time, timer.remaining_time());
548+
lp_run_time = std::min(lp_run_time, timer.remaining_time());
548549
run_lp_with_vars_fixed(*lp_offspring.problem_ptr,
549550
lp_offspring,
550551
lp_offspring.problem_ptr->integer_indices,
@@ -555,7 +556,7 @@ diversity_manager_t<i_t, f_t>::recombine_and_local_search(solution_t<i_t, f_t>&
555556
cuopt_assert(lp_offspring.test_number_all_integer(), "All must be integers after LP");
556557
f_t lp_qual = lp_offspring.get_quality(population.weights);
557558
CUOPT_LOG_DEBUG("After LP offspring sol cost:feas %f : %d", lp_qual, lp_offspring.get_feasible());
558-
f_t offspring_qual = min(offspring.get_quality(population.weights), lp_qual);
559+
f_t offspring_qual = std::min(offspring.get_quality(population.weights), lp_qual);
559560
recombine_stats.update_improve_stats(
560561
offspring_qual, sol1.get_quality(population.weights), sol2.get_quality(population.weights));
561562
return std::make_pair(std::move(offspring), std::move(lp_offspring));

cpp/src/mip/diversity/population.cu

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ template <typename i_t, typename f_t>
6565
void population_t<i_t, f_t>::initialize_population()
6666
{
6767
var_threshold =
68-
max(problem_ptr->n_variables - var_threshold, (problem_ptr->n_variables / 10) * 8);
68+
std::max(problem_ptr->n_variables - var_threshold, (problem_ptr->n_variables / 10) * 8);
6969
initial_threshold_ratio = (f_t)var_threshold / problem_ptr->n_variables;
7070
solutions.reserve(max_solutions);
7171
indices.reserve(max_solutions);
@@ -378,7 +378,7 @@ void population_t<i_t, f_t>::compute_new_weights()
378378
infeasibility_importance *= weight_increase_ratio;
379379
}
380380

381-
infeasibility_importance = min(max_infeasibility_weight, infeasibility_importance);
381+
infeasibility_importance = std::min(max_infeasibility_weight, infeasibility_importance);
382382
thrust::for_each(best_sol.handle_ptr->get_thrust_policy(),
383383
thrust::counting_iterator(0),
384384
thrust::counting_iterator(0) + weights.cstr_weights.size(),
@@ -394,7 +394,7 @@ void population_t<i_t, f_t>::compute_new_weights()
394394
} else {
395395
CUOPT_LOG_DEBUG("Decreasing weights!");
396396
infeasibility_importance *= weight_decrease_ratio;
397-
infeasibility_importance = max(min_infeasibility_weight, infeasibility_importance);
397+
infeasibility_importance = std::max(min_infeasibility_weight, infeasibility_importance);
398398

399399
thrust::for_each(
400400
best_sol.handle_ptr->get_thrust_policy(),
@@ -535,7 +535,7 @@ template <typename i_t>
535535
i_t get_max_var_threshold(i_t n_vars)
536536
{
537537
if (n_vars < 50) {
538-
return max(1, n_vars - 1);
538+
return std::max(1, n_vars - 1);
539539
} else if (n_vars < 80) {
540540
return n_vars - 2;
541541
} else if (n_vars < 200) {
@@ -559,7 +559,7 @@ void population_t<i_t, f_t>::halve_the_population()
559559
size_t max_var_threshold = get_max_var_threshold(problem_ptr->n_integer_vars);
560560
while (current_size() > max_solutions / 2) {
561561
clear_except_best_feasible();
562-
var_threshold = max(var_threshold * 0.97, 0.5 * problem_ptr->n_integer_vars);
562+
var_threshold = std::max(var_threshold * 0.97, 0.5 * problem_ptr->n_integer_vars);
563563
for (auto& sol : sol_vec) {
564564
add_solution(solution_t<i_t, f_t>(sol));
565565
}
@@ -569,9 +569,9 @@ void population_t<i_t, f_t>::halve_the_population()
569569
// if we removed too many decrease the diversity a little
570570
while (current_size() < max_solutions / 4) {
571571
clear_except_best_feasible();
572-
var_threshold =
573-
min(max_var_threshold,
574-
min((size_t)(var_threshold * 0.97), (size_t)(0.995 * problem_ptr->n_integer_vars)));
572+
var_threshold = std::min(
573+
max_var_threshold,
574+
std::min((size_t)(var_threshold * 0.97), (size_t)(0.995 * problem_ptr->n_integer_vars)));
575575
for (auto& sol : sol_vec) {
576576
add_solution(solution_t<i_t, f_t>(sol));
577577
}

cpp/src/mip/diversity/recombiners/recombiner_stats.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ struct recombine_stats {
3939

4040
void update_improve_stats(double cost_new, double cost_first, double cost_second)
4141
{
42-
if (cost_new < (min(cost_first, cost_second) - OBJECTIVE_EPSILON)) ++better_than_both;
43-
if (cost_new < (max(cost_first, cost_second) - OBJECTIVE_EPSILON)) ++better_than_one;
42+
if (cost_new < (std::min(cost_first, cost_second) - OBJECTIVE_EPSILON)) ++better_than_both;
43+
if (cost_new < (std::max(cost_first, cost_second) - OBJECTIVE_EPSILON)) ++better_than_one;
4444
}
4545

4646
void add_attempt() { ++attempts; }

cpp/src/mip/feasibility_jump/load_balancing.cuh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include <cuda_runtime_api.h>
19+
1820
#include "feasibility_jump_kernels.cuh"
1921

2022
#include <cub/block/block_merge_sort.cuh>
@@ -380,8 +382,7 @@ __global__ void load_balancing_mtm_compute_candidates(
380382
i_t lane_id = threadIdx.x % raft::WarpSize;
381383

382384
const i_t stride = get_warp_id_stride();
383-
for (auto [var_idx, subworkid, offset_begin, offset_end, csr_offset, skip] :
384-
csr_load_balancer<i_t, f_t>{
385+
for (auto [var_idx, subworkid, offset_begin, offset_end, _, skip] : csr_load_balancer<i_t, f_t>{
385386
fj, fj.row_size_nonbin_prefix_sum, fj.work_id_to_nonbin_var_idx}) {
386387
cuopt_assert(!fj.pb.is_binary_variable[var_idx], "variable is binary");
387388

@@ -483,8 +484,7 @@ __launch_bounds__(TPB_loadbalance, 16) __global__
483484
i_t lane_id = threadIdx.x % raft::WarpSize;
484485

485486
const i_t stride = get_warp_id_stride();
486-
for (auto [var_idx, subworkid, offset_begin, offset_end, csr_offset, skip] :
487-
csr_load_balancer<i_t, f_t>{
487+
for (auto [var_idx, subworkid, offset_begin, offset_end, _, skip] : csr_load_balancer<i_t, f_t>{
488488
fj, fj.row_size_nonbin_prefix_sum, fj.work_id_to_nonbin_var_idx}) {
489489
cuopt_assert(!fj.pb.is_binary_variable[var_idx], "variable is binary");
490490

0 commit comments

Comments
 (0)