Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add warmup for CudaStream #422

Merged
merged 5 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
test-deploy:
name: Test deployment of docs webiste
name: Test deployment of docs website
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
11 changes: 6 additions & 5 deletions wrappers/rust/icicle-core/src/msm/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::curve::{Affine, Curve, Projective};
use crate::msm::{msm, MSMConfig, MSM};
use crate::traits::{FieldImpl, GenerateRandom};
use icicle_cuda_runtime::device::{get_device_count, set_device};
use icicle_cuda_runtime::device::{get_device_count, set_device, warmup};
use icicle_cuda_runtime::memory::HostOrDeviceSlice;
use icicle_cuda_runtime::stream::CudaStream;
use rayon::iter::IntoParallelIterator;
Expand Down Expand Up @@ -108,6 +108,8 @@ where
{
let test_sizes = [1000, 1 << 16];
let batch_sizes = [1, 3, 1 << 4];
let stream = CudaStream::create().unwrap();
warmup(&stream).unwrap();
for test_size in test_sizes {
for batch_size in batch_sizes {
let points = generate_random_affine_points_with_zeroes(test_size, 10);
Expand All @@ -123,7 +125,6 @@ where
let mut msm_results_1 = HostOrDeviceSlice::cuda_malloc(batch_size).unwrap();
let mut msm_results_2 = HostOrDeviceSlice::cuda_malloc(batch_size).unwrap();
let mut points_d = HostOrDeviceSlice::cuda_malloc(test_size * batch_size).unwrap();
let stream = CudaStream::create().unwrap();
points_d
.copy_from_host_async(&points_cloned, &stream)
.unwrap();
Expand All @@ -147,9 +148,6 @@ where
stream
.synchronize()
.unwrap();
stream
.destroy()
.unwrap();

let points_ark: Vec<_> = points_h
.as_slice()
Expand All @@ -172,6 +170,9 @@ where
}
}
}
stream
.destroy()
.unwrap();
}

pub fn check_msm_skewed_distributions<C: Curve + MSM<C>>()
Expand Down
1 change: 1 addition & 0 deletions wrappers/rust/icicle-cuda-runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn main() {
.allowlist_function("cudaMemset")
.allowlist_function("cudaMemsetAsync")
.allowlist_function("cudaDeviceGetDefaultMemPool")
.allowlist_function("cudaMemGetInfo")
.rustified_enum("cudaMemcpyKind")
// Stream Ordered Memory Allocator
// https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY__POOLS.html
Expand Down
17 changes: 16 additions & 1 deletion wrappers/rust/icicle-cuda-runtime/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{
bindings::{cudaGetDevice, cudaGetDeviceCount, cudaSetDevice},
bindings::{cudaFreeAsync, cudaGetDevice, cudaGetDeviceCount, cudaMallocAsync, cudaMemGetInfo, cudaSetDevice},
error::{CudaResult, CudaResultWrap},
stream::CudaStream,
};
use std::mem::MaybeUninit;

pub fn set_device(device_id: usize) -> CudaResult<()> {
unsafe { cudaSetDevice(device_id as i32) }.wrap()
Expand All @@ -16,3 +18,16 @@ pub fn get_device() -> CudaResult<usize> {
let mut device_id = 0;
unsafe { cudaGetDevice(&mut device_id) }.wrap_value(device_id as usize)
}

// This function pre-allocates default memory pool and warms the GPU up
// so that subsequent memory allocations and other calls are not slowed down
pub fn warmup(stream: &CudaStream) -> CudaResult<()> {
let mut device_ptr = MaybeUninit::<*mut std::ffi::c_void>::uninit();
let mut free_memory: usize = 0;
let mut _total_memory: usize = 0;
unsafe {
cudaMemGetInfo(&mut free_memory as *mut usize, &mut _total_memory as *mut usize).wrap()?;
cudaMallocAsync(device_ptr.as_mut_ptr(), free_memory >> 1, stream.handle).wrap()?;
cudaFreeAsync(device_ptr.assume_init(), stream.handle).wrap()
}
}
8 changes: 6 additions & 2 deletions wrappers/rust/icicle-cuda-runtime/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ impl<'a, T> HostOrDeviceSlice<'a, T> {

pub fn as_mut_slice(&mut self) -> &mut [T] {
match self {
Self::Device(_, _) => panic!("Use copy_to_host and copy_to_host_async to move device data to a slice"),
Self::Device(_, _) => {
panic!("Use copy_to_host and copy_to_host_async to move device data to a slice")
}
Self::Host(v) => v.as_mut_slice(),
}
}

pub fn as_slice(&self) -> &[T] {
match self {
Self::Device(_, _) => panic!("Use copy_to_host and copy_to_host_async to move device data to a slice"),
Self::Device(_, _) => {
panic!("Use copy_to_host and copy_to_host_async to move device data to a slice")
}
Self::Host(v) => v.as_slice(),
}
}
Expand Down
Loading