Skip to content

fix: write empty constraint rows - #1946

Open
neuriv wants to merge 1 commit into
NVIDIA:mainfrom
neuriv:fix/rows
Open

neuriv wants to merge 1 commit into
NVIDIA:mainfrom
neuriv:fix/rows

Conversation

@neuriv

@neuriv neuriv commented Sep 18, 2026

Copy link
Copy Markdown

Description

Fixes #1663.

Iterate over the available CSR row-offset pairs when writing matrix coefficients. Keep writing constraint rows and bounds independently, preserving constant-only constraints even when the coefficient matrix is empty.

Testing

u22.04, 1xa100, cuda 12.9.86

cmake --build "$build" --target NUMOPT_INTERNAL_TEST --parallel 20
export RAPIDS_DATASET_ROOT_DIR="$PWD/datasets"
"$build/tests/internal/NUMOPT_INTERNAL_TEST" \
  --gtest_filter='mps_roundtrip.linear_programming_basic'

python -m pytest -q -r a \
  python/cuopt/cuopt/tests/linear_programming/test_python_API.py::test_read_write_mps_and_relaxation

pre-commit run --all-files
unit test

script passed.

#!/usr/bin/env bash
set -euo pipefail
repo=$(cd "${1:?provide the cuopt checkout}" && pwd)
shift
test_dir=$(mktemp -d)
trap 'rm -rf "$test_dir"' EXIT
cat > "$test_dir/unit_test.cpp" <<'CPP'
#include <cuopt/mathematical_optimization/io/mps_writer.hpp>
#include <mps_parser_internal.hpp>

#include <cassert>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

using namespace cuopt::mathematical_optimization::io;

template <typename T>
void check(const std::string& path, bool set_offsets)
{
  std::vector<T> objective{1}, lower{0}, upper{1}, rhs{1};
  std::vector<char> types{'L'};
  std::vector<int> offsets{0, 0};
  data_model_view_t<int, T> view;
  view.set_objective_coefficients(objective.data(), 1);
  view.set_variable_lower_bounds(lower.data(), 1);
  view.set_variable_upper_bounds(upper.data(), 1);
  view.set_constraint_bounds(rhs.data(), 1);
  view.set_row_types(types.data(), 1);
  view.set_variable_names({"x"});
  view.set_row_names({"empty"});
  if (set_offsets) view.set_csr_constraint_matrix(nullptr, 0, nullptr, 0, offsets.data(), 2);
  mps_writer_t<int, T> writer(view);
  writer.write(path);
  std::ifstream file(path);
  std::string text{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
  assert(text.find(" L  empty\n") != std::string::npos);
  assert(text.find("x OBJ 1\n") != std::string::npos);
  assert(text.find("empty 1\n") != std::string::npos);
  assert(text.find("UP BOUND1    x 1\n") != std::string::npos);
  std::cout << "PASS " << path << '\n';
}

void roundtrip()
{
  std::string_view input = R"(NAME empty_rows
ROWS
 N  OBJ
 L  empty_le
 G  empty_ge
 E  empty_eq
COLUMNS
    x OBJ 1
RHS
    RHS1 empty_le 1
    RHS1 empty_ge 1
BOUNDS
 UP BND1 x 1
ENDATA
)";
  mps_data_model_t<int, double> original;
  mps_parser_t<int, double> parsed(original, input, false);
  assert(original.get_constraint_matrix_values().empty());
  assert(original.get_n_constraints() == 3);
  std::string path = "roundtrip.mps";
  mps_writer_t<int, double> writer(original);
  writer.write(path);
  mps_data_model_t<int, double> reloaded;
  mps_parser_t<int, double> reparsed(reloaded, path, false);
  assert(reloaded.get_constraint_matrix_values().empty());
  assert(reloaded.get_constraint_matrix_offsets() == original.get_constraint_matrix_offsets());
  assert(reloaded.get_constraint_bounds() == original.get_constraint_bounds());
  assert(reloaded.get_constraint_lower_bounds() == original.get_constraint_lower_bounds());
  assert(reloaded.get_constraint_upper_bounds() == original.get_constraint_upper_bounds());
  assert(reloaded.get_row_names() == original.get_row_names());
  assert(reloaded.get_variable_names() == original.get_variable_names());
  assert(reloaded.get_variable_lower_bounds() == original.get_variable_lower_bounds());
  assert(reloaded.get_variable_upper_bounds() == original.get_variable_upper_bounds());
  assert(reloaded.get_objective_coefficients() == original.get_objective_coefficients());
  assert(reloaded.get_n_constraints() == 3);
  std::cout << "PASS constant-row roundtrip\n";
}

int main(int argc, char** argv)
{
  const bool set_offsets = argc > 1 && std::string(argv[1]) == "offsets";
  check<double>("empty-double.mps", set_offsets);
  check<float>("empty-float.mps", set_offsets);
  if (!set_offsets) roundtrip();
}
CPP
"${CXX:-c++}" -std=c++20 -g -O1 -fsanitize=address,undefined "$@" \
  -I"$repo/cpp/include" -I"$repo/cpp/src" -I"$repo/cpp/src/io" \
  "$test_dir/unit_test.cpp" "$repo/cpp/src/io/mps_writer.cpp" \
  "$repo/cpp/src/io/mps_data_model.cpp" "$repo/cpp/src/io/data_model_view.cpp" \
  "$repo/cpp/src/io/mps_parser.cpp" "$repo/cpp/src/io/file_to_string.cpp" \
  -o "$test_dir/unit-test"
cd "$test_dir"
./unit-test
./unit-test offsets

Signed-off-by: neuriv <330472862+neuriv@users.noreply.github.com>
@copy-pr-bot

copy-pr-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@neuriv
neuriv marked this pull request as ready for review September 18, 2026 22:53
@neuriv
neuriv requested a review from a team as a code owner September 18, 2026 22:53
@coderabbitai

coderabbitai Bot commented Sep 18, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: NVIDIA/cuopt/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 83a442d6-7ebf-4851-b14c-ea1e1422bd34

📥 Commits

Reviewing files that changed from the base of the PR and between 1d591b2 and 25ca2cf.

📒 Files selected for processing (1)
  • cpp/src/io/mps_writer.cpp

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.


📝 Walkthrough

Walkthrough

The MPS writer now traverses constraint-matrix rows from CSR offsets. This prevents invalid access when the matrix contains an empty row.

Changes

MPS writer fix

Layer / File(s) Summary
CSR row traversal
cpp/src/io/mps_writer.cpp
The constraint-matrix loop uses the number of CSR rows derived from constraint_matrix_offsets instead of n_constraints.

Priority: ➖ Normal

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: yuwenchen95

Merge Risk: ⚪ Minimal · up to 25ca2

The MPS writer now preserves constant-only constraints without the reported empty-row crash, and no unresolved merge-blocking risk was identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main fix: writing empty constraint rows in MPS output.
Description check ✅ Passed The description directly explains the empty-row fix, linked issue, implementation, and validation steps.
Linked Issues check ✅ Passed Issue #1663 requires MPS writing to avoid a segmentation fault for an empty constraint row and to preserve the valid row. The only PR change iterates over available CSR offset pairs with `csr_row + 1 …
Out of Scope Changes check ✅ Passed The complete PR diff changes one loop bound in cpp/src/io/mps_writer.cpp. The change is directly connected to the empty-row MPS crash in issue #1663. No unrelated source, API, or repository changes …
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] segfault when writing MPS with an empty row

1 participant