Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cpp/include/cuopt/linear_programming/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
#define CUOPT_METHOD_DUAL_SIMPLEX 2
#define CUOPT_METHOD_BARRIER 3

/* @brief File format constants for problem I/O */
#define CUOPT_FILE_FORMAT_MPS 0

/* @brief Status codes constants */
#define CUOPT_SUCCESS 0
#define CUOPT_INVALID_ARGUMENT 1
Expand Down
61 changes: 61 additions & 0 deletions cpp/include/cuopt/linear_programming/cuopt_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ cuopt_int_t cuOptGetVersion(cuopt_int_t* version_major,
*/
cuopt_int_t cuOptReadProblem(const char* filename, cuOptOptimizationProblem* problem_ptr);

/**
* @brief Write an optimization problem to a file.
*
* @param[in] problem - The optimization problem to write.
* @param[in] filename - The path to the output file.
* @param[in] format - The file format to use. Currently only CUOPT_FILE_FORMAT_MPS is supported.
*
* @return A status code indicating success or failure. Returns CUOPT_INVALID_ARGUMENT
* if an unsupported format is specified.
*/
cuopt_int_t cuOptWriteProblem(cuOptOptimizationProblem problem,
const char* filename,
cuopt_int_t format);

/** @brief Create an optimization problem of the form
*
* @verbatim
Expand Down Expand Up @@ -681,6 +695,53 @@ cuopt_int_t cuOptGetFloatParameter(cuOptSolverSettings settings,
const char* parameter_name,
cuopt_float_t* parameter_value);

/**
* @brief Set the initial primal solution for an LP solve.
*
* @param[in] settings - The solver settings object.
* @param[in] primal_solution - A pointer to an array of type cuopt_float_t
* of size num_variables containing the initial primal values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to specify if data should be on the CPU and/or GPU?

Copy link
Contributor Author

@aliceb-nv aliceb-nv Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the C API abstracts away the reality of a GPU implementation - therefore all pointers involved are host pointers (as a result, we do not expose the stream(s) involved)
I think that if the need arises, we could create alternative versions of these functions to upload GPU data, or use something similar to cuSparse's pointer mode flag

Copy link

@smish-nvidia smish-nvidia Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note that as a new cuOpt user, I had the exact same question about host/device pointer for inputs.

I think it would be helpful to explicitly say they expect host pointers (either in the individual docstrings or in some high level place in the user guide "all C API pointers are expected to be pointers to host memory").

* @param[in] num_variables - The number of variables (size of the primal_solution array).
*
* @return A status code indicating success or failure.
*/
cuopt_int_t cuOptSetInitialPrimalSolution(cuOptSolverSettings settings,
const cuopt_float_t* primal_solution,
cuopt_int_t num_variables);

/**
* @brief Set the initial dual solution for an LP solve.
*
* @param[in] settings - The solver settings object.
* @param[in] dual_solution - A pointer to an array of type cuopt_float_t
* of size num_constraints containing the initial dual values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

* @param[in] num_constraints - The number of constraints (size of the dual_solution array).
*
* @return A status code indicating success or failure.
*/
cuopt_int_t cuOptSetInitialDualSolution(cuOptSolverSettings settings,
const cuopt_float_t* dual_solution,
cuopt_int_t num_constraints);

/**
* @brief Add an initial solution (MIP start) for MIP solving.
*
* This function can be called multiple times to add multiple MIP starts.
* The solver will use these as starting points for the MIP search.
*
* @param[in] settings - The solver settings object.
* @param[in] solution - A pointer to an array of type cuopt_float_t
* of size num_variables containing the solution values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

* @param[in] num_variables - The number of variables (size of the solution array).
*
* @attention Currently unsupported with presolve on.
*
* @return A status code indicating success or failure.
*/
cuopt_int_t cuOptAddMIPStart(cuOptSolverSettings settings,
const cuopt_float_t* solution,
cuopt_int_t num_variables);

/** @brief Check if an optimization problem is a mixed integer programming problem.
*
* @param[in] problem - The optimization problem.
Expand Down
14 changes: 14 additions & 0 deletions cpp/include/cuopt/linear_programming/optimization_problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ class optimization_problem_t {
optimization_problem_t(raft::handle_t const* handle_ptr);
optimization_problem_t(const optimization_problem_t<i_t, f_t>& other);

/**
* @brief Check if this optimization problem is equivalent to another.
*
* Two problems are considered equivalent if they represent the same mathematical
* optimization problem, potentially with variables and constraints in a different order.
* The mapping between problems is determined by matching variable names and row names.
* Essentially checks for graph isomorphism given label mappings.
*
* @param other The other optimization problem to compare against.
* @return true if the problems are equivalent (up to permutation of variables/constraints),
* false otherwise.
*/
bool is_equivalent(const optimization_problem_t<i_t, f_t>& other) const;

std::vector<internals::base_solution_callback_t*> mip_callbacks_;

/**
Expand Down
54 changes: 45 additions & 9 deletions cpp/libmps_parser/src/mps_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void mps_writer_t<i_t, f_t>::write(const std::string& mps_file_path)

// Keep a single integer section marker by going over constraints twice and writing out
// integral/nonintegral nonzeros ordered map
std::vector<bool> var_in_constraint(n_variables, false);
std::map<i_t, std::vector<std::pair<i_t, f_t>>> integral_col_nnzs;
std::map<i_t, std::vector<std::pair<i_t, f_t>>> continuous_col_nnzs;
for (size_t row_id = 0; row_id < (size_t)n_constraints; row_id++) {
Expand All @@ -150,12 +151,37 @@ void mps_writer_t<i_t, f_t>::write(const std::string& mps_file_path)
} else {
continuous_col_nnzs[var].emplace_back(row_id, constraint_matrix_values[k]);
}
var_in_constraint[var] = true;
}
}

// Record and explicitely declared variables not contained in any constraint.
std::vector<i_t> orphan_continuous_vars;
std::vector<i_t> orphan_integer_vars;
for (i_t var = 0; var < n_variables; ++var) {
if (!var_in_constraint[var]) {
if (variable_types[var] == 'I') {
orphan_integer_vars.push_back(var);
} else {
orphan_continuous_vars.push_back(var);
}
}
}

for (size_t is_integral = 0; is_integral < 2; is_integral++) {
auto& col_map = is_integral ? integral_col_nnzs : continuous_col_nnzs;
auto& col_map = is_integral ? integral_col_nnzs : continuous_col_nnzs;
auto& orphan_vars = is_integral ? orphan_integer_vars : orphan_continuous_vars;
if (is_integral) mps_file << " MARK0001 'MARKER' 'INTORG'\n";
for (auto& var_id : orphan_vars) {
std::string col_name = var_id < problem_.get_variable_names().size()
? problem_.get_variable_names()[var_id]
: "C" + std::to_string(var_id);
// Write that column even if it is orphan as has a zero objective coefficient.
// Some tools require variables to be declared in "COLUMNS" before any "BOUNDS" statements.
mps_file << " " << col_name << " "
<< (problem_.get_objective_name().empty() ? "OBJ" : problem_.get_objective_name())
<< " " << objective_coefficients[var_id] << "\n";
}
for (auto& [var_id, nnzs] : col_map) {
std::string col_name = var_id < problem_.get_variable_names().size()
? problem_.get_variable_names()[var_id]
Expand Down Expand Up @@ -222,24 +248,34 @@ void mps_writer_t<i_t, f_t>::write(const std::string& mps_file_path)
// BOUNDS section
mps_file << "BOUNDS\n";
for (size_t j = 0; j < (size_t)n_variables; j++) {
std::string col_name = j < problem_.get_variable_names().size()
? problem_.get_variable_names()[j]
: "C" + std::to_string(j);
std::string col_name = j < problem_.get_variable_names().size()
? problem_.get_variable_names()[j]
: "C" + std::to_string(j);
std::string lower_bound_str = variable_types[j] == 'I' ? "LI" : "LO";
std::string upper_bound_str = variable_types[j] == 'I' ? "UI" : "UP";

if (variable_lower_bounds[j] == -std::numeric_limits<f_t>::infinity() &&
variable_upper_bounds[j] == std::numeric_limits<f_t>::infinity()) {
mps_file << " FR BOUND1 " << col_name << "\n";
}
// Ambiguity exists in the spec about the case where upper_bound == 0 and lower_bound == 0, and
// only UP is specified. Handle fixed variables explicitely to avoid this pitfall.
else if (variable_lower_bounds[j] == variable_upper_bounds[j]) {
mps_file << " FX BOUND1 " << col_name << " " << variable_lower_bounds[j] << "\n";
} else {
if (variable_lower_bounds[j] != 0.0 || objective_coefficients[j] == 0.0 ||
variable_types[j] != 'C') {
if (variable_lower_bounds[j] != 0.0) {
if (variable_lower_bounds[j] == -std::numeric_limits<f_t>::infinity()) {
mps_file << " MI BOUND1 " << col_name << "\n";
} else {
mps_file << " LO BOUND1 " << col_name << " " << variable_lower_bounds[j] << "\n";
mps_file << " " << lower_bound_str << " BOUND1 " << col_name << " "
<< variable_lower_bounds[j] << "\n";
}
}
if (variable_upper_bounds[j] != std::numeric_limits<f_t>::infinity()) {
mps_file << " UP BOUND1 " << col_name << " " << variable_upper_bounds[j] << "\n";
// Integer variables get different default bounds compared to continuous variables
if (variable_upper_bounds[j] != std::numeric_limits<f_t>::infinity() ||
variable_types[j] == 'I') {
mps_file << " " << upper_bound_str << " BOUND1 " << col_name << " "
<< variable_upper_bounds[j] << "\n";
}
}
}
Expand Down
100 changes: 75 additions & 25 deletions cpp/src/linear_programming/cuopt_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cuopt/linear_programming/solve.hpp>
#include <cuopt/linear_programming/solver_settings.hpp>
#include <cuopt/utilities/timestamp_utils.hpp>
#include <linear_programming/cuopt_c_internal.hpp>
#include <utilities/logger.hpp>

#include <mps_parser/parser.hpp>
Expand All @@ -24,31 +25,6 @@
using namespace cuopt::mps_parser;
using namespace cuopt::linear_programming;

struct problem_and_stream_view_t {
problem_and_stream_view_t()
: op_problem(nullptr), stream_view(rmm::cuda_stream_per_thread), handle(stream_view)
{
}
raft::handle_t* get_handle_ptr() { return &handle; }
cuopt::linear_programming::optimization_problem_t<cuopt_int_t, cuopt_float_t>* op_problem;
rmm::cuda_stream_view stream_view;
raft::handle_t handle;
};

struct solution_and_stream_view_t {
solution_and_stream_view_t(bool solution_for_mip, rmm::cuda_stream_view stream_view)
: is_mip(solution_for_mip),
mip_solution_ptr(nullptr),
lp_solution_ptr(nullptr),
stream_view(stream_view)
{
}
bool is_mip;
mip_solution_t<cuopt_int_t, cuopt_float_t>* mip_solution_ptr;
optimization_problem_solution_t<cuopt_int_t, cuopt_float_t>* lp_solution_ptr;
rmm::cuda_stream_view stream_view;
};

int8_t cuOptGetFloatSize() { return sizeof(cuopt_float_t); }

int8_t cuOptGetIntSize() { return sizeof(cuopt_int_t); }
Expand Down Expand Up @@ -92,6 +68,26 @@ cuopt_int_t cuOptReadProblem(const char* filename, cuOptOptimizationProblem* pro
return CUOPT_SUCCESS;
}

cuopt_int_t cuOptWriteProblem(cuOptOptimizationProblem problem,
const char* filename,
cuopt_int_t format)
{
if (problem == nullptr) { return CUOPT_INVALID_ARGUMENT; }
if (filename == nullptr) { return CUOPT_INVALID_ARGUMENT; }
if (strlen(filename) == 0) { return CUOPT_INVALID_ARGUMENT; }
if (format != CUOPT_FILE_FORMAT_MPS) { return CUOPT_INVALID_ARGUMENT; }

problem_and_stream_view_t* problem_and_stream_view =
static_cast<problem_and_stream_view_t*>(problem);
try {
problem_and_stream_view->op_problem->write_to_mps(std::string(filename));
} catch (const std::exception& e) {
CUOPT_LOG_INFO("Error writing MPS file: %s", e.what());
return CUOPT_MPS_FILE_ERROR;
}
return CUOPT_SUCCESS;
}

cuopt_int_t cuOptCreateProblem(cuopt_int_t num_constraints,
cuopt_int_t num_variables,
cuopt_int_t objective_sense,
Expand Down Expand Up @@ -706,6 +702,60 @@ cuopt_int_t cuOptGetFloatParameter(cuOptSolverSettings settings,
return CUOPT_SUCCESS;
}

cuopt_int_t cuOptSetInitialPrimalSolution(cuOptSolverSettings settings,
const cuopt_float_t* primal_solution,
cuopt_int_t num_variables)
{
if (settings == nullptr) { return CUOPT_INVALID_ARGUMENT; }
if (primal_solution == nullptr) { return CUOPT_INVALID_ARGUMENT; }
if (num_variables <= 0) { return CUOPT_INVALID_ARGUMENT; }

solver_settings_t<cuopt_int_t, cuopt_float_t>* solver_settings =
static_cast<solver_settings_t<cuopt_int_t, cuopt_float_t>*>(settings);
try {
solver_settings->set_initial_pdlp_primal_solution(primal_solution, num_variables);
} catch (const std::exception& e) {
return CUOPT_INVALID_ARGUMENT;
}
return CUOPT_SUCCESS;
}

cuopt_int_t cuOptSetInitialDualSolution(cuOptSolverSettings settings,
const cuopt_float_t* dual_solution,
cuopt_int_t num_constraints)
{
if (settings == nullptr) { return CUOPT_INVALID_ARGUMENT; }
if (dual_solution == nullptr) { return CUOPT_INVALID_ARGUMENT; }
if (num_constraints <= 0) { return CUOPT_INVALID_ARGUMENT; }

solver_settings_t<cuopt_int_t, cuopt_float_t>* solver_settings =
static_cast<solver_settings_t<cuopt_int_t, cuopt_float_t>*>(settings);
try {
solver_settings->set_initial_pdlp_dual_solution(dual_solution, num_constraints);
} catch (const std::exception& e) {
return CUOPT_INVALID_ARGUMENT;
}
return CUOPT_SUCCESS;
}

cuopt_int_t cuOptAddMIPStart(cuOptSolverSettings settings,
const cuopt_float_t* solution,
cuopt_int_t num_variables)
{
if (settings == nullptr) { return CUOPT_INVALID_ARGUMENT; }
if (solution == nullptr) { return CUOPT_INVALID_ARGUMENT; }
if (num_variables <= 0) { return CUOPT_INVALID_ARGUMENT; }

solver_settings_t<cuopt_int_t, cuopt_float_t>* solver_settings =
static_cast<solver_settings_t<cuopt_int_t, cuopt_float_t>*>(settings);
try {
solver_settings->get_mip_settings().add_initial_solution(solution, num_variables);
} catch (const std::exception& e) {
return CUOPT_INVALID_ARGUMENT;
}
return CUOPT_SUCCESS;
}

cuopt_int_t cuOptIsMIP(cuOptOptimizationProblem problem, cuopt_int_t* is_mip_ptr)
{
if (problem == nullptr) { return CUOPT_INVALID_ARGUMENT; }
Expand Down
46 changes: 46 additions & 0 deletions cpp/src/linear_programming/cuopt_c_internal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* clang-format off */
/*
* SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* clang-format on */

#pragma once

#include <cuopt/linear_programming/cuopt_c.h>
#include <cuopt/linear_programming/mip/solver_solution.hpp>
#include <cuopt/linear_programming/optimization_problem.hpp>
#include <cuopt/linear_programming/pdlp/solver_solution.hpp>

#include <raft/core/handle.hpp>

#include <rmm/cuda_stream_view.hpp>

namespace cuopt::linear_programming {

struct problem_and_stream_view_t {
problem_and_stream_view_t()
: op_problem(nullptr), stream_view(rmm::cuda_stream_per_thread), handle(stream_view)
{
}
raft::handle_t* get_handle_ptr() { return &handle; }
optimization_problem_t<cuopt_int_t, cuopt_float_t>* op_problem;
rmm::cuda_stream_view stream_view;
raft::handle_t handle;
};

struct solution_and_stream_view_t {
solution_and_stream_view_t(bool solution_for_mip, rmm::cuda_stream_view stream_view)
: is_mip(solution_for_mip),
mip_solution_ptr(nullptr),
lp_solution_ptr(nullptr),
stream_view(stream_view)
{
}
bool is_mip;
mip_solution_t<cuopt_int_t, cuopt_float_t>* mip_solution_ptr;
optimization_problem_solution_t<cuopt_int_t, cuopt_float_t>* lp_solution_ptr;
rmm::cuda_stream_view stream_view;
};

} // namespace cuopt::linear_programming
Loading
Loading