ncGraphAllocateWithFifosEx()
Info | Value |
---|---|
Header | mvnc.h |
Library | libmvnc.so |
Version | 2.0 |
See also | struct ncGraphHandle_t, struct ncFifoHandle_t, ncGraphCreate(), ncGraphAllocate(), ncGraphAllocateWithFifos(), ncGraphDestroy() |
Overview
This function allocates a graph and creates associated FIFOs. This is a convenient way to set up inferences on a graph without setting up FIFOs explicitly.
This function is similar to ncGraphAllocateWithFifos() but rather than making assumptions about the FIFOs that are created this function takes parameters to specify the size, type, and data types for them.
Calling ncGraphDestroy() for the graph and ncFifoDestroy() for each FIFO frees the memory allocated with this function.
Prototype
ncStatus_t ncGraphAllocateWithFifosEx(
struct ncDeviceHandle_t* deviceHandle,
struct ncGraphHandle_t* graphHandle,
const void *graphBuffer,
unsigned int graphBufferLength,
struct ncFifoHandle_t** inFifoHandle,
ncFifoType_t inFifoType,
int inNumElem,
ncFifoDataType_t inDataType,
struct ncFifoHandle_t** outFifoHandle,
ncFifoType_t outFifoType,
int outNumElem,
ncFifoDataType_t outDataType
);
Parameters
Name | Type | Description |
---|---|---|
deviceHandle | struct ncDeviceHandle_t* | A pointer to an initialized and opened ncDeviceHandle_t struct for the device to which the graph and FIFOs will be allocated. The device state must be NC_DEVICE_OPENED. |
graphHandle | struct ncGraphHandle_t* | A pointer to an initialized ncGraphHandle_t struct for the graph that will be allocated. The graph state must be NC_GRAPH_CREATED. |
graphBuffer | const void* | A pointer to a buffer that contains raw data read from a neural network graph file that was created with the NCSDK mvNCCompile tool. |
graphBufferLength | unsigned int | The size, in bytes, of the buffer allocated to the graphBuffer parameter. |
inFifoHandle | struct ncFifoHandle_t** | The address of a pointer to a ncFifoHandle_t struct for the input FIFO that will be initialized and allocated by this function. |
inFifoType | ncFifoType_t | A value from the ncFifoType_t enumeration that specifies the FIFO access type. The API should be able to write to the input FIFO. |
inNumElem | int | The maximum number of elements that the input FIFO will be able to contain. |
inDataType | ncFifoDataType_t | A value from the ncFifoDataType_t enumeration that specifies the data type for input FIFO elements. |
outFifoHandle | struct ncFifoHandle_t** | The address of a pointer to a ncFifoHandle_t struct for the output FIFO that will be initialized and allocated by this function. |
outFifoType | ncFifoType_t | A value from the ncFifoType_t enumeration that specifies the FIFO access type. The API should be able to read from the output FIFO. |
outNumElem | int | The maximum number of elements that the output FIFO will be able to contain. |
outDataType | ncFifoDataType_t | A value from the ncFifoDataType_t enumeration that specifies the data type for output FIFO elements. |
Return
An appropriate value from the ncStatus_t enumeration.
Notes
- This function takes the place of explicit calls to ncGraphAllocate(), ncFifoCreate(), and ncFifoAllocate().
- The ncGraphHandle_t struct must be passed to ncGraphCreate() and then this function or ncGraphAllocate()/ncGraphAllocateWithFifos() before it can be passed to any API function other than ncGraphGetOption() or ncGraphSetOption(). When the graph has been allocated, the graph state will be NC_GRAPH_ALLOCATED.
Example
#include <stdio.h>
#include <stdlib.h>
#include <mvnc.h>
int main() {
// You can check that this return code is equal to NC_OK after each API function call
// This is omitted in this example for better readability
ncStatus_t retCode;
// Initialize and open a device
struct ncDeviceHandle_t* deviceHandle;
retCode = ncDeviceCreate(0, &deviceHandle);
retCode = ncDeviceOpen(deviceHandle);
// Load a graph from file
// void* graphBuffer = ...
// unsigned int graphBufferLength = ...
// Initialize the graph
struct ncGraphHandle_t* graphHandle;
retCode = ncGraphCreate("graph1", &graphHandle);
// CONVENIENCE FUNCTION: Allocate the graph to the device and create input/output FIFOs with explicit options
// Alternatively, use ncGraphAllocateWithFifos() to do this with default FIFO options
struct ncFifoHandle_t* inputFIFO;
struct ncFifoHandle_t* outputFIFO;
retCode = ncGraphAllocateWithFifosEx(deviceHandle, graphHandle, graphBuffer, graphBufferLength,
&inputFIFO, NC_FIFO_HOST_WO, 2, NC_FIFO_FP16,
&outputFIFO, NC_FIFO_HOST_RO, 2, NC_FIFO_FP16);
free(graphBuffer);
// Read and preprocess input...
// float* imageBuffer = ...
// unsigned int imageBufferLength = ...
// Write the image to the input FIFO and queue the inference
retCode = ncGraphQueueInferenceWithFifoElem(
graphHandle, &inputFIFO, &outputFIFO,
imageBuffer, &imageBufferLength, 0);
// Get the results from the output FIFO
void* result;
unsigned int fifoOutputSize = 0;
unsigned int optionDataLen = sizeof(fifoOutputSize);
ncFifoGetOption(outFifoHandlePtr, NC_RO_FIFO_ELEMENT_DATA_SIZE, &fifoOutputSize, &optionDataLen);
result = malloc(fifoOutputSize);
retCode = ncFifoReadElem(outputFIFO, result, &fifoOutputSize, NULL);
free(result);
// Do something with the results...
// Clean up
retCode = ncFifoDestroy(&inputFIFO);
retCode = ncFifoDestroy(&outputFIFO);
retCode = ncGraphDestroy(&graphHandle);
retCode = ncDeviceClose(deviceHandle);
retCode = ncDeviceDestroy(&deviceHandle);
}