From 00a4184b6215c26d6e611e6c55a8c920b6d5c075 Mon Sep 17 00:00:00 2001 From: Ernesto Casablanca Date: Wed, 1 Oct 2025 17:42:10 +0100 Subject: [PATCH 1/4] feat: cuda build support with bazel --- BUILD.bazel | 35 ++++++++++++++++++++++++++++++++++- MODULE.bazel | 11 +++++++++++ highs/HConfig.h.bazel.in | 1 - 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index cd97f3365ad..10ba47999c0 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,5 +1,7 @@ load("@bazel_skylib//rules:copy_file.bzl", "copy_file") +load("@bazel_skylib//lib:selects.bzl", "selects") load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") +load("@rules_cuda//cuda:defs.bzl", "cuda_library", "requires_cuda") copy_file( name = "highs-config", @@ -8,12 +10,36 @@ copy_file( visibility = ["//visibility:public"], ) +selects.config_setting_group( + name = "cuda_enabled", + match_all = [ + "@rules_cuda//cuda:is_valid_toolchain_found", + "@rules_cuda//cuda:is_enabled", + ], +) + cc_library( name = "config", srcs = ["HConfig.h"], visibility = ["//visibility:public"], ) +cuda_library( + name = "cupdlp", + srcs = glob([ + "highs/pdlp/cupdlp/cuda/*.cu", + ]), + hdrs = glob([ + "highs/pdlp/cupdlp/cuda/*.cuh", + ]), + defines = ["CUPDLP_GPU"], + target_compatible_with = requires_cuda(), + deps = [ + "@local_cuda//:cublas", + "@local_cuda//:cusparse", + ], +) + cc_library( name = "highs", srcs = ["highs/interfaces/highs_c_api.cpp"] + glob([ @@ -49,6 +75,10 @@ cc_library( "-Wno-unused-but-set-variable", ], }), + defines = select({ + ":cuda_enabled": ["CUPDLP_GPU"], + "//conditions:default": ["CUPDLP_CPU"], + }), includes = [ "extern", # "extern/filereaderlp", @@ -76,7 +106,10 @@ cc_library( deps = [ "//:config", "@zlib", - ], + ] + select({ + ":cuda_enabled": [":cupdlp"], + "//conditions:default": [], + }), ) cc_library( diff --git a/MODULE.bazel b/MODULE.bazel index ae55331b058..abe3b01f012 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -19,3 +19,14 @@ bazel_dep( name = "zlib", version = "1.3.1.bcr.5", ) + +bazel_dep( + name = "rules_cuda", + version = "0.2.5", +) + +local_cuda = use_extension("@rules_cuda//cuda:extensions.bzl", "toolchain") +local_cuda.local_toolchain( + toolkit_path = "", +) +use_repo(local_cuda, "local_cuda") diff --git a/highs/HConfig.h.bazel.in b/highs/HConfig.h.bazel.in index 584d65f4eb3..456229e7e4c 100644 --- a/highs/HConfig.h.bazel.in +++ b/highs/HConfig.h.bazel.in @@ -3,7 +3,6 @@ #define FAST_BUILD /* #undef ZLIB_FOUND */ -#define CUPDLP_CPU #define CMAKE_BUILD_TYPE "RELEASE" #define HiGHSRELEASE /* #undef HIGHSINT64 */ From c42d002e531b546b1401f4f49a64ae425f70fab1 Mon Sep 17 00:00:00 2001 From: Ernesto Casablanca Date: Wed, 1 Oct 2025 18:01:47 +0100 Subject: [PATCH 2/4] docs: explain how cuda build works with bazel --- highs/pdlp/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/highs/pdlp/README.md b/highs/pdlp/README.md index 2e0222afcb8..bc37b603b08 100644 --- a/highs/pdlp/README.md +++ b/highs/pdlp/README.md @@ -46,8 +46,18 @@ When both work, on Linux, you should check that `/usr/local/cuda` exists. #### Build +##### CMake + ``` cmake -S . -B build -DCUPDLP_GPU=ON -DALL_TESTS=ON .. cmake --build build --parallel ``` +##### Bazel + +```bash +# Will build with CUDA if a CUDA toolchain is detected +bazel build //:highs +# Ensure the target is built without CUDA +bazel build //:highs --@rules_cuda//cuda:enable=False +``` From e8dc947e2a837aa29a07f40ade4f7c5832f1f4d2 Mon Sep 17 00:00:00 2001 From: Ernesto Casablanca Date: Thu, 2 Oct 2025 11:37:30 +0100 Subject: [PATCH 3/4] fix: add configurable tag for cuda build --- BUILD.bazel | 25 +++++++++++++++---------- highs/pdlp/README.md | 6 +++--- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 10ba47999c0..280196744ee 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,5 +1,5 @@ +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") load("@bazel_skylib//rules:copy_file.bzl", "copy_file") -load("@bazel_skylib//lib:selects.bzl", "selects") load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") load("@rules_cuda//cuda:defs.bzl", "cuda_library", "requires_cuda") @@ -10,12 +10,18 @@ copy_file( visibility = ["//visibility:public"], ) -selects.config_setting_group( - name = "cuda_enabled", - match_all = [ - "@rules_cuda//cuda:is_valid_toolchain_found", - "@rules_cuda//cuda:is_enabled", - ], +bool_flag( + name = "cupdlp_gpu", + build_setting_default = False, +) + +config_setting( + name = "cupdlp_gpu_enabled", + flag_values = { + "@rules_cuda//cuda:enable": "True", + "@local_cuda//:valid_toolchain_found": "True", + ":cupdlp_gpu": "True", + }, ) cc_library( @@ -76,7 +82,7 @@ cc_library( ], }), defines = select({ - ":cuda_enabled": ["CUPDLP_GPU"], + ":cupdlp_gpu_enabled": ["CUPDLP_GPU"], "//conditions:default": ["CUPDLP_CPU"], }), includes = [ @@ -96,7 +102,6 @@ cc_library( # "highs/simplex", # "highs/test_kkt", # "highs/util", - "bazel-bin", ], linkopts = select({ "@rules_cc//cc/compiler:msvc-cl": ["-DEFAULTLIB:shell32.lib"], @@ -107,7 +112,7 @@ cc_library( "//:config", "@zlib", ] + select({ - ":cuda_enabled": [":cupdlp"], + ":cupdlp_gpu_enabled": [":cupdlp"], "//conditions:default": [], }), ) diff --git a/highs/pdlp/README.md b/highs/pdlp/README.md index bc37b603b08..bd7865f050e 100644 --- a/highs/pdlp/README.md +++ b/highs/pdlp/README.md @@ -56,8 +56,8 @@ cmake --build build --parallel ##### Bazel ```bash -# Will build with CUDA if a CUDA toolchain is detected +# Will NOT build with CUDA, only CPU support bazel build //:highs -# Ensure the target is built without CUDA -bazel build //:highs --@rules_cuda//cuda:enable=False +# Will build with CUDA if a CUDA toolchain is detected +bazel build //:highs --//:cupdlp_gpu ``` From 44f5b6c4c13772b16e0f1b25b1f8424fa5cab815 Mon Sep 17 00:00:00 2001 From: Ernesto Casablanca Date: Fri, 3 Oct 2025 09:57:00 +0100 Subject: [PATCH 4/4] fix: allow dependent rules to use the bool flag --- BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/BUILD.bazel b/BUILD.bazel index 280196744ee..71a1a9df9fc 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -13,6 +13,7 @@ copy_file( bool_flag( name = "cupdlp_gpu", build_setting_default = False, + visibility = ["//visibility:public"], ) config_setting(