ncGraphAllocateWithFifos()

Info Value
Header mvnc.h
Library libmvnc.so
Version 2.0
See also struct ncGraphHandle_t, struct ncFifoHandle_t, ncGraphCreate(), ncGraphAllocate(), ncGraphAllocateWithFifosEx(), ncGraphDestroy()

Overview

This function allocates a graph and creates and allocates associated FIFOs. This is a convenient way to set up inferences on a graph without setting up FIFOs explicitly.

This function internally calls ncFifoCreate() and ncFifoAllocate() with the following FIFO options:

ncGraphAllocateWithFifosEx() can be used instead to set FIFO options explicitly.

Calling ncGraphDestroy() for the graph and ncFifoDestroy() for each FIFO frees the memory allocated with this function.

Prototype

ncStatus_t ncGraphAllocateWithFifos(
    struct ncDeviceHandle_t* deviceHandle,
    struct ncGraphHandle_t* graphHandle,
    const void *graphBuffer, 
    unsigned int graphBufferLength,
    struct ncFifoHandle_t** inFifoHandle,
    struct ncFifoHandle_t** outFifoHandle
);

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.
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.

Return

An appropriate value from the ncStatus_t enumeration.

Notes

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 default options
    // Alternatively, use ncGraphAllocateWithFifosEx() to do this with explicit FIFO options
    struct ncFifoHandle_t* inputFIFO;
    struct ncFifoHandle_t* outputFIFO;
    retCode = ncGraphAllocateWithFifos(deviceHandle, graphHandle, graphBuffer, graphBufferLength, &inputFIFO, &outputFIFO);
    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);
}