Skip to content

Commit

Permalink
feat: enable compilation without Bellman CUDA
Browse files Browse the repository at this point in the history
  • Loading branch information
robik75 committed Sep 24, 2024
1 parent 6f50e28 commit ee19c98
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 1,111 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ version = "0.150.9"

[workspace.dependencies]
# Local dependencies
bindings-generator = { version = "=0.150.9", path = "crates/bindings-generator" }
boojum-cuda = { version = "=0.150.9", path = "crates/boojum-cuda" }
era_criterion_cuda = { version = "=0.150.9", path = "crates/criterion-cuda" }
era_cudart = { version = "=0.150.9", path = "crates/cudart" }
era_cudart_sys = { version = "=0.150.9", path = "crates/cudart-sys" }
era_cudart_sys_bindings_generator = { version = "=0.150.9", path = "crates/cudart-sys-bindings-generator" }
gpu-ffi = { version = "=0.150.9", path = "crates/gpu-ffi", package = "zksync-gpu-ffi" }
gpu-ffi-bindings-generator = { version = "=0.150.9", path = "crates/gpu-ffi", package = "zksync-gpu-ffi-bindings-generator" }
gpu-prover = { version = "=0.150.9", path = "crates/gpu-prover", package = "zksync-gpu-prover" }
shivini = { version = "=0.150.9", path = "crates/shivini" }
wrapper-prover = { version = "=0.150.9", path = "crates/wrapper-prover", package = "zksync-wrapper-prover" }
Expand Down
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ GPU acceleration for cryptography used in ZKsync project.
In order to work with the workspace, you'll need.

- [Cmake 3.24 or higher](https://apt.kitware.com/)
- [CUDA toolkit](https://developer.nvidia.com/cuda-downloads)
- [CUDA toolkit 12.0 or higher](https://developer.nvidia.com/cuda-downloads)
- [Bellman CUDA](github.com/matter-labs/matter-labs/era-bellman-cuda)

For Bellman CUDA the easiest way to install is:
For Bellman CUDA, clone it to a directory of your choice:

```
git clone [email protected]:matter-labs/era-bellman-cuda.git
```

Optionally also build it with the following commands (otherwise it will be built automatically while building the `gpu-ffi` crate):

```
cmake -Bera-bellman-cuda/build -Sera-bellman-cuda/ -DCMAKE_BUILD_TYPE=Release
cmake --build era-bellman-cuda/build/
```
Expand All @@ -25,20 +30,21 @@ 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.
Alternatively, if you can't or don't want to install the CUDA toolkit or Bellman CUDA, you can compile the crates in this workspace without a CUDA toolkit installation and Bellman CUDA.
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/)
- [cudart-sys](./crates/cudart-sys/)
- [cudart](./crates/cudart/)
- [criterion-cuda](./crates/criterion-cuda/)
- [boojum-cuda](./crates/boojum-cuda/)
- [shivini](./crates/shivini/)
- [criterion-cuda](./crates/criterion-cuda/)
- [cudart](./crates/cudart/)
- [cudart-sys](./crates/cudart-sys/)
- [cudart-sys-bindings-generator](crates/cudart-sys-bindings-generator/)
- [gpu-ffi](./crates/gpu-ffi/)
- [gpu-ffi-bindings-generator](./crates/gpu-ffi-bindings-generator/)
- [gpu-prover](./crates/gpu-prover/)
- [shivini](./crates/shivini/)
- [wrapper-prover](./crates/wrapper-prover/)

## License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords.workspace = true
categories.workspace = true
repository.workspace = true
version.workspace = true
name = "era_cuda_bindings_generator"
name = "era_cudart_sys_bindings_generator"
description = "CUDA Bindings generator for ZKsync"
publish = false

Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions crates/gpu-ffi-bindings-generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
edition.workspace = true
authors.workspace = true
homepage.workspace = true
license.workspace = true
keywords.workspace = true
categories.workspace = true
repository.workspace = true
version.workspace = true
name = "zksync-gpu-ffi-bindings-generator"
description = "ZKsync gpu-ffi bindings generator"
publish = false

[dependencies]
bindgen = "0.70"
14 changes: 14 additions & 0 deletions crates/gpu-ffi-bindings-generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() {
let bellman_cuda_dir = std::env::var("BELLMAN_CUDA_DIR").unwrap();
let bellman_cuda_path = std::path::Path::new(&bellman_cuda_dir);
let header = bellman_cuda_path.join("src").join("bellman-cuda.h");
let bindings = bindgen::Builder::default()
.header(header.to_str().unwrap())
.generate_comments(false)
.layout_tests(false)
.size_t_is_usize(false)
.generate()
.expect("Unable to generate bindings")
.to_string();
println!("{bindings}");
}
2 changes: 1 addition & 1 deletion crates/gpu-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ name = "zksync-gpu-ffi"
description = "ZKsync GPU FFI"

[dependencies]
era_cudart_sys.workspace = true
futures = "0.3.8"
futures-locks = "0.7"
derivative = "2.2"
Expand All @@ -19,7 +20,6 @@ crossbeam = "0.8"

[build-dependencies]
era_cudart_sys.workspace = true
bindgen = "0.59.1"
cmake = "0.1"

[dev-dependencies]
Expand Down
114 changes: 39 additions & 75 deletions crates/gpu-ffi/build.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,45 @@
extern crate bindgen;
use era_cudart_sys::get_cuda_lib_path;
#![allow(unexpected_cfgs)]

use era_cudart_sys::{get_cuda_lib_path, get_cuda_version, is_no_cuda, no_cuda_message};
use std::env::var;
use std::path::Path;
use std::{env, path::PathBuf};
// build.rs
use std::env;

fn main() {
let bellman_cuda_path = if let Ok(path) = std::env::var("BELLMAN_CUDA_DIR") {
path
} else {
// we need to instruct rustc so that it will find libbellman-cuda.a
// - if dep is resolved via git(cargo checks ~/.cargo/git/checkouts/)
// - if dep is resolved via local path
// - if you want to build on a macos or only for rust analyzer
// just `export BELLMAN_CUDA_DIR=$PWD/bellman-cuda`
// so we will benefit from env variable for now
todo!("set BELLMAN_CUDA_DIR=$PWD")
};

generate_bindings(&bellman_cuda_path);

#[cfg(not(target_os = "macos"))]
link_multiexp_library(&bellman_cuda_path); // FIXME enable
}

fn generate_bindings(bellman_cuda_path: &str) {
println!("generating bindings");
let header_file = &format!("{}/src/bellman-cuda.h", bellman_cuda_path);
const OUT_FILE: &str = "bindings.rs";
println!("cargo:rerun-if-changed={}", header_file);

let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header(header_file)
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(format!(
"{}/{}",
env::current_dir().unwrap().to_str().unwrap(),
"src"
));
println!("out path {:?}", out_path.to_str());
bindings
.write_to_file(out_path.join(OUT_FILE))
.expect("Couldn't write bindings!");
}

fn link_multiexp_library(bellman_cuda_path: &str) {
let bellman_cuda_lib_path = if Path::new(bellman_cuda_path).join("build").exists() {
Path::new(bellman_cuda_path)
.join("build")
.join("src")
.to_str()
.unwrap()
.to_string()
println!("cargo::rustc-check-cfg=cfg(no_cuda)");
if is_no_cuda() {
println!("cargo::warning={}", no_cuda_message!());
println!("cargo::rustc-cfg=no_cuda");
} else {
let cudaarchs = var("CUDAARCHS").unwrap_or("native".to_string());
let dst = cmake::Config::new(bellman_cuda_path)
.profile("Release")
.define("CMAKE_CUDA_ARCHITECTURES", cudaarchs)
.build();
dst.to_str().unwrap().to_string()
};
println!("cargo:rustc-link-search=native={bellman_cuda_lib_path}");
println!("cargo:rustc-link-lib=static=bellman-cuda");
let cuda_lib_path = get_cuda_lib_path().unwrap();
let cuda_lib_path_str = cuda_lib_path.to_str().unwrap();
println!("cargo:rustc-link-search=native={cuda_lib_path_str}");
println!("cargo:rustc-link-lib=cudart");
#[cfg(target_os = "linux")]
println!("cargo:rustc-link-lib=stdc++");
let cuda_version =
get_cuda_version().expect("Failed to determine the CUDA Toolkit version.");
if !cuda_version.starts_with("12.") {
println!("cargo::warning=CUDA Toolkit version {cuda_version} detected. This crate is only tested with CUDA Toolkit version 12.*.");
}
let bellman_cuda_dir = var("BELLMAN_CUDA_DIR").unwrap();
let bellman_cuda_path = Path::new(&bellman_cuda_dir);
let bellman_cuda_lib_path = if Path::new(bellman_cuda_path).join("build").exists() {
Path::new(bellman_cuda_path)
.join("build")
.join("src")
.to_str()
.unwrap()
.to_string()
} else {
let cudaarchs = var("CUDAARCHS").unwrap_or("native".to_string());
let dst = cmake::Config::new(bellman_cuda_path)
.profile("Release")
.define("CMAKE_CUDA_ARCHITECTURES", cudaarchs)
.build();
dst.to_str().unwrap().to_string()
};
println!("cargo:rustc-link-search=native={bellman_cuda_lib_path}");
println!("cargo:rustc-link-lib=static=bellman-cuda");
let cuda_lib_path = get_cuda_lib_path().unwrap();
let cuda_lib_path_str = cuda_lib_path.to_str().unwrap();
println!("cargo:rustc-link-search=native={cuda_lib_path_str}");
println!("cargo:rustc-link-lib=cudart");
#[cfg(target_os = "linux")]
println!("cargo:rustc-link-lib=stdc++");
}
}
Loading

0 comments on commit ee19c98

Please sign in to comment.