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

Allow multiple 'local' installs of cuda to be specified #66

Merged
merged 9 commits into from
Apr 12, 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
8 changes: 4 additions & 4 deletions .github/workflows/build-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ jobs:
- run: bazelisk build @rules_cuda_examples//if_cuda:main
- run: bazelisk build @rules_cuda_examples//if_cuda:main --enable_cuda=False

- run: bazelisk build @rules_cuda_examples//basic:main --config=bzlmod
- run: bazelisk build @rules_cuda_examples//rdc:main --config=bzlmod
- run: bazelisk build @rules_cuda_examples//if_cuda:main --config=bzlmod
- run: bazelisk build @rules_cuda_examples//if_cuda:main --enable_cuda=False --config=bzlmod
- run: cd examples && bazelisk build //basic:main --config=bzlmod
- run: cd examples && bazelisk build //rdc:main --config=bzlmod
- run: cd examples && bazelisk build //if_cuda:main --config=bzlmod
- run: cd examples && bazelisk build //if_cuda:main --enable_cuda=False --config=bzlmod

- run: bazelisk shutdown
6 changes: 3 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module(
name = "rules_cuda",
compatibility_level = 1,
version = "0.1.1",
version = "0.0.0",
)

bazel_dep(name = "bazel_skylib", version = "1.2.1")
bazel_dep(name = "platforms", version = "0.0.4")
bazel_dep(name = "platforms", version = "0.0.6")

toolchain = use_extension("@rules_cuda//cuda:extensions.bzl", "local_toolchain")
toolchain = use_extension("@rules_cuda//cuda:extensions.bzl", "toolchain")
use_repo(toolchain, "local_cuda")

register_toolchains(
Expand Down
4 changes: 0 additions & 4 deletions WORKSPACE.bzlmod

This file was deleted.

25 changes: 23 additions & 2 deletions cuda/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,28 @@

load("//cuda/private:repositories.bzl", "local_cuda")

cuda_toolkit = tag_class(attrs = {
"name": attr.string(doc = "Name for the toolchain repository", default = "local_cuda"),
"toolkit_path": attr.string(doc = "Path to the CUDA SDK, if empty the environment variable CUDA_PATH will be used to deduce this path."),
})

def _init(module_ctx):
local_cuda(name = "local_cuda")
registrations = {}
for mod in module_ctx.modules:
for toolchain in mod.tags.local_toolchain:
if not mod.is_root:
fail("Only the root module may override the path for the local cuda toolchain")
jsharpe marked this conversation as resolved.
Show resolved Hide resolved
if toolchain.name in registrations.keys():
if toolchain.toolkit_path == registrations[toolchain.name]:
# No problem to register a matching toolchain twice
continue
fail("Multiple conflicting toolchains declared for name {} ({} and {}".format(toolchain.name, toolchain.toolkit_path, registrations[toolchain.name]))
else:
registrations[toolchain.name] = toolchain.toolkit_path
for name, toolkit_path in registrations.items():
local_cuda(name = name, toolkit_path = toolkit_path)

local_toolchain = module_extension(implementation = _init)
toolchain = module_extension(
implementation = _init,
tag_classes = {"local_toolchain": cuda_toolkit},
)
19 changes: 11 additions & 8 deletions cuda/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ def detect_cuda_toolkit(repository_ctx):
"""Detect CUDA Toolkit.

The path to CUDA Toolkit is determined as:

- the value of `toolkit_path` passed to local_cuda as an attribute
- taken from `CUDA_PATH` environment variable or
- determined through `which ptxas` or
- defaults to `/usr/local/cuda`
- determined through 'which ptxas' or
- defaults to '/usr/local/cuda'

Args:
repository_ctx: repository_ctx

Returns:
A struct contains the information of CUDA Toolkit.
"""
cuda_path = repository_ctx.os.environ.get("CUDA_PATH", None)
cuda_path = repository_ctx.attr.toolkit_path
if cuda_path == "":
cuda_path = repository_ctx.os.environ.get("CUDA_PATH", None)
if cuda_path == None:
ptxas_path = repository_ctx.which("ptxas")
if ptxas_path:
Expand All @@ -65,13 +67,13 @@ def detect_cuda_toolkit(repository_ctx):
fatbinary = "@rules_cuda//cuda/dummy:fatbinary"
if cuda_path != None:
if repository_ctx.path(cuda_path + "/bin/nvlink" + bin_ext).exists:
nvlink = "@local_cuda//:cuda/bin/nvlink" + bin_ext
nvlink = ":cuda/bin/nvlink{}".format(bin_ext)
if repository_ctx.path(cuda_path + "/bin/crt/link.stub").exists:
link_stub = "@local_cuda//:cuda/bin/crt/link.stub"
link_stub = ":cuda/bin/crt/link.stub"
if repository_ctx.path(cuda_path + "/bin/bin2c" + bin_ext).exists:
bin2c = "@local_cuda//:cuda/bin/bin2c" + bin_ext
bin2c = ":cuda/bin/bin2c{}".format(bin_ext)
if repository_ctx.path(cuda_path + "/bin/fatbinary" + bin_ext).exists:
fatbinary = "@local_cuda//:cuda/bin/fatbinary" + bin_ext
fatbinary = ":cuda/bin/fatbinary{}".format(bin_ext)

nvcc_version_major = -1
nvcc_version_minor = -1
Expand Down Expand Up @@ -197,6 +199,7 @@ def _local_cuda_impl(repository_ctx):

local_cuda = repository_rule(
implementation = _local_cuda_impl,
attrs = {"toolkit_path": attr.string(mandatory = False)},
configure = True,
local = True,
environ = ["CUDA_PATH", "PATH", "CUDA_CLANG_PATH", "BAZEL_LLVM"],
Expand Down
3 changes: 2 additions & 1 deletion cuda/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//cuda/private:repositories.bzl", _rules_cuda_dependencies = "rules_cuda_dependencies")
load("//cuda/private:repositories.bzl", _local_cuda = "local_cuda", _rules_cuda_dependencies = "rules_cuda_dependencies")
load("//cuda/private:toolchain.bzl", _register_detected_cuda_toolchains = "register_detected_cuda_toolchains")

rules_cuda_dependencies = _rules_cuda_dependencies
local_cuda = _local_cuda
register_detected_cuda_toolchains = _register_detected_cuda_toolchains
10 changes: 6 additions & 4 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ module(
version = "0.0.0",
)

bazel_dep(name = "rules_cuda", version = "0.1.1")
bazel_dep(name = "rules_cuda", version = "0.1.3")
local_path_override(module_name = "rules_cuda", path = "..")

cuda_toolchain = use_extension("@rules_cuda//cuda:extensions.bzl", "local_toolchain")
use_repo(cuda_toolchain, "local_cuda")
cuda = use_extension("@rules_cuda//cuda:extensions.bzl", "toolchain")

local_path_override(module_name = "rules_cuda", path = "..")
cuda.local_toolchain(name = "local_cuda", toolkit_path = "")

use_repo(cuda, "local_cuda")