ncGraphQueueInference()

Info Value
Header mvnc.h
Library libmvnc.so
Version 2.0
See also struct ncGraphHandle_t, ncGraphQueueInferenceWithFifoElem()

Overview

This function queues an inference to be processed by a graph with specified input and output FIFOs.

Prototype

ncStatus_t ncGraphQueueInference(struct ncGraphHandle_t* graphHandle,
                                 struct ncFifoHandle_t** fifoIn,
                                 unsigned int inFifoCount,
                                 struct ncFifoHandle_t** fifoOut,
                                 unsigned int outFifoCount);

Parameters

Name Type Description
graphHandle struct ncGraphHandle_t* A pointer to an ncGraphHandle_t struct for the graph that will be used for inference. The graph state must be NC_GRAPH_ALLOCATED.
fifoIn struct ncFifoHandle_t** An array of pointers to ncFifoHandle_t structs for the input queue(s) from which the tensor will be read. The FIFO states must be NC_FIFO_ALLOCATED.
inFifoCount unsigned int The number of ncFifoHandle_t structs pointed to by the fifoIn parameter.

Only 1 currently supported.
fifoOut struct ncFifoHandle_t** An array of pointers to ncFifoHandle_t structs for the output queue(s) on which the inference result will be placed. The FIFO states must be NC_FIFO_ALLOCATED.
outFifoCount unsigned int The number of ncFifoHandle_t structs pointed to by the fifoOut parameter.

Only 1 currently supported.

Return

An appropriate value from the ncStatus_t enumeration.

Notes

Example

#include <stdio.h>
#include <stdlib.h>
#include <mvnc.h>

int main(int argc, char** argv)
{
    float networkMean[] = {0.40787054*255.0, 0.45752458*255.0, 0.48109378*255.0};
    ncStatus_t retCode;
    struct ncDeviceHandle_t* deviceHandlePtr = NULL;
    struct ncGraphHandle_t* graphHandlePtr = NULL;
    struct ncFifoHandle_t* inFifoHandlePtr = NULL;
    struct ncFifoHandle_t* outFifoHandlePtr = NULL;
    struct ncTensorDescriptor_t inTensorDesc;
    struct ncTensorDescriptor_t outTensorDesc;
    float* imageInMemoryPtr = NULL;
    unsigned int optionSize;

    ncDeviceCreate(0, &deviceHandlePtr);
    ncDeviceOpen(deviceHandlePtr);
    ncGraphCreate("My Graph", &graphHandlePtr);
    
    unsigned int graphSizeInBytes = 0;
    void* graphInMemoryPtr = LoadGraphFile("./graph", &graphSizeInBytes);
    ncGraphAllocate(deviceHandlePtr, graphHandlePtr, graphInMemoryPtr, graphSizeInBytes);
    free(graphInMemoryPtr);

    ncFifoCreate("MY Input FIFO", NC_FIFO_HOST_WO, &inFifoHandlePtr);
    optionSize = sizeof(ncTensorDescriptor_t);
    ncGraphGetOption(graphHandlePtr, NC_RO_GRAPH_INPUT_TENSOR_DESCRIPTORS, &inTensorDesc, &optionSize);
    ncFifoAllocate(inFifoHandlePtr, deviceHandlePtr, &inTensorDesc, 2);
    
    ncFifoCreate("MY Output FIFO", NC_FIFO_HOST_RO, &outFifoHandlePtr);
    optionSize = sizeof(ncTensorDescriptor_t);
    ncGraphGetOption(graphHandlePtr, NC_RO_GRAPH_OUTPUT_TENSOR_DESCRIPTORS, &outTensorDesc, &optionSize);
    ncFifoAllocate(outFifoHandlePtr, deviceHandlePtr, &outTensorDesc, 2);

    unsigned int networkDim = 224;
    unsigned int numChannels = 3;
    imageInMemoryPtr = LoadImage32("./my_image.png", networkDim, networkMean);
    imageSize = sizeof(float) * networkDim * networkDim * numChannels;
    ncFifoWriteElem(inFifoHandlePtr, imageInMemoryPtr, &imageSize, 0);
    free(imageInMemoryPtr);

    retCode = ncGraphQueueInference(graphHandlePtr, &inFifoHandlePtr, 1, &outFifoHandlePtr, 1);
    if (retCode != NC_OK)
    {   
        // Could not queue inference for graph 
        printf("Error queueing inference for graph graph [%d]\n", retCode);
    }
    else
    {
        // Inference queued.  When its done finished the result will be on the output FIFO
        void* outputPtr = NULL;
        unsigned int fifoOutputSize = 0;
        unsigned int optionDataLen = sizeof(unsigned int);
        printf("Graph inference queued OK!\n");
        ncFifoGetOption(outFifoHandlePtr, NC_RO_FIFO_ELEMENT_DATA_SIZE, &fifoOutputSize, &optionDataLen);
        outputPtr = malloc(fifoOutputSize);
        printf("output size is: %d\n", outputDataLen);
        ncFifoReadElem(outFifoHandlePtr, outputPtr, &fifoOutputSize, NULL);
        free(outputPtr);
    }

    // clean up the graph and device
    ncFifoDestroy(&inFifoHandlePtr);
    ncFifoDestroy(&outFifoHandlePtr);
    ncGraphDestroy(&graphHandlePtr);
    ncDeviceClose(deviceHandlePtr);
    ncDeviceDestroy(&deviceHandlePtr);
}