<s> import pycuda . gpuarray <EOL> from theano . sandbox import cuda <EOL> if cuda . cuda_available is False : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> def to_gpuarray ( x , copyif = False ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( x , cuda . CudaNdarray ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> size = <NUM_LIT:1> <EOL> c_contiguous = True <EOL> for i in range ( x . ndim - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if x . shape [ i ] == <NUM_LIT:1> : <EOL> continue <EOL> if x . _strides [ i ] != size : <EOL> c_contiguous = False <EOL> break <EOL> size *= x . shape [ i ] <EOL> if not c_contiguous : <EOL> if copyif : <EOL> x = x . copy ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> px = pycuda . gpuarray . GPUArray ( x . shape , x . dtype , base = x , gpudata = x . gpudata ) <EOL> return px <EOL> def to_cudandarray ( x ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( x , pycuda . gpuarray . GPUArray ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif x . dtype != "<STR_LIT>" : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> strides = [ <NUM_LIT:1> ] <EOL> for i in x . shape [ : : - <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] : <EOL> strides . append ( strides [ - <NUM_LIT:1> ] * i ) <EOL> strides = tuple ( strides [ : : - <NUM_LIT:1> ] ) <EOL> ptr = int ( x . gpudata ) <EOL> z = cuda . from_gpu_pointer ( ptr , x . shape , strides , x ) <EOL> return z </s>
