@@ -827,21 +827,29 @@ def test_IPC(size):
827827 assert p .exitcode == 0
828828
829829
830- def _arr_copy_to_host (carr ):
831- # TODO replace below with copy to device when exposed in python
832- buffers = []
833- for cbuf in carr .buffers ():
834- if cbuf is None :
835- buffers .append (None )
836- else :
837- buf = global_context .foreign_buffer (
838- cbuf .address , cbuf .size , cbuf
839- ).copy_to_host ()
840- buffers .append (buf )
841-
842- child = pa .Array .from_buffers (carr .type .value_type , 3 , buffers [2 :])
843- new = pa .Array .from_buffers (carr .type , 2 , buffers [:2 ], children = [child ])
844- return new
830+ def test_copy_to ():
831+ _ , buf = make_random_buffer (size = 10 , target = 'device' )
832+ mm_cuda = buf .memory_manager
833+
834+ for dest in [mm_cuda , mm_cuda .device ]:
835+ arr = pa .array ([0 , 1 , 2 ])
836+ arr_cuda = arr .copy_to (dest )
837+ assert not arr_cuda .buffers ()[1 ].is_cpu
838+ assert arr_cuda .buffers ()[1 ].device_type == pa .DeviceAllocationType .CUDA
839+ assert arr_cuda .buffers ()[1 ].device == mm_cuda .device
840+
841+ arr_roundtrip = arr_cuda .copy_to (pa .default_cpu_memory_manager ())
842+ assert arr_roundtrip .equals (arr )
843+
844+ batch = pa .record_batch ({"col" : arr })
845+ batch_cuda = batch .copy_to (dest )
846+ buf_cuda = batch_cuda ["col" ].buffers ()[1 ]
847+ assert not buf_cuda .is_cpu
848+ assert buf_cuda .device_type == pa .DeviceAllocationType .CUDA
849+ assert buf_cuda .device == mm_cuda .device
850+
851+ batch_roundtrip = batch_cuda .copy_to (pa .default_cpu_memory_manager ())
852+ assert batch_roundtrip .equals (batch )
845853
846854
847855def test_device_interface_array ():
@@ -856,19 +864,10 @@ def test_device_interface_array():
856864 typ = pa .list_ (pa .int32 ())
857865 arr = pa .array ([[1 ], [2 , 42 ]], type = typ )
858866
859- # TODO replace below with copy to device when exposed in python
860- cbuffers = []
861- for buf in arr .buffers ():
862- if buf is None :
863- cbuffers .append (None )
864- else :
865- cbuf = global_context .new_buffer (buf .size )
866- cbuf .copy_from_host (buf , position = 0 , nbytes = buf .size )
867- cbuffers .append (cbuf )
868-
869- carr = pa .Array .from_buffers (typ , 2 , cbuffers [:2 ], children = [
870- pa .Array .from_buffers (typ .value_type , 3 , cbuffers [2 :])
871- ])
867+ # copy to device
868+ _ , buf = make_random_buffer (size = 10 , target = 'device' )
869+ mm_cuda = buf .memory_manager
870+ carr = arr .copy_to (mm_cuda )
872871
873872 # Type is known up front
874873 carr ._export_to_c_device (ptr_array )
@@ -882,7 +881,7 @@ def test_device_interface_array():
882881 del carr
883882 carr_new = pa .Array ._import_from_c_device (ptr_array , typ )
884883 assert carr_new .type == pa .list_ (pa .int32 ())
885- arr_new = _arr_copy_to_host ( carr_new )
884+ arr_new = carr_new . copy_to ( pa . default_cpu_memory_manager () )
886885 assert arr_new .equals (arr )
887886
888887 del carr_new
@@ -891,15 +890,13 @@ def test_device_interface_array():
891890 pa .Array ._import_from_c_device (ptr_array , typ )
892891
893892 # Schema is exported and imported at the same time
894- carr = pa .Array .from_buffers (typ , 2 , cbuffers [:2 ], children = [
895- pa .Array .from_buffers (typ .value_type , 3 , cbuffers [2 :])
896- ])
893+ carr = arr .copy_to (mm_cuda )
897894 carr ._export_to_c_device (ptr_array , ptr_schema )
898895 # Delete and recreate C++ objects from exported pointers
899896 del carr
900897 carr_new = pa .Array ._import_from_c_device (ptr_array , ptr_schema )
901898 assert carr_new .type == pa .list_ (pa .int32 ())
902- arr_new = _arr_copy_to_host ( carr_new )
899+ arr_new = carr_new . copy_to ( pa . default_cpu_memory_manager () )
903900 assert arr_new .equals (arr )
904901
905902 del carr_new
@@ -908,21 +905,6 @@ def test_device_interface_array():
908905 pa .Array ._import_from_c_device (ptr_array , ptr_schema )
909906
910907
911- def _batch_copy_to_host (cbatch ):
912- # TODO replace below with copy to device when exposed in python
913- arrs = []
914- for col in cbatch .columns :
915- buffers = [
916- global_context .foreign_buffer (buf .address , buf .size , buf ).copy_to_host ()
917- if buf is not None else None
918- for buf in col .buffers ()
919- ]
920- new = pa .Array .from_buffers (col .type , len (col ), buffers )
921- arrs .append (new )
922-
923- return pa .RecordBatch .from_arrays (arrs , schema = cbatch .schema )
924-
925-
926908def test_device_interface_batch_array ():
927909 cffi = pytest .importorskip ("pyarrow.cffi" )
928910 ffi = cffi .ffi
@@ -949,7 +931,7 @@ def test_device_interface_batch_array():
949931 del cbatch
950932 cbatch_new = pa .RecordBatch ._import_from_c_device (ptr_array , schema )
951933 assert cbatch_new .schema == schema
952- batch_new = _batch_copy_to_host ( cbatch_new )
934+ batch_new = cbatch_new . copy_to ( pa . default_cpu_memory_manager () )
953935 assert batch_new .equals (batch )
954936
955937 del cbatch_new
@@ -964,7 +946,7 @@ def test_device_interface_batch_array():
964946 del cbatch
965947 cbatch_new = pa .RecordBatch ._import_from_c_device (ptr_array , ptr_schema )
966948 assert cbatch_new .schema == schema
967- batch_new = _batch_copy_to_host ( cbatch_new )
949+ batch_new = cbatch_new . copy_to ( pa . default_cpu_memory_manager () )
968950 assert batch_new .equals (batch )
969951
970952 del cbatch_new
0 commit comments