// put scalar value to string int PutScalarValueToString(vx_enum type, const void * value, char str[]) { if (type == VX_TYPE_FLOAT32) { sprintf(str, "%g", *(float *)value); } else if (type == VX_TYPE_FLOAT64) { sprintf(str, "%lg", *(double *)value); } else if (type == VX_TYPE_SIZE) { sprintf(str, VX_FMT_SIZE, *(vx_size *)value); } else if (type == VX_TYPE_INT8 || type == VX_TYPE_CHAR) { sprintf(str, "%d", *(vx_int8 *)value); } else if (type == VX_TYPE_INT16) { sprintf(str, "%d", *(vx_int16 *)value); } else if (type == VX_TYPE_INT32 || type == VX_TYPE_BOOL) { sprintf(str, "%d", *(vx_int32 *)value); } else if (type == VX_TYPE_INT64) { sprintf(str, "%" PRId64, *(vx_int64 *)value); } else if (type == VX_TYPE_UINT8) { sprintf(str, "%u", *(vx_uint8 *)value); } else if (type == VX_TYPE_UINT16) { sprintf(str, "%u", *(vx_uint16 *)value); } else if (type == VX_TYPE_UINT32) { sprintf(str, "%u", *(vx_uint32 *)value); } else if (type == VX_TYPE_UINT64) { sprintf(str, "%" PRIu64, *(vx_uint64 *)value); } else if (type == VX_TYPE_ENUM) { vx_enum v = *(vx_enum *)value; const char * name = ovxEnum2Name(v); if (name) strcpy(str, name); else sprintf(str, "0x%x", v); } else if (type == VX_TYPE_DF_IMAGE || type == VX_TYPE_STRING_AMD) { if (type == VX_TYPE_DF_IMAGE) { str[4] = 0; strncpy(str, (const char *)value, 4); } else strcpy(str, (const char *)value); } else { return -1; } return 0; }
int CVxParamArray::Initialize(vx_context context, vx_graph graph, const char * desc) { // get object parameters and create object char objType[64]; const char * ioParams = ScanParameters(desc, "array|virtual-array:", "s:", objType); if (!_stricmp(objType, "array") || !_stricmp(objType, "virtual-array") || !_stricmp(objType, "array-virtual")) { // syntax: [virtual-]array:<format>,<capacity>[:<io-params>] char itemType[64]; ioParams = ScanParameters(ioParams, "<format>,<capacity>", "s,D", &itemType, &m_capacity); bool found_userStruct = false; for (auto it = m_userStructMap->begin(); it != m_userStructMap->end(); ++it){ if (strcmp(itemType, it->first.c_str()) == 0){ found_userStruct = true; m_format = it->second; } } if (found_userStruct == false){ m_format = ovxName2Enum(itemType); if (m_format == 0) { ReportError("ERROR: invalid array item type specified: %s\n", itemType); } } // create array object if (!_stricmp(objType, "virtual-array") || !_stricmp(objType, "array-virtual")) { m_array = vxCreateVirtualArray(graph, m_format, m_capacity); m_isVirtualObject = true; } else { m_array = vxCreateArray(context, m_format, m_capacity); } } else ReportError("ERROR: unsupported array type: %s\n", desc); vx_status ovxStatus = vxGetStatus((vx_reference)m_array); if (ovxStatus != VX_SUCCESS){ printf("ERROR: array creation failed => %d (%s)\n", ovxStatus, ovxEnum2Name(ovxStatus)); if (m_array) vxReleaseArray(&m_array); throw - 1; } m_vxObjRef = (vx_reference)m_array; // io initialize return InitializeIO(context, graph, m_vxObjRef, ioParams); }
int CVxParamDistribution::Initialize(vx_context context, vx_graph graph, const char * desc) { // get object parameters and create object char objType[64]; const char * ioParams = ScanParameters(desc, "distribution:<numBins>,<offset>,<range>", "s:D,d,d", objType, &m_numBins, &m_offset, &m_range); if (!_stricmp(objType, "distribution")) { m_distribution = vxCreateDistribution(context, m_numBins, m_offset, m_range); } else ReportError("ERROR: unsupported distribution type: %s\n", desc); vx_status ovxStatus = vxGetStatus((vx_reference)m_distribution); if (ovxStatus != VX_SUCCESS){ printf("ERROR: distribution creation failed => %d (%s)\n", ovxStatus, ovxEnum2Name(ovxStatus)); if (m_distribution) vxReleaseDistribution(&m_distribution); throw - 1; } m_vxObjRef = (vx_reference)m_distribution; // io initialize return InitializeIO(context, graph, m_vxObjRef, ioParams); }
int CVxParamRemap::Initialize(vx_context context, vx_graph graph, const char * desc) { // get object parameters and create object // syntax: remap:<srcWidth>,<srcHeight>,<dstWidth>,<dstHeight>[:<io-params>] char objType[64]; const char * ioParams = ScanParameters(desc, "remap:<srcWidth>,<srcHeight>,<dstWidth>,<dstHeight>", "s:d,d,d,d", objType, &m_srcWidth, &m_srcHeight, &m_dstWidth, &m_dstHeight); if (!_stricmp(objType, "remap")) { m_remap = vxCreateRemap(context, m_srcWidth, m_srcHeight, m_dstWidth, m_dstHeight); } else ReportError("ERROR: invalid remap type: %s\n", objType); vx_status ovxStatus = vxGetStatus((vx_reference)m_remap); if (ovxStatus != VX_SUCCESS){ printf("ERROR: pyramid creation failed => %d (%s)\n", ovxStatus, ovxEnum2Name(ovxStatus)); if (m_remap) vxReleaseRemap(&m_remap); throw - 1; } // io initialize return InitializeIO(context, graph, (vx_reference)m_remap, ioParams); }
int CVxParamMatrix::Initialize(vx_context context, vx_graph graph, const char * desc) { // get object parameters and create object char objType[64], data_type[64]; const char * ioParams = ScanParameters(desc, "matrix:<data-type>,<columns>,<rows>", "s:s,D,D", objType, data_type, &m_columns, &m_rows); if (!_stricmp(objType, "matrix")) { m_data_type = ovxName2Enum(data_type); m_matrix = vxCreateMatrix(context, m_data_type, m_columns, m_rows); } else ReportError("ERROR: unsupported matrix type: %s\n", desc); vx_status ovxStatus = vxGetStatus((vx_reference)m_matrix); if (ovxStatus != VX_SUCCESS){ printf("ERROR: matrix creation failed => %d (%s)\n", ovxStatus, ovxEnum2Name(ovxStatus)); if (m_matrix) vxReleaseMatrix(&m_matrix); throw - 1; } m_vxObjRef = (vx_reference)m_matrix; // io initialize return InitializeIO(context, graph, m_vxObjRef, ioParams); }
// read scalar value into a string int ReadScalarToString(vx_scalar scalar, char str[]) { vx_enum type; ERROR_CHECK(vxQueryScalar(scalar, VX_SCALAR_ATTRIBUTE_TYPE, &type, sizeof(type))); if (type == VX_TYPE_FLOAT32) { float v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%g", v); } else if (type == VX_TYPE_FLOAT64) { double v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%lg", v); } else if (type == VX_TYPE_SIZE) { vx_size v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, VX_FMT_SIZE, v); } else if (type == VX_TYPE_INT8 || type == VX_TYPE_CHAR) { vx_int8 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%d", v); } else if (type == VX_TYPE_INT16) { vx_int16 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%d", v); } else if (type == VX_TYPE_INT32 || type == VX_TYPE_BOOL) { vx_int32 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%d", v); } else if (type == VX_TYPE_INT64) { vx_int64 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%" PRId64, v); } else if (type == VX_TYPE_UINT8) { vx_uint8 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%d", v); } else if (type == VX_TYPE_UINT16) { vx_uint16 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%d", v); } else if (type == VX_TYPE_UINT32) { vx_uint32 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%d", v); } else if (type == VX_TYPE_UINT64) { vx_uint64 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "%" PRIu64, v); } else if (type == VX_TYPE_ENUM) { vx_enum v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); const char * name = ovxEnum2Name(v); if (name) strcpy(str, name); else sprintf(str, "0x%x", v); } else if (type == VX_TYPE_DF_IMAGE || type == VX_TYPE_STRING_AMD) { str[4] = 0; // needed for VX_TYPE_DF_IMAGE ERROR_CHECK(vxReadScalarValue(scalar, str)); } else { // unknown types will be printed in hex vx_uint64 v = 0; ERROR_CHECK(vxReadScalarValue(scalar, &v)); sprintf(str, "0x%" PRIx64, v); } return 0; }
// ovxEnum2String -- return enum name or hex value as a string void ovxEnum2String(vx_enum e, char str[]) { const char * name = ovxEnum2Name(e); if (e) strcpy(str, name); else sprintf(str, "0x%x", e); }
int CVxParamTensor::Initialize(vx_context context, vx_graph graph, const char * desc) { // get object parameters and create object const char * ioParams = desc; if (!_strnicmp(desc, "tensor:", 7) || !_strnicmp(desc, "virtual-tensor:", 15)) { bool isVirtual = false; if (!_strnicmp(desc, "virtual-tensor:", 15)) { isVirtual = true; desc += 8; } char objType[64], data_type[64]; ioParams = ScanParameters(desc, "tensor:<num-of-dims>,{dims},<data-type>,<fixed-point-pos>", "s:D,L,s,d", objType, &m_num_of_dims, &m_num_of_dims, m_dims, data_type, &m_fixed_point_pos); m_data_type = ovxName2Enum(data_type); if (isVirtual) { m_tensor = vxCreateVirtualTensor(graph, m_num_of_dims, m_dims, m_data_type, m_fixed_point_pos); } else { m_tensor = vxCreateTensor(context, m_num_of_dims, m_dims, m_data_type, m_fixed_point_pos); } } else if (!_strnicmp(desc, "tensor-from-roi:", 16)) { char objType[64], masterName[64]; ioParams = ScanParameters(desc, "tensor-from-view:<tensor>,<view>", "s:s,D,L,L", objType, masterName, &m_num_of_dims, &m_num_of_dims, m_start, &m_num_of_dims, m_end); auto itMaster = m_paramMap->find(masterName); if (itMaster == m_paramMap->end()) ReportError("ERROR: tensor [%s] doesn't exist for %s\n", masterName, desc); vx_tensor masterTensor = (vx_tensor)itMaster->second->GetVxObject(); m_tensor = vxCreateTensorFromView(masterTensor, m_num_of_dims, m_start, m_end); } else if (!_strnicmp(desc, "tensor-from-handle:", 19)) { char objType[64], data_type[64], memory_type_str[64]; ioParams = ScanParameters(desc, "tensor-from-handle:<num-of-dims>,{dims},<data-type>,<fixed-point-pos>,{strides},<num-handles>,<memory-type>", "s:D,L,s,d,L,D,s", objType, &m_num_of_dims, &m_num_of_dims, m_dims, data_type, &m_fixed_point_pos, &m_num_of_dims, m_stride, &m_num_handles, memory_type_str); if(m_num_handles > MAX_BUFFER_HANDLES) ReportError("ERROR: num-handles is out of range: " VX_FMT_SIZE " (must be less than %d)\n", m_num_handles, MAX_BUFFER_HANDLES); m_data_type = ovxName2Enum(data_type); vx_uint64 memory_type = 0; if (GetScalarValueFromString(VX_TYPE_ENUM, memory_type_str, &memory_type) < 0) ReportError("ERROR: invalid memory type enum: %s\n", memory_type_str); m_memory_type = (vx_enum)memory_type; memset(m_memory_handle, 0, sizeof(m_memory_handle)); if (m_memory_type == VX_MEMORY_TYPE_HOST) { // allocate all handles on host for (vx_size active_handle = 0; active_handle < m_num_handles; active_handle++) { vx_size size = m_dims[m_num_of_dims-1] * m_stride[m_num_of_dims-1]; m_memory_handle[active_handle] = malloc(size); if (!m_memory_handle[active_handle]) ReportError("ERROR: malloc(%d) failed\n", (int)size); } } #if ENABLE_OPENCL else if (m_memory_type == VX_MEMORY_TYPE_OPENCL) { // allocate all handles on opencl cl_context opencl_context = nullptr; vx_status status = vxQueryContext(context, VX_CONTEXT_ATTRIBUTE_AMD_OPENCL_CONTEXT, &opencl_context, sizeof(opencl_context)); if (status) ReportError("ERROR: vxQueryContext(*,VX_CONTEXT_ATTRIBUTE_AMD_OPENCL_CONTEXT,...) failed (%d)\n", status); for (vx_size active_handle = 0; active_handle < m_num_handles; active_handle++) { vx_size size = m_dims[m_num_of_dims-1] * m_stride[m_num_of_dims-1]; cl_int err = CL_SUCCESS; m_memory_handle[active_handle] = clCreateBuffer(opencl_context, CL_MEM_READ_WRITE, size, NULL, &err); if (!m_memory_handle[active_handle] || err) ReportError("ERROR: clCreateBuffer(*,CL_MEM_READ_WRITE,%d,NULL,*) failed (%d)\n", (int)size, err); } } #endif else ReportError("ERROR: invalid memory-type enum: %s\n", memory_type_str); m_active_handle = 0; m_tensor = vxCreateTensorFromHandle(context, m_num_of_dims, m_dims, m_data_type, m_fixed_point_pos, m_stride, m_memory_handle[m_active_handle], m_memory_type); } else ReportError("ERROR: unsupported tensor type: %s\n", desc); vx_status ovxStatus = vxGetStatus((vx_reference)m_tensor); if (ovxStatus != VX_SUCCESS){ printf("ERROR: tensor creation failed => %d (%s)\n", ovxStatus, ovxEnum2Name(ovxStatus)); if (m_tensor) vxReleaseTensor(&m_tensor); throw - 1; } m_vxObjRef = (vx_reference)m_tensor; // io initialize return InitializeIO(context, graph, m_vxObjRef, ioParams); }