// get scalar value from string int GetScalarValueFromString(vx_enum type, const char str[], vx_uint64 * value) { if (type == VX_TYPE_FLOAT32) { float v = 0; (void)sscanf(str, "%g", &v); *(float *)value = v; } else if (type == VX_TYPE_FLOAT64) { double v = 0; (void)sscanf(str, "%lg", &v); *(double *)value = v; } else if (type == VX_TYPE_SIZE) { vx_size v = 0; (void)sscanf(str, VX_FMT_SIZE, &v); *(vx_size *)value = v; } else if (type == VX_TYPE_INT8 || type == VX_TYPE_INT16 || type == VX_TYPE_INT32 || type == VX_TYPE_UINT8 || type == VX_TYPE_UINT16 || type == VX_TYPE_UINT32 || type == VX_TYPE_CHAR || type == VX_TYPE_BOOL || type == VX_TYPE_DF_IMAGE) { vx_int32 v = 0; (void)sscanf(str, "%i", &v); *(vx_int32 *)value = v; } else if (type == VX_TYPE_INT64 || type == VX_TYPE_UINT64) { vx_int64 v = 0; (void)sscanf(str, "%" PRIi64, &v); *(vx_int64 *)value = v; } else if (type == VX_TYPE_ENUM) { vx_enum v = ovxName2Enum(str); *(vx_enum *)value = v; } 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 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); }
// write scalar value from a string int WriteScalarFromString(vx_scalar scalar, const char str[]) { vx_enum type; ERROR_CHECK(vxQueryScalar(scalar, VX_SCALAR_ATTRIBUTE_TYPE, &type, sizeof(type))); if (type == VX_TYPE_FLOAT32) { float v = 0; (void)sscanf(str, "%g", &v); ERROR_CHECK(vxWriteScalarValue(scalar, &v)); } else if (type == VX_TYPE_FLOAT64) { double v = 0; (void)sscanf(str, "%lg", &v); ERROR_CHECK(vxWriteScalarValue(scalar, &v)); } else if (type == VX_TYPE_SIZE) { vx_size v = 0; (void)sscanf(str, VX_FMT_SIZE, &v); ERROR_CHECK(vxWriteScalarValue(scalar, &v)); } else if (type == VX_TYPE_INT8 || type == VX_TYPE_INT16 || type == VX_TYPE_INT32 || type == VX_TYPE_UINT8 || type == VX_TYPE_UINT16 || type == VX_TYPE_UINT32 || type == VX_TYPE_CHAR || type == VX_TYPE_BOOL) { vx_int32 v = 0; (void)sscanf(str, "%i", &v); ERROR_CHECK(vxWriteScalarValue(scalar, &v)); } else if (type == VX_TYPE_INT64 || type == VX_TYPE_UINT64) { vx_int64 v = 0; (void)sscanf(str, "%" PRIi64, &v); ERROR_CHECK(vxWriteScalarValue(scalar, &v)); } else if (type == VX_TYPE_ENUM) { vx_enum v = ovxName2Enum(str); ERROR_CHECK(vxWriteScalarValue(scalar, &v)); } else if (type == VX_TYPE_DF_IMAGE || type == VX_TYPE_STRING_AMD) { ERROR_CHECK(vxWriteScalarValue(scalar, str)); } else { // unknown types will be assumed to be in hex format vx_int64 v = 0; (void)sscanf(str, "%" PRIi64, &v); ERROR_CHECK(vxWriteScalarValue(scalar, &v)); } return 0; }
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); }