7

once tensorflow be active. it will make every cuda code crash even I use sess.close()...

the error msg is:

pycuda._driver.LogicError: cuFuncSetBlockShape failed: invalid resource handle

The following code it a simple example cuda code run by pycuda:
Once I add sess = tf.Session(). My cuda code crash. It work fine without sess = tf.Session().

import tensorflow as tf
import pycuda.autoinit
import pycuda.driver as drv
import numpy
from pycuda.compiler import SourceModule
mod = SourceModule("""
__global__ void multiply_them(float *dest, float *a, float *b)
{
  const int i = threadIdx.x;
  dest[i] = a[i] * b[i];
}
""")

## tensorflow will make any other cuda code crash............
sess = tf.Session()
sess.close()
## tensorflow will make any other cuda code crash............

multiply_them = mod.get_function("multiply_them")
a = numpy.random.randn(400).astype(numpy.float32)
b = numpy.random.randn(400).astype(numpy.float32)
dest = numpy.zeros_like(a)
multiply_them(drv.Out(dest), drv.In(a), drv.In(b), block=(400,1,1), grid=(1,1))
print (dest-a*b)
print("finish")

Any suggestion? Thanks~~~

Chi-Fang Hsieh
  • 235
  • 1
  • 3
  • 13

1 Answers1

2

I suspect same problem as here: Could pycuda and scikit-cuda work together?

Short answer: Do not use import pycuda.autoinit; this creates a new CUDA context that you would have to manage (push and pop) manually.

Instead, use import pycuda.autoprimaryctx to retain the already existing primary CUDA context that most other CUDA applications use automatically.

Yuri Feldman
  • 2,354
  • 1
  • 20
  • 23
dimitsev
  • 127
  • 1
  • 10