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

[RFC] Handle device pointer for state initialization #1982

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
69 changes: 59 additions & 10 deletions runtime/nvqir/custatevec/CuStateVecCircuitSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,43 @@ class CuStateVecCircuitSimulator
}

// User state provided...

// FIXME handle case where pointer is a device pointer
// Check if the pointer is a device pointer
cudaPointerAttributes attributes;
HANDLE_CUDA_ERROR(cudaPointerGetAttributes(&attributes, state));

// First allocation, so just set the user provided data here
ScopedTraceWithContext(
"CuStateVecCircuitSimulator::addQubitsToState cudaMemcpy",
stateDimension * sizeof(CudaDataType));
HANDLE_CUDA_ERROR(cudaMemcpy(deviceStateVector, state,
stateDimension * sizeof(CudaDataType),
cudaMemcpyHostToDevice));

if (attributes.type == cudaMemoryTypeDevice) {
int currentDevice;
HANDLE_CUDA_ERROR(cudaGetDevice(&currentDevice));

if (attributes.device != currentDevice) {
// Memory is on a different GPU
// Set the device to the device where the memory is located
HANDLE_CUDA_ERROR(cudaSetDevice(attributes.device));

// Perform device to device copy
HANDLE_CUDA_ERROR(cudaMemcpy(deviceStateVector, state,
stateDimension * sizeof(CudaDataType),
cudaMemcpyDeviceToDevice));

// Restore the current device
HANDLE_CUDA_ERROR(cudaSetDevice(currentDevice));
} else {
// Memory is on the same GPU
HANDLE_CUDA_ERROR(cudaMemcpy(deviceStateVector, state,
stateDimension * sizeof(CudaDataType),
cudaMemcpyDeviceToDevice));
}
} else {
// Else, copy from host to device
HANDLE_CUDA_ERROR(cudaMemcpy(deviceStateVector, state,
stateDimension * sizeof(CudaDataType),
cudaMemcpyHostToDevice));
}
return;
}

Expand All @@ -221,11 +248,33 @@ class CuStateVecCircuitSimulator
n_blocks, threads_per_block, otherState, (1UL << count));
HANDLE_CUDA_ERROR(cudaGetLastError());
} else {

// FIXME Handle case where data is already on GPU
HANDLE_CUDA_ERROR(cudaMemcpy(otherState, state,
(1UL << count) * sizeof(CudaDataType),
cudaMemcpyHostToDevice));
// Check if the pointer is a device pointer
cudaPointerAttributes attributes;
HANDLE_CUDA_ERROR(cudaPointerGetAttributes(&attributes, state));

if (attributes.type == cudaMemoryTypeDevice) {
int currentDevice;
HANDLE_CUDA_ERROR(cudaGetDevice(&currentDevice));

if (attributes.device != currentDevice) {
// Memory is on a different GPU
// Set the device to the device where the memory is located
HANDLE_CUDA_ERROR(cudaSetDevice(attributes.device));

// Perform device to device copy
HANDLE_CUDA_ERROR(cudaMemcpy(otherState, state,
stateDimension * sizeof(CudaDataType),
cudaMemcpyDeviceToDevice));

// Restore the current device
HANDLE_CUDA_ERROR(cudaSetDevice(currentDevice));
}
} else {
// Else, copy from host to device
HANDLE_CUDA_ERROR(cudaMemcpy(otherState, state,
(1UL << count) * sizeof(CudaDataType),
cudaMemcpyHostToDevice));
}
}

{
Expand Down
Loading