I.II Programming models for parallel platforms
2.2 OpenVX advanced features
To recap what hast been introduced in Section 2, Listing 5.1 shows the typical structure of an OpenVX application:
• an execution context is created (line 1); • a graph instance is created (line 2);
• each image is defined with a call to vxCreateImage (lines 3-4) or vxCreateVirtualImage (5-6), specifying size and type (e.g., RGB or grayscale);
• each kernel is added to the graph as a node with a call to vx<KernelName>Node (lines 8-9), specifying a list of one or more input images, a list of scalar parameters (e.g., a threshold) and a list of output images;
• the vxVerifyGraph function (line 13) checks the graph consistency; • the vxProcessGraph function (line 15) executes the graph on the
target device, using a specific framework.
1 v x _ c o n t e x t ctx = v x C r e a t e C o n t e x t () ; 2 v x _ g r a p h graph = v x C r e a t e G r a p h () ; 3 v x _ i m a g e img0 = v x C r e a t e I m a g e ( ctx , < width > , 4 < height > , < type >) , 5 v x _ i m a g e vimg0 = v x C r e a t e V i r t u a l I m a g e ( ctx , < width > , 6 < height > , < type >) , 7 ...
8 v x _ n o d e node0 = vx < KernelName > Node ( graph ,
9 < input0 > ,... ,
10 < param0 > , ... ,
11 < output0 > , . . . ) ; 12 ...
2 Background 117
14 ...
15 status = v x P r o c e s s G r a p h ( graph ) ;
Listing 5.1: OpenVX program template.
OpenVX includes advanced features that allow for more dynamic behav- ior, such as graph updates and node callbacks.
Graph updates are dynamic modifications to a graph data structure following its first execution. This can be accomplished by nesting graph creation constructs (vx<KernelName>Node and vxRemoveNode, which re- moves a node from its parent graph) within conditional control flow in the program. When the original execution path is altered due to control flow, then the OpenVX standard requires a further verification stage be- fore executing a modified graph. Listing 5.2 shows the typical structure of an OpenVX application using graph updates, extending the code of Listing 5.1 with additional lines. The graph is executed multiple times in a loop structure, and when an application specific condition is met (e.g., environment conditions are changing) the graph is modified accordingly (e.g., a set of nodes implementing a specific algorithm is replaced with a different one). An else clause or additional if blocks can apply alter- native or additional modifications.
12 ... 13 status = v x V e r i f y G r a p h ( graph ) ; 14w h i l e( < r u n n i n g _ c o n d i t i o n >) 15 { 16 s t a t u s = v x P r o c e s s G r a p h ( g r a p h ) ; 17 ... 18 if( < g r a p h _ u p d a t e _ r e q u i r e d _ c o n d i t i o n >) 19 { 20 vx < K e r n e l N a m e > N o d e ( graph , . . . ) ; 21 ... 22 v x R e m o v e N o d e ( < n o d e _ v a r >) ; 23 ...
118 2 Background 24 s t a t u s = v x V e r i f y G r a p h ( g r a p h ) ; 25 } 26 e l s e 27 ... 28 }
Listing 5.2: Graph modifications
Node callbacks represent a mechanism to control the graph execu- tion flow on the host side by specifying a function to be called after the execution of a particular node. Node callbacks are set through the vx- AssignNodeCallback function with two parameters, the graph node and the callback function. Callbacks are intended to provide simple early exit conditions, based on the return value of the passed function. If VX_ACTION_CONTINUE is returned, the graph execution on the target de- vice will continue. If VX_ACTION_ABANDON is returned, execution is un- specified for all nodes whose execution must follow the callback owner. In practice, the execution of the graph on the device may be aborted due to application constraints (e.g., a deadline was missed or an intermediate result was sufficient to take some decisions). Listing 5.3 shows the typical usage of a node callback.
12 ... 13 v x A s s i g n N o d e C a l l b a c k ( < n o d e _ v a r > , f u n c ) ; 14 s t a t u s = v x V e r i f y G r a p h ( g r a p h ) ; 15 s t a t u s = v x P r o c e s s G r a p h ( g r a p h ) ; 16 ... 17 } 18 19 v x _ n o d e c o m p l e t e _ f func ( v x _ n o d e n ) 20 { 21 ... 22 if( < e x i t _ c o n d i t i o n >) 23 r e t u r n V X _ A C T I O N _ A B A N D O N ; 24 r e t u r n V X _ A C T I O N _ C O N T I N U E ; 25 }
2 Background 119
Listing 5.3: Node callback