<s> import ctypes <EOL> import numpy <EOL> from numba . cuda . cudadrv import driver , drvapi , devices <EOL> from numba . cuda . testing import unittest , CUDATestCase <EOL> from numba . utils import IS_PY3 <EOL> from numba . cuda . testing import skip_on_cudasim <EOL> @ skip_on_cudasim ( '<STR_LIT>' ) <EOL> class TestCudaMemory ( CUDATestCase ) : <EOL> def setUp ( self ) : <EOL> self . context = devices . get_context ( ) <EOL> def tearDown ( self ) : <EOL> del self . context <EOL> def _template ( self , obj ) : <EOL> self . assertTrue ( driver . is_device_memory ( obj ) ) <EOL> driver . require_device_memory ( obj ) <EOL> self . assertTrue ( isinstance ( obj . device_ctypes_pointer , <EOL> drvapi . cu_device_ptr ) ) <EOL> def test_device_memory ( self ) : <EOL> devmem = self . context . memalloc ( <NUM_LIT> ) <EOL> self . _template ( devmem ) <EOL> def test_device_view ( self ) : <EOL> devmem = self . context . memalloc ( <NUM_LIT> ) <EOL> self . _template ( devmem . view ( <NUM_LIT:10> ) ) <EOL> def test_host_alloc ( self ) : <EOL> devmem = self . context . memhostalloc ( <NUM_LIT> , mapped = True ) <EOL> self . _template ( devmem ) <EOL> def test_pinned_memory ( self ) : <EOL> ary = numpy . arange ( <NUM_LIT:10> ) <EOL> arybuf = ary if IS_PY3 else buffer ( ary ) <EOL> devmem = self . context . mempin ( arybuf , ary . ctypes . data , <EOL> ary . size * ary . dtype . itemsize , <EOL> mapped = True ) <EOL> self . _template ( devmem ) <EOL> @ skip_on_cudasim ( '<STR_LIT>' ) <EOL> class TestCudaMemoryFunctions ( CUDATestCase ) : <EOL> def setUp ( self ) : <EOL> self . context = devices . get_context ( ) <EOL> def tearDown ( self ) : <EOL> del self . context <EOL> def test_memcpy ( self ) : <EOL> hstary = numpy . arange ( <NUM_LIT:100> , dtype = numpy . uint32 ) <EOL> hstary2 = numpy . arange ( <NUM_LIT:100> , dtype = numpy . uint32 ) <EOL> sz = hstary . size * hstary . dtype . itemsize <EOL> devary = self . context . memalloc ( sz ) <EOL> driver . host_to_device ( devary , hstary , sz ) <EOL> driver . device_to_host ( hstary2 , devary , sz ) <EOL> self . assertTrue ( numpy . all ( hstary == hstary2 ) ) <EOL> def test_memset ( self ) : <EOL> dtype = numpy . dtype ( '<STR_LIT>' ) <EOL> n = <NUM_LIT:10> <EOL> sz = dtype . itemsize * <NUM_LIT:10> <EOL> devary = self . context . memalloc ( sz ) <EOL> driver . device_memset ( devary , <NUM_LIT> , sz ) <EOL> hstary = numpy . empty ( n , dtype = dtype ) <EOL> driver . device_to_host ( hstary , devary , sz ) <EOL> hstary2 = numpy . array ( [ <NUM_LIT> ] * n , dtype = numpy . dtype ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( numpy . all ( hstary == hstary2 ) ) <EOL> def test_d2d ( self ) : <EOL> hst = numpy . arange ( <NUM_LIT:100> , dtype = numpy . uint32 ) <EOL> hst2 = numpy . empty_like ( hst ) <EOL> sz = hst . size * hst . dtype . itemsize <EOL> dev1 = self . context . memalloc ( sz ) <EOL> dev2 = self . context . memalloc ( sz ) <EOL> driver . host_to_device ( dev1 , hst , sz ) <EOL> driver . device_to_device ( dev2 , dev1 , sz ) <EOL> driver . device_to_host ( hst2 , dev2 , sz ) <EOL> self . assertTrue ( numpy . all ( hst == hst2 ) ) <EOL> @ skip_on_cudasim ( '<STR_LIT>' ) <EOL> class TestMVExtent ( CUDATestCase ) : <EOL> def test_c_contiguous_array ( self ) : <EOL> ary = numpy . arange ( <NUM_LIT:100> ) <EOL> arysz = ary . dtype . itemsize * ary . size <EOL> s , e = driver . host_memory_extents ( ary ) <EOL> self . assertTrue ( ary . ctypes . data == s ) <EOL> self . assertTrue ( arysz == driver . host_memory_size ( ary ) ) <EOL> def test_f_contiguous_array ( self ) : <EOL> ary = numpy . asfortranarray ( numpy . arange ( <NUM_LIT:100> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:50> ) ) <EOL> arysz = ary . dtype . itemsize * numpy . prod ( ary . shape ) <EOL> s , e = driver . host_memory_extents ( ary ) <EOL> self . assertTrue ( ary . ctypes . data == s ) <EOL> self . assertTrue ( arysz == driver . host_memory_size ( ary ) ) <EOL> def test_single_element_array ( self ) : <EOL> ary = numpy . asarray ( numpy . uint32 ( <NUM_LIT> ) ) <EOL> arysz = ary . dtype . itemsize <EOL> s , e = driver . host_memory_extents ( ary ) <EOL> self . assertTrue ( ary . ctypes . data == s ) <EOL> self . assertTrue ( arysz == driver . host_memory_size ( ary ) ) <EOL> def test_ctypes_struct ( self ) : <EOL> class mystruct ( ctypes . Structure ) : <EOL> _fields_ = [ ( '<STR_LIT:x>' , ctypes . c_int ) , ( '<STR_LIT:y>' , ctypes . c_int ) ] <EOL> data = mystruct ( x = <NUM_LIT> , y = <NUM_LIT> ) <EOL> sz = driver . host_memory_size ( data ) <EOL> self . assertTrue ( ctypes . sizeof ( data ) == sz ) <EOL> def test_ctypes_double ( self ) : <EOL> data = ctypes . c_double ( <NUM_LIT> ) <EOL> sz = driver . host_memory_size ( data ) <EOL> self . assertTrue ( ctypes . sizeof ( data ) == sz ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
