<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function , division <EOL> import sys <EOL> import numpy <EOL> import theano <EOL> import theano . sandbox . cuda as cuda_ndarray <EOL> import theano . misc . pycuda_init <EOL> if not theano . misc . pycuda_init . pycuda_available : <EOL> from nose . plugins . skip import SkipTest <EOL> raise SkipTest ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if cuda_ndarray . cuda_available is False : <EOL> from nose . plugins . skip import SkipTest <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> import pycuda <EOL> import pycuda . driver as drv <EOL> import pycuda . gpuarray <EOL> def test_pycuda_only ( ) : <EOL> """<STR_LIT>""" <EOL> from pycuda . compiler import SourceModule <EOL> mod = SourceModule ( """<STR_LIT>""" ) <EOL> multiply_them = mod . get_function ( "<STR_LIT>" ) <EOL> a = numpy . random . randn ( <NUM_LIT:100> ) . astype ( numpy . float32 ) <EOL> b = numpy . random . randn ( <NUM_LIT:100> ) . astype ( numpy . float32 ) <EOL> dest = numpy . zeros_like ( a ) <EOL> multiply_them ( <EOL> drv . Out ( dest ) , drv . In ( a ) , drv . In ( b ) , <EOL> block = ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) , grid = ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> assert ( dest == a * b ) . all ( ) <EOL> def test_pycuda_theano ( ) : <EOL> """<STR_LIT>""" <EOL> from pycuda . compiler import SourceModule <EOL> mod = SourceModule ( """<STR_LIT>""" ) <EOL> multiply_them = mod . get_function ( "<STR_LIT>" ) <EOL> a = numpy . random . randn ( <NUM_LIT:100> ) . astype ( numpy . float32 ) <EOL> b = numpy . random . randn ( <NUM_LIT:100> ) . astype ( numpy . float32 ) <EOL> ga = cuda_ndarray . CudaNdarray ( a ) <EOL> gb = cuda_ndarray . CudaNdarray ( b ) <EOL> dest = cuda_ndarray . CudaNdarray . zeros ( a . shape ) <EOL> multiply_them ( dest , ga , gb , <EOL> block = ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) , grid = ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> assert ( numpy . asarray ( dest ) == a * b ) . all ( ) <EOL> def test_pycuda_memory_to_theano ( ) : <EOL> y = pycuda . gpuarray . zeros ( ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ) , '<STR_LIT>' ) <EOL> print ( sys . getrefcount ( y ) ) <EOL> print ( "<STR_LIT>" , end = '<STR_LIT:U+0020>' ) <EOL> print ( sys . getrefcount ( y ) ) <EOL> assert sys . getrefcount ( y ) == <NUM_LIT:2> <EOL> rand = numpy . random . randn ( * y . shape ) . astype ( numpy . float32 ) <EOL> cuda_rand = cuda_ndarray . CudaNdarray ( rand ) <EOL> strides = [ <NUM_LIT:1> ] <EOL> for i in y . shape [ : : - <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] : <EOL> strides . append ( strides [ - <NUM_LIT:1> ] * i ) <EOL> strides = tuple ( strides [ : : - <NUM_LIT:1> ] ) <EOL> print ( '<STR_LIT>' , strides ) <EOL> assert cuda_rand . _strides == strides , ( cuda_rand . _strides , strides ) <EOL> y_ptr = int ( y . gpudata ) <EOL> z = cuda_ndarray . from_gpu_pointer ( y_ptr , y . shape , strides , y ) <EOL> print ( "<STR_LIT>" , sys . getrefcount ( y ) ) <EOL> assert sys . getrefcount ( y ) == <NUM_LIT:3> <EOL> assert ( numpy . asarray ( z ) == <NUM_LIT:0> ) . all ( ) <EOL> assert z . base is y <EOL> zz = z . view ( ) <EOL> assert sys . getrefcount ( y ) == <NUM_LIT:4> <EOL> assert zz . base is y <EOL> del zz <EOL> assert sys . getrefcount ( y ) == <NUM_LIT:3> <EOL> cuda_ones = cuda_ndarray . CudaNdarray ( numpy . asarray ( [ [ [ <NUM_LIT:1> ] ] ] , <EOL> dtype = '<STR_LIT>' ) ) <EOL> z += cuda_ones <EOL> assert ( numpy . asarray ( z ) == numpy . ones ( y . shape ) ) . all ( ) <EOL> assert ( numpy . asarray ( z ) == <NUM_LIT:1> ) . all ( ) <EOL> assert cuda_rand . shape == z . shape <EOL> assert cuda_rand . _strides == z . _strides , ( cuda_rand . _strides , z . _strides ) <EOL> assert ( numpy . asarray ( cuda_rand ) == rand ) . all ( ) <EOL> z += cuda_rand <EOL> assert ( numpy . asarray ( z ) == ( rand + <NUM_LIT:1> ) ) . all ( ) <EOL> del z <EOL> print ( "<STR_LIT>" , end = '<STR_LIT:U+0020>' ) <EOL> print ( sys . getrefcount ( y ) ) <EOL> assert sys . getrefcount ( y ) == <NUM_LIT:2> </s>
