-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable compilation without Bellman CUDA
- Loading branch information
Showing
10 changed files
with
162 additions
and
1,111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/ | ||
``` | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++"); | ||
} | ||
} |
Oops, something went wrong.