提交 10278c5e authored 作者: Frederic's avatar Frederic

better pycuda/theano tests.

上级 ea49570d
...@@ -27,9 +27,8 @@ import pycuda.driver as drv ...@@ -27,9 +27,8 @@ import pycuda.driver as drv
import pycuda.gpuarray import pycuda.gpuarray
def test_pycuda_simple(): def test_pycuda_only():
x = cuda_ndarray.CudaNdarray.zeros((5, 5)) """Run pycuda only example to test that pycuda work."""
from pycuda.compiler import SourceModule from pycuda.compiler import SourceModule
mod = SourceModule(""" mod = SourceModule("""
__global__ void multiply_them(float *dest, float *a, float *b) __global__ void multiply_them(float *dest, float *a, float *b)
...@@ -41,9 +40,9 @@ __global__ void multiply_them(float *dest, float *a, float *b) ...@@ -41,9 +40,9 @@ __global__ void multiply_them(float *dest, float *a, float *b)
multiply_them = mod.get_function("multiply_them") multiply_them = mod.get_function("multiply_them")
# Test with pycuda in/out of numpy.ndarray
a = numpy.random.randn(100).astype(numpy.float32) a = numpy.random.randn(100).astype(numpy.float32)
b = numpy.random.randn(100).astype(numpy.float32) b = numpy.random.randn(100).astype(numpy.float32)
dest = numpy.zeros_like(a) dest = numpy.zeros_like(a)
multiply_them( multiply_them(
drv.Out(dest), drv.In(a), drv.In(b), drv.Out(dest), drv.In(a), drv.In(b),
...@@ -51,6 +50,31 @@ __global__ void multiply_them(float *dest, float *a, float *b) ...@@ -51,6 +50,31 @@ __global__ void multiply_them(float *dest, float *a, float *b)
assert (dest == a * b).all() assert (dest == a * b).all()
def test_pycuda_theano():
"""Simple example with pycuda function and Theano CudaNdarray object."""
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];
}
""")
multiply_them = mod.get_function("multiply_them")
a = numpy.random.randn(100).astype(numpy.float32)
b = numpy.random.randn(100).astype(numpy.float32)
# Test with Theano object
ga = cuda_ndarray.CudaNdarray(a)
gb = cuda_ndarray.CudaNdarray(b)
dest = cuda_ndarray.CudaNdarray.zeros(a.shape)
multiply_them(dest, ga, gb,
block=(400, 1, 1), grid=(1, 1))
assert (numpy.asarray(dest) == a * b).all()
def test_pycuda_memory_to_theano(): def test_pycuda_memory_to_theano():
#Test that we can use the GpuArray memory space in pycuda in a CudaNdarray #Test that we can use the GpuArray memory space in pycuda in a CudaNdarray
y = pycuda.gpuarray.zeros((3, 4, 5), 'float32') y = pycuda.gpuarray.zeros((3, 4, 5), 'float32')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论