#NEWLINE# def test_pycuda_memory_to_theano( ) : #NEWLINE# #INDENT# y = pycuda . gpuarray . zeros ( ( 3 , 4 , 5 ) , 'float32' ) #NEWLINE# print ( sys . getrefcount ( y ) ) #NEWLINE# print ( 'gpuarray ref count before creating a CudaNdarray' , end = ' ' ) #NEWLINE# print ( sys . getrefcount ( y ) ) #NEWLINE# assert ( sys . getrefcount ( y ) == 2 ) #NEWLINE# rand = numpy . random . randn ( * y . shape ) . astype ( numpy . float32 ) #NEWLINE# cuda_rand = cuda_ndarray . CudaNdarray ( rand ) #NEWLINE# strides = [ 1 ] #NEWLINE# for i in y . shape [ : : ( - 1 ) ] [ : ( - 1 ) ] : #NEWLINE# #INDENT# strides . append ( ( strides [ ( - 1 ) ] * i ) ) #NEWLINE# #UNINDENT# strides = tuple ( strides [ : : ( - 1 ) ] ) #NEWLINE# print ( 'strides' , strides ) #NEWLINE# assert ( cuda_rand . _strides == strides ) , ( cuda_rand . _strides , strides ) #NEWLINE# y_ptr = int ( y . gpudata ) #NEWLINE# z = cuda_ndarray . from_gpu_pointer ( y_ptr , y . shape , strides , y ) #NEWLINE# print ( 'gpuarray ref count after creating a CudaNdarray' , sys . getrefcount ( y ) ) #NEWLINE# assert ( sys . getrefcount ( y ) == 3 ) #NEWLINE# assert ( numpy . asarray ( z ) == 0 ) . all ( ) #NEWLINE# assert ( z . base is y ) #NEWLINE# zz = z . view ( ) #NEWLINE# assert ( sys . getrefcount ( y ) == 4 ) #NEWLINE# assert ( zz . base is y ) #NEWLINE# del zz #NEWLINE# assert ( sys . getrefcount ( y ) == 3 ) #NEWLINE# cuda_ones = cuda_ndarray . CudaNdarray ( numpy . asarray ( [ [ [ 1 ] ] ] , dtype = 'float32' ) ) #NEWLINE# z += cuda_ones #NEWLINE# assert ( numpy . asarray ( z ) == numpy . ones ( y . shape ) ) . all ( ) #NEWLINE# assert ( numpy . asarray ( z ) == 1 ) . all ( ) #NEWLINE# assert ( cuda_rand . shape == y_ptr . shape ) #NEWLINE# assert ( cuda_rand . _strides == z . _strides ) , ( cuda_rand . _strides , z . _strides ) #NEWLINE# assert ( numpy . asarray ( cuda_rand ) == rand ) . all ( ) #NEWLINE# z += cuda_rand #NEWLINE# assert ( numpy . asarray ( z ) == ( rand + 1 ) ) . all ( ) #NEWLINE# del z #NEWLINE# print ( 'gpuarray ref count after deleting the CudaNdarray' , end = ' ' ) #NEWLINE# print ( sys . getrefcount ( y ) ) #NEWLINE# assert ( sys . getrefcount ( y ) == 2 )