-
Notifications
You must be signed in to change notification settings - Fork 115
Add C api callbacks for getting and setting solutions #779
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.02
Are you sure you want to change the base?
Changes from all commits
943b632
41a3b68
1e7a2e5
fda3f4f
4e50db4
826b3c0
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| /* clang-format off */ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| /* clang-format on */ | ||
|
|
@@ -681,6 +681,56 @@ cuopt_int_t cuOptGetFloatParameter(cuOptSolverSettings settings, | |
| const char* parameter_name, | ||
| cuopt_float_t* parameter_value); | ||
|
|
||
| /** | ||
| * @brief Callback for receiving incumbent MIP solutions with user context. | ||
| * | ||
| * @param[in] solution - Device pointer to incumbent solution values. | ||
| * @param[in] objective_value - Device pointer to incumbent objective value. | ||
|
Comment on lines
+687
to
+688
Contributor
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. I wonder if these should be host pointers instead, in terms of API design. So far the API has been abstracting away the reality of the CUDA implementation (such as the corresponding context, the streams used...) away from the user, so this may represent a break in the philosophy. Plus we don't expose the GPU problem structure either, so it is likely the user will have to copy it back to the host either way to do any useful work
Contributor
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. I agree. The rest of the API uses abstracts out the device. Probably we should provide a host pointer here. |
||
| * @param[in] user_data - User context pointer. | ||
|
Contributor
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. Maybe |
||
| */ | ||
| typedef void (*cuOptMipGetSolutionCallback)(const cuopt_float_t* solution, | ||
|
Contributor
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. In other parts of the C API we capitalize MIP. So I would call this |
||
| const cuopt_float_t* objective_value, | ||
| void* user_data); | ||
|
|
||
| /** | ||
| * @brief Callback for injecting MIP solutions with user context. | ||
| * | ||
| * @param[out] solution - Device pointer to solution values to set. | ||
|
Contributor
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. We probably should mention the dimension of the Also, do we crush the solution from the original variables into the presolve variables when MIP presolve is turned on?
Contributor
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. And this should probably be a host pointer rather than a device pointer |
||
| * @param[out] objective_value - Device pointer to objective value to set. | ||
| * @param[in] user_data - User context pointer. | ||
| */ | ||
| typedef void (*cuOptMipSetSolutionCallback)(cuopt_float_t* solution, | ||
|
Contributor
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. Same here: I would call this |
||
| cuopt_float_t* objective_value, | ||
| void* user_data); | ||
|
|
||
| /** | ||
| * @brief Register a callback to receive incumbent MIP solutions. | ||
| * | ||
| * @param[in] settings - The solver settings object. | ||
| * @param[in] callback - Callback function to receive incumbent solutions. | ||
| * @param[in] user_data - User-defined pointer passed through to the callback. | ||
| * It will be forwarded to ``cuOptMipGetSolutionCallback`` when invoked. | ||
| * | ||
| * @return A status code indicating success or failure. | ||
| */ | ||
| cuopt_int_t cuOptSetMipGetSolutionCallback(cuOptSolverSettings settings, | ||
|
Contributor
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. Change to |
||
| cuOptMipGetSolutionCallback callback, | ||
| void* user_data); | ||
|
|
||
| /** | ||
| * @brief Register a callback to inject MIP solutions. | ||
| * | ||
| * @param[in] settings - The solver settings object. | ||
| * @param[in] callback - Callback function to inject solutions. | ||
| * @param[in] user_data - User-defined pointer passed through to the callback. | ||
| * It will be forwarded to ``cuOptMipSetSolutionCallback`` when invoked. | ||
| * | ||
| * @return A status code indicating success or failure. | ||
| */ | ||
| cuopt_int_t cuOptSetMipSetSolutionCallback(cuOptSolverSettings settings, | ||
|
Contributor
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. Change to |
||
| cuOptMipSetSolutionCallback callback, | ||
| void* user_data); | ||
|
|
||
| /** @brief Check if an optimization problem is a mixed integer programming problem. | ||
| * | ||
| * @param[in] problem - The optimization problem. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,16 +31,20 @@ class base_solution_callback_t : public Callback { | |
| this->n_variables = n_variables_; | ||
| } | ||
|
|
||
| void set_user_data(void* user_data_) { user_data = user_data_; } | ||
|
Contributor
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. Nit: Google C++ style guide and other common conventions reserves the underscore postfix to refer to the protected member variable. So it's a bit confusing to have |
||
| void* get_user_data() const { return user_data; } | ||
|
|
||
akifcorduk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| virtual base_solution_callback_type get_type() const = 0; | ||
|
|
||
| protected: | ||
| bool isFloat = true; | ||
| size_t n_variables = 0; | ||
| void* user_data = nullptr; | ||
| }; | ||
|
|
||
| class get_solution_callback_t : public base_solution_callback_t { | ||
| public: | ||
| virtual void get_solution(void* data, void* objective_value) = 0; | ||
| virtual void get_solution(void* data, void* objective_value, void* user_data) = 0; | ||
| base_solution_callback_type get_type() const override | ||
| { | ||
| return base_solution_callback_type::GET_SOLUTION; | ||
|
|
@@ -49,7 +53,7 @@ class get_solution_callback_t : public base_solution_callback_t { | |
|
|
||
| class set_solution_callback_t : public base_solution_callback_t { | ||
| public: | ||
| virtual void set_solution(void* data, void* objective_value) = 0; | ||
| virtual void set_solution(void* data, void* objective_value, void* user_data) = 0; | ||
| base_solution_callback_type get_type() const override | ||
| { | ||
| return base_solution_callback_type::SET_SOLUTION; | ||
|
|
||
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.
Nit: I would say
Signature of callback for receiving ...orType of callback for receiving ...so it is clear to the reader that this is declaring the type.