How to copy vx_array?

I just want to deep copy a vx_array ‘arr1’ to another vx_array ‘arr2’, it seems arr2=arr1 only copy the pointer, how can i copy the inner data?

Hi, @AastaLLL, How can i copy data from one graph to another?

Hi,

Please try this API:

vx_status VX_API_CALL vxCopyArrayRange ( vx_array array,
        vx_size range_start,
        vx_size range_end,
        vx_size user_stride,
        void *  user_ptr,
        vx_enum usage,
        vx_enum user_mem_type )

Document is located at:
[i]> More

File List
VX
vx_types.h
vx_array
[/i]

@AastaLLL, thank you for reply. I noticed that API before, while i thought it’s used for copy data between visionworks and CPU memory. If i want to copy between two vx_array, how should i set the params of this API, especially the user_ptr?

@AastaLLL, i have try this api. It works sometimes, but sometimes it dumped. Could you please have a look at my code?

Here is my code, sometimes i get “Segmentation fault (core dumped)” after call vxAddArrayItems.

BTW, i’m not sure what the param ‘stride’ means, i thought it was the size of elements.

inline void vxCopyArray(vx_array src, vx_array dst)
    {
        vx_size src_num = 0;
        NVXIO_SAFE_CALL( vxQueryArray(src, VX_ARRAY_ATTRIBUTE_NUMITEMS, &src_num, sizeof(src_num)) );
        vx_size dst_num = 0;
        NVXIO_SAFE_CALL( vxQueryArray(dst, VX_ARRAY_ATTRIBUTE_NUMITEMS, &dst_num, sizeof(dst_num)) );

        vx_size item_size = 0;
        NVXIO_SAFE_CALL( vxQueryArray(dst, VX_ARRAY_ITEMSIZE, &item_size, sizeof(item_size)) );

        if(src_num < dst_num)
        {
            NVXIO_SAFE_CALL( vxTruncateArray(dst, src_num) );
        }
        else if (src_num > dst_num)
        {
            vx_keypoint_t add;
            void * ptr = &add;
            vx_size num_add = src_num - dst_num;
            NVXIO_SAFE_CALL( vxAddArrayItems (dst, num_add, ptr ,item_size) );
        }
        vx_size stride;
        void *base = NULL;
        vx_map_id map_id;
        NVXIO_SAFE_CALL( vxMapArrayRange(src, 0, src_num, &map_id, &stride, &base, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, 0) );
        NVXIO_SAFE_CALL( vxCopyArrayRange(dst, 0, src_num, stride, base, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST) );
        NVXIO_SAFE_CALL( vxUnmapArrayRange(src, map_id) );
    }

Hi all, i fix the “Segmentation fault (core dumped)” problem when calling vxAddArrayItems().
It dumped because i didn’t allocate the memory for ptr, i fix it by allocate the array in cpu. The right code is below.

vx_size item_size = 0;
 NVXIO_SAFE_CALL( vxQueryArray(arr, VX_ARRAY_ITEMSIZE, &item_size, sizeof(item_size)) );
 vx_size num_add = n - arr_num;
 vx_keypoint_t *ptr = new vx_keypoint_t[num_add];
 NVXIO_SAFE_CALL( vxAddArrayItems (arr, num_add, ptr ,item_size) );
 delete [] ptr;

Hi all, i’d like to post my final code for copy an vx_array to another. It seems work well. You can use it.

inline vx_size vxLen(vx_array a)
{
    vx_size num_items = 0;
    NVXIO_SAFE_CALL( vxQueryArray(a, VX_ARRAY_ATTRIBUTE_NUMITEMS, &num_items, sizeof(num_items)) );
    return num_items;
}

inline void vxResizeArray(vx_array arr, vx_size n)
{
    vx_size arr_num = 0;
    NVXIO_SAFE_CALL( vxQueryArray(arr, VX_ARRAY_ATTRIBUTE_NUMITEMS, &arr_num, sizeof(arr_num)) );

    if(arr_num>n)
    {
        NVXIO_SAFE_CALL( vxTruncateArray(arr, n) );
    }
    else if(arr_num<n)
    {
        vx_size item_size = 0;
        NVXIO_SAFE_CALL( vxQueryArray(arr, VX_ARRAY_ITEMSIZE, &item_size, sizeof(item_size)) );
        vx_size num_add = n - arr_num;
        void *ptr = malloc(item_size*num_add);
        NVXIO_SAFE_CALL( vxAddArrayItems (arr, num_add, ptr ,item_size) );
        free(ptr);
    }
    NVXIO_SAFE_CALL( vxQueryArray(arr, VX_ARRAY_ATTRIBUTE_NUMITEMS, &arr_num, sizeof(arr_num)) );
}

inline void vxCopyArray(vx_array src, vx_array dst)
{
    vx_size src_num = vxLen(src);
    vxResizeArray(dst, src_num);

    vx_size stride;
    void *base = NULL;
    vx_map_id map_id;
    NVXIO_SAFE_CALL( vxMapArrayRange(src, 0, src_num, &map_id, &stride, &base, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, 0) );
    NVXIO_SAFE_CALL( vxCopyArrayRange(dst, 0, src_num, stride, base, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST) );
    NVXIO_SAFE_CALL( vxUnmapArrayRange(src, map_id) );
}