How should I interpret this CUDA error?

2019-07-16 04:09发布

问题:

I am teaching myself CUDA with pyCUDA. In this exercise, I want to send over a simply array of 1024 floats to the GPU and store it in shared memory. As I specify below in my arguments, I run this kernel on just a single block with 1024 threads.

import pycuda.driver as cuda
from pycuda.compiler import SourceModule
import pycuda.autoinit
import numpy as np
import matplotlib.pyplot as plt

arrayOfFloats = np.float64(np.random.sample(1024))
mod = SourceModule("""
  __global__ void myVeryFirstKernel(float* arrayOfFloats) {
    extern __shared__ float sharedData[];

    // Copy data to shared memory.
    sharedData[threadIdx.x] = arrayOfFloats[threadIdx.x];
  }
""")
func = mod.get_function('myVeryFirstKernel')
func(cuda.InOut(arrayOfFloats), block=(1024, 1, 1), grid=(1, 1))
print str(arrayOfFloats)

Strangely, I am getting this error.

[dfaux@harbinger CUDA_tutorials]$ python sharedMemoryExercise.py 
Traceback (most recent call last):
  File "sharedMemoryExercise.py", line 17, in <module>
    func(cuda.InOut(arrayOfFloats), block=(1024, 1, 1), grid=(1, 1))
  File "/software/linux/x86_64/epd-7.3-1-pycuda/lib/python2.7/site-packages/pycuda-2012.1-py2.7-linux-x86_64.egg/pycuda/driver.py", line 377, in function_call
    Context.synchronize()
pycuda._driver.LaunchError: cuCtxSynchronize failed: launch failed
PyCUDA WARNING: a clean-up operation failed (dead context maybe?)
cuMemFree failed: launch failed
PyCUDA WARNING: a clean-up operation failed (dead context maybe?)
cuModuleUnload failed: launch failed

I have tried to debug this error by changing the type of elements I am sending to my GPU (instead of float64, I use float32 for instance). I have also tried altering my block and grid sizes to no avail.

What could be wrong? What is a dead context? Any advice or ideas appreciated.

回答1:

One problem i see with your code is that you use extern __shared__ .. which means that you need to submit the size of the shared memory when you launch the kernel.

In pycuda this is done by:
func(cuda.InOut(arrayOfFloats), block=(1024, 1, 1), grid=(1, 1),shared=smem_size)
where smem_size is the size of the shared memory in bytes.

In your case smem_size = 1024*sizeof(float).



标签: cuda pycuda