VisionWorks學習之OpenVX到VPI的遷移


本文主要介紹一下openVX遷移到VPI的一些注意事項,主要包含一些數據結構,以及如何初始化這些數據結構。數據結構包含object,images,pyramids,arrays

第一部分 Data Object Differences

OpenVx objects是半透明的,應用程序可以獲取對不透明數據對象的引用,openvx實現管理object memory (數據內容可以在給定時間駐留在系統中的任何位置),並且顯式地請求的對象的數據內容可以直接訪問。
VPI objects是以C結構體的方式對外展示的。應用程序分配和初始化object控制結構和Cuda內存,應用程序管理 CPU-GPU之間的同步。
下面顯示VX-VPI數據類型對應關係。
在這裏插入圖片描述
下面看一下VPI API 複雜數據對象的方法

Data Object Description
nvxcu__t 是基類,包含一些共有的信息
nvxcu_<object> _<property>_t 子類,我們要實例化的就是這些,

cv基元函數使用指向基結構的指針作爲參數。不需要前綴/後綴原語函數,因爲單個函數可以支持同一類的多種類型的數據對象。原語很容易擴展。說白了就是定義的函數使用的參數都是父類,使用的時候可以傳入子類作爲參數。
例如:

<primitive>(nvxcu_<object>_t *input, nvxcu_<object>_t *output)

VPI Images

下面來看一下nvxcu_image_t這個基類裏面包含哪些共有的數據參數:

參數 描述
image_type 指定image變體類型
format 指定image類型
width,height 指定圖像的大小

nv_cv_pitch_linear_image_t是使用pitch linear的cuda buffer實現的一個image,有以下成員:

成員 描述
base The base image structure (nvxcu_image_t)base.image.type must be NVXCU_PITCH_LINEAR_IMAGE.
planes Plan descriptor array. Must be consistent with base.format.
dev_ptr CUDA buffer的指針
pitch_in_bytes Specifies the pitch of the CUDA buffer, in bytes.

nvxcu__uniform_image_t實現所有像素值相同的圖像。它有以下成員:

Member Description
base The base image structure (nvxcu_image_t). base.image.type must be NVXCU_UNIFORM_IMAGE.
uniform_value Specifies the value of the pixel (nvxcu_pixel_value_t). Must be consistent with base.format.

創建一個Image

使用openVX創建:

vx_image im = vxCreateImage(context, 1920,1080, VX_DF_IMAGE_U8);

使用VPI創建一個image:

void *dev_ptr = NULL;
size_t pitch =0;
cudaMallocPitch(&dev_ptr,&pitch, 1920*sizeof(uint8_t),1080);
nvxcu_pitch_linear_image_t image;
image.base.image_type = NVXCU_PITCH_LINEAR_IMAGE;
image.base.format = NVXCU_DF_IMAGE_U8;
image.base.width = 1920;
image.base.height = 1080;
image.planes[0].dev_ptr = dev_ptr;
image.planes[0].pitch_in_bytes = pitch;

看一下VPI創建還是比較麻煩的,不過爲了效率也不要怕麻煩,畢竟人家快。

image使用示例

下面比較OpenVX和VPI之間的圖像使用情況OpenVX中的圖像使用

vx_image in = vxCreateImage(...);
vx_image_out = vxCreateImage(...);
vxuBox3x3(context, in, out);

使用VPI使用image usage

nvxcu_pitch_linear_image_t in = { ... } ; 
nvxcu_pitch_linear_image_t out = { ... } ; 
nvxcu_border_t border_mode = { ... } ; 
nvxcu_stream_exec_target_t target =  { ... } ;
nvxcuBox3x3(&in.base, &out.base, &border_mode, &target.base);

VPI pyramids

nvxcu_pyramid_t 包含以下參數:

Parameter Description
pyramid_type 指定金字塔變體類型.
num_levels 指定 金字塔有幾層.
scale 指定層級間的縮放因子.

nvxcu_pitch_linear_pyramid_t 使用pitch linear的cuda buffer實現的一個pyramid.

Member Description
base The base pyramid structure (nvxcu_pyramid_t). base.pyramid_type must be NVXCU_PITCH_LINEAR_PYRAMID.
levels A pointer to an array of base.num_levels image descriptors of type nvxcu_pitch_linear_image_t. Images must be consistent in terms of type and dimensions. levels[0] is the base of the pyramid (largest dimension).

創建一個pyramid

使用openVX創建一個pyramid

vx_pyramid pyr = vxCreatePyramid(context, num_levels, VX_SCALE_PYRAMID_HALF, 1920, 1080, VX_DF_IMAGE_U8);

使用VPI創建一個VPI:

nvxcu_pitch_linear_pyramid_t pyr;
pyr.base.pyramid_type = NVXCU_PITCH_LINEAR_PYRAMID;
pyr.base.num_levels = num_levels;
pyr.base.scale = NVXCU_SCALE_PYRAMID_HALF;
pyr.levels = malloc(num_levels * sizeof(nxcu_pitch_linear_image_t));
uint32_t cur_width = width, cur_height = height; float cur_scale = NVXCU_SCALE_PYRAMID_HALF;
for (uint32_t i = 0; i < num_levels; ++i) {
    cudaMallocPitch(&pyr.levels[i].planes[0].dev_ptr,
                &pyr.levels[i].planes[0].pitch_in_bytes,
                cur_width * sizeof(uint8_t), cur_height);
    pyr.levels[i].base.image_type = NVXCU_PITCH_LINEAR_IMAGE;
pyr.levels[i].base.format = NVXCU_DF_IMAGE_U8;
pyr.levels[i].base.width = cur_width;
pyr.levels[i].base.height = cur_height;
    cur_scale *= pyr.base.scale; 
cur_width = (uint32_t)ceilf(width * cur_scale);
cur_height = (uint32_t)ceilf(height * cur_scale);
}

VPI arrays

nvxcu_array_t is the base image structure. It includes the following parameters:

Parameter Description
array_type Specifies the variant of the array.
item_type Specifies the type of elements in the array.
capacity Specifies the maximum number of elements in the array.

nvxcu_plan_array_t array implementation uses plain linear CUDA memory. It has the following members:

Member Description
base The base array structure (nvxcu_array_t). base.array_type must be NVXCU_PLAIN_ARRAY.
dev_ptr A pointer to a CUDA buffer that can store at least base.capacity elements.
num_items_dev_ptr A pointer to the element counter in CUDA memory.

創建一個array

使用openVX:

vx_array array = vxCreateArray(context, NVX_TYPE_POINT2F, 1000);

使用VPI:

void * dev_ptr = NULL;
cudaMalloc(&dev_ptr, 1000 * sizeof(nvxcu_point2f_t));
uint32_t * num_items_dev_ptr = NULL;
cudaMalloc((void **)&num_items_dev_ptr, sizeof(uint32_t));
cudaMemset(num_items_dev_ptr, 0, sizeof(uint32_t));
nvxcu_plain_array_t array;
array.base.array_type = NVXCU_PLAIN_ARRAY;
array.base.item_type = NVXCU_TYPE_POINT2F;
array.base.capacity = 1000;
array.dev_ptr = dev_ptr;
array.num_items_dev_ptr = num_items_dev_ptr;

第二部分 OpenVX和VPI Primitives

提供一個OpenVX和VPI執行模型,target assignments和border mode

執行模型比較

下面看一下OpenVX和VPI執行模型的比較

OpenVX

openVX提供兩個可選的執行模型。

  1. 立即執行模式
    立即模式有一個類似於opencv的同步執行模型。這一切都是在運行時完成的,包括臨時內存分配。
vx_status status = vxu<Primitive>(context, <params>);
  1. Graph模式
    在圖形模式下,先把如參數檢查、內存分配和優化等先於執行。它執行異步節點執行和同步圖形執行。
// Ahead of time
vx_graph vxCreateCraph(context);
vx_node node = vx<Primitive>Node(graph, <params>);
vx_status verif_status = vxVerifyGraph(graph);
// Data process time
vx_status exec_status = vxProcessGraph(graph);

VPI模式

VPI執行模型包含三步:

  1. 查詢臨時內存中的需求(僅限複雜原語)。
nvxcu_tmp_buf_size_t tmp_size;
tmp_size = nvxcu<Primitive> GetBuff(<param metadata>, const struct cudaDeviceProp*);
  1. 分配臨時內存這可以是CUDA和主機內存,並且可以提前完成
nvxcu_tmp_buf_t tmp_buf = {NULL, NULL};
cudaMalloc(&tmp_buf.dev_ptr, tmp_size.dev_buf_size);
cudaMallocHost(&tmp_buf.host_ptr, tmp_size.host_buf_size);
  1. 異步原語執行
nvxcu_tmp_buf_size_t tmp_size;
tmp_size = nvxcu<Primitive>(<params, including tmp bufs>);

VPI執行模型例子

nvxcu_border_t              border = { ... } ; 
nvxcu_stream_exec_target_t  target = { ... } ;
// Query for needed temporary memory
nvxcu_tmp_buf_size_t gauss_pyr_buf_size_ = 
          nvxcuGaussianPyramid_GetBufSize(width, height, nb_levels, &border, &exec_target_.dev_prop);
// Allocate required buffers
nvxcu_tmp_buf_t tmp_buf = {NULL, NULL};
if (tmp_size.dev_buf_size > 0)
    cudaMalloc(&tmp_buf.dev_ptr, tmp_size.dev_buf_size);
if (tmp_size.host_buf_size > 0)
   cudaMallocHost(&tmp_buf.host_ptr, tmp_size.host_buf_size)
// Process data
nvxcu_pitch_linear_pyramid_t pyr = { /* must be width x height with nb_levels*/ }
nvxcuGaussianPyramid(&pyr.base, tmp_buf, &border, &exec_target_.base) );
// Synchronize the stream to get results
cudaStreamSynchronize(exec_target_.stream)

目標分配比較

Border Mode 比較

openvx中的目標分配是可選的,默認情況下是自動分配的。您可以使用以下選項手動將原語分配給GPU或CPU:

vxSetNodeTarget, vxSetImmediateModeTarget(...)

VPI中的運行在哪個設備上是固定好的,必須在每個基元執行調用(nvxcu_exec_target_t參數)處提供目標CUDA流

VPI 目標指定

nvxcu_exec_target_t是基本執行目標結構,包括以下參數:

Parameter Description
exec_target_type 這是當前支持的唯一CUDA流目標。

nvxcu_stream_exec_target_t array實現使用普通線性CUDA內存它包括下列成員:

Member Description
base The base target structure (nvxcu_exec_target_t). base.exec_target_type must be NVXCU_STREAM_EXEC_TARGET.
stream Specifies the CUDA stream.
dev_prop Specifies the CUDA device property for the stream (cudaDeviceProp).
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章