ncFifoWriteElem()

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

Overview

This function writes an element to a FIFO, usually an input tensor for inference.

After the tensor data is written to the FIFO, an inference can be queued with ncGraphQueueInference(). Alternatively, ncGraphQueueInferenceWithFifoElem() can be used to write the tensor to the input FIFO and queue the inference in one call.

Prototype

ncStatus_t ncFifoWriteElem(struct ncFifoHandle_t* fifoHandle, const void* inputTensor,
                           unsigned int* inputTensorLength, void* userParam);

Parameters

Name Type Description
fifoHandle struct ncFifoHandle_t* A pointer to the ncFifoHandle_t struct for the FIFO. The FIFO state must be NC_FIFO_ALLOCATED.
inputTensor const void* A pointer to a tensor data buffer that contains data in the format specified by the FIFO’s NC_RW_FIFO_DATA_TYPE option. This data is typically a representation of each color channel in each pixel of an image.
inputTensorLength unsigned int* A pointer to an unsigned int that contains the length, in bytes, of the buffer that the inputTensor parameter points to.

If the size of the input tensor does not match the expected input size for this FIFO, the return status code will be NC_INVALID_DATA_LENGTH and inputTensorLength will be set to the expected input size.
userParam void* A pointer to additional user data to associate with the input tensor, or NULL. The pointer value (not the data that it points to) will be copied, stored, and then returned with the inference result with ncFifoReadElem(). This pointer can point to anything that the caller would like to associate with the inference result, such as the original inference input or a window handle. The caller is responsible for allocating and freeing any memory that this pointer references.

Return

An appropriate value from the ncStatus_t enumeration.

If the size of the input tensor does not match the expected input size for this FIFO, the return status code will be NC_INVALID_DATA_LENGTH and the inputTensorLength will be set to the expected input size.

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);
    if (retCode != NC_OK)
    {   // Could not write FIFO element
        printf("Error writing FIFO element [%d]\n", retCode);
        exit(-1);
    }

    // the input FIFO now has the input data for an inference.  
    // now we can Queue the inference to be started
    printf("Successfully wrote FIFO element\n");        

    ncGraphQueueInference(graphHandlePtr, &inFifoHandlePtr, 1, &outFifoHandlePtr, 1);

    void* outputPtr = NULL;
    unsigned int fifoOutputSize = 0;
    unsigned int optionDataLen = sizeof(unsigned int);
    ncFifoGetOption(outFifoHandlePtr, NC_RO_FIFO_ELEMENT_DATA_SIZE, &fifoOutputSize, &optionDataLen);
    outputPtr = malloc(fifoOutputSize);
    ncFifoReadElem(outFifoHandlePtr, outputPtr, &fifoOutputSize, NULL);
    free(outputPtr);


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