Skip to main content

A Python client and command-line

Project description

cuOpt Self-hosted Service Python Client

The cuOpt Self-hosted Service Python Client is a Python interface to enable access to NVIDIA cuOpt running on user-managed hardware.

Install cuopt-sh-client via pip

$ pip install .

Run client using the CLI

After installation, the command-line utility can be accessed using

$ cuopt_sh cuopt_problem_data.json

If a problem times out, a request id will be returned. The id may be passed back to the CLI to re-poll as shown. This may be done any number of times until a result is returned.

$ cuopt_sh cuopt_problem_data.json
Request timed out.
Re-check the status with the following command:
cuopt_sh '{"reqId": "78238a58-d052-40b4-8dae-852be8f7906e"}'

$ cuopt_sh '{"reqId": "78238a58-d052-40b4-8dae-852be8f7906e"}'
{'response': ...}

List of Arguments:

Check the help with 'cuopt_sh -h' for more detailed information.

  data:   cuOpt problem data file or a request id to repoll. If the -f option is used, this indicates the path of a file accessible to the server.
    -f:   Indicates that the DATA argument is the relative path of a cuOpt data file under the server's data directory.
    -o:   Override the default name of the result file if the server has been configured with a results directory.
-pt:  Number of seconds to poll for a result before timing out and returning a request id to re-query. Defaults to 120.
    -i:   Host address of the cuOpt server (default 0.0.0.0)
    -p:   Port of the cuOpt server (default 5000)
    -s:   Use ssl (default False)
    -c:   Path to self signed certificates only, skip for standard certificates (default "") 
    -l:   Log level (critical,error,warning,info,debug)
-ov:  If set, only validates input

Best practice: Using the local data file options (-f and -o)

Data describing a cuOpt problem can be quite large, and performance in most cases will be much better if the server can read files from its local filesystem instead of receiving them over HTTP. To use this feature do the following:

  • Configure the server to use a local data directory (see below)
  • Write the cuOpt data file to the server's local data directory. The file may be at the root of the data directory, or it may be placed at a path somewhere under the data directory.
  • Specify the filename using the -f option to the cuOpt CLI. Give the path of the data file relative to the root of the server's local data directory.

Example:

# Copy the file to the server's data directory with scp (or some other mechanism)
$ scp cuopt_problem_data.json my_server:/path/of/cuOpt/data/directory/

# Indicate to the CLI that the data is in the server's local data directory with the name 'cuopt_problem_data.json'. Note that a relative path is used.
$ cuopt_sh -f cuopt_problem_data.json

The cuOpt results are usually much smaller than the input data and can easily be returned over HTTP. However, it may also be convenient to have results written to output files as well. To use this feature do the following:

  • Configure the server to use a local results directory and set the result size threshold for writing the results to disk (see below)
  • Use the -o option to specify a specific name for the result file, or let the server use a default name.
  • Read the result from the file indicated in cuOpt's response.

Example:

$ cuopt_sh -f cuopt_problem_data.json
{'result_file': 'cuopt_problem_data.json.result'}

$ scp my_server:/path/of/cuOpt/results/directory/cuopt_problem_data.json.result .

Data compression

The cuOpt server can accept data that has been compressed with the Python msgpack library or the Python zlib library. For the highest efficiency, it is recommended that JSON data be generated with Python and written to a file using msgpack. However, if the data is not written with msgpack, it can still be compressed later using msgpack or zlib, or left uncompressed.

Run client directly from Python

Initialize the CuOptServiceSelfHostClient with optional ip, port, and use_ssl args

	from cuopt_sh_client import CuOptServiceSelfHostClient
        cuopt_service_client = CuOptServiceSelfHostClient(ip, port)

Get optimized routes

To submit a cuOpt data file over HTTP

        # Send cuOpt data over HTTP
	# Data may be the path to a file OR a dictionary containing
	# a cuOpt problem
        optimized_routes = cuopt_service_client.get_optimized_routes(
            path_to_cuopt_problem_data
        )

To specify a data file that is located on the server in the server's data directory

        # Tell the cuOpt server that the data is in its local data directory
        optimized_routes = cuopt_service_client.get_optimized_routes(
            relative_path_under_servers_data_directory, filepath = True
        )

The problem data file should contain a JSON object with the following details

    cost_waypoint_graph_data
    travel_time_waypoint_graph_data
    cost_matrix_data
    travel_time_matrix_data
    fleet_data
    task_data
    solver_config

An example data file 'cuopt_problem_data.json' is included with this client.

For more details see https://docs.nvidia.com/cuopt/user-guide/serv-api.html

Configuring the cuOpt Server for the Local File Feature

By default, the local file feature is not enabled in the cuOpt server. To configure the feature, set the environment variables described below in the server's container environment.

Environment variables

To enable reading of cuOpt data files from the local filesystem, set the following

  • CUOPT_DATA_DIR: the absolute path of a directory in the cuOpt server's container environment. Typically this path is the mount point for a volume that exists outside of the container.

To enable writing of cuOpt data files to the local filesystem, set

  • CUOPT_RESULT_DIR: the absolute path of a directory in the cuOpt server's container environment. Typically this path is the mount point for a volume that exists outside of the container.
  • CUOPT_MAX_RESULT: the maximum size in kilobytes of a result that cuOpt will return over HTTP. To have all results written to disk, set this value to 0. The default is 250.
  • CUOPT_RESULT_MODE: the Linux file mode (as with the chmod command) to apply to result files created by cuOpt. The default is 644.

Docker example

In this example, we run the image cuoptimage and mount the local directories ./data and ./results on the container at /cuopt_data and /cuopt_resulst respectively. We set the environment variables to tell cuOpt where the data and results directories are, and to write all results to files instead of HTTP (CUOPT_MAX_RESULT=0).

$ docker run --rm --gpus=all --network=host -v `pwd`/data:/cuopt_data  -v `pwd`/results:/cuopt_results -e CUOPT_DATA_DIR=/cuopt_data -e CUOPT_RESULT_DIR=/cuopt_results -e CUOPT_MAX_RESULT=0 -it cuoptimage

Directory permissions

The data and results directories mounted on the cuOpt container need to be readable and writable by the container user, and also have the execute permission set. If they are not, the container will print an error message and exit. Be careful to set permissions correctly on those directories before running the cuOpt server.

When running cuOpt with docker and using the default container user:

  • the cuOpt data directory should be readable and executable by group 0 (ie the root group)
  • the cuOpt results directory should be writable and executable by group 0

When running cuOpt with docker and using the --user flag to set only the UID:

  • the cuOpt data directory should be readable and executable by group 0 or the specified UID
  • the cuOpt results directory should be writable and executable by group 0 or the specified UID

When running cuOpt with docker and using the --user flag to set both the UID and GID:

  • the cuOpt data directory should be readable and executable by the specified UID or the specified GID
  • the cuOpt results directory should be writable and executable by the specified UID or the specified GID

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cuopt_sh_client-24.11.0.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file cuopt_sh_client-24.11.0.tar.gz.

File metadata

  • Download URL: cuopt_sh_client-24.11.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for cuopt_sh_client-24.11.0.tar.gz
Algorithm Hash digest
SHA256 4079fdfffc777b2c1509cf7bf62ce4a21ddec25d0dcb21d1de98dc955237b7b1
MD5 83dfadd01509b9e4c2752d5f343abd9c
BLAKE2b-256 845f05f5c999048f71cdaa4ec1952aaa5ffee786ebac96341c4bb1b603057344

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page