-
Notifications
You must be signed in to change notification settings - Fork 140
Write quadratic terms to mps file #949
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
rg20
wants to merge
5
commits into
NVIDIA:main
Choose a base branch
from
rg20:qp_improvements_new
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
137 changes: 137 additions & 0 deletions
137
cpp/libmps_parser/src/utilities/sparse_matrix_utils.hpp
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,137 @@ | ||
| /* clang-format off */ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| /* clang-format on */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <vector> | ||
|
|
||
| namespace cuopt::mps_parser { | ||
|
|
||
| /** | ||
| * @brief Symmetrize a CSR matrix by computing A + A^T | ||
| * | ||
| * Given a CSR matrix A, computes the symmetric matrix H = A + A^T. | ||
| * Diagonal entries are doubled (A[i,i] + A[i,i] = 2*A[i,i]). | ||
| * Off-diagonal entries are summed (H[i,j] = A[i,j] + A[j,i]). | ||
| * | ||
| * @tparam i_t Integer type for indices | ||
| * @tparam f_t Floating point type for values | ||
| * | ||
| * @param[in] in_values CSR values array | ||
| * @param[in] in_indices CSR column indices array | ||
| * @param[in] in_offsets CSR row offsets array (size = n_rows + 1) | ||
| * @param[in] n_rows Number of rows (and columns, assuming square matrix) | ||
| * @param[out] out_values Output CSR values | ||
| * @param[out] out_indices Output CSR column indices | ||
| * @param[out] out_offsets Output CSR row offsets | ||
| */ | ||
| template <typename i_t, typename f_t> | ||
| void symmetrize_csr(const f_t* in_values, | ||
| const i_t* in_indices, | ||
| const i_t* in_offsets, | ||
| i_t n_rows, | ||
| std::vector<f_t>& out_values, | ||
| std::vector<i_t>& out_indices, | ||
| std::vector<i_t>& out_offsets) | ||
| { | ||
| // Optimized 3-pass algorithm | ||
| // Pass 1: Count entries per row in A + A^T | ||
| std::vector<i_t> row_counts(n_rows, 0); | ||
| for (i_t i = 0; i < n_rows; ++i) { | ||
| for (i_t p = in_offsets[i]; p < in_offsets[i + 1]; ++p) { | ||
| i_t j = in_indices[p]; | ||
| row_counts[i]++; | ||
| if (i != j) { row_counts[j]++; } | ||
| } | ||
| } | ||
|
|
||
| // Build temporary offsets via prefix sum | ||
| std::vector<i_t> temp_offsets(n_rows + 1); | ||
| temp_offsets[0] = 0; | ||
| for (i_t i = 0; i < n_rows; ++i) { | ||
| temp_offsets[i + 1] = temp_offsets[i] + row_counts[i]; | ||
| } | ||
|
|
||
| i_t total_entries = temp_offsets[n_rows]; | ||
| std::vector<i_t> temp_indices(total_entries); | ||
| std::vector<f_t> temp_values(total_entries); | ||
|
|
||
| // Pass 2: Fill entries directly | ||
| std::vector<i_t> row_pos = temp_offsets; // Copy for tracking insertion positions | ||
|
|
||
| for (i_t i = 0; i < n_rows; ++i) { | ||
| for (i_t p = in_offsets[i]; p < in_offsets[i + 1]; ++p) { | ||
| i_t j = in_indices[p]; | ||
| f_t x = in_values[p]; | ||
|
|
||
| // Add entry (i, j) with value 2x for diagonal, x for off-diagonal | ||
| temp_indices[row_pos[i]] = j; | ||
| temp_values[row_pos[i]] = (i == j) ? (2 * x) : x; | ||
| row_pos[i]++; | ||
|
|
||
| // Add transpose entry (j, i) if off-diagonal | ||
| if (i != j) { | ||
| temp_indices[row_pos[j]] = i; | ||
| temp_values[row_pos[j]] = x; | ||
| row_pos[j]++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Pass 3: Deduplicate and build final CSR | ||
| std::vector<i_t> workspace(n_rows, -1); | ||
| out_offsets.resize(n_rows + 1); | ||
| out_indices.resize(total_entries); | ||
| out_values.resize(total_entries); | ||
|
|
||
| i_t nz = 0; | ||
| for (i_t i = 0; i < n_rows; ++i) { | ||
| i_t row_start_out = nz; | ||
| out_offsets[i] = row_start_out; | ||
|
|
||
| for (i_t p = temp_offsets[i]; p < temp_offsets[i + 1]; ++p) { | ||
| i_t j = temp_indices[p]; | ||
| f_t x = temp_values[p]; | ||
|
|
||
| if (workspace[j] >= row_start_out) { | ||
| out_values[workspace[j]] += x; | ||
| } else { | ||
| workspace[j] = nz; | ||
| out_indices[nz] = j; | ||
| out_values[nz] = x; | ||
| nz++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| out_offsets[n_rows] = nz; | ||
| out_indices.resize(nz); | ||
| out_values.resize(nz); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Symmetrize a CSR matrix using std::vector (convenience overload) | ||
| */ | ||
| template <typename i_t, typename f_t> | ||
| void symmetrize_csr(const std::vector<f_t>& in_values, | ||
| const std::vector<i_t>& in_indices, | ||
| const std::vector<i_t>& in_offsets, | ||
| std::vector<f_t>& out_values, | ||
| std::vector<i_t>& out_indices, | ||
| std::vector<i_t>& out_offsets) | ||
| { | ||
| i_t n_rows = static_cast<i_t>(in_offsets.size()) - 1; | ||
| symmetrize_csr(in_values.data(), | ||
| in_indices.data(), | ||
| in_offsets.data(), | ||
| n_rows, | ||
| out_values, | ||
| out_indices, | ||
| out_offsets); | ||
| } | ||
|
|
||
| } // namespace cuopt::mps_parser | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle edge case: empty
in_offsetsvector.When
in_offsetsis empty,in_offsets.size() - 1will underflow (for unsigned) or produce an unexpected value. Add a guard for this edge case.🛡️ Proposed fix
template <typename i_t, typename f_t> void symmetrize_csr(const std::vector<f_t>& in_values, const std::vector<i_t>& in_indices, const std::vector<i_t>& in_offsets, std::vector<f_t>& out_values, std::vector<i_t>& out_indices, std::vector<i_t>& out_offsets) { + if (in_offsets.empty()) { + out_values.clear(); + out_indices.clear(); + out_offsets.clear(); + return; + } i_t n_rows = static_cast<i_t>(in_offsets.size()) - 1; symmetrize_csr(in_values.data(), in_indices.data(), in_offsets.data(), n_rows, out_values, out_indices, out_offsets); }🤖 Prompt for AI Agents