@@ -794,21 +794,20 @@ def test_IPC(size):
794794 assert p .exitcode == 0
795795
796796
797- def _arr_copy_to_host (carr ):
798- # TODO replace below with copy to device when exposed in python
799- buffers = []
800- for cbuf in carr .buffers ():
801- if cbuf is None :
802- buffers .append (None )
803- else :
804- buf = global_context .foreign_buffer (
805- cbuf .address , cbuf .size , cbuf
806- ).copy_to_host ()
807- buffers .append (buf )
808-
809- child = pa .Array .from_buffers (carr .type .value_type , 3 , buffers [2 :])
810- new = pa .Array .from_buffers (carr .type , 2 , buffers [:2 ], children = [child ])
811- return new
797+ def test_copy_to ():
798+ _ , buf = make_random_buffer (size = 10 , target = 'device' )
799+ mm_cuda = buf .memory_manager
800+
801+ arr = pa .array ([0 , 1 , 2 ])
802+ arr_cuda = arr .copy_to (mm_cuda )
803+ assert not arr_cuda .buffers ()[1 ].is_cpu
804+ assert arr_cuda .buffers ()[1 ].device_type == pa .DeviceAllocationType .CUDA
805+
806+ batch = pa .record_batch ({"col" : arr })
807+ batch_cuda = batch .copy_to (mm_cuda )
808+ buf_cuda = batch_cuda ["col" ].buffers ()[1 ]
809+ assert not buf_cuda .is_cpu
810+ assert buf_cuda .device_type == pa .DeviceAllocationType .CUDA
812811
813812
814813def test_device_interface_array ():
@@ -823,19 +822,10 @@ def test_device_interface_array():
823822 typ = pa .list_ (pa .int32 ())
824823 arr = pa .array ([[1 ], [2 , 42 ]], type = typ )
825824
826- # TODO replace below with copy to device when exposed in python
827- cbuffers = []
828- for buf in arr .buffers ():
829- if buf is None :
830- cbuffers .append (None )
831- else :
832- cbuf = global_context .new_buffer (buf .size )
833- cbuf .copy_from_host (buf , position = 0 , nbytes = buf .size )
834- cbuffers .append (cbuf )
835-
836- carr = pa .Array .from_buffers (typ , 2 , cbuffers [:2 ], children = [
837- pa .Array .from_buffers (typ .value_type , 3 , cbuffers [2 :])
838- ])
825+ # copy to device
826+ _ , buf = make_random_buffer (size = 10 , target = 'device' )
827+ mm_cuda = buf .memory_manager
828+ carr = arr .copy_to (mm_cuda )
839829
840830 # Type is known up front
841831 carr ._export_to_c_device (ptr_array )
@@ -849,7 +839,7 @@ def test_device_interface_array():
849839 del carr
850840 carr_new = pa .Array ._import_from_c_device (ptr_array , typ )
851841 assert carr_new .type == pa .list_ (pa .int32 ())
852- arr_new = _arr_copy_to_host ( carr_new )
842+ arr_new = carr_new . copy_to ( pa . default_cpu_memory_manager () )
853843 assert arr_new .equals (arr )
854844
855845 del carr_new
@@ -858,15 +848,13 @@ def test_device_interface_array():
858848 pa .Array ._import_from_c_device (ptr_array , typ )
859849
860850 # Schema is exported and imported at the same time
861- carr = pa .Array .from_buffers (typ , 2 , cbuffers [:2 ], children = [
862- pa .Array .from_buffers (typ .value_type , 3 , cbuffers [2 :])
863- ])
851+ carr = arr .copy_to (mm_cuda )
864852 carr ._export_to_c_device (ptr_array , ptr_schema )
865853 # Delete and recreate C++ objects from exported pointers
866854 del carr
867855 carr_new = pa .Array ._import_from_c_device (ptr_array , ptr_schema )
868856 assert carr_new .type == pa .list_ (pa .int32 ())
869- arr_new = _arr_copy_to_host ( carr_new )
857+ arr_new = carr_new . copy_to ( pa . default_cpu_memory_manager () )
870858 assert arr_new .equals (arr )
871859
872860 del carr_new
@@ -875,21 +863,6 @@ def test_device_interface_array():
875863 pa .Array ._import_from_c_device (ptr_array , ptr_schema )
876864
877865
878- def _batch_copy_to_host (cbatch ):
879- # TODO replace below with copy to device when exposed in python
880- arrs = []
881- for col in cbatch .columns :
882- buffers = [
883- global_context .foreign_buffer (buf .address , buf .size , buf ).copy_to_host ()
884- if buf is not None else None
885- for buf in col .buffers ()
886- ]
887- new = pa .Array .from_buffers (col .type , len (col ), buffers )
888- arrs .append (new )
889-
890- return pa .RecordBatch .from_arrays (arrs , schema = cbatch .schema )
891-
892-
893866def test_device_interface_batch_array ():
894867 cffi = pytest .importorskip ("pyarrow.cffi" )
895868 ffi = cffi .ffi
@@ -916,7 +889,7 @@ def test_device_interface_batch_array():
916889 del cbatch
917890 cbatch_new = pa .RecordBatch ._import_from_c_device (ptr_array , schema )
918891 assert cbatch_new .schema == schema
919- batch_new = _batch_copy_to_host ( cbatch_new )
892+ batch_new = cbatch_new . copy_to ( pa . default_cpu_memory_manager () )
920893 assert batch_new .equals (batch )
921894
922895 del cbatch_new
@@ -931,7 +904,7 @@ def test_device_interface_batch_array():
931904 del cbatch
932905 cbatch_new = pa .RecordBatch ._import_from_c_device (ptr_array , ptr_schema )
933906 assert cbatch_new .schema == schema
934- batch_new = _batch_copy_to_host ( cbatch_new )
907+ batch_new = cbatch_new . copy_to ( pa . default_cpu_memory_manager () )
935908 assert batch_new .equals (batch )
936909
937910 del cbatch_new
0 commit comments