Configuring Engine
An engine is a basic functional unit of service software defined by the Matrix framework. Users inherit the engine template class defined by the Matrix framework and create engines for each functional module in services (such as reading input files, image pre-processing, neural network inference, inference result post-processing, and host/device data transmission). For details about the functional modules, see the Matrix API Reference. Each engine defines the Init() and Process() functions. When the graph is initialized, the Init() function is automatically executed to initialize engine parameters (including memory allocation and model loading). The Process() function implements data transmission and service logic.
- Table 1 describes the common APIs.
Table 1 API description API
Description
Remarks
HIAI_DEFINE_PROCESS (inputPortNum, outputPortNum)
Specifies the number of interfaces for inputting and outputting engine data.
The Matrix framework creates a queue for each interface to cache data. The transmission relationship between engines is specified in the graph configuration file. For details, see Graph Keywords.
HIAI_StatusT Engine::Init(const AIConfig &config, const vector<AIModelDescription>&modelDesc)
Initializes the engine.
During graph creation, this API is called to initialize engines. The ai_config value defined in the graph configuration file is transferred to the argument config of the function. You can add custom items to the configuration file and use them in the initialization function.
HIAI_IMPL_ENGINE_PROCESS(name, engineClass, inPortNum)
Indicates the Process() function of an engine, corresponding to a device-side thread.
The Process() function of the engine is driven by data on the device side. That is, after the input interface receives data, the framework starts the process. If multiple input interfaces are configured, each input interface triggers a process after receiving data. Therefore, if service processing depends on multiple inputs, you need to implement the synchronization logic of multiple inputs. The framework has encapsulated the Process() function into a macro definition for implementation.
- The engine reads data from the input interface. The framework supports a maximum of 16 input interfaces (arg0–arg15) that can be directly used. From the perspective of service applications, shared pointers are transmitted. The following shows the transmission code sample:
// Transmission engine: transmits the user-defined data USER_DEFINE_TYPE to the target engine. std::shared_ptr<USER_DEFINE_TYPE > streamData= std::make_shared< USER_DEFINE_TYPE >(); // After a value is assigned to streamData, call Senddata to send the value. The shared pointer needs to be converted to the void type for data transmission. hiai::Engine::SendData(0, " USER_DEFINE_TYPE ", std::static_pointer_cast<void>(deviceStreamData)); // Receiving engine: receives data and converts it to the user-defined type USER_DEFINE_TYPE. std::shared_ptr< USER_DEFINE_TYPE > inputArg = std::static_pointer_cast< USER_DEFINE_TYPE >(arg0);
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot