|
| 1 | +/** |
| 2 | + * @file pipelining_multicore.c |
| 3 | + * @brief OpenVX Pipelining Extension — Multicore Sample |
| 4 | + * |
| 5 | + * This sample demonstrates how to use the OpenVX pipelining extension |
| 6 | + * with multicore (wave-based parallel) execution on a compute graph. |
| 7 | + * |
| 8 | + * Requirements: |
| 9 | + * - rustVX built with -DOPENVX_USE_PIPELINING=ON |
| 10 | + * - OPENVX_PIPELINING_THREADS env var (optional) |
| 11 | + * |
| 12 | + * Build: |
| 13 | + * gcc -o pipelining_multicore pipelining_multicore.c -lopenvx -I/path/to/openvx/include |
| 14 | + * |
| 15 | + * Run: |
| 16 | + * ./pipelining_multicore # auto-detect thread count |
| 17 | + * OPENVX_PIPELINING_THREADS=4 ./pipelining_multicore # use 4 threads |
| 18 | + */ |
| 19 | + |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <string.h> |
| 23 | +#include <vx/vx.h> |
| 24 | + |
| 25 | +#define WIDTH 640 |
| 26 | +#define HEIGHT 480 |
| 27 | +#define ITERS 100 |
| 28 | + |
| 29 | +/* Simple user kernel: fill image with a constant value */ |
| 30 | +vx_status VX_CALLBACK fillKernel(vx_node node, const vx_reference *params, vx_uint32 num) |
| 31 | +{ |
| 32 | + (void)node; |
| 33 | + (void)num; |
| 34 | + vx_image out = (vx_image)params[0]; |
| 35 | + vx_scalar val_s = (vx_scalar)params[1]; |
| 36 | + vx_uint8 val = 0; |
| 37 | + vxCopyScalar(val_s, &val, VX_READ_ONLY, VX_MEMORY_TYPE_HOST); |
| 38 | + |
| 39 | + vx_rectangle_t rect = {0, 0, WIDTH, HEIGHT}; |
| 40 | + vx_imagepatch_addressing_t addr; |
| 41 | + void *base = NULL; |
| 42 | + vx_map_id map_id; |
| 43 | + vxMapImagePatch(out, &rect, 0, &map_id, &addr, &base, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST, 0); |
| 44 | + |
| 45 | + memset(base, val, addr.stride_y * HEIGHT); |
| 46 | + |
| 47 | + vxUnmapImagePatch(out, map_id); |
| 48 | + return VX_SUCCESS; |
| 49 | +} |
| 50 | + |
| 51 | +int main(int argc, char **argv) |
| 52 | +{ |
| 53 | + (void)argc; (void)argv; |
| 54 | + vx_status status; |
| 55 | + vx_context context = vxCreateContext(); |
| 56 | + |
| 57 | + /* Query pipelining extension availability */ |
| 58 | + vx_bool pipelining = vx_false_e; |
| 59 | + vxQueryContext(context, VX_CONTEXT_EXTENSIONS, &pipelining, sizeof(pipelining)); |
| 60 | + if (!pipelining) { |
| 61 | + fprintf(stderr, "OpenVX pipelining extension not available.\n"); |
| 62 | + fprintf(stderr, "Build rustVX with: -DOPENVX_USE_PIPELINING=ON\n"); |
| 63 | + return 1; |
| 64 | + } |
| 65 | + printf("✓ OpenVX Pipelining Extension available\n"); |
| 66 | + |
| 67 | + /* Create a graph with parallel branches for multicore execution */ |
| 68 | + vx_graph graph = vxCreateGraph(context); |
| 69 | + |
| 70 | + /* Graph parameters: input image + 4 output images (parallel branches) */ |
| 71 | + vx_image input = vxCreateImage(context, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 72 | + vx_image out_a = vxCreateImage(context, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 73 | + vx_image out_b = vxCreateImage(context, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 74 | + vx_image out_c = vxCreateImage(context, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 75 | + vx_image out_d = vxCreateImage(context, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 76 | + |
| 77 | + /* Scalar parameter (fill value) */ |
| 78 | + vx_uint8 fill_val = 128; |
| 79 | + vx_scalar scalar = vxCreateScalar(context, VX_TYPE_UINT8, &fill_val); |
| 80 | + |
| 81 | + /* Create user kernel for fill operation */ |
| 82 | + vx_kernel kernel = vxAddUserKernel(context, "example.fill", VX_KERNEL_BASE(VX_ID_USER, 0) + 1, |
| 83 | + fillKernel, 2, |
| 84 | + NULL, NULL, NULL); |
| 85 | + if (kernel) { |
| 86 | + vxAddParameterToKernel(kernel, 0, VX_OUTPUT, VX_TYPE_IMAGE, VX_PARAMETER_STATE_REQUIRED); |
| 87 | + vxAddParameterToKernel(kernel, 1, VX_INPUT, VX_TYPE_SCALAR, VX_PARAMETER_STATE_REQUIRED); |
| 88 | + vxFinalizeKernel(kernel); |
| 89 | + } |
| 90 | + |
| 91 | + /* Build graph: input → [parallel branches] → outputs */ |
| 92 | + /* Branch A: Gaussian blur */ |
| 93 | + vx_image tmp_a = vxCreateVirtualImage(graph, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 94 | + vx_node n_gauss = vxGaussian3x3Node(graph, input, tmp_a); |
| 95 | + |
| 96 | + /* Branch B: Box filter */ |
| 97 | + vx_image tmp_b = vxCreateVirtualImage(graph, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 98 | + vx_node n_box = vxBox3x3Node(graph, input, tmp_b); |
| 99 | + |
| 100 | + /* Branch C: Dilate */ |
| 101 | + vx_image tmp_c = vxCreateVirtualImage(graph, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 102 | + vx_node n_dilate = vxDilate3x3Node(graph, input, tmp_c); |
| 103 | + |
| 104 | + /* Branch D: Erode */ |
| 105 | + vx_image tmp_d = vxCreateVirtualImage(graph, WIDTH, HEIGHT, VX_DF_IMAGE_U8); |
| 106 | + vx_node n_erode = vxErode3x3Node(graph, input, tmp_d); |
| 107 | + |
| 108 | + /* Second wave: user kernel fills on each branch output */ |
| 109 | + vx_node n_fill_a = vxCreateGenericNode(graph, kernel); |
| 110 | + vxSetParameterByIndex(n_fill_a, 0, (vx_reference)out_a); |
| 111 | + vxSetParameterByIndex(n_fill_a, 1, (vx_reference)scalar); |
| 112 | + |
| 113 | + vx_node n_fill_b = vxCreateGenericNode(graph, kernel); |
| 114 | + vxSetParameterByIndex(n_fill_b, 0, (vx_reference)out_b); |
| 115 | + vxSetParameterByIndex(n_fill_b, 1, (vx_reference)scalar); |
| 116 | + |
| 117 | + vx_node n_fill_c = vxCreateGenericNode(graph, kernel); |
| 118 | + vxSetParameterByIndex(n_fill_c, 0, (vx_reference)out_c); |
| 119 | + vxSetParameterByIndex(n_fill_c, 1, (vx_reference)scalar); |
| 120 | + |
| 121 | + vx_node n_fill_d = vxCreateGenericNode(graph, kernel); |
| 122 | + vxSetParameterByIndex(n_fill_d, 0, (vx_reference)out_d); |
| 123 | + vxSetParameterByIndex(n_fill_d, 1, (vx_reference)scalar); |
| 124 | + |
| 125 | + /* Configure graph parameters for pipelining */ |
| 126 | + vxAddParameterToGraph(graph, (vx_parameter)vxGetParameterByIndex(n_gauss, 0)); /* input */ |
| 127 | + vxAddParameterToGraph(graph, (vx_parameter)vxGetParameterByIndex(n_fill_a, 0)); /* out_a */ |
| 128 | + vxAddParameterToGraph(graph, (vx_parameter)vxGetParameterByIndex(n_fill_b, 0)); /* out_b */ |
| 129 | + vxAddParameterToGraph(graph, (vx_parameter)vxGetParameterByIndex(n_fill_c, 0)); /* out_c */ |
| 130 | + vxAddParameterToGraph(graph, (vx_parameter)vxGetParameterByIndex(n_fill_d, 0)); /* out_d */ |
| 131 | + |
| 132 | + /* Verify graph — this computes topological waves for multicore execution */ |
| 133 | + status = vxVerifyGraph(graph); |
| 134 | + if (status != VX_SUCCESS) { |
| 135 | + fprintf(stderr, "Graph verification failed: %d\n", status); |
| 136 | + return 1; |
| 137 | + } |
| 138 | + printf("✓ Graph verified (topological waves computed)\n"); |
| 139 | + |
| 140 | + /* Enable pipelining with QUEUE_AUTO mode */ |
| 141 | + vx_graph_parameter_queue_params_t queue_params[5]; |
| 142 | + for (int i = 0; i < 5; i++) { |
| 143 | + queue_params[i].graph_parameter_index = i; |
| 144 | + queue_params[i].refs_list = NULL; /* Will be set per enqueue */ |
| 145 | + queue_params[i].refs_list_size = 1; |
| 146 | + } |
| 147 | + |
| 148 | + vxSetGraphScheduleConfig(graph, VX_GRAPH_SCHEDULE_MODE_QUEUE_AUTO, |
| 149 | + 5, queue_params); |
| 150 | + printf("✓ Pipelining mode set to QUEUE_AUTO\n"); |
| 151 | + |
| 152 | + /* Schedule graph (starts background executor thread) */ |
| 153 | + vxScheduleGraph(graph); |
| 154 | + printf("✓ Graph scheduled (executor thread started)\n"); |
| 155 | + |
| 156 | + /* Warmup */ |
| 157 | + printf("Warming up...\n"); |
| 158 | + for (int i = 0; i < 10; i++) { |
| 159 | + vx_graph_parameter_enqueue_ready_ref(graph, 0, (vx_reference)input, 1); |
| 160 | + vx_reference out_refs[4] = {(vx_reference)out_a, (vx_reference)out_b, |
| 161 | + (vx_reference)out_c, (vx_reference)out_d}; |
| 162 | + for (int j = 1; j < 5; j++) { |
| 163 | + vx_graph_parameter_enqueue_ready_ref(graph, j, out_refs[j-1], 1); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /* Benchmark */ |
| 168 | + printf("Running benchmark (%d iterations)...\n", ITERS); |
| 169 | + vx_uint64 t0 = vxGetTimestamp(context); |
| 170 | + |
| 171 | + for (int i = 0; i < ITERS; i++) { |
| 172 | + /* Enqueue input frame */ |
| 173 | + vx_graph_parameter_enqueue_ready_ref(graph, 0, (vx_reference)input, 1); |
| 174 | + |
| 175 | + /* Enqueue output buffers */ |
| 176 | + vx_reference out_refs[4] = {(vx_reference)out_a, (vx_reference)out_b, |
| 177 | + (vx_reference)out_c, (vx_reference)out_d}; |
| 178 | + for (int j = 1; j < 5; j++) { |
| 179 | + vx_graph_parameter_enqueue_ready_ref(graph, j, out_refs[j-1], 1); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /* Flush remaining frames */ |
| 184 | + vxWaitGraph(graph); |
| 185 | + |
| 186 | + vx_uint64 t1 = vxGetTimestamp(context); |
| 187 | + double ms = (double)(t1 - t0) / 1000000.0; |
| 188 | + double fps = (ITERS * 1000.0) / ms; |
| 189 | + |
| 190 | + printf("\n=== Results ===\n"); |
| 191 | + printf("Total time: %.2f ms\n", ms); |
| 192 | + printf("Iterations: %d\n", ITERS); |
| 193 | + printf("Throughput: %.2f FPS\n", fps); |
| 194 | + printf("\nNotes:\n"); |
| 195 | + printf("- Nodes in Wave 0 (Gaussian, Box, Dilate, Erode) execute in parallel\n"); |
| 196 | + printf("- Nodes in Wave 1 (4× Fill) execute in parallel after Wave 0\n"); |
| 197 | + printf("- Set OPENVX_PIPELINING_THREADS=N to control thread pool size\n"); |
| 198 | + |
| 199 | + /* Cleanup */ |
| 200 | + vxReleaseGraph(&graph); |
| 201 | + vxReleaseImage(&input); |
| 202 | + vxReleaseImage(&out_a); |
| 203 | + vxReleaseImage(&out_b); |
| 204 | + vxReleaseImage(&out_c); |
| 205 | + vxReleaseImage(&out_d); |
| 206 | + vxReleaseScalar(&scalar); |
| 207 | + vxRemoveKernel(kernel); |
| 208 | + vxReleaseContext(&context); |
| 209 | + |
| 210 | + return 0; |
| 211 | +} |
0 commit comments