Use GPU flag

This commit is contained in:
Hanh 2022-08-22 01:48:34 +08:00
parent 4d1d4a9d59
commit de506ba074
5 changed files with 58 additions and 21 deletions

View File

@ -144,3 +144,7 @@ void disable_wal(char *db_path);
bool has_cuda(void);
bool has_metal(void);
bool has_gpu(void);
void use_gpu(bool v);

View File

@ -671,3 +671,13 @@ pub unsafe extern "C" fn has_cuda() -> bool {
pub unsafe extern "C" fn has_metal() -> bool {
crate::has_metal()
}
#[no_mangle]
pub unsafe extern "C" fn has_gpu() -> bool {
crate::has_gpu()
}
#[no_mangle]
pub unsafe extern "C" fn use_gpu(v: bool) {
crate::use_gpu(v)
}

View File

@ -34,7 +34,7 @@ use zcash_primitives::zip32::ExtendedFullViewingKey;
use crate::gpu::cuda::{CudaProcessor, CUDA_CONTEXT};
#[cfg(feature = "apple_metal")]
use crate::gpu::metal::MetalProcessor;
use crate::gpu::trial_decrypt;
use crate::gpu::{trial_decrypt, USE_GPU};
pub static DOWNLOADED_BYTES: AtomicUsize = AtomicUsize::new(0);
pub static TRIAL_DECRYPTIONS: AtomicUsize = AtomicUsize::new(0);
@ -417,14 +417,20 @@ impl DecryptNode {
network: &Network,
blocks: Vec<CompactBlock>,
) -> Vec<DecryptedBlock> {
#[cfg(feature = "cuda")]
return self.cuda_decrypt_blocks(network, blocks);
let use_gpu = USE_GPU.load(Ordering::Acquire);
log::info!("use gpu = {}", use_gpu);
if use_gpu {
#[cfg(feature = "cuda")]
return self.cuda_decrypt_blocks(network, blocks);
#[cfg(feature = "apple_metal")]
return self.metal_decrypt_blocks(network, blocks);
#[cfg(feature = "apple_metal")]
return self.metal_decrypt_blocks(network, blocks);
#[allow(unreachable_code)]
self.decrypt_blocks_soft(network, blocks)
#[allow(unreachable_code)]
self.decrypt_blocks_soft(network, blocks)
} else {
self.decrypt_blocks_soft(network, blocks)
}
}
pub fn decrypt_blocks_soft(

View File

@ -2,12 +2,41 @@ use crate::chain::{DecryptedBlock, DecryptedNote, Nf};
use crate::db::AccountViewKey;
use crate::CompactBlock;
use anyhow::Result;
use lazy_static::lazy_static;
use std::sync::atomic::{AtomicBool, Ordering};
use zcash_note_encryption::Domain;
use zcash_primitives::consensus::{BlockHeight, Network};
use zcash_primitives::sapling::note_encryption::SaplingDomain;
use zcash_primitives::sapling::SaplingIvk;
use zcash_primitives::zip32::ExtendedFullViewingKey;
lazy_static! {
pub static ref USE_GPU: AtomicBool = AtomicBool::new(true);
}
#[cfg(feature = "cuda")]
pub fn has_cuda() -> bool {
let cuda = cuda::CUDA_CONTEXT.lock().unwrap();
return cuda.is_some();
}
#[cfg(not(feature = "cuda"))]
pub fn has_cuda() -> bool {
false
}
pub fn has_metal() -> bool {
cfg!(feature = "apple_metal")
}
pub fn has_gpu() -> bool {
cfg!(any(feature = "cuda", feature = "apple_metal"))
}
pub fn use_gpu(v: bool) {
USE_GPU.store(v, Ordering::Release);
}
#[cfg(feature = "cuda")]
pub mod cuda;

View File

@ -3,6 +3,7 @@
#[path = "generated/cash.z.wallet.sdk.rpc.rs"]
pub mod lw_rpc;
use std::sync::atomic::Ordering;
pub use zcash_params::coin::{get_branch, get_coin_type, CoinType};
// Mainnet
@ -98,17 +99,4 @@ pub mod nodejs;
mod gpu;
#[cfg(feature = "cuda")]
pub fn has_cuda() -> bool {
let cuda = gpu::cuda::CUDA_CONTEXT.lock().unwrap();
return cuda.is_some();
}
#[cfg(not(feature = "cuda"))]
pub fn has_cuda() -> bool {
false
}
pub fn has_metal() -> bool {
cfg!(feature = "apple_metal")
}
pub use gpu::{has_cuda, has_gpu, has_metal, use_gpu};