Repository navigation
docs: add Python async gRPC client guide under gRPC remote execution #1653
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
912a847
docs: add Python async gRPC client guide under gRPC remote execution
tmckayus 3240c02
apply coderabbit feedback on grpc doc changes
tmckayus 1093965
apply feedback on gRPC doc PR (#1653)
tmckayus 1d8c0a3
Merge branch 'main' into grpcdocs
tmckayus 366d2fd
fix doc error introduced in MILP -> MIP normalization
tmckayus 8d56e0e
fix docs build: blank line before section, resolvable xrefs
ramakrishnap-nv 7b0302c
Merge branch 'main' into grpcdocs
ramakrishnap-nv 25af4df
docs: capitalize 'Connect and Solve' heading
ramakrishnap-nv 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /* Rubric styled closer to an H2/H3 for in-page subheads that must stay out of the TOC. */ | ||
| p.rubric.large-rubric { | ||
| font-size: 1.4em; | ||
| font-weight: 600; | ||
| margin-top: 1.75rem; | ||
| margin-bottom: 0.75rem; | ||
| } |
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
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
98 changes: 98 additions & 0 deletions
98
docs/cuopt/source/cuopt-grpc/examples/incumbent_stream_demo.py
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,98 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """MIP incumbent streaming via the Python async gRPC client. | ||
|
|
||
| Same callback registration as a local solve | ||
| (:meth:`SolverSettings.set_mip_callback`), plus | ||
| :meth:`Client.start_incumbent_stream` so those callbacks fire while the | ||
| remote job runs. | ||
|
|
||
| Start the server first:: | ||
|
|
||
| cuopt_grpc_server --port 50051 --workers 1 | ||
|
|
||
| Then:: | ||
|
|
||
| python incumbent_stream_demo.py --host localhost --port 50051 | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import sys | ||
|
|
||
| from cuopt.grpc.linear_programming import Client, JobStatus | ||
| from cuopt.linear_programming.internals import GetSolutionCallback | ||
| from cuopt.linear_programming.problem import INTEGER, MAXIMIZE, Problem | ||
| from cuopt.linear_programming.solver_settings import SolverSettings | ||
|
|
||
|
|
||
| class IncumbentPrinter(GetSolutionCallback): | ||
| """Same callback type used for local ``problem.solve(settings)``.""" | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.entries = [] | ||
|
|
||
| def get_solution(self, solution, solution_cost, solution_bound, user_data): | ||
| cost = float(solution_cost[0]) | ||
| values = solution.tolist() | ||
| self.entries.append({"cost": cost, "solution": values}) | ||
| print(f"incumbent cost={cost:.4f} values={values}", flush=True) | ||
|
|
||
|
|
||
| def build_problem(): | ||
| problem = Problem("incumbent_stream_demo") | ||
| x = problem.addVariable(lb=0, ub=10, vtype=INTEGER, name="x") | ||
| y = problem.addVariable(lb=0, ub=10, vtype=INTEGER, name="y") | ||
| problem.addConstraint(x + y <= 10, name="c1") | ||
| problem.addConstraint(x - y >= 0, name="c2") | ||
| problem.setObjective(x + 2 * y, sense=MAXIMIZE) | ||
| return problem | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def main(argv=None): | ||
|
tmckayus marked this conversation as resolved.
Resolved
|
||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--host", default="localhost") | ||
| parser.add_argument("--port", type=int, default=50051) | ||
| args = parser.parse_args(argv) | ||
|
|
||
| printer = IncumbentPrinter() | ||
| settings = SolverSettings() | ||
| # Local solves: register callback, then problem.solve(settings). | ||
| # Remote: same registration — submit enables server-side tracking, and | ||
| # start_incumbent_stream(..., settings=settings) delivers into printer. | ||
| settings.set_mip_callback(printer, None) | ||
| settings.set_parameter("time_limit", 30) | ||
|
|
||
| problem = build_problem() | ||
| client = Client(args.host, args.port) | ||
| job_id = client.submit(problem, settings) | ||
| client.start_incumbent_stream(job_id, settings=settings) | ||
|
|
||
| status = client.wait(job_id, timeout=120) | ||
| if status != JobStatus.COMPLETED: | ||
| print(f"unexpected status: {status}", file=sys.stderr) | ||
| return 1 | ||
| client.join_incumbent_stream(job_id) | ||
|
|
||
| names = [v.getVariableName() for v in problem.getVariables()] | ||
| solution = client.result(job_id, variable_names=names) | ||
| print( | ||
| f"final reason={solution.get_termination_reason()} " | ||
| f"obj={solution.get_primal_objective()}" | ||
| ) | ||
| client.delete(job_id) | ||
|
tmckayus marked this conversation as resolved.
Resolved
|
||
|
|
||
| if not printer.entries: | ||
| print( | ||
| "no incumbents delivered via settings callbacks", file=sys.stderr | ||
| ) | ||
| return 1 | ||
| print(f"OK ({len(printer.entries)} incumbent(s))") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
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
Oops, something went wrong.
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.
Something I noticed: MIP and MILP still seem to be used interchangeably throughout the docs? Is that on purpose?
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.
normalized on MIP, and in #1655 normalized references in the other docs as well. Thanks!