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

Check for device memory pointer #2066

Merged
Merged
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
19 changes: 16 additions & 3 deletions runtime/nvqir/custatevec/CuStateVecCircuitSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,14 @@ class CuStateVecCircuitSimulator
return;
}

// User state provided...
// Check if the pointer is a device pointer
cudaPointerAttributes attributes;
HANDLE_CUDA_ERROR(cudaPointerGetAttributes(&attributes, state));

// FIXME handle case where pointer is a device pointer
if (attributes.type == cudaMemoryTypeDevice) {
throw std::invalid_argument(
"[CuStateVecCircuitSimulator] Incompatible host pointer");
}

// First allocation, so just set the user provided data here
ScopedTraceWithContext(
Expand All @@ -200,6 +205,7 @@ class CuStateVecCircuitSimulator
HANDLE_CUDA_ERROR(cudaMemcpy(deviceStateVector, state,
stateDimension * sizeof(CudaDataType),
cudaMemcpyHostToDevice));

sacpis marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand All @@ -221,8 +227,15 @@ class CuStateVecCircuitSimulator
n_blocks, threads_per_block, otherState, (1UL << count));
HANDLE_CUDA_ERROR(cudaGetLastError());
} else {
// Check if the pointer is a device pointer
cudaPointerAttributes attributes;
HANDLE_CUDA_ERROR(cudaPointerGetAttributes(&attributes, state));

if (attributes.type == cudaMemoryTypeDevice) {
throw std::invalid_argument(
"[CuStateVecCircuitSimulator] Incompatible host pointer");
}

// FIXME Handle case where data is already on GPU
HANDLE_CUDA_ERROR(cudaMemcpy(otherState, state,
(1UL << count) * sizeof(CudaDataType),
cudaMemcpyHostToDevice));
Expand Down
Loading