Program Listing for File Tensor.h

Return to documentation for file (nvcv_types/include/nvcv/Tensor.h)

/*
 * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef NVCV_TENSOR_H
#define NVCV_TENSOR_H

#include "Export.h"
#include "Fwd.h"
#include "Image.h"
#include "Status.h"
#include "TensorData.h"
#include "alloc/Allocator.h"
#include "alloc/Requirements.h"
#include "detail/CudaFwd.h"

#include <nvcv/ImageFormat.h>

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct NVCVTensor *NVCVTensorHandle;

typedef void (*NVCVTensorDataCleanupFunc)(void *ctx, const NVCVTensorData *data);

typedef struct NVCVTensorRequirementsRec
{
    /*< Type of each element */
    NVCVDataType dtype;

    /*< Tensor dimension layout.
     * It's optional. If layout not available, set it to NVCV_TENSOR_NONE. */
    NVCVTensorLayout layout;

    /*< Rank, a.k.a number of dimensions */
    int32_t rank;

    /*< Shape of the tensor */
    int64_t shape[NVCV_TENSOR_MAX_RANK];

    /*< Distance in bytes between each element of a given dimension. */
    int64_t strides[NVCV_TENSOR_MAX_RANK];

    /*< Alignment/block size in bytes */
    int32_t alignBytes;

    /*< Tensor resource requirements. */
    NVCVRequirements mem;
} NVCVTensorRequirements;

NVCV_PUBLIC NVCVStatus nvcvTensorCalcRequirements(int32_t rank, const int64_t *shape, NVCVDataType dtype,
                                                  NVCVTensorLayout layout, int32_t baseAddrAlignment,
                                                  int32_t rowAddrAlignment, NVCVTensorRequirements *reqs);

NVCV_PUBLIC NVCVStatus nvcvTensorCalcRequirementsForImages(int32_t numImages, int32_t width, int32_t height,
                                                           NVCVImageFormat format, int32_t baseAddrAlignment,
                                                           int32_t rowAddrAlignment, NVCVTensorRequirements *reqs);

NVCV_PUBLIC NVCVStatus nvcvTensorConstruct(const NVCVTensorRequirements *reqs, NVCVAllocatorHandle alloc,
                                           NVCVTensorHandle *handle);

NVCV_PUBLIC NVCVStatus nvcvTensorWrapDataConstruct(const NVCVTensorData *data, NVCVTensorDataCleanupFunc cleanup,
                                                   void *ctxCleanup, NVCVTensorHandle *handle);

NVCV_PUBLIC NVCVStatus nvcvTensorWrapImageConstruct(NVCVImageHandle img, NVCVTensorHandle *handle);

NVCV_PUBLIC NVCVStatus nvcvTensorDecRef(NVCVTensorHandle handle, int *newRefCount);

NVCV_PUBLIC NVCVStatus nvcvTensorIncRef(NVCVTensorHandle handle, int *newRefCount);

NVCV_PUBLIC NVCVStatus nvcvTensorRefCount(NVCVTensorHandle handle, int *newRefCount);

NVCV_PUBLIC NVCVStatus nvcvTensorSetUserPointer(NVCVTensorHandle handle, void *userPtr);

NVCV_PUBLIC NVCVStatus nvcvTensorGetUserPointer(NVCVTensorHandle handle, void **outUserPtr);

NVCV_PUBLIC NVCVStatus nvcvTensorGetDataType(NVCVTensorHandle handle, NVCVDataType *type);

NVCV_PUBLIC NVCVStatus nvcvTensorGetLayout(NVCVTensorHandle handle, NVCVTensorLayout *layout);

NVCV_PUBLIC NVCVStatus nvcvTensorGetAllocator(NVCVTensorHandle handle, NVCVAllocatorHandle *alloc);

NVCV_PUBLIC NVCVStatus nvcvTensorExportData(NVCVTensorHandle handle, NVCVTensorData *data);

NVCV_PUBLIC NVCVStatus nvcvTensorGetShape(NVCVTensorHandle handle, int32_t *rank, int64_t *shape);

NVCV_PUBLIC NVCVStatus nvcvTensorReshape(NVCVTensorHandle handle, int32_t rank, const int64_t *shape,
                                         NVCVTensorLayout layout, NVCVTensorHandle *out_handle);

#ifdef __cplusplus
}
#endif

#endif // NVCV_TENSOR_H