Skip to content

Can not import pycuda #446

Closed
Closed

Description

@annoyingCode

When I try to import pycuda, it gives me the following error:
image
Here is the code:

from pycuda.compiler import SourceModule
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda import gpuarray
import numpy as np

# Sample kernel to perform simple addition
cuda_code = """
_global_ void add(float *a, float *b, float *c, int n) {
    int idx = threadIdx.x + blockIdx.x * blockDim.x;
    if (idx < n) {
        c[idx] = a[idx] + b[idx];
    }
}
"""

# Compile and get the CUDA function
module = SourceModule(cuda_code)
add = module.get_function("add")

# Allocate memory and transfer data to GPU
n = 1000
a = np.random.randn(n).astype(np.float32)
b = np.random.randn(n).astype(np.float32)
c = np.empty_like(a)

a_gpu = cuda.mem_alloc(a.nbytes)
b_gpu = cuda.mem_alloc(b.nbytes)
c_gpu = cuda.mem_alloc(c.nbytes)

cuda.memcpy_htod(a_gpu, a)
cuda.memcpy_htod(b_gpu, b)

# Execute kernel
block_size = 256
grid_size = (n + block_size - 1) // block_size

print("Executing CUDA kernel...")
add(a_gpu, b_gpu, c_gpu, np.int32(n), block=(
    block_size, 1, 1), grid=(grid_size, 1))

# Retrieve results
cuda.memcpy_dtoh(c, c_gpu)
print("Kernel execution completed.")
  • OS: Windows 10
  • CUDA version: 12.4
  • CUDA driver version: 555.85
  • PyCUDA version: 2024.1
  • Python version: 3.11

Activity

  1. Repository owner locked and limited conversation to collaborators on Jun 24, 2024
  2. converted this issue into a discussion #448 on Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions