Skip to content
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

[workspace] Enable SDP support in Clarabel #20475

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tools/workspace/clarabel_cpp_internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:drake_cc.bzl", "drake_cc_test")

drake_cc_test(
name = "clarabel_example_lp",
name = "clarabel_example_sdp",
srcs = select({
"//tools:with_clarabel": [
"test/clarabel_example_lp.cc",
"test/clarabel_example_sdp.cc",
],
"//conditions:default": [
"test/noop.cc",
Expand All @@ -21,7 +21,7 @@ drake_cc_test(

add_lint_tests(
cpplint_extra_srcs = [
"test/clarabel_example_lp.cc",
"test/clarabel_example_sdp.cc",
"test/noop.cc",
],
)
6 changes: 6 additions & 0 deletions tools/workspace/clarabel_cpp_internal/package.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,23 @@ rust_static_library(
name = "clarabel_cpp_rust_wrapper",
edition = "2021",
srcs = glob(["rust_wrapper/src/**/*.rs"], allow_empty = False),
crate_features = ["sdp"],
deps = all_crate_deps(),
)

# Combine the public headers with the object code.
cc_library(
name = "clarabel_cpp",
linkstatic = True,
defines = [
"FEATURE_SDP",
],
deps = [
":hdrs",
":clarabel_cpp_rust_wrapper",
"@blas",
"@eigen",
"@lapack",
],
visibility = ["//visibility:public"],
)
Expand Down
16 changes: 16 additions & 0 deletions tools/workspace/clarabel_cpp_internal/patches/sdp.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Clarabel.cpp] Enable the SDP feature by default

--- rust_wrapper/Cargo.toml
+++ rust_wrapper/Cargo.toml
@@ -14,9 +14,10 @@ opt-level = 3
lto = true
codegen-units = 1

[features]
+default = ["sdp"]
# Define features for SDP support in Clarabel.rs
-sdp = []
+sdp = ["clarabel/sdp"]
sdp-accelerate = ["sdp", "clarabel/sdp", "clarabel/sdp-accelerate"]
sdp-netlib = ["sdp", "clarabel/sdp", "clarabel/sdp-netlib"]
sdp-openblas = ["sdp", "clarabel/sdp", "clarabel/sdp-openblas"]
1 change: 1 addition & 0 deletions tools/workspace/clarabel_cpp_internal/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def clarabel_cpp_internal_repository(
build_file = ":package.BUILD.bazel",
patches = [
":patches/git_submodule.patch",
":patches/sdp.patch",
],
mirrors = mirrors,
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copied from (Apache-2.0):
// https://github.com/oxfordcontrol/Clarabel.cpp/blob/main/examples/cpp/example_lp.cpp
// https://github.com/oxfordcontrol/Clarabel.cpp/blob/main/examples/cpp/example_sdp.cpp
// https://github.com/oxfordcontrol/Clarabel.cpp/blob/main/examples/cpp/utils.h

#include <cstddef>
#include <cstdio>
#include <cmath>
#include <vector>

#include <Clarabel>
Expand Down Expand Up @@ -34,38 +33,47 @@ void print_solution(const clarabel::DefaultSolution<double>& solution) {

} // namespace

int main() {
Eigen::MatrixXd P_dense = Eigen::MatrixXd::Zero(2, 2);
Eigen::SparseMatrix<double> P = P_dense.sparseView();
P.makeCompressed();
int main(void) {
// SDP Example
int n = 3;
int nvec = (n * (n + 1)) >> 1;

Eigen::Vector<double, 2> q = {1.0, -1.0};
// 6 x 6 zero matrix
Eigen::SparseMatrix<double> P =
Eigen::MatrixXd::Zero(nvec, nvec).sparseView();
P.makeCompressed();

// a 2-d box constraint, separated into 4 inequalities.
// A = [I; -I]
Eigen::MatrixXd A_dense(4, 2);
A_dense << 1., 0., 0., 1., -1., 0., 0., -1.;
Eigen::Vector<double, 6> c{1, 0, 1, 0, 0, 1};

Eigen::MatrixXd A_dense(7, 6);
const double s = M_SQRT2;
// clang-format off
A_dense <<
-1, 0, 0, 0, 0, 0,
0, -s, 0, 0, 0, 0,
0, 0, -1, 0, 0, 0,
0, 0, 0, -s, 0, 0,
0, 0, 0, 0, -s, 0,
0, 0, 0, 0, 0, -1,
1, 4, 3, 8, 10, 6;
// clang-format on

Eigen::SparseMatrix<double> A = A_dense.sparseView();
A.makeCompressed();

Eigen::Vector<double, 4> b = {1.0, 1.0, 1.0, 1.0};
Eigen::Vector<double, 7> b = {0, 0, 0, 0, 0, 0, 1};

std::vector<clarabel::SupportedConeT<double>> cones{
clarabel::NonnegativeConeT<double>(4),
// {.tag = SupportedConeT<double>::Tag::NonnegativeConeT,
// .nonnegative_cone_t = {._0 = 4 }}
clarabel::PSDTriangleConeT<double>(n),
clarabel::ZeroConeT<double>(1),
};

// Settings
clarabel::DefaultSettings<double> settings =
clarabel::DefaultSettingsBuilder<double>::default_settings()
.equilibrate_enable(true)
.equilibrate_max_iter(50)
.build();
clarabel::DefaultSettings<double>::default_settings();

// Build solver
clarabel::DefaultSolver<double> solver(P, q, A, b, cones, settings);
clarabel::DefaultSolver<double> solver(P, c, A, b, cones, settings);

// Solve
solver.solve();
Expand Down
9 changes: 8 additions & 1 deletion tools/workspace/crate_universe/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
load("//tools/install:install.bzl", "install")
load("//tools/workspace/crate_universe:lock/archives.bzl", "ARCHIVES")
load("//tools/lint:lint.bzl", "add_lint_tests")
load("@rules_rust//crate_universe:defs.bzl", "crates_vendor")
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_vendor")

exports_files(glob(["lock/**"]))

# This is a maintainer-only tool for Drake Developers to manage our Rust
# dependencies. See README.md for instructions on how to run `upgrade.sh`.
crates_vendor(
name = "crate",
annotations = {
"clarabel": [crate.annotation(
patches = [
"@drake//tools/workspace/crate_universe:patches/clarabel_blas.patch", # noqa
],
)],
},
cargo_lockfile = ":lock/Cargo.toml.lock",
manifests = [
"@clarabel_cpp_internal//:rust_wrapper/Cargo.toml",
Expand Down
51 changes: 51 additions & 0 deletions tools/workspace/crate_universe/lock/archives.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ ARCHIVES = [
strip_prefix = "autocfg-1.1.0",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.autocfg-1.1.0.bazel"),
),
dict(
name = "crate__blas-0.22.0",
sha256 = "ae980f75c3215bfe8203c349b28149b0f4130a262e072913ccb55f877cd239dc",
type = "tar.gz",
urls = ["https://crates.io/api/v1/crates/blas/0.22.0/download"],
strip_prefix = "blas-0.22.0",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.blas-0.22.0.bazel"),
),
dict(
name = "crate__blas-sys-0.7.1",
sha256 = "13b1b279ceb25d7c4faaea95a5f7addbe7d8c34f9462044bd8e630cebcfc2440",
type = "tar.gz",
urls = ["https://crates.io/api/v1/crates/blas-sys/0.7.1/download"],
strip_prefix = "blas-sys-0.7.1",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.blas-sys-0.7.1.bazel"),
),
dict(
name = "crate__cfg-if-1.0.0",
sha256 = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd",
Expand All @@ -26,6 +42,9 @@ ARCHIVES = [
),
dict(
name = "crate__clarabel-0.6.0",
patches = [
"@drake//tools/workspace/crate_universe:patches/clarabel_blas.patch",
],
sha256 = "fb77b79847293795eb71eb995ea2222af9c67c6f13c8734bd56b02b8415164b6",
type = "tar.gz",
urls = ["https://crates.io/api/v1/crates/clarabel/0.6.0/download"],
Expand Down Expand Up @@ -120,6 +139,22 @@ ARCHIVES = [
strip_prefix = "itertools-0.11.0",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.itertools-0.11.0.bazel"),
),
dict(
name = "crate__lapack-0.19.0",
sha256 = "ad676a6b4df7e76a9fd80a0c50c619a3948d6105b62a0ab135f064d99c51d207",
type = "tar.gz",
urls = ["https://crates.io/api/v1/crates/lapack/0.19.0/download"],
strip_prefix = "lapack-0.19.0",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.lapack-0.19.0.bazel"),
),
dict(
name = "crate__lapack-sys-0.14.0",
sha256 = "447f56c85fb410a7a3d36701b2153c1018b1d2b908c5fbaf01c1b04fac33bcbe",
type = "tar.gz",
urls = ["https://crates.io/api/v1/crates/lapack-sys/0.14.0/download"],
strip_prefix = "lapack-sys-0.14.0",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.lapack-sys-0.14.0.bazel"),
),
dict(
name = "crate__lazy_static-1.4.0",
sha256 = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646",
Expand All @@ -128,6 +163,22 @@ ARCHIVES = [
strip_prefix = "lazy_static-1.4.0",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.lazy_static-1.4.0.bazel"),
),
dict(
name = "crate__libc-0.2.149",
sha256 = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b",
type = "tar.gz",
urls = ["https://crates.io/api/v1/crates/libc/0.2.149/download"],
strip_prefix = "libc-0.2.149",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.libc-0.2.149.bazel"),
),
dict(
name = "crate__num-complex-0.4.4",
sha256 = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214",
type = "tar.gz",
urls = ["https://crates.io/api/v1/crates/num-complex/0.4.4/download"],
strip_prefix = "num-complex-0.4.4",
build_file = Label("@drake//tools/workspace/crate_universe/lock/details:BUILD.num-complex-0.4.4.bazel"),
),
dict(
name = "crate__num-traits-0.2.17",
sha256 = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
###############################################################################
# @generated
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# tools/workspace/crate_universe/upgrade.sh
###############################################################################

load("@rules_rust//rust:defs.bzl", "rust_library")

package(default_visibility = ["//visibility:public"])

# licenses([
# "TODO", # Apache-2.0/MIT
# ])
filegroup(
name = "license_glob",
srcs = glob(
["LICENSE*"],
allow_empty = False,
),
)

rust_library(
name = "blas",
srcs = glob(["**/*.rs"]),
compile_data = glob(
include = ["**"],
exclude = [
"**/* *",
".tmp_git_root/**/*",
"BUILD",
"BUILD.bazel",
"WORKSPACE",
"WORKSPACE.bazel",
],
),
crate_root = "src/lib.rs",
edition = "2015",
rustc_flags = ["--cap-lints=allow"],
tags = [
"cargo-bazel",
"crate-name=blas",
"manual",
"noclippy",
"norustfmt",
],
target_compatible_with = select({
"@rules_rust//rust/platform:aarch64-apple-darwin": [],
"@rules_rust//rust/platform:aarch64-apple-ios": [],
"@rules_rust//rust/platform:aarch64-apple-ios-sim": [],
"@rules_rust//rust/platform:aarch64-fuchsia": [],
"@rules_rust//rust/platform:aarch64-linux-android": [],
"@rules_rust//rust/platform:aarch64-pc-windows-msvc": [],
"@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [],
"@rules_rust//rust/platform:arm-unknown-linux-gnueabi": [],
"@rules_rust//rust/platform:armv7-linux-androideabi": [],
"@rules_rust//rust/platform:armv7-unknown-linux-gnueabi": [],
"@rules_rust//rust/platform:i686-apple-darwin": [],
"@rules_rust//rust/platform:i686-linux-android": [],
"@rules_rust//rust/platform:i686-pc-windows-msvc": [],
"@rules_rust//rust/platform:i686-unknown-freebsd": [],
"@rules_rust//rust/platform:i686-unknown-linux-gnu": [],
"@rules_rust//rust/platform:powerpc-unknown-linux-gnu": [],
"@rules_rust//rust/platform:riscv32imc-unknown-none-elf": [],
"@rules_rust//rust/platform:riscv64gc-unknown-none-elf": [],
"@rules_rust//rust/platform:s390x-unknown-linux-gnu": [],
"@rules_rust//rust/platform:thumbv7em-none-eabi": [],
"@rules_rust//rust/platform:thumbv8m.main-none-eabi": [],
"@rules_rust//rust/platform:wasm32-unknown-unknown": [],
"@rules_rust//rust/platform:wasm32-wasi": [],
"@rules_rust//rust/platform:x86_64-apple-darwin": [],
"@rules_rust//rust/platform:x86_64-apple-ios": [],
"@rules_rust//rust/platform:x86_64-fuchsia": [],
"@rules_rust//rust/platform:x86_64-linux-android": [],
"@rules_rust//rust/platform:x86_64-pc-windows-msvc": [],
"@rules_rust//rust/platform:x86_64-unknown-freebsd": [],
"@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [],
"@rules_rust//rust/platform:x86_64-unknown-none": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
version = "0.22.0",
deps = [
"@crate__blas-sys-0.7.1//:blas_sys",
"@crate__libc-0.2.149//:libc",
"@crate__num-complex-0.4.4//:num_complex",
],
)
Loading