Skip to content

Commit

Permalink
Merge pull request filecoin-project#99 from filecoin-project/use-gpu-…
Browse files Browse the repository at this point in the history
…tools

refact(gpu): big refactoring and use rust-gpu-tools
  • Loading branch information
keyvank authored Aug 12, 2020
2 parents 1c96dce + 5a907e1 commit 6ac3181
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 463 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ rand_core = "0.5"
byteorder = "1"
log = "0.4.8"
lazy_static = "1.4.0"
ocl = { version = "0.19.4", package = "fil-ocl", optional = true }
ff-cl-gen = { version = "0.1.1", optional = true }
fs2 = { version = "0.4.3", optional = true }
rand = "0.7"
rayon = "1.3.0"
memmap = "0.7.0"
thiserror = "1.0.10"
ahash = "0.3.4"
rust-gpu-tools = { version = "0.1.0", optional = true }

[dev-dependencies]
hex-literal = "0.2"
Expand All @@ -42,7 +42,7 @@ criterion = "0.3.2"

[features]
default = ["groth16", "multicore"]
gpu = ["ocl", "ff-cl-gen", "fs2"]
gpu = ["rust-gpu-tools", "ff-cl-gen", "fs2"]
groth16 = ["paired"]
multicore = ["futures-cpupool", "crossbeam", "num_cpus"]

Expand Down
35 changes: 9 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ booleans and number abstractions.
This fork contains GPU parallel acceleration to the FFT and Multiexponentation algorithms in the groth16 prover codebase under a conditional compilation feature `#[cfg(feature = "gpu")]` and `gpu-test` for testing.

### Requirements
- NVIDIA or AMD GPU Graphics Driver
- NVIDIA or AMD GPU Graphics Driver
- OpenCL

( For AMD devices we recommend [ROCm](https://rocm-documentation.readthedocs.io/en/latest/Installation_Guide/Installation-Guide.html) )
Expand All @@ -29,31 +29,15 @@ The gpu extension contains some env vars that may be set externally to this libr
env::set_var("BELLMAN_NO_GPU", "1");
```

- `BELLMAN_PLATFORM`
- `BELLMAN_VERIFIER`

Can be used to select the default OpenCL platform:
Chooses the device in which the batched verifier is going to run. Can be `cpu`, `gpu` or `auto`.

```rust
// Example
env::set_var("BELLMAN_PLATFORM", "AMD Accelerated Parallel Processing");
Example
env::set_var("BELLMAN_VERIFIER", "gpu");
```

Some possible values:

`BELLMAN_VERIFIER`

Chooses the device in which the batched verifier is going to run. Can be `cpu`, `gpu` or `auto`.

```
Example
env::set_var("BELLMAN_VERIFIER", "gpu");
```
- NVIDIA CUDA
- AMD Accelerated Parallel Processing
If not set, and the code does not select any platform, "NVIDIA CUDA" will be selected.
- `BELLMAN_CUSTOM_GPU`

Will allow for adding a GPU not in the tested list. This requires researching the name of the GPU device and the number of cores in the format `["name:cores"]`.
Expand Down Expand Up @@ -106,12 +90,11 @@ To run the multiexp_consistency test you can use:
RUST_LOG=info cargo test --features gpu -- --exact multiexp::gpu_multiexp_consistency --nocapture
```

to run on some specific platform you can do
### Considerations

```bash
export BELLMAN_PLATFORM="AMD Accelerated Parallel Processing"
RUST_LOG=info cargo test --features gpu -- --exact multiexp::gpu_multiexp_consistency --nocapture
```
Bellperson uses `rust-gpu-tools` as its OpenCL backend, therefore you may see a
directory named `~/.rust-gpu-tools` in your home folder, which contains the
compiled binaries of OpenCL kernels used in this repository.

## License

Expand Down
6 changes: 3 additions & 3 deletions src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,11 @@ fn parallel_fft_consistency() {
test_consistency::<Bls12, _>(rng);
}

pub fn create_fft_kernel<E>(log_d: usize, priority: bool) -> Option<gpu::FFTKernel<E>>
pub fn create_fft_kernel<E>(_log_d: usize, priority: bool) -> Option<gpu::FFTKernel<E>>
where
E: Engine,
{
match gpu::FFTKernel::create(1 << log_d, priority) {
match gpu::FFTKernel::create(priority) {
Ok(k) => {
info!("GPU FFT kernel instantiated!");
Some(k)
Expand Down Expand Up @@ -594,7 +594,7 @@ mod tests {

let worker = Worker::new();
let log_cpus = worker.log_num_cpus();
let mut kern = gpu::FFTKernel::create(1 << 24, false).expect("Cannot initialize kernel!");
let mut kern = gpu::FFTKernel::create(false).expect("Cannot initialize kernel!");

for log_d in 1..25 {
let d = 1 << log_d;
Expand Down
16 changes: 7 additions & 9 deletions src/gpu/error.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
#[cfg(feature = "gpu")]
use rust_gpu_tools::opencl;

#[derive(thiserror::Error, Debug)]
pub enum GPUError {
#[error("GPUError: {0}")]
Simple(&'static str),
#[cfg(feature = "gpu")]
#[error("Ocl Error: {0}")]
Ocl(ocl::Error),
#[error("OpenCL Error: {0}")]
OpenCL(#[from] opencl::GPUError),
#[cfg(feature = "gpu")]
#[error("GPU taken by a high priority process!")]
GPUTaken,
#[cfg(feature = "gpu")]
#[error("No kernel is initialized!")]
KernelUninitialized,
#[error("GPU accelerator is disabled!")]
GPUDisabled,
}

pub type GPUResult<T> = std::result::Result<T, GPUError>;

#[cfg(feature = "gpu")]
impl From<ocl::Error> for GPUError {
fn from(error: ocl::Error) -> Self {
GPUError::Ocl(error)
}
}

#[cfg(feature = "gpu")]
impl From<std::boxed::Box<dyn std::any::Any + std::marker::Send>> for GPUError {
fn from(e: std::boxed::Box<dyn std::any::Any + std::marker::Send>) -> Self {
Expand Down
Loading

0 comments on commit 6ac3181

Please sign in to comment.