-
Notifications
You must be signed in to change notification settings - Fork 147
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
base: release/26.04
Are you sure you want to change the base?
Changes from all commits
85902c3
bbdbbdb
e9f7a79
5b5a2bb
eb79da4
1b94b55
0f3ca8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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++; | ||
| } | ||
| } | ||
|
Comment on lines
+86
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Workspace sentinel The workspace is initialized with Either enforce signed types via 🛠️ Proposed fix using explicit sentinel+#include <limits>
+#include <type_traits>
+
// Pass 3: Deduplicate and build final CSR
- std::vector<i_t> workspace(n_rows, -1);
+ constexpr i_t SENTINEL = std::is_signed<i_t>::value
+ ? static_cast<i_t>(-1)
+ : std::numeric_limits<i_t>::max();
+ std::vector<i_t> workspace(n_rows, SENTINEL);
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) {
+ if (workspace[j] != SENTINEL && workspace[j] >= row_start_out) {
out_values[workspace[j]] += x;
} else {
workspace[j] = nz;
out_indices[nz] = j;
out_values[nz] = x;
nz++;
}
}
}Alternatively, add a static_assert to enforce signed index types: static_assert(std::is_signed<i_t>::value, "i_t must be a signed integer type");🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
Comment on lines
+119
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle edge case: empty When 🛡️ 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 |
||
|
|
||
| } // namespace cuopt::mps_parser | ||
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.
Don’t skip CSR setup when
A_valuesis emptyLine 43 should not gate on values. A valid zero-nnz CSR still needs offsets; otherwise
write()can readconstraint_matrix_offsets[row_id + 1]out of bounds for constrained-but-empty matrices.✅ Proposed fix
🤖 Prompt for AI Agents