TIOVX User Guide
vx_tutorial_graph_pipeline_two_nodes.c
Go to the documentation of this file.
1 /*
2 *
3 * Copyright (c) 2017 Texas Instruments Incorporated
4 *
5 * All rights reserved not granted herein.
6 *
7 * Limited License.
8 *
9 * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
10 * license under copyrights and patents it now or hereafter owns or controls to make,
11 * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
12 * terms herein. With respect to the foregoing patent license, such license is granted
13 * solely to the extent that any such patent is necessary to Utilize the software alone.
14 * The patent license shall not apply to any combinations which include this software,
15 * other than combinations with devices manufactured by or for TI ("TI Devices").
16 * No hardware patent is licensed hereunder.
17 *
18 * Redistributions must preserve existing copyright notices and reproduce this license
19 * (including the above copyright notice and the disclaimer and (if applicable) source
20 * code license limitations below) in the documentation and/or other materials provided
21 * with the distribution
22 *
23 * Redistribution and use in binary form, without modification, are permitted provided
24 * that the following conditions are met:
25 *
26 * * No reverse engineering, decompilation, or disassembly of this software is
27 * permitted with respect to any software provided in binary form.
28 *
29 * * any redistribution and use are licensed by TI for use only with TI Devices.
30 *
31 * * Nothing shall obligate TI to provide you with source code for the software
32 * licensed and provided to you in object code.
33 *
34 * If software source code is provided to you, modification and redistribution of the
35 * source code are permitted provided that the following conditions are met:
36 *
37 * * any redistribution and use of the source code, including any resulting derivative
38 * works, are licensed by TI for use only with TI Devices.
39 *
40 * * any redistribution and use of any object code compiled from the source code
41 * and any resulting derivative works, are licensed by TI for use only with TI Devices.
42 *
43 * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
44 *
45 * may be used to endorse or promote products derived from this software without
46 * specific prior written permission.
47 *
48 * DISCLAIMER.
49 *
50 * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
51 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
57 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
58 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
60 *
61 */
62 
81 #include <stdio.h>
82 #include <VX/vx_khr_pipelining.h>
83 #include <TI/tivx.h>
84 
85 /*
86  * Utility API used to add a graph parameter from a node, node parameter index
87  */
88 static void add_graph_parameter_by_node_index(vx_graph graph, vx_node node, vx_uint32 node_parameter_index)
89 {
90  vx_parameter parameter = vxGetParameterByIndex(node, node_parameter_index);
91 
92  vxAddParameterToGraph(graph, parameter);
93  vxReleaseParameter(&parameter);
94 }
95 
96 static void print_performance(vx_perf_t performance, uint32_t numPixels, const char* testName)
97 {
98  printf("[ %c%c ] Execution time for %9d pixels (avg = %4.6f ms, min = %4.6f ms, max = %4.6f ms)\n",
99  testName[0], testName[1],
100  numPixels,
101  performance.avg/1000000.0,
102  performance.min/1000000.0,
103  performance.max/1000000.0
104  );
105 }
106 
107 static void print_graph_pipeline_performance(vx_graph graph,
108  vx_node nodes[], uint32_t num_nodes,
109  uint64_t exe_time, uint32_t loop_cnt, uint32_t numPixels)
110 {
111  vx_perf_t perf_ref;
112  char ref_name[8];
113  uint32_t i;
114  uint64_t avg_exe_time;
115 
116  avg_exe_time = exe_time / loop_cnt;
117 
118  for(i=0; i<num_nodes; i++)
119  {
120  vxQueryNode(nodes[i], (vx_enum)VX_NODE_PERFORMANCE, &perf_ref, sizeof(perf_ref));
121  snprintf(ref_name, 8, "N%d ", i);
122  print_performance(perf_ref, numPixels, ref_name);
123  }
124 
125  printf("[ SYS ] Execution time (avg = %4d.%03d ms, sum = %4d.%03d ms, num = %d)\n",
126  (uint32_t)(avg_exe_time/1000u), (uint32_t)(avg_exe_time%1000u),
127  (uint32_t)(exe_time/1000u), (uint32_t)(exe_time%1000u),
128  loop_cnt
129  );
130 }
131 
136 {
137 /* max number of buffers to allocate for input and output */
138 #define MAX_NUM_BUF (2u)
139 
151  vx_context context;
152  vx_image in_img[MAX_NUM_BUF], tmp, out_img[MAX_NUM_BUF];
153  vx_node n0, n1;
154  vx_graph_parameter_queue_params_t graph_parameters_queue_params_list[2];
155  vx_graph graph;
156  uint32_t width, height, num_buf, pipeline_depth, buf_id, loop_id, loop_cnt, exe_time;
159  printf(" vx_tutorial_graph_pipeline_two_nodes: Tutorial Started !!! \n");
160 
168  context = vxCreateContext();
171  width = 64;
172  height = 64;
173  num_buf = MAX_NUM_BUF;
174  pipeline_depth = MAX_NUM_BUF;
175  loop_cnt = 10 - num_buf; /* runs the graph 10 times */
176 
177  graph = vxCreateGraph(context);
178 
186  for(buf_id=0; buf_id<num_buf; buf_id++)
187  {
188  in_img[buf_id] = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_U8);
189  out_img[buf_id] = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_U8);
190  }
191  tmp = vxCreateImage(context, width, height, (vx_df_image)VX_DF_IMAGE_U8);
199  n0 = vxNotNode(graph, in_img[0], tmp);
200  n1 = vxNotNode(graph, tmp, out_img[0]);
201 
203  #if defined(SOC_AM62A)
205  #else
207  #endif
208 
216  /* input @ n0 index 0, becomes graph parameter 0 */
217  add_graph_parameter_by_node_index(graph, n0, 0);
218  /* output @ n1 index 1, becomes graph parameter 1 */
219  add_graph_parameter_by_node_index(graph, n1, 1);
232  /* set graph schedule config such that graph parameter @ index 0 and 1 are enqueuable */
233  graph_parameters_queue_params_list[0].graph_parameter_index = 0;
234  graph_parameters_queue_params_list[0].refs_list_size = num_buf;
235  graph_parameters_queue_params_list[0].refs_list = (vx_reference*)&in_img[0];
236 
237  graph_parameters_queue_params_list[1].graph_parameter_index = 1;
238  graph_parameters_queue_params_list[1].refs_list_size = num_buf;
239  graph_parameters_queue_params_list[1].refs_list = (vx_reference*)&out_img[0];
240 
241  /* Schedule mode auto is used, here we dont need to call vxScheduleGraph
242  * Graph gets scheduled automatically as refs are enqueued to it
243  */
246  2,
247  graph_parameters_queue_params_list
248  );
256  /* explicitly set graph pipeline depth */
257  tivxSetGraphPipelineDepth(graph, pipeline_depth);
258 
259  /* make the 'tmp' reference which is output of n0 @ index 1 a "multi-buffer" */
260  tivxSetNodeParameterNumBufByIndex(n0, 1, num_buf);
270  vxVerifyGraph(graph);
271 
272  #if 0
273  tivxExportGraphToDot(graph, ".", "vx_tutorial_graph_pipeline_two_nodes");
274  #endif
275 
277  exe_time = tivxPlatformGetTimeInUsecs();
278 
285  for(buf_id=0; buf_id<num_buf; buf_id++)
286  {
287  /* reset output */
288  vxGraphParameterEnqueueReadyRef(graph, 1, (vx_reference*)&out_img[buf_id], 1);
289  /* load input */
290  vxGraphParameterEnqueueReadyRef(graph, 0, (vx_reference*)&in_img[buf_id], 1);
291  }
294  buf_id = 0;
295 
301  for(loop_id=0; loop_id<(loop_cnt+num_buf); loop_id++)
302  {
303  vx_image cur_out_img, cur_in_img;
304  uint32_t num_refs;
305 
306  /* Get output reference, waits until a reference is available */
307  vxGraphParameterDequeueDoneRef(graph, 1, (vx_reference*)&cur_out_img, 1, &num_refs);
308 
309  /* Get consumed input reference, waits until a reference is available
310  */
311  vxGraphParameterDequeueDoneRef(graph, 0, (vx_reference*)&cur_in_img, 1, &num_refs);
312 
313  /* A graph execution completed, since we dequeued both input and output refs */
314 
315  /* use output */
316 
317  buf_id = (buf_id+1)%num_buf;
318 
319  /* recycles dequeued input and output refs 'loop_cnt' times */
320  if(loop_id<loop_cnt)
321  {
322  /* input and output can be enqueued in any order */
323  vxGraphParameterEnqueueReadyRef(graph, 1, (vx_reference*)&cur_out_img, 1);
324  vxGraphParameterEnqueueReadyRef(graph, 0, (vx_reference*)&cur_in_img, 1);
325  }
326  }
327  /* ensure all graph processing is complete */
328  vxWaitGraph(graph);
331  exe_time = tivxPlatformGetTimeInUsecs() - exe_time;
332 
333  {
334  vx_node nodes[] = { n0, n1 };
335 
336  print_graph_pipeline_performance(graph, nodes, 2, exe_time, loop_cnt+num_buf, width*height);
337  }
338 
343  vxReleaseNode(&n0);
344  vxReleaseNode(&n1);
345  for(buf_id=0; buf_id<num_buf; buf_id++)
346  {
347  vxReleaseImage(&in_img[buf_id]);
348  vxReleaseImage(&out_img[buf_id]);
349  }
350  vxReleaseImage(&tmp);
351  vxReleaseGraph(&graph);
352  vxReleaseContext(&context);
355  printf(" vx_tutorial_graph_pipeline_two_nodes: Tutorial Done !!! \n");
356  printf(" \n");
357 }
struct _vx_image * vx_image
vx_uint64 avg
Schedule graph in queueing mode with auto scheduling.
vx_uint32 refs_list_size
Number of elements in array &#39;refs_list&#39;.
vx_status VX_API_CALL vxAddParameterToGraph(vx_graph graph, vx_parameter parameter)
uint64_t tivxPlatformGetTimeInUsecs(void)
Get the time in micro seconds.
Interface to TI extension APIs.
int32_t vx_enum
vx_status VX_API_CALL tivxSetNodeParameterNumBufByIndex(vx_node node, vx_uint32 index, vx_uint32 num_buf)
Set number of buffers to allocate at output of a node parameter.
VX_API_ENTRY vx_status VX_API_CALL vxSetGraphScheduleConfig(vx_graph graph, vx_enum graph_schedule_mode, vx_uint32 graph_parameters_list_size, const vx_graph_parameter_queue_params_t graph_parameters_queue_params_list[])
Sets the graph scheduler config.
struct _vx_parameter * vx_parameter
VX_DF_IMAGE_U8
vx_uint64 max
vx_status VX_API_CALL vxReleaseContext(vx_context *context)
VX_NODE_PERFORMANCE
struct _vx_context * vx_context
VX_API_ENTRY vx_status VX_API_CALL vxGraphParameterDequeueDoneRef(vx_graph graph, vx_uint32 graph_parameter_index, vx_reference *refs, vx_uint32 max_refs, vx_uint32 *num_refs)
Dequeues &#39;consumed&#39; references from a graph parameter.
VX_API_ENTRY vx_status VX_API_CALL vxGraphParameterEnqueueReadyRef(vx_graph graph, vx_uint32 graph_parameter_index, vx_reference *refs, vx_uint32 num_refs)
Enqueues new references into a graph parameter for processing.
vx_status VX_API_CALL vxSetNodeTarget(vx_node node, vx_enum target_enum, const char *target_string)
struct _vx_reference * vx_reference
The OpenVX Pipelining, Streaming and Batch Processing extension API.
#define TIVX_TARGET_DSP2
Name for DSP target class, instance 2.
VX_TARGET_STRING
uint32_t vx_df_image
vx_status VX_API_CALL tivxSetGraphPipelineDepth(vx_graph graph, vx_uint32 pipeline_depth)
Indicates to the implementation the depth of the graph pipeline.
vx_uint64 min
vx_reference * refs_list
Array of references that could be enqueued at a later point of time at this graph parameter...
vx_image VX_API_CALL vxCreateImage(vx_context context, vx_uint32 width, vx_uint32 height, vx_df_image color)
vx_status VX_API_CALL vxWaitGraph(vx_graph graph)
#define TIVX_TARGET_DSP1
Name for DSP target class, instance 1.
vx_status VX_API_CALL vxReleaseGraph(vx_graph *graph)
vx_node VX_API_CALL vxNotNode(vx_graph graph, vx_image input, vx_image output)
vx_parameter VX_API_CALL vxGetParameterByIndex(vx_node node, vx_uint32 index)
vx_status VX_API_CALL vxQueryNode(vx_node node, vx_enum attribute, void *ptr, vx_size size)
Queueing parameters for a specific graph parameter.
uint32_t vx_uint32
vx_graph VX_API_CALL vxCreateGraph(vx_context context)
vx_status VX_API_CALL vxVerifyGraph(vx_graph graph)
vx_context VX_API_CALL vxCreateContext()
uint32_t graph_parameter_index
Index of graph parameter to which these properties apply.
vx_status VX_API_CALL vxReleaseParameter(vx_parameter *param)
struct _vx_graph * vx_graph
vx_status VX_API_CALL tivxExportGraphToDot(vx_graph graph, const char *output_file_path, const char *output_file_prefix)
Export graph representation as DOT graph file.
vx_status VX_API_CALL vxReleaseNode(vx_node *node)
struct _vx_node * vx_node
vx_status VX_API_CALL vxReleaseImage(vx_image *image)
void vx_tutorial_graph_pipeline_two_nodes()
Tutorial Entry Point.