//////// // The node creation interface for the "app.userkernels.tensor_cos" kernel. // This user kernel example expects parameters in the following order: // parameter #0 -- input tensor of format VX_TYPE_INT16 // parameter #1 -- output tensor of format VX_TYPE_INT16 // // TODO STEP 01:******** // 1. Use vxGetKernelByEnum API to get a kernel object from USER_KERNEL_TENSOR_COS. // Note that you need to use vxGetContext API to get the context from a graph object. // 2. Use vxCreateGenericNode API to create a node from the kernel object. // 3. Use vxSetParameterByIndex API to set node arguments. // 4. Release the kernel object that are not needed any more. // 5. Use ERROR_CHECK_OBJECT and ERROR_CHECK_STATUS macros for error detection. vx_node userTensorCosNode( vx_graph graph, vx_tensor input, vx_tensor output ) { vx_context context = vxGetContext( ( vx_reference ) graph ); vx_kernel kernel = vxGetKernelByEnum( context, USER_KERNEL_TENSOR_COS ); ERROR_CHECK_OBJECT( kernel ); vx_node node = vxCreateGenericNode( graph, kernel ); ERROR_CHECK_OBJECT( node ); // ERROR_CHECK_STATUS( vxSetParameterByIndex( node, 0, ( vx_reference ) /* Fill in parameter */ ) ); // ERROR_CHECK_STATUS( vxSetParameterByIndex( node, 1, ( vx_reference ) /* Fill in parameter */ ) ); ERROR_CHECK_STATUS( vxReleaseKernel( &kernel ) ); return node; }
static VALUE Kernel_init(VALUE self, VALUE param) { Check_Type(self, T_DATA); switch (TYPE(param)) { case T_FIXNUM: DATA_PTR(self) = (void *)vxGetKernelByEnum(context, FIX2INT(param)); break; case T_STRING: DATA_PTR(self) = (void *)vxGetKernelByName(context, RSTRING(param)->ptr); break; default: REXT_PRINT("TYPE(param) = %d\n", TYPE(param)); rb_raise(rb_eTypeError, "wrong type"); break; } return Qnil; }
vx_node vxCreateNodeByStructure(vx_graph graph, vx_enum kernelenum, vx_parameter_item_t *params, vx_uint32 num) { vx_status status = VX_SUCCESS; vx_node node = 0; vx_context context = vxGetContext(graph); vx_kernel kernel = vxGetKernelByEnum(context, kernelenum); if (kernel) { node = vxCreateNode(graph, kernel); if (node) { vx_uint32 p = 0; for (p = 0; p < num; p++) { status = vxSetParameterByIndex(node, p, params[p].direction, params[p].reference); if (status != VX_SUCCESS) { vxAddLogEntry(graph, status, "Kernel %d Parameter %u is invalid.\n", kernelenum, p); vxReleaseNode(&node); node = 0; break; } } } else { vxAddLogEntry(graph, VX_ERROR_INVALID_PARAMETERS, "Failed to create node with kernel enum %d\n", kernelenum); status = VX_ERROR_NO_MEMORY; } vxReleaseKernel(&kernel); } else { vxAddLogEntry(graph, VX_ERROR_INVALID_PARAMETERS, "failed to retrieve kernel enum %d\n", kernelenum); status = VX_ERROR_NOT_SUPPORTED; } return node; }
vx_node vx_create_specific_sobel(vx_context context, vx_graph graph, vx_bool easy) { vx_node n = 0; if (easy == vx_false_e) { //! [firstmethod] vx_kernel kernel = vxGetKernelByEnum(context, VX_KERNEL_SOBEL_3x3); vx_node node = vxCreateGenericNode(graph, kernel); //! [firstmethod] #if defined(EXPERIMENTAL_USE_TARGET) vx_target target = vxGetTargetByName(context, "gpu"); vxAssignNodeAffinity(node, target); #endif #if defined(EXPERIMENTAL_USE_VARIANTS) //! [variant:firstmethod] vxChooseKernelVariant(node, "faster"); //! [variant:firstmethod] #endif n = node; } else { #if defined(EXPERIMENTAL_USE_VARIANTS) //! [variant:second] #if defined(EXPERIMENTAL_USE_TARGET) vx_kernel kernel = vxGetKernelByName(context, "cpu:org.khronos.openvx.sobel3x3:faster"); #else vx_kernel kernel = vxGetKernelByName(context, "org.khronos.openvx.sobel3x3:faster"); #endif vx_node node = vxCreateGenericNode(graph, kernel); //! [variant:second] #else /*defined(EXPERIMENTAL_USE_VARIANTS)*/ //! [secondmethod] vx_kernel kernel = vxGetKernelByName(context, "org.khronos.openvx.sobel3x3"); vx_node node = vxCreateGenericNode(graph, kernel); //! [secondmethod] #endif n = node; } return n; }
int main(int argc, char *argv[]) { vx_status status = VX_SUCCESS; vx_context context = vxCreateContext(); if (vxGetStatus((vx_reference)context) == VX_SUCCESS) { vx_char implementation[VX_MAX_IMPLEMENTATION_NAME]; vx_char *extensions = NULL; vx_int32 m, modules = 0; vx_uint32 k, kernels = 0; vx_uint32 p, parameters = 0; vx_uint32 a = 0; vx_uint16 vendor, version; vx_size size = 0; vx_kernel_info_t *table = NULL; // take each arg as a module name to load for (m = 1; m < argc; m++) { if (vxLoadKernels(context, argv[m]) != VX_SUCCESS) printf("Failed to load module %s\n", argv[m]); else printf("Loaded module %s\n", argv[m]); } vxPrintAllLog(context); vxRegisterHelperAsLogReader(context); vxQueryContext(context, VX_CONTEXT_VENDOR_ID, &vendor, sizeof(vendor)); vxQueryContext(context, VX_CONTEXT_VERSION, &version, sizeof(version)); vxQueryContext(context, VX_CONTEXT_IMPLEMENTATION, implementation, sizeof(implementation)); vxQueryContext(context, VX_CONTEXT_MODULES, &modules, sizeof(modules)); vxQueryContext(context, VX_CONTEXT_EXTENSIONS_SIZE, &size, sizeof(size)); printf("implementation=%s (%02x:%02x) has %u modules\n", implementation, vendor, version, modules); extensions = malloc(size); if (extensions) { vx_char *line = extensions, *token = NULL; vxQueryContext(context, VX_CONTEXT_EXTENSIONS, extensions, size); do { token = strtok(line, " "); if (token) printf("extension: %s\n", token); line = NULL; } while (token); free(extensions); } status = vxQueryContext(context, VX_CONTEXT_UNIQUE_KERNELS, &kernels, sizeof(kernels)); if (status != VX_SUCCESS) goto exit; printf("There are %u kernels\n", kernels); size = kernels * sizeof(vx_kernel_info_t); table = malloc(size); status = vxQueryContext(context, VX_CONTEXT_UNIQUE_KERNEL_TABLE, table, size); for (k = 0; k < kernels && table != NULL && status == VX_SUCCESS; k++) { vx_kernel kernel = vxGetKernelByEnum(context, table[k].enumeration); if (kernel && vxGetStatus((vx_reference)kernel) == VX_SUCCESS) { status = vxQueryKernel(kernel, VX_KERNEL_PARAMETERS, ¶meters, sizeof(parameters)); printf("\t\tkernel[%u]=%s has %u parameters (%d)\n", table[k].enumeration, table[k].name, parameters, status); for (p = 0; p < parameters; p++) { vx_parameter parameter = vxGetKernelParameterByIndex(kernel, p); vx_enum type = VX_TYPE_INVALID, dir = VX_INPUT; vx_uint32 tIdx, dIdx; status = VX_SUCCESS; status |= vxQueryParameter(parameter, VX_PARAMETER_TYPE, &type, sizeof(type)); status |= vxQueryParameter(parameter, VX_PARAMETER_DIRECTION, &dir, sizeof(dir)); for (tIdx = 0; tIdx < dimof(parameter_names); tIdx++) if (parameter_names[tIdx].tenum == type) break; for (dIdx = 0; dIdx < dimof(direction_names); dIdx++) if (direction_names[dIdx].tenum == dir) break; if (status == VX_SUCCESS) printf("\t\t\tparameter[%u] type:%s dir:%s\n", p, parameter_names[tIdx].name, direction_names[dIdx].name); vxReleaseParameter(¶meter); } for (a = 0; a < dimof(attribute_names); a++) { switch (attribute_names[a].type) { case VX_TYPE_SIZE: { vx_size value = 0; if (VX_SUCCESS == vxQueryKernel(kernel, attribute_names[a].tenum, &value, sizeof(value))) printf("\t\t\tattribute[%u] %s = "VX_FMT_SIZE"\n", attribute_names[a].tenum & VX_ATTRIBUTE_ID_MASK, attribute_names[a].name, value); break; } case VX_TYPE_UINT32: { vx_uint32 value = 0; if (VX_SUCCESS == vxQueryKernel(kernel, attribute_names[a].tenum, &value, sizeof(value))) printf("\t\t\tattribute[%u] %s = %u\n", attribute_names[a].tenum & VX_ATTRIBUTE_ID_MASK, attribute_names[a].name, value); break; } default: break; } } vxReleaseKernel(&kernel); } else { printf("ERROR: kernel %s is invalid (%d) !\n", table[k].name, status); } } for (m = 1; m < argc; m++) { if (vxUnloadKernels(context, argv[m]) != VX_SUCCESS) printf("Failed to unload module %s\n", argv[m]); else printf("Unloaded module %s\n", argv[m]); } exit: if (table) free(table); vxReleaseContext(&context); } return 0; }
static VALUE Node_init(int argc, VALUE *args, VALUE self) { vx_graph graph = 0; vx_kernel kernel = 0; VALUE w,h,f; Check_Type(self, T_DATA); if (argc <= 1) rb_raise(rb_eArgError, "Not enough arguments"); graph = (vx_graph)DATA_PTR(args[0]); if (argc == 2) // Kernel { Check_Type(args[1], T_DATA); kernel = (vx_kernel)DATA_PTR(args[1]); DATA_PTR(self) = (void *)vxCreateNode(graph, kernel); } else if (argc == 3) // graph, [string|enum], array of hashes { vx_node node = 0; vx_uint32 p = 0; VALUE kern = args[1]; VALUE array = args[2]; long param = 0; if (TYPE(kern) == T_STRING) kernel = vxGetKernelByName(context, RSTRING(kern)->ptr); else if (TYPE(kern) == T_FIXNUM) kernel = vxGetKernelByEnum(context, FIX2INT(kern)); else if (TYPE(kern) == T_DATA) // a OpenVX::Kernel kernel = (vx_kernel)DATA_PTR(kern); else rb_raise(rb_eTypeError, "kernel must be a string, fixnum, or OpenVX::Kernel"); if (kernel == 0) rb_raise(rb_eNameError, "kernel could not be found in OpenVX"); Check_Type(array, T_ARRAY); node = vxCreateNode(graph, kernel); if (node == 0) rb_raise(rb_eTypeError, "node could not be created!"); REXT_PRINT("Array of parameters has len = %ld\n", RARRAY(array)->len); for (param = 0; param < RARRAY(array)->len ; param++) { VALUE dir,ref,hash; vx_reference ref2 = 0; vx_status status = 0; vx_enum type = VX_TYPE_INVALID; const char *name; hash = rb_ary_entry(array, param); Check_Type(hash, T_HASH); dir = rb_hash_aref(hash, ID2SYM(rb_intern("dir"))); ref = rb_hash_aref(hash, ID2SYM(rb_intern("ref"))); name = rb_obj_classname(ref); REXT_PRINT("rb_type(dir)=0x%x\n", TYPE(dir)); REXT_PRINT("ref class = %s\n", name); Check_Type(dir, T_FIXNUM); Check_Type(ref, T_DATA); REXT_PRINT("dir=%ld\n", FIX2UINT(dir)); ref2 = (vx_reference)DATA_PTR(ref); if (strcmp("OpenVX::Image", name) == 0) type = VX_TYPE_IMAGE; else if (strcmp("OpenVX::Buffer", name) == 0) type = VX_TYPE_BUFFER; else if (strcmp("OpenVX::Scalar", name) == 0) type = VX_TYPE_MAX; REXT_PRINT("vx type = %d (0x%08x)\n", type, type); if (type == VX_TYPE_IMAGE) // replace with format status = vxQueryImage(ref2, VX_QUERY_IMAGE_FORMAT, &type, sizeof(vx_fourcc)); else if (type == VX_TYPE_MAX) status = vxQueryReference(ref2, VX_QUERY_REF_TYPE, &type, sizeof(type)); REXT_PRINT("status = %d vx type = %d (0x%08x)\n", status, type, type); status = vxSetParameterByIndex(node, param, FIX2UINT(dir), type, ref2); REXT_PRINT("status = %d\n", status); } DATA_PTR(self) = (void *)node; } else { rb_raise(rb_eArgError, "incorrect number of arguments"); } return Qnil; }
static VALUE Node_init(int argc, VALUE *args, VALUE self) { vx_graph graph = 0; vx_kernel kernel = 0; Check_Type(self, T_DATA); if (argc <= 1) rb_raise(rb_eArgError, "Not enough arguments"); graph = (vx_graph)DATA_PTR(args[0]); if (argc == 2) // Kernel { Check_Type(args[1], T_DATA); kernel = (vx_kernel)DATA_PTR(args[1]); DATA_PTR(self) = (void *)vxCreateGenericNode(graph, kernel); } else if (argc == 3) // graph, [string|enum], array of hashes { vx_node node = 0; VALUE kern = args[1]; VALUE array = args[2]; long param = 0; if (TYPE(kern) == T_STRING) kernel = vxGetKernelByName(context, RSTRING_PTR(kern)); else if (TYPE(kern) == T_FIXNUM) kernel = vxGetKernelByEnum(context, FIX2INT(kern)); else if (TYPE(kern) == T_DATA) // a OpenVX::Kernel kernel = (vx_kernel)DATA_PTR(kern); else rb_raise(rb_eTypeError, "kernel must be a string, fixnum, or OpenVX::Kernel"); if (kernel == 0) rb_raise(rb_eNameError, "kernel could not be found in OpenVX"); Check_Type(array, T_ARRAY); node = vxCreateGenericNode(graph, kernel); if (node == 0) rb_raise(rb_eTypeError, "node could not be created!"); REXT_PRINT("Array of parameters has len = %ld\n", RARRAY_LEN(array)); for (param = 0; param < RARRAY_LEN(array) ; param++) { VALUE ref,hash; vx_reference ref2 = 0; vx_status status = 0; const char *name = NULL; hash = rb_ary_entry(array, param); Check_Type(hash, T_HASH); ref = rb_hash_aref(hash, ID2SYM(rb_intern("ref"))); name = rb_obj_classname(ref); REXT_PRINT("ref class = %s\n", name); Check_Type(ref, T_DATA); ref2 = (vx_reference)DATA_PTR(ref); status = vxSetParameterByIndex(node, param, ref2); REXT_PRINT("status = %d\n", status); } DATA_PTR(self) = (void *)node; } else { rb_raise(rb_eArgError, "incorrect number of arguments"); } return Qnil; }