Skip to content

Commit

Permalink
feat: enable compilation with CUDA stubs via the `ZKSYNC_USE_CUDA_STU…
Browse files Browse the repository at this point in the history
…BS` environment variable (#29)

# What ❔

This PR is a follow-up to

matter-labs/era-cuda#9
matter-labs/era-boojum-cuda#36

making it possible to enable the compilation with CUDA stubs by setting
the environment variable `ZKSYNC_USE_CUDA_STUBS` to `1` or `true` or
`yes` in any capitalization in addition to the `no_cuda` configuration
flag.

## Why ❔

Other way to enable the compilation with stubs besides a configuration
flag is desirable.

## Checklist

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Code has been formatted via `cargo fmt` and linted via `cargo
clippy`.
  • Loading branch information
robik75 authored Sep 24, 2024
1 parent 9bba6d2 commit f77ff80
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Then add the following variable to your config (`.bashrc`/`.zshrc`):
export BELLMAN_CUDA_DIR=<PATH_TO>/era-bellman-cuda
```

Alternatively, if you can't or don't want to install the CUDA toolkit, you can compile the crates `cudart-sys` and `boojum-cuda` and crates that depend on them without CUDA toolkit.
Doing so will result in stubs replacing calls to CUDA API and any GPU device code. The code will compile but any call to one of the stubs will result in an error during runtime.
To compile in this mode, either include the rucstc `cfg` flag named `no_cuda`, for example by setting the `RUSTFLAGS` environment variable to `--cfg no_cuda`, or by setting the environment variable `ZKSYNC_USE_CUDA_STUBS` to `1` or `true` or `yes` in any capitalization.

## Crates

- [bindings-generator](./crates/bindings-generator/)
Expand Down
8 changes: 3 additions & 5 deletions crates/boojum-cuda/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ fn main() {
gates::generate();
poseidon_constants::generate();
println!("cargo::rustc-check-cfg=cfg(no_cuda)");
#[cfg(no_cuda)]
{
if era_cudart_sys::is_no_cuda() {
println!("cargo::warning={}", era_cudart_sys::no_cuda_message!());
}
#[cfg(not(no_cuda))]
{
println!("cargo::rustc-cfg=no_cuda");
} else {
use era_cudart_sys::{get_cuda_lib_path, get_cuda_version};
use std::env::var;
let cuda_version =
Expand Down
8 changes: 3 additions & 5 deletions crates/cudart-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ include!("src/utils.rs");

fn main() {
println!("cargo::rustc-check-cfg=cfg(no_cuda)");
#[cfg(no_cuda)]
{
if is_no_cuda() {
println!("cargo::warning={}", no_cuda_message!());
}
#[cfg(not(no_cuda))]
{
println!("cargo::rustc-cfg=no_cuda");
} else {
let cuda_version =
get_cuda_version().expect("Failed to determine the CUDA Toolkit version.");
if !cuda_version.starts_with("12.") {
Expand Down
10 changes: 9 additions & 1 deletion crates/cudart-sys/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ pub fn get_cuda_version() -> Option<String> {
}
}

#[cfg(no_cuda)]
pub fn is_no_cuda() -> bool {
if cfg!(no_cuda) {
true
} else {
let no_cuda = option_env!("ZKSYNC_USE_CUDA_STUBS").unwrap_or("");
no_cuda == "1" || no_cuda.to_lowercase() == "true" || no_cuda.to_lowercase() == "yes"
}
}

#[macro_export]
macro_rules! no_cuda_message {
() => {
Expand Down

0 comments on commit f77ff80

Please sign in to comment.