Move cuDLA, NVSci sampels to 8_Platform_Specific/Tegra

This commit is contained in:
Rob Armstrong
2024-12-13 11:58:52 -08:00
parent 7568673fa6
commit ee8ff3cf5b
85 changed files with 0 additions and 7 deletions

View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../../../Common"
],
"defines": [],
"compilerPath": "/usr/local/cuda/bin/nvcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@@ -0,0 +1,7 @@
{
"recommendations": [
"nvidia.nsight-vscode-edition",
"ms-vscode.cpptools",
"ms-vscode.makefile-tools"
]
}

View File

@@ -0,0 +1,10 @@
{
"configurations": [
{
"name": "CUDA C++: Launch",
"type": "cuda-gdb",
"request": "launch",
"program": "${workspaceFolder}/cuDLAErrorReporting"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "sample",
"type": "shell",
"command": "make dbg=1",
"problemMatcher": ["$nvcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -0,0 +1,413 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################
#
# Makefile project only supported on Mac OS X and Linux Platforms)
#
################################################################################
# Location of the CUDA Toolkit
CUDA_PATH ?= /usr/local/cuda
##############################
# start deprecated interface #
##############################
ifeq ($(x86_64),1)
$(info WARNING - x86_64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=x86_64 instead)
TARGET_ARCH ?= x86_64
endif
ifeq ($(ARMv7),1)
$(info WARNING - ARMv7 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=armv7l instead)
TARGET_ARCH ?= armv7l
endif
ifeq ($(aarch64),1)
$(info WARNING - aarch64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=aarch64 instead)
TARGET_ARCH ?= aarch64
endif
ifeq ($(ppc64le),1)
$(info WARNING - ppc64le variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=ppc64le instead)
TARGET_ARCH ?= ppc64le
endif
ifneq ($(GCC),)
$(info WARNING - GCC variable has been deprecated)
$(info WARNING - please use HOST_COMPILER=$(GCC) instead)
HOST_COMPILER ?= $(GCC)
endif
ifneq ($(abi),)
$(error ERROR - abi variable has been removed)
endif
############################
# end deprecated interface #
############################
# architecture
HOST_ARCH := $(shell uname -m)
TARGET_ARCH ?= $(HOST_ARCH)
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l))
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le))
TARGET_SIZE := 64
else ifneq (,$(filter $(TARGET_ARCH),armv7l))
TARGET_SIZE := 32
endif
else
TARGET_SIZE := $(shell getconf LONG_BIT)
endif
else
$(error ERROR - unsupported value $(TARGET_ARCH) for TARGET_ARCH!)
endif
# sbsa and aarch64 systems look similar. Need to differentiate them at host level for now.
ifeq ($(HOST_ARCH),aarch64)
ifeq ($(CUDA_PATH)/targets/sbsa-linux,$(shell ls -1d $(CUDA_PATH)/targets/sbsa-linux 2>/dev/null))
HOST_ARCH := sbsa
TARGET_ARCH := sbsa
endif
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq (,$(filter $(HOST_ARCH)-$(TARGET_ARCH),aarch64-armv7l x86_64-armv7l x86_64-aarch64 x86_64-sbsa x86_64-ppc64le))
$(error ERROR - cross compiling from $(HOST_ARCH) to $(TARGET_ARCH) is not supported!)
endif
endif
# When on native aarch64 system with userspace of 32-bit, change TARGET_ARCH to armv7l
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_SIZE),aarch64-aarch64-32)
TARGET_ARCH = armv7l
endif
# operating system
HOST_OS := $(shell uname -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
TARGET_OS ?= $(HOST_OS)
ifeq (,$(filter $(TARGET_OS),linux darwin qnx android))
$(error ERROR - unsupported value $(TARGET_OS) for TARGET_OS!)
endif
# host compiler
ifdef HOST_COMPILER
CUSTOM_HOST_COMPILER = 1
endif
ifeq ($(TARGET_OS),darwin)
ifeq ($(shell expr `xcodebuild -version | grep -i xcode | awk '{print $$2}' | cut -d'.' -f1` \>= 5),1)
HOST_COMPILER ?= clang++
endif
else ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
ifeq ($(TARGET_OS),linux)
HOST_COMPILER ?= arm-linux-gnueabihf-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/arm-unknown-nto-qnx6.6.0eabi-g++
else ifeq ($(TARGET_OS),android)
HOST_COMPILER ?= arm-linux-androideabi-g++
endif
else ifeq ($(TARGET_ARCH),aarch64)
ifeq ($(TARGET_OS), linux)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/q++
else ifeq ($(TARGET_OS), android)
HOST_COMPILER ?= aarch64-linux-android-clang++
endif
else ifeq ($(TARGET_ARCH),sbsa)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_ARCH),ppc64le)
HOST_COMPILER ?= powerpc64le-linux-gnu-g++
endif
endif
HOST_COMPILER ?= g++
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(HOST_COMPILER)
# internal flags
NVCCFLAGS := -m${TARGET_SIZE}
CCFLAGS :=
LDFLAGS :=
# build flags
# Link flag for customized HOST_COMPILER with gcc realpath
GCC_PATH := $(shell which gcc)
ifeq ($(CUSTOM_HOST_COMPILER),1)
ifneq ($(filter /%,$(HOST_COMPILER)),)
ifneq ($(findstring gcc,$(HOST_COMPILER)),)
ifneq ($(GCC_PATH),$(HOST_COMPILER))
LDFLAGS += -lstdc++
endif
endif
endif
endif
ifeq ($(TARGET_OS),darwin)
LDFLAGS += -rpath $(CUDA_PATH)/lib
CCFLAGS += -arch $(HOST_ARCH)
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
LDFLAGS += --dynamic-linker=/lib/ld-linux-armhf.so.3
CCFLAGS += -mfloat-abi=hard
else ifeq ($(TARGET_OS),android)
LDFLAGS += -pie
CCFLAGS += -fpie -fpic -fexceptions
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib -L$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/lib/aarch64-linux-gnu -L$(TARGET_FS)/lib/aarch64-linux-gnu
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib -L$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/aarch64-linux-gnu -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
LDFLAGS += --unresolved-symbols=ignore-in-shared-libs
CCFLAGS += -isystem=$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include/libdrm
CCFLAGS += -isystem=$(TARGET_FS)/usr/include/aarch64-linux-gnu -I$(TARGET_FS)/usr/include/aarch64-linux-gnu
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
NVCCFLAGS += -D_QNX_SOURCE
NVCCFLAGS += --qpp-config 8.3.0,gcc_ntoaarch64le
CCFLAGS += -DWIN_INTERFACE_CUSTOM -I/usr/include/aarch64-qnx-gnu
LDFLAGS += -lsocket
LDFLAGS += -L/usr/lib/aarch64-qnx-gnu
CCFLAGS += "-Wl\,-rpath-link\,/usr/lib/aarch64-qnx-gnu"
ifdef TARGET_OVERRIDE
LDFLAGS += -lslog2
endif
ifneq ($(TARGET_FS),)
LDFLAGS += -L$(TARGET_FS)/usr/lib
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/lib"
LDFLAGS += -L$(TARGET_FS)/usr/libnvidia
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/libnvidia"
CCFLAGS += -I$(TARGET_FS)/../include
endif
endif
endif
ifdef TARGET_OVERRIDE # cuda toolkit targets override
NVCCFLAGS += -target-dir $(TARGET_OVERRIDE)
endif
# Install directory of different arch
CUDA_INSTALL_TARGET_DIR :=
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-gnueabihf/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_INSTALL_TARGET_DIR = targets/sbsa-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_INSTALL_TARGET_DIR = targets/ARMv7-linux-QNX/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-qnx/
else ifeq ($(TARGET_ARCH),ppc64le)
CUDA_INSTALL_TARGET_DIR = targets/ppc64le-linux/
endif
# Debug build flags
ifeq ($(dbg),1)
NVCCFLAGS += -g -G
BUILD_TYPE := debug
else
BUILD_TYPE := release
endif
ALL_CCFLAGS :=
ALL_CCFLAGS += $(NVCCFLAGS)
ALL_CCFLAGS += $(EXTRA_NVCCFLAGS)
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(EXTRA_CCFLAGS))
SAMPLE_ENABLED := 1
# This sample is not supported on Linux x86_64
ifeq ($(TARGET_OS),linux)
ifeq ($(TARGET_ARCH),x86_64)
$(info >>> WARNING - cuDLAErrorReporting is not supported on Linux x86_64 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
endif
# This sample is not supported on Mac OSX
ifeq ($(TARGET_OS),darwin)
$(info >>> WARNING - cuDLAErrorReporting is not supported on Mac OSX - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on ARMv7
ifeq ($(TARGET_ARCH),armv7l)
$(info >>> WARNING - cuDLAErrorReporting is not supported on ARMv7 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on sbsa
ifeq ($(TARGET_ARCH),sbsa)
$(info >>> WARNING - cuDLAErrorReporting is not supported on sbsa - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ALL_LDFLAGS :=
ALL_LDFLAGS += $(ALL_CCFLAGS)
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
ALL_LDFLAGS += $(addprefix -Xlinker ,$(EXTRA_LDFLAGS))
# Common includes and paths for CUDA
INCLUDES := -I../../../Common
LIBRARIES :=
################################################################################
#Detect if installed version of GCC supports required C++11
ifeq ($(TARGET_OS),linux)
empty :=
space := $(empty) $(empty)
GCCVERSIONSTRING := $(shell expr `$(HOST_COMPILER) -dumpversion`)
#Create version number without "."
GCCVERSION := $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f1 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f2 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f3 -d.)
# Make sure the version number has at least 3 decimals
GCCVERSION += 00
# Remove spaces from the version number
GCCVERSION := $(subst $(space),$(empty),$(GCCVERSION))
#$(warning $(GCCVERSION))
IS_MIN_VERSION := $(shell expr `echo $(GCCVERSION)` \>= 47000)
ifneq ($(CUSTOM_HOST_COMPILER), 1)
ifeq ($(IS_MIN_VERSION), 1)
$(info >>> GCC Version is greater or equal to 4.7.0 <<<)
else
$(info >>> Waiving build. Minimum GCC version required is 4.7.0<<<)
SAMPLE_ENABLED := 0
endif
else
$(warning >>> Custom HOST_COMPILER set; skipping GCC version check. This may lead to unintended behavior. Please note the minimum equivalent GCC version is 4.7.0 <<<)
endif
endif
# Gencode arguments
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),armv7l aarch64 sbsa))
SMS ?= 53 61 70 72 75 80 86 87 90
else
SMS ?= 50 52 60 61 70 75 80 86 89 90
endif
ifeq ($(SMS),)
$(info >>> WARNING - no SM architectures have been specified - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ($(GENCODE_FLAGS),)
# Generate SASS code for each SM architecture listed in $(SMS)
$(foreach sm,$(SMS),$(eval GENCODE_FLAGS += -gencode arch=compute_$(sm),code=sm_$(sm)))
# Generate PTX code from the highest SM architecture in $(SMS) to guarantee forward-compatibility
HIGHEST_SM := $(lastword $(sort $(SMS)))
ifneq ($(HIGHEST_SM),)
GENCODE_FLAGS += -gencode arch=compute_$(HIGHEST_SM),code=compute_$(HIGHEST_SM)
endif
endif
ALL_CCFLAGS += --std=c++11 --threads 0
LIBRARIES += -lcudla
ifeq ($(SAMPLE_ENABLED),0)
EXEC ?= @echo "[@]"
endif
################################################################################
# Target rules
all: build
build: cuDLAErrorReporting
check.deps:
ifeq ($(SAMPLE_ENABLED),0)
@echo "Sample will be waived due to the above missing dependencies"
else
@echo "Sample is ready - all dependencies have been met"
endif
main.o:main.cu
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
cuDLAErrorReporting: main.o
$(EXEC) $(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
$(EXEC) mkdir -p ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
$(EXEC) cp $@ ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
run: build
$(EXEC) ./cuDLAErrorReporting
testrun: build
clean:
rm -f cuDLAErrorReporting main.o
rm -rf ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)/cuDLAErrorReporting
clobber: clean

View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entry SYSTEM "SamplesInfo.dtd">
<entry>
<name>cuDLAErrorReporting</name>
<cflags>
<flag>--std=c++11</flag>
</cflags>
<cuda_api_list>
<toolkit>cudaStreamCreateWithFlags</toolkit>
<toolkit>cudaStreamDestroy</toolkit>
<toolkit>cudaFree</toolkit>
<toolkit>cudaGetErrorName</toolkit>
<toolkit>cudaSetDevice</toolkit>
<toolkit>cudaStreamSynchronize</toolkit>
<toolkit>cudaMalloc</toolkit>
<toolkit>cudaMemsetAsync</toolkit>
<toolkit>cudaMemcpyAsync</toolkit>
</cuda_api_list>
<description><![CDATA[This sample demonstrates how DLA errors can be detected via CUDA.]]></description>
<devicecompilation>whole</devicecompilation>
<includepaths>
<path>./</path>
<path>../</path>
<path>../../../Common</path>
</includepaths>
<keyconcepts>
<concept level="basic">cuDLA</concept>
<concept level="advanced">Data Parallel Algorithms</concept>
<concept level="advanced">Image Processing</concept>
</keyconcepts>
<keywords>
<keyword>CUDA</keyword>
<keyword>CPP11</keyword>
</keywords>
<libraries>
<library>cudla</library>
</libraries>
<librarypaths>
</librarypaths>
<nsight_eclipse>true</nsight_eclipse>
<primary_file>main.cu</primary_file>
<scopes>
<scope>1:CUDA Advanced Topics</scope>
<scope>1:cuDLA</scope>
</scopes>
<sm-arch>sm60</sm-arch>
<sm-arch>sm61</sm-arch>
<sm-arch>sm70</sm-arch>
<sm-arch>sm72</sm-arch>
<sm-arch>sm75</sm-arch>
<sm-arch>sm80</sm-arch>
<sm-arch>sm86</sm-arch>
<sm-arch>sm87</sm-arch>
<sm-arch>sm89</sm-arch>
<sm-arch>sm90</sm-arch>
<supported_envs>
<env>
<arch>aarch64</arch>
<platform>linux</platform>
</env>
<env>
<arch>aarch64</arch>
<platform>qnx</platform>
</env>
</supported_envs>
<supported_sm_architectures>
<from>6.0</from>
</supported_sm_architectures>
<title>cuDLA Error Reporting</title>
<type>exe</type>
</entry>

View File

@@ -0,0 +1,60 @@
# cuDLAErrorReporting - cuDLA Error Reporting
## Description
This sample demonstrates how DLA errors can be detected via CUDA.
## Key Concepts
cuDLA, Data Parallel Algorithms, Image Processing
## Supported SM Architectures
[SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
## Supported OSes
Linux, QNX
## Supported CPU Architecture
aarch64
## CUDA APIs involved
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
cudaStreamCreateWithFlags, cudaStreamDestroy, cudaFree, cudaGetErrorName, cudaSetDevice, cudaStreamSynchronize, cudaMalloc, cudaMemsetAsync, cudaMemcpyAsync
## Prerequisites
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
## Build and Run
### Linux
The Linux samples are built using makefiles. To use the makefiles, change the current directory to the sample directory you wish to build, and run make:
```
$ cd <sample_dir>
$ make
```
The samples makefiles can take advantage of certain options:
* **TARGET_ARCH=<arch>** - cross-compile targeting a specific architecture. Allowed architectures are aarch64.
By default, TARGET_ARCH is set to HOST_ARCH. On a x86_64 machine, not setting TARGET_ARCH is the equivalent of setting TARGET_ARCH=x86_64.<br/>
`$ make TARGET_ARCH=aarch64` <br/>
See [here](http://docs.nvidia.com/cuda/cuda-samples/index.html#cross-samples) for more details.
* **dbg=1** - build with debug symbols
```
$ make dbg=1
```
* **SMS="A B ..."** - override the SM architectures for which the sample will be built, where `"A B ..."` is a space-delimited list of SM architectures. For example, to generate SASS for SM 50 and SM 60, use `SMS="50 60"`.
```
$ make SMS="50 60"
```
* **HOST_COMPILER=<host_compiler>** - override the default g++ host compiler. See the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements) for a list of supported host compilers.
```
$ make HOST_COMPILER=g++
```
## References (for more details)

View File

@@ -0,0 +1,431 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cudla.h"
#include "cuda_runtime.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <fstream>
#include <sstream>
#define DPRINTF(...) printf(__VA_ARGS__)
static void printTensorDesc(cudlaModuleTensorDescriptor* tensorDesc) {
DPRINTF("\tTENSOR NAME : %s\n", tensorDesc->name);
DPRINTF("\tsize: %lu\n", tensorDesc->size);
DPRINTF("\tdims: [%lu, %lu, %lu, %lu]\n", tensorDesc->n, tensorDesc->c,
tensorDesc->h, tensorDesc->w);
DPRINTF("\tdata fmt: %d\n", tensorDesc->dataFormat);
DPRINTF("\tdata type: %d\n", tensorDesc->dataType);
DPRINTF("\tdata category: %d\n", tensorDesc->dataCategory);
DPRINTF("\tpixel fmt: %d\n", tensorDesc->pixelFormat);
DPRINTF("\tpixel mapping: %d\n", tensorDesc->pixelMapping);
DPRINTF("\tstride[0]: %d\n", tensorDesc->stride[0]);
DPRINTF("\tstride[1]: %d\n", tensorDesc->stride[1]);
DPRINTF("\tstride[2]: %d\n", tensorDesc->stride[2]);
DPRINTF("\tstride[3]: %d\n", tensorDesc->stride[3]);
}
typedef struct {
cudlaDevHandle devHandle;
cudlaModule moduleHandle;
unsigned char* loadableData;
cudaStream_t stream;
unsigned char* inputBuffer;
unsigned char* outputBuffer;
void* inputBufferGPU;
void* outputBufferGPU;
cudlaModuleTensorDescriptor* inputTensorDesc;
cudlaModuleTensorDescriptor* outputTensorDesc;
} ResourceList;
void cleanUp(ResourceList* resourceList);
void cleanUp(ResourceList* resourceList) {
if (resourceList->inputTensorDesc != NULL) {
free(resourceList->inputTensorDesc);
resourceList->inputTensorDesc = NULL;
}
if (resourceList->outputTensorDesc != NULL) {
free(resourceList->outputTensorDesc);
resourceList->outputTensorDesc = NULL;
}
if (resourceList->loadableData != NULL) {
free(resourceList->loadableData);
resourceList->loadableData = NULL;
}
if (resourceList->moduleHandle != NULL) {
cudlaModuleUnload(resourceList->moduleHandle, 0);
resourceList->moduleHandle = NULL;
}
if (resourceList->devHandle != NULL) {
cudlaDestroyDevice(resourceList->devHandle);
resourceList->devHandle = NULL;
}
if (resourceList->inputBufferGPU != 0) {
cudaFree(resourceList->inputBufferGPU);
resourceList->inputBufferGPU = 0;
}
if (resourceList->outputBufferGPU != 0) {
cudaFree(resourceList->outputBufferGPU);
resourceList->outputBufferGPU = 0;
}
if (resourceList->inputBuffer != NULL) {
free(resourceList->inputBuffer);
resourceList->inputBuffer = NULL;
}
if (resourceList->outputBuffer != NULL) {
free(resourceList->outputBuffer);
resourceList->outputBuffer = NULL;
}
if (resourceList->stream != NULL) {
cudaStreamDestroy(resourceList->stream);
resourceList->stream = NULL;
}
}
int main(int argc, char** argv) {
cudlaDevHandle devHandle;
cudlaModule moduleHandle;
cudlaStatus err;
FILE* fp = NULL;
struct stat st;
size_t file_size;
size_t actually_read = 0;
unsigned char* loadableData = NULL;
cudaStream_t stream;
cudaError_t result;
const char* errPtr = NULL;
ResourceList resourceList;
memset(&resourceList, 0x00, sizeof(ResourceList));
if (argc != 2) {
DPRINTF("Usage : ./cuDLAErrorReporting <loadable>\n");
return 1;
}
// Read loadable into buffer.
fp = fopen(argv[1], "rb");
if (fp == NULL) {
DPRINTF("Cannot open file %s\n", argv[1]);
return 1;
}
if (stat(argv[1], &st) != 0) {
DPRINTF("Cannot stat file\n");
return 1;
}
file_size = st.st_size;
DPRINTF("The file size = %ld\n", file_size);
loadableData = (unsigned char*)malloc(file_size);
if (loadableData == NULL) {
DPRINTF("Cannot Allocate memory for loadable\n");
return 1;
}
actually_read = fread(loadableData, 1, file_size, fp);
if (actually_read != file_size) {
free(loadableData);
DPRINTF("Read wrong size\n");
return 1;
}
fclose(fp);
resourceList.loadableData = loadableData;
// Initialize CUDA.
result = cudaFree(0);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cudaFree = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
result = cudaSetDevice(0);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cudaSetDevice = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
err = cudlaCreateDevice(0, &devHandle, CUDLA_CUDA_DLA);
if (err != cudlaSuccess) {
DPRINTF("Error in cuDLA create device = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Device created successfully\n");
resourceList.devHandle = devHandle;
err = cudlaModuleLoadFromMemory(devHandle, loadableData, file_size,
&moduleHandle, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in cudlaModuleLoadFromMemory = %d\n", err);
cleanUp(&resourceList);
return 1;
} else {
DPRINTF("Successfully loaded module\n");
}
resourceList.moduleHandle = moduleHandle;
// Create CUDA stream.
result = cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cuda stream = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
resourceList.stream = stream;
// Get tensor attributes.
uint32_t numInputTensors = 0;
uint32_t numOutputTensors = 0;
cudlaModuleAttribute attribute;
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_NUM_INPUT_TENSORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting numInputTensors = %d\n", err);
cleanUp(&resourceList);
return 1;
}
numInputTensors = attribute.numInputTensors;
DPRINTF("numInputTensors = %d\n", numInputTensors);
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_NUM_OUTPUT_TENSORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting numOutputTensors = %d\n", err);
cleanUp(&resourceList);
return 1;
}
numOutputTensors = attribute.numOutputTensors;
DPRINTF("numOutputTensors = %d\n", numOutputTensors);
cudlaModuleTensorDescriptor* inputTensorDesc =
(cudlaModuleTensorDescriptor*)malloc(sizeof(cudlaModuleTensorDescriptor) *
numInputTensors);
cudlaModuleTensorDescriptor* outputTensorDesc =
(cudlaModuleTensorDescriptor*)malloc(sizeof(cudlaModuleTensorDescriptor) *
numOutputTensors);
if ((inputTensorDesc == NULL) || (outputTensorDesc == NULL)) {
if (inputTensorDesc != NULL) {
free(inputTensorDesc);
inputTensorDesc = NULL;
}
if (outputTensorDesc != NULL) {
free(outputTensorDesc);
outputTensorDesc = NULL;
}
cleanUp(&resourceList);
return 1;
}
resourceList.inputTensorDesc = inputTensorDesc;
resourceList.outputTensorDesc = outputTensorDesc;
attribute.inputTensorDesc = inputTensorDesc;
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_INPUT_TENSOR_DESCRIPTORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting input tensor descriptor = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Printing input tensor descriptor\n");
printTensorDesc(inputTensorDesc);
attribute.outputTensorDesc = outputTensorDesc;
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_OUTPUT_TENSOR_DESCRIPTORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting output tensor descriptor = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Printing output tensor descriptor\n");
printTensorDesc(outputTensorDesc);
// Setup the input and output buffers which will be used as an input to CUDA.
unsigned char* inputBuffer = (unsigned char*)malloc(inputTensorDesc[0].size);
if (inputBuffer == NULL) {
DPRINTF("Error in allocating input memory\n");
cleanUp(&resourceList);
return 1;
}
resourceList.inputBuffer = inputBuffer;
unsigned char* outputBuffer =
(unsigned char*)malloc(outputTensorDesc[0].size);
if (outputBuffer == NULL) {
DPRINTF("Error in allocating output memory\n");
cleanUp(&resourceList);
return 1;
}
resourceList.outputBuffer = outputBuffer;
memset(inputBuffer, 0x01, inputTensorDesc[0].size);
memset(outputBuffer, 0x00, outputTensorDesc[0].size);
// Allocate memory on GPU.
void* inputBufferGPU;
void* outputBufferGPU;
result = cudaMalloc(&inputBufferGPU, inputTensorDesc[0].size);
if (result != cudaSuccess) {
DPRINTF("Error in allocating input memory on GPU\n");
cleanUp(&resourceList);
return 1;
}
resourceList.inputBufferGPU = inputBufferGPU;
result = cudaMalloc(&outputBufferGPU, outputTensorDesc[0].size);
if (result != cudaSuccess) {
DPRINTF("Error in allocating output memory on GPU\n");
cleanUp(&resourceList);
return 1;
}
resourceList.outputBufferGPU = outputBufferGPU;
// Register the CUDA-allocated buffers.
uint64_t* inputBufferRegisteredPtr = NULL;
uint64_t* outputBufferRegisteredPtr = NULL;
err = cudlaMemRegister(devHandle, (uint64_t*)inputBufferGPU,
inputTensorDesc[0].size, &inputBufferRegisteredPtr, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in registering input memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
err =
cudlaMemRegister(devHandle, (uint64_t*)outputBufferGPU,
outputTensorDesc[0].size, &outputBufferRegisteredPtr, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in registering output memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("ALL MEMORY REGISTERED SUCCESSFULLY\n");
// Copy data from CPU buffers to GPU buffers.
result = cudaMemcpyAsync(inputBufferGPU, inputBuffer, inputTensorDesc[0].size,
cudaMemcpyHostToDevice, stream);
if (result != cudaSuccess) {
DPRINTF("Error in enqueueing memcpy for input\n");
cleanUp(&resourceList);
return 1;
}
result =
cudaMemsetAsync(outputBufferGPU, 0, outputTensorDesc[0].size, stream);
if (result != cudaSuccess) {
DPRINTF("Error in enqueueing memset for output\n");
cleanUp(&resourceList);
return 1;
}
// Enqueue a cuDLA task.
cudlaTask task;
task.moduleHandle = moduleHandle;
task.outputTensor = &outputBufferRegisteredPtr;
task.numOutputTensors = 1;
task.numInputTensors = 1;
task.inputTensor = &inputBufferRegisteredPtr;
task.waitEvents = NULL;
task.signalEvents = NULL;
err = cudlaSubmitTask(devHandle, &task, 1, stream, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in submitting task\n");
cleanUp(&resourceList);
return 1;
}
DPRINTF("SUBMIT IS DONE !!!\n");
// Wait for stream operations to finish and bring output buffer to CPU.
result =
cudaMemcpyAsync(outputBuffer, outputBufferGPU, outputTensorDesc[0].size,
cudaMemcpyDeviceToHost, stream);
if (result != cudaSuccess) {
if (result != cudaErrorExternalDevice) {
DPRINTF("Error in bringing result back to CPU\n");
cleanUp(&resourceList);
return 1;
} else {
cudlaStatus hwStatus = cudlaGetLastError(devHandle);
if (hwStatus != cudlaSuccess) {
DPRINTF("Asynchronous error in HW = %u\n", hwStatus);
}
}
}
result = cudaStreamSynchronize(stream);
if (result != cudaSuccess) {
DPRINTF("Error in synchronizing stream = %s\n", cudaGetErrorName(result));
if (result == cudaErrorExternalDevice) {
cudlaStatus hwStatus = cudlaGetLastError(devHandle);
if (hwStatus != cudlaSuccess) {
DPRINTF("Asynchronous error in HW = %u\n", hwStatus);
}
}
}
cleanUp(&resourceList);
DPRINTF("cuDLAErrorReporting DONE !!!\n");
return 0;
}

View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../../../Common"
],
"defines": [],
"compilerPath": "/usr/local/cuda/bin/nvcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@@ -0,0 +1,7 @@
{
"recommendations": [
"nvidia.nsight-vscode-edition",
"ms-vscode.cpptools",
"ms-vscode.makefile-tools"
]
}

View File

@@ -0,0 +1,10 @@
{
"configurations": [
{
"name": "CUDA C++: Launch",
"type": "cuda-gdb",
"request": "launch",
"program": "${workspaceFolder}/cuDLAHybridMode"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "sample",
"type": "shell",
"command": "make dbg=1",
"problemMatcher": ["$nvcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -0,0 +1,413 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################
#
# Makefile project only supported on Mac OS X and Linux Platforms)
#
################################################################################
# Location of the CUDA Toolkit
CUDA_PATH ?= /usr/local/cuda
##############################
# start deprecated interface #
##############################
ifeq ($(x86_64),1)
$(info WARNING - x86_64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=x86_64 instead)
TARGET_ARCH ?= x86_64
endif
ifeq ($(ARMv7),1)
$(info WARNING - ARMv7 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=armv7l instead)
TARGET_ARCH ?= armv7l
endif
ifeq ($(aarch64),1)
$(info WARNING - aarch64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=aarch64 instead)
TARGET_ARCH ?= aarch64
endif
ifeq ($(ppc64le),1)
$(info WARNING - ppc64le variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=ppc64le instead)
TARGET_ARCH ?= ppc64le
endif
ifneq ($(GCC),)
$(info WARNING - GCC variable has been deprecated)
$(info WARNING - please use HOST_COMPILER=$(GCC) instead)
HOST_COMPILER ?= $(GCC)
endif
ifneq ($(abi),)
$(error ERROR - abi variable has been removed)
endif
############################
# end deprecated interface #
############################
# architecture
HOST_ARCH := $(shell uname -m)
TARGET_ARCH ?= $(HOST_ARCH)
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l))
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le))
TARGET_SIZE := 64
else ifneq (,$(filter $(TARGET_ARCH),armv7l))
TARGET_SIZE := 32
endif
else
TARGET_SIZE := $(shell getconf LONG_BIT)
endif
else
$(error ERROR - unsupported value $(TARGET_ARCH) for TARGET_ARCH!)
endif
# sbsa and aarch64 systems look similar. Need to differentiate them at host level for now.
ifeq ($(HOST_ARCH),aarch64)
ifeq ($(CUDA_PATH)/targets/sbsa-linux,$(shell ls -1d $(CUDA_PATH)/targets/sbsa-linux 2>/dev/null))
HOST_ARCH := sbsa
TARGET_ARCH := sbsa
endif
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq (,$(filter $(HOST_ARCH)-$(TARGET_ARCH),aarch64-armv7l x86_64-armv7l x86_64-aarch64 x86_64-sbsa x86_64-ppc64le))
$(error ERROR - cross compiling from $(HOST_ARCH) to $(TARGET_ARCH) is not supported!)
endif
endif
# When on native aarch64 system with userspace of 32-bit, change TARGET_ARCH to armv7l
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_SIZE),aarch64-aarch64-32)
TARGET_ARCH = armv7l
endif
# operating system
HOST_OS := $(shell uname -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
TARGET_OS ?= $(HOST_OS)
ifeq (,$(filter $(TARGET_OS),linux darwin qnx android))
$(error ERROR - unsupported value $(TARGET_OS) for TARGET_OS!)
endif
# host compiler
ifdef HOST_COMPILER
CUSTOM_HOST_COMPILER = 1
endif
ifeq ($(TARGET_OS),darwin)
ifeq ($(shell expr `xcodebuild -version | grep -i xcode | awk '{print $$2}' | cut -d'.' -f1` \>= 5),1)
HOST_COMPILER ?= clang++
endif
else ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
ifeq ($(TARGET_OS),linux)
HOST_COMPILER ?= arm-linux-gnueabihf-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/arm-unknown-nto-qnx6.6.0eabi-g++
else ifeq ($(TARGET_OS),android)
HOST_COMPILER ?= arm-linux-androideabi-g++
endif
else ifeq ($(TARGET_ARCH),aarch64)
ifeq ($(TARGET_OS), linux)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/q++
else ifeq ($(TARGET_OS), android)
HOST_COMPILER ?= aarch64-linux-android-clang++
endif
else ifeq ($(TARGET_ARCH),sbsa)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_ARCH),ppc64le)
HOST_COMPILER ?= powerpc64le-linux-gnu-g++
endif
endif
HOST_COMPILER ?= g++
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(HOST_COMPILER)
# internal flags
NVCCFLAGS := -m${TARGET_SIZE}
CCFLAGS :=
LDFLAGS :=
# build flags
# Link flag for customized HOST_COMPILER with gcc realpath
GCC_PATH := $(shell which gcc)
ifeq ($(CUSTOM_HOST_COMPILER),1)
ifneq ($(filter /%,$(HOST_COMPILER)),)
ifneq ($(findstring gcc,$(HOST_COMPILER)),)
ifneq ($(GCC_PATH),$(HOST_COMPILER))
LDFLAGS += -lstdc++
endif
endif
endif
endif
ifeq ($(TARGET_OS),darwin)
LDFLAGS += -rpath $(CUDA_PATH)/lib
CCFLAGS += -arch $(HOST_ARCH)
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
LDFLAGS += --dynamic-linker=/lib/ld-linux-armhf.so.3
CCFLAGS += -mfloat-abi=hard
else ifeq ($(TARGET_OS),android)
LDFLAGS += -pie
CCFLAGS += -fpie -fpic -fexceptions
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib -L$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/lib/aarch64-linux-gnu -L$(TARGET_FS)/lib/aarch64-linux-gnu
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib -L$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/aarch64-linux-gnu -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
LDFLAGS += --unresolved-symbols=ignore-in-shared-libs
CCFLAGS += -isystem=$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include/libdrm
CCFLAGS += -isystem=$(TARGET_FS)/usr/include/aarch64-linux-gnu -I$(TARGET_FS)/usr/include/aarch64-linux-gnu
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
NVCCFLAGS += -D_QNX_SOURCE
NVCCFLAGS += --qpp-config 8.3.0,gcc_ntoaarch64le
CCFLAGS += -DWIN_INTERFACE_CUSTOM -I/usr/include/aarch64-qnx-gnu
LDFLAGS += -lsocket
LDFLAGS += -L/usr/lib/aarch64-qnx-gnu
CCFLAGS += "-Wl\,-rpath-link\,/usr/lib/aarch64-qnx-gnu"
ifdef TARGET_OVERRIDE
LDFLAGS += -lslog2
endif
ifneq ($(TARGET_FS),)
LDFLAGS += -L$(TARGET_FS)/usr/lib
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/lib"
LDFLAGS += -L$(TARGET_FS)/usr/libnvidia
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/libnvidia"
CCFLAGS += -I$(TARGET_FS)/../include
endif
endif
endif
ifdef TARGET_OVERRIDE # cuda toolkit targets override
NVCCFLAGS += -target-dir $(TARGET_OVERRIDE)
endif
# Install directory of different arch
CUDA_INSTALL_TARGET_DIR :=
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-gnueabihf/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_INSTALL_TARGET_DIR = targets/sbsa-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_INSTALL_TARGET_DIR = targets/ARMv7-linux-QNX/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-qnx/
else ifeq ($(TARGET_ARCH),ppc64le)
CUDA_INSTALL_TARGET_DIR = targets/ppc64le-linux/
endif
# Debug build flags
ifeq ($(dbg),1)
NVCCFLAGS += -g -G
BUILD_TYPE := debug
else
BUILD_TYPE := release
endif
ALL_CCFLAGS :=
ALL_CCFLAGS += $(NVCCFLAGS)
ALL_CCFLAGS += $(EXTRA_NVCCFLAGS)
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(EXTRA_CCFLAGS))
SAMPLE_ENABLED := 1
# This sample is not supported on Linux x86_64
ifeq ($(TARGET_OS),linux)
ifeq ($(TARGET_ARCH),x86_64)
$(info >>> WARNING - cuDLAHybridMode is not supported on Linux x86_64 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
endif
# This sample is not supported on Mac OSX
ifeq ($(TARGET_OS),darwin)
$(info >>> WARNING - cuDLAHybridMode is not supported on Mac OSX - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on ARMv7
ifeq ($(TARGET_ARCH),armv7l)
$(info >>> WARNING - cuDLAHybridMode is not supported on ARMv7 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on sbsa
ifeq ($(TARGET_ARCH),sbsa)
$(info >>> WARNING - cuDLAHybridMode is not supported on sbsa - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ALL_LDFLAGS :=
ALL_LDFLAGS += $(ALL_CCFLAGS)
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
ALL_LDFLAGS += $(addprefix -Xlinker ,$(EXTRA_LDFLAGS))
# Common includes and paths for CUDA
INCLUDES := -I../../../Common
LIBRARIES :=
################################################################################
#Detect if installed version of GCC supports required C++11
ifeq ($(TARGET_OS),linux)
empty :=
space := $(empty) $(empty)
GCCVERSIONSTRING := $(shell expr `$(HOST_COMPILER) -dumpversion`)
#Create version number without "."
GCCVERSION := $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f1 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f2 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f3 -d.)
# Make sure the version number has at least 3 decimals
GCCVERSION += 00
# Remove spaces from the version number
GCCVERSION := $(subst $(space),$(empty),$(GCCVERSION))
#$(warning $(GCCVERSION))
IS_MIN_VERSION := $(shell expr `echo $(GCCVERSION)` \>= 47000)
ifneq ($(CUSTOM_HOST_COMPILER), 1)
ifeq ($(IS_MIN_VERSION), 1)
$(info >>> GCC Version is greater or equal to 4.7.0 <<<)
else
$(info >>> Waiving build. Minimum GCC version required is 4.7.0<<<)
SAMPLE_ENABLED := 0
endif
else
$(warning >>> Custom HOST_COMPILER set; skipping GCC version check. This may lead to unintended behavior. Please note the minimum equivalent GCC version is 4.7.0 <<<)
endif
endif
# Gencode arguments
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),armv7l aarch64 sbsa))
SMS ?= 53 61 70 72 75 80 86 87 90
else
SMS ?= 50 52 60 61 70 75 80 86 89 90
endif
ifeq ($(SMS),)
$(info >>> WARNING - no SM architectures have been specified - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ($(GENCODE_FLAGS),)
# Generate SASS code for each SM architecture listed in $(SMS)
$(foreach sm,$(SMS),$(eval GENCODE_FLAGS += -gencode arch=compute_$(sm),code=sm_$(sm)))
# Generate PTX code from the highest SM architecture in $(SMS) to guarantee forward-compatibility
HIGHEST_SM := $(lastword $(sort $(SMS)))
ifneq ($(HIGHEST_SM),)
GENCODE_FLAGS += -gencode arch=compute_$(HIGHEST_SM),code=compute_$(HIGHEST_SM)
endif
endif
ALL_CCFLAGS += --std=c++11 --threads 0
LIBRARIES += -lcudla
ifeq ($(SAMPLE_ENABLED),0)
EXEC ?= @echo "[@]"
endif
################################################################################
# Target rules
all: build
build: cuDLAHybridMode
check.deps:
ifeq ($(SAMPLE_ENABLED),0)
@echo "Sample will be waived due to the above missing dependencies"
else
@echo "Sample is ready - all dependencies have been met"
endif
main.o:main.cu
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
cuDLAHybridMode: main.o
$(EXEC) $(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
$(EXEC) mkdir -p ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
$(EXEC) cp $@ ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
run: build
$(EXEC) ./cuDLAHybridMode
testrun: build
clean:
rm -f cuDLAHybridMode main.o
rm -rf ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)/cuDLAHybridMode
clobber: clean

View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entry SYSTEM "SamplesInfo.dtd">
<entry>
<name>cuDLAHybridMode</name>
<cflags>
<flag>--std=c++11</flag>
</cflags>
<cuda_api_list>
<toolkit>cudaStreamCreateWithFlags</toolkit>
<toolkit>cudaStreamDestroy</toolkit>
<toolkit>cudaFree</toolkit>
<toolkit>cudaGetErrorName</toolkit>
<toolkit>cudaSetDevice</toolkit>
<toolkit>cudaStreamSynchronize</toolkit>
<toolkit>cudaMalloc</toolkit>
<toolkit>cudaMemsetAsync</toolkit>
<toolkit>cudaMemcpyAsync</toolkit>
</cuda_api_list>
<description><![CDATA[This sample demonstrates cuDLA hybrid mode wherein DLA can be programmed using CUDA.]]></description>
<devicecompilation>whole</devicecompilation>
<includepaths>
<path>./</path>
<path>../</path>
<path>../../../Common</path>
</includepaths>
<keyconcepts>
<concept level="basic">cuDLA</concept>
<concept level="advanced">Data Parallel Algorithms</concept>
<concept level="advanced">Image Processing</concept>
</keyconcepts>
<keywords>
<keyword>CUDA</keyword>
<keyword>CPP11</keyword>
</keywords>
<libraries>
<library>cudla</library>
</libraries>
<librarypaths>
</librarypaths>
<nsight_eclipse>true</nsight_eclipse>
<primary_file>main.cu</primary_file>
<scopes>
<scope>1:CUDA Advanced Topics</scope>
<scope>1:cuDLA</scope>
</scopes>
<sm-arch>sm60</sm-arch>
<sm-arch>sm61</sm-arch>
<sm-arch>sm70</sm-arch>
<sm-arch>sm72</sm-arch>
<sm-arch>sm75</sm-arch>
<sm-arch>sm80</sm-arch>
<sm-arch>sm86</sm-arch>
<sm-arch>sm87</sm-arch>
<sm-arch>sm89</sm-arch>
<sm-arch>sm90</sm-arch>
<supported_envs>
<env>
<arch>aarch64</arch>
<platform>linux</platform>
</env>
<env>
<arch>aarch64</arch>
<platform>qnx</platform>
</env>
</supported_envs>
<supported_sm_architectures>
<from>6.0</from>
</supported_sm_architectures>
<title>cuDLA Hybrid Mode</title>
<type>exe</type>
</entry>

View File

@@ -0,0 +1,60 @@
# cuDLAHybridMode - cuDLA Hybrid Mode
## Description
This sample demonstrates cuDLA hybrid mode wherein DLA can be programmed using CUDA.
## Key Concepts
cuDLA, Data Parallel Algorithms, Image Processing
## Supported SM Architectures
[SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
## Supported OSes
Linux, QNX
## Supported CPU Architecture
aarch64
## CUDA APIs involved
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
cudaStreamCreateWithFlags, cudaStreamDestroy, cudaFree, cudaGetErrorName, cudaSetDevice, cudaStreamSynchronize, cudaMalloc, cudaMemsetAsync, cudaMemcpyAsync
## Prerequisites
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
## Build and Run
### Linux
The Linux samples are built using makefiles. To use the makefiles, change the current directory to the sample directory you wish to build, and run make:
```
$ cd <sample_dir>
$ make
```
The samples makefiles can take advantage of certain options:
* **TARGET_ARCH=<arch>** - cross-compile targeting a specific architecture. Allowed architectures are aarch64.
By default, TARGET_ARCH is set to HOST_ARCH. On a x86_64 machine, not setting TARGET_ARCH is the equivalent of setting TARGET_ARCH=x86_64.<br/>
`$ make TARGET_ARCH=aarch64` <br/>
See [here](http://docs.nvidia.com/cuda/cuda-samples/index.html#cross-samples) for more details.
* **dbg=1** - build with debug symbols
```
$ make dbg=1
```
* **SMS="A B ..."** - override the SM architectures for which the sample will be built, where `"A B ..."` is a space-delimited list of SM architectures. For example, to generate SASS for SM 50 and SM 60, use `SMS="50 60"`.
```
$ make SMS="50 60"
```
* **HOST_COMPILER=<host_compiler>** - override the default g++ host compiler. See the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements) for a list of supported host compilers.
```
$ make HOST_COMPILER=g++
```
## References (for more details)

View File

@@ -0,0 +1,496 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cudla.h"
#include "cuda_runtime.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <fstream>
#include <sstream>
#define DPRINTF(...) printf(__VA_ARGS__)
static void printTensorDesc(cudlaModuleTensorDescriptor* tensorDesc) {
DPRINTF("\tTENSOR NAME : %s\n", tensorDesc->name);
DPRINTF("\tsize: %lu\n", tensorDesc->size);
DPRINTF("\tdims: [%lu, %lu, %lu, %lu]\n", tensorDesc->n, tensorDesc->c,
tensorDesc->h, tensorDesc->w);
DPRINTF("\tdata fmt: %d\n", tensorDesc->dataFormat);
DPRINTF("\tdata type: %d\n", tensorDesc->dataType);
DPRINTF("\tdata category: %d\n", tensorDesc->dataCategory);
DPRINTF("\tpixel fmt: %d\n", tensorDesc->pixelFormat);
DPRINTF("\tpixel mapping: %d\n", tensorDesc->pixelMapping);
DPRINTF("\tstride[0]: %d\n", tensorDesc->stride[0]);
DPRINTF("\tstride[1]: %d\n", tensorDesc->stride[1]);
DPRINTF("\tstride[2]: %d\n", tensorDesc->stride[2]);
DPRINTF("\tstride[3]: %d\n", tensorDesc->stride[3]);
}
static int initializeInputBuffers(char* filePath,
cudlaModuleTensorDescriptor* tensorDesc,
unsigned char* buf) {
// Read the file in filePath and fill up 'buf' according to format
// specified by the user.
return 0;
}
typedef struct {
cudlaDevHandle devHandle;
cudlaModule moduleHandle;
unsigned char* loadableData;
cudaStream_t stream;
unsigned char* inputBuffer;
unsigned char* outputBuffer;
void* inputBufferGPU;
void* outputBufferGPU;
cudlaModuleTensorDescriptor* inputTensorDesc;
cudlaModuleTensorDescriptor* outputTensorDesc;
} ResourceList;
void cleanUp(ResourceList* resourceList);
void cleanUp(ResourceList* resourceList) {
if (resourceList->inputTensorDesc != NULL) {
free(resourceList->inputTensorDesc);
resourceList->inputTensorDesc = NULL;
}
if (resourceList->outputTensorDesc != NULL) {
free(resourceList->outputTensorDesc);
resourceList->outputTensorDesc = NULL;
}
if (resourceList->loadableData != NULL) {
free(resourceList->loadableData);
resourceList->loadableData = NULL;
}
if (resourceList->moduleHandle != NULL) {
cudlaModuleUnload(resourceList->moduleHandle, 0);
resourceList->moduleHandle = NULL;
}
if (resourceList->devHandle != NULL) {
cudlaDestroyDevice(resourceList->devHandle);
resourceList->devHandle = NULL;
}
if (resourceList->inputBufferGPU != 0) {
cudaFree(resourceList->inputBufferGPU);
resourceList->inputBufferGPU = 0;
}
if (resourceList->outputBufferGPU != 0) {
cudaFree(resourceList->outputBufferGPU);
resourceList->outputBufferGPU = 0;
}
if (resourceList->inputBuffer != NULL) {
free(resourceList->inputBuffer);
resourceList->inputBuffer = NULL;
}
if (resourceList->outputBuffer != NULL) {
free(resourceList->outputBuffer);
resourceList->outputBuffer = NULL;
}
if (resourceList->stream != NULL) {
cudaStreamDestroy(resourceList->stream);
resourceList->stream = NULL;
}
}
int main(int argc, char** argv) {
cudlaDevHandle devHandle;
cudlaModule moduleHandle;
cudlaStatus err;
FILE* fp = NULL;
struct stat st;
size_t file_size;
size_t actually_read = 0;
unsigned char* loadableData = NULL;
cudaStream_t stream;
cudaError_t result;
const char* errPtr = NULL;
ResourceList resourceList;
memset(&resourceList, 0x00, sizeof(ResourceList));
if (argc != 3) {
DPRINTF("Usage : ./cuDLAHybridMode <loadable> <imageFile>\n");
return 1;
}
// Read loadable into buffer.
fp = fopen(argv[1], "rb");
if (fp == NULL) {
DPRINTF("Cannot open file %s\n", argv[1]);
return 1;
}
if (stat(argv[1], &st) != 0) {
DPRINTF("Cannot stat file\n");
return 1;
}
file_size = st.st_size;
DPRINTF("The file size = %ld\n", file_size);
loadableData = (unsigned char*)malloc(file_size);
if (loadableData == NULL) {
DPRINTF("Cannot Allocate memory for loadable\n");
return 1;
}
actually_read = fread(loadableData, 1, file_size, fp);
if (actually_read != file_size) {
free(loadableData);
DPRINTF("Read wrong size\n");
return 1;
}
fclose(fp);
resourceList.loadableData = loadableData;
// Initialize CUDA.
result = cudaFree(0);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cudaFree = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
result = cudaSetDevice(0);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cudaSetDevice = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
err = cudlaCreateDevice(0, &devHandle, CUDLA_CUDA_DLA);
if (err != cudlaSuccess) {
DPRINTF("Error in cuDLA create device = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Device created successfully\n");
resourceList.devHandle = devHandle;
err = cudlaModuleLoadFromMemory(devHandle, loadableData, file_size,
&moduleHandle, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in cudlaModuleLoadFromMemory = %d\n", err);
cleanUp(&resourceList);
return 1;
} else {
DPRINTF("Successfully loaded module\n");
}
resourceList.moduleHandle = moduleHandle;
// Create CUDA stream.
result = cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cuda stream = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
resourceList.stream = stream;
// Get tensor attributes.
uint32_t numInputTensors = 0;
uint32_t numOutputTensors = 0;
cudlaModuleAttribute attribute;
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_NUM_INPUT_TENSORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting numInputTensors = %d\n", err);
cleanUp(&resourceList);
return 1;
}
numInputTensors = attribute.numInputTensors;
DPRINTF("numInputTensors = %d\n", numInputTensors);
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_NUM_OUTPUT_TENSORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting numOutputTensors = %d\n", err);
cleanUp(&resourceList);
return 1;
}
numOutputTensors = attribute.numOutputTensors;
DPRINTF("numOutputTensors = %d\n", numOutputTensors);
cudlaModuleTensorDescriptor* inputTensorDesc =
(cudlaModuleTensorDescriptor*)malloc(sizeof(cudlaModuleTensorDescriptor) *
numInputTensors);
cudlaModuleTensorDescriptor* outputTensorDesc =
(cudlaModuleTensorDescriptor*)malloc(sizeof(cudlaModuleTensorDescriptor) *
numOutputTensors);
if ((inputTensorDesc == NULL) || (outputTensorDesc == NULL)) {
if (inputTensorDesc != NULL) {
free(inputTensorDesc);
inputTensorDesc = NULL;
}
if (outputTensorDesc != NULL) {
free(outputTensorDesc);
outputTensorDesc = NULL;
}
cleanUp(&resourceList);
return 1;
}
resourceList.inputTensorDesc = inputTensorDesc;
resourceList.outputTensorDesc = outputTensorDesc;
attribute.inputTensorDesc = inputTensorDesc;
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_INPUT_TENSOR_DESCRIPTORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting input tensor descriptor = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Printing input tensor descriptor\n");
printTensorDesc(inputTensorDesc);
attribute.outputTensorDesc = outputTensorDesc;
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_OUTPUT_TENSOR_DESCRIPTORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting output tensor descriptor = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Printing output tensor descriptor\n");
printTensorDesc(outputTensorDesc);
// Setup the input and output buffers which will be used as an input to CUDA.
unsigned char* inputBuffer = (unsigned char*)malloc(inputTensorDesc[0].size);
if (inputBuffer == NULL) {
DPRINTF("Error in allocating input memory\n");
cleanUp(&resourceList);
return 1;
}
resourceList.inputBuffer = inputBuffer;
unsigned char* outputBuffer =
(unsigned char*)malloc(outputTensorDesc[0].size);
if (outputBuffer == NULL) {
DPRINTF("Error in allocating output memory\n");
cleanUp(&resourceList);
return 1;
}
resourceList.outputBuffer = outputBuffer;
memset(inputBuffer, 0x00, inputTensorDesc[0].size);
memset(outputBuffer, 0x00, outputTensorDesc[0].size);
// Fill up the buffers with data.
if (initializeInputBuffers(argv[2], inputTensorDesc, inputBuffer) != 0) {
DPRINTF("Error in initializing input buffer\n");
cleanUp(&resourceList);
return 1;
}
// Allocate memory on GPU.
void* inputBufferGPU;
void* outputBufferGPU;
result = cudaMalloc(&inputBufferGPU, inputTensorDesc[0].size);
if (result != cudaSuccess) {
DPRINTF("Error in allocating input memory on GPU\n");
cleanUp(&resourceList);
return 1;
}
resourceList.inputBufferGPU = inputBufferGPU;
result = cudaMalloc(&outputBufferGPU, outputTensorDesc[0].size);
if (result != cudaSuccess) {
DPRINTF("Error in allocating output memory on GPU\n");
cleanUp(&resourceList);
return 1;
}
resourceList.outputBufferGPU = outputBufferGPU;
// Register the CUDA-allocated buffers.
uint64_t* inputBufferRegisteredPtr = NULL;
uint64_t* outputBufferRegisteredPtr = NULL;
err = cudlaMemRegister(devHandle, (uint64_t*)inputBufferGPU,
inputTensorDesc[0].size, &inputBufferRegisteredPtr, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in registering input memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
err =
cudlaMemRegister(devHandle, (uint64_t*)outputBufferGPU,
outputTensorDesc[0].size, &outputBufferRegisteredPtr, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in registering output memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("ALL MEMORY REGISTERED SUCCESSFULLY\n");
// Copy data from CPU buffers to GPU buffers.
result = cudaMemcpyAsync(inputBufferGPU, inputBuffer, inputTensorDesc[0].size,
cudaMemcpyHostToDevice, stream);
if (result != cudaSuccess) {
DPRINTF("Error in enqueueing memcpy for input\n");
cleanUp(&resourceList);
return 1;
}
result =
cudaMemsetAsync(outputBufferGPU, 0, outputTensorDesc[0].size, stream);
if (result != cudaSuccess) {
DPRINTF("Error in enqueueing memset for output\n");
cleanUp(&resourceList);
return 1;
}
// Enqueue a cuDLA task.
cudlaTask task;
task.moduleHandle = moduleHandle;
task.outputTensor = &outputBufferRegisteredPtr;
task.numOutputTensors = 1;
task.numInputTensors = 1;
task.inputTensor = &inputBufferRegisteredPtr;
task.waitEvents = NULL;
task.signalEvents = NULL;
err = cudlaSubmitTask(devHandle, &task, 1, stream, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in submitting task\n");
cleanUp(&resourceList);
return 1;
}
DPRINTF("SUBMIT IS DONE !!!\n");
// Wait for stream operations to finish and bring output buffer to CPU.
result =
cudaMemcpyAsync(outputBuffer, outputBufferGPU, outputTensorDesc[0].size,
cudaMemcpyDeviceToHost, stream);
if (result != cudaSuccess) {
DPRINTF("Error in bringing result back to CPU\n");
cleanUp(&resourceList);
return 1;
}
result = cudaStreamSynchronize(stream);
if (result != cudaSuccess) {
DPRINTF("Error in synchronizing stream\n");
cleanUp(&resourceList);
return 1;
}
// Output is available in outputBuffer.
// Teardown.
err = cudlaMemUnregister(devHandle, inputBufferRegisteredPtr);
if (err != cudlaSuccess) {
DPRINTF("Error in unregistering input memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
err = cudlaMemUnregister(devHandle, outputBufferRegisteredPtr);
if (err != cudlaSuccess) {
DPRINTF("Error in registering output memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("ALL MEMORY UNREGISTERED SUCCESSFULLY\n");
free(inputTensorDesc);
free(outputTensorDesc);
free(loadableData);
free(inputBuffer);
free(outputBuffer);
cudaFree(inputBufferGPU);
cudaFree(outputBufferGPU);
resourceList.inputTensorDesc = NULL;
resourceList.outputTensorDesc = NULL;
resourceList.loadableData = NULL;
resourceList.inputBuffer = NULL;
resourceList.outputBuffer = NULL;
resourceList.inputBufferGPU = 0;
resourceList.outputBufferGPU = 0;
result = cudaStreamDestroy(stream);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in destroying cuda stream = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
resourceList.stream = NULL;
err = cudlaModuleUnload(moduleHandle, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in cudlaModuleUnload = %d\n", err);
cleanUp(&resourceList);
return 1;
} else {
DPRINTF("Successfully unloaded module\n");
}
resourceList.moduleHandle = NULL;
err = cudlaDestroyDevice(devHandle);
if (err != cudlaSuccess) {
DPRINTF("Error in cuDLA destroy device = %d\n", err);
return 1;
}
DPRINTF("Device destroyed successfully\n");
resourceList.devHandle = NULL;
DPRINTF("cuDLAHybridMode DONE !!!\n");
return 0;
}

View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../../../Common"
],
"defines": [],
"compilerPath": "/usr/local/cuda/bin/nvcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@@ -0,0 +1,7 @@
{
"recommendations": [
"nvidia.nsight-vscode-edition",
"ms-vscode.cpptools",
"ms-vscode.makefile-tools"
]
}

View File

@@ -0,0 +1,10 @@
{
"configurations": [
{
"name": "CUDA C++: Launch",
"type": "cuda-gdb",
"request": "launch",
"program": "${workspaceFolder}/cuDLALayerwiseStatsHybrid"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "sample",
"type": "shell",
"command": "make dbg=1",
"problemMatcher": ["$nvcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -0,0 +1,413 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################
#
# Makefile project only supported on Mac OS X and Linux Platforms)
#
################################################################################
# Location of the CUDA Toolkit
CUDA_PATH ?= /usr/local/cuda
##############################
# start deprecated interface #
##############################
ifeq ($(x86_64),1)
$(info WARNING - x86_64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=x86_64 instead)
TARGET_ARCH ?= x86_64
endif
ifeq ($(ARMv7),1)
$(info WARNING - ARMv7 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=armv7l instead)
TARGET_ARCH ?= armv7l
endif
ifeq ($(aarch64),1)
$(info WARNING - aarch64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=aarch64 instead)
TARGET_ARCH ?= aarch64
endif
ifeq ($(ppc64le),1)
$(info WARNING - ppc64le variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=ppc64le instead)
TARGET_ARCH ?= ppc64le
endif
ifneq ($(GCC),)
$(info WARNING - GCC variable has been deprecated)
$(info WARNING - please use HOST_COMPILER=$(GCC) instead)
HOST_COMPILER ?= $(GCC)
endif
ifneq ($(abi),)
$(error ERROR - abi variable has been removed)
endif
############################
# end deprecated interface #
############################
# architecture
HOST_ARCH := $(shell uname -m)
TARGET_ARCH ?= $(HOST_ARCH)
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l))
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le))
TARGET_SIZE := 64
else ifneq (,$(filter $(TARGET_ARCH),armv7l))
TARGET_SIZE := 32
endif
else
TARGET_SIZE := $(shell getconf LONG_BIT)
endif
else
$(error ERROR - unsupported value $(TARGET_ARCH) for TARGET_ARCH!)
endif
# sbsa and aarch64 systems look similar. Need to differentiate them at host level for now.
ifeq ($(HOST_ARCH),aarch64)
ifeq ($(CUDA_PATH)/targets/sbsa-linux,$(shell ls -1d $(CUDA_PATH)/targets/sbsa-linux 2>/dev/null))
HOST_ARCH := sbsa
TARGET_ARCH := sbsa
endif
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq (,$(filter $(HOST_ARCH)-$(TARGET_ARCH),aarch64-armv7l x86_64-armv7l x86_64-aarch64 x86_64-sbsa x86_64-ppc64le))
$(error ERROR - cross compiling from $(HOST_ARCH) to $(TARGET_ARCH) is not supported!)
endif
endif
# When on native aarch64 system with userspace of 32-bit, change TARGET_ARCH to armv7l
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_SIZE),aarch64-aarch64-32)
TARGET_ARCH = armv7l
endif
# operating system
HOST_OS := $(shell uname -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
TARGET_OS ?= $(HOST_OS)
ifeq (,$(filter $(TARGET_OS),linux darwin qnx android))
$(error ERROR - unsupported value $(TARGET_OS) for TARGET_OS!)
endif
# host compiler
ifdef HOST_COMPILER
CUSTOM_HOST_COMPILER = 1
endif
ifeq ($(TARGET_OS),darwin)
ifeq ($(shell expr `xcodebuild -version | grep -i xcode | awk '{print $$2}' | cut -d'.' -f1` \>= 5),1)
HOST_COMPILER ?= clang++
endif
else ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
ifeq ($(TARGET_OS),linux)
HOST_COMPILER ?= arm-linux-gnueabihf-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/arm-unknown-nto-qnx6.6.0eabi-g++
else ifeq ($(TARGET_OS),android)
HOST_COMPILER ?= arm-linux-androideabi-g++
endif
else ifeq ($(TARGET_ARCH),aarch64)
ifeq ($(TARGET_OS), linux)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/q++
else ifeq ($(TARGET_OS), android)
HOST_COMPILER ?= aarch64-linux-android-clang++
endif
else ifeq ($(TARGET_ARCH),sbsa)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_ARCH),ppc64le)
HOST_COMPILER ?= powerpc64le-linux-gnu-g++
endif
endif
HOST_COMPILER ?= g++
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(HOST_COMPILER)
# internal flags
NVCCFLAGS := -m${TARGET_SIZE}
CCFLAGS :=
LDFLAGS :=
# build flags
# Link flag for customized HOST_COMPILER with gcc realpath
GCC_PATH := $(shell which gcc)
ifeq ($(CUSTOM_HOST_COMPILER),1)
ifneq ($(filter /%,$(HOST_COMPILER)),)
ifneq ($(findstring gcc,$(HOST_COMPILER)),)
ifneq ($(GCC_PATH),$(HOST_COMPILER))
LDFLAGS += -lstdc++
endif
endif
endif
endif
ifeq ($(TARGET_OS),darwin)
LDFLAGS += -rpath $(CUDA_PATH)/lib
CCFLAGS += -arch $(HOST_ARCH)
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
LDFLAGS += --dynamic-linker=/lib/ld-linux-armhf.so.3
CCFLAGS += -mfloat-abi=hard
else ifeq ($(TARGET_OS),android)
LDFLAGS += -pie
CCFLAGS += -fpie -fpic -fexceptions
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib -L$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/lib/aarch64-linux-gnu -L$(TARGET_FS)/lib/aarch64-linux-gnu
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib -L$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/aarch64-linux-gnu -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
LDFLAGS += --unresolved-symbols=ignore-in-shared-libs
CCFLAGS += -isystem=$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include/libdrm
CCFLAGS += -isystem=$(TARGET_FS)/usr/include/aarch64-linux-gnu -I$(TARGET_FS)/usr/include/aarch64-linux-gnu
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
NVCCFLAGS += -D_QNX_SOURCE
NVCCFLAGS += --qpp-config 8.3.0,gcc_ntoaarch64le
CCFLAGS += -DWIN_INTERFACE_CUSTOM -I/usr/include/aarch64-qnx-gnu
LDFLAGS += -lsocket
LDFLAGS += -L/usr/lib/aarch64-qnx-gnu
CCFLAGS += "-Wl\,-rpath-link\,/usr/lib/aarch64-qnx-gnu"
ifdef TARGET_OVERRIDE
LDFLAGS += -lslog2
endif
ifneq ($(TARGET_FS),)
LDFLAGS += -L$(TARGET_FS)/usr/lib
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/lib"
LDFLAGS += -L$(TARGET_FS)/usr/libnvidia
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/libnvidia"
CCFLAGS += -I$(TARGET_FS)/../include
endif
endif
endif
ifdef TARGET_OVERRIDE # cuda toolkit targets override
NVCCFLAGS += -target-dir $(TARGET_OVERRIDE)
endif
# Install directory of different arch
CUDA_INSTALL_TARGET_DIR :=
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-gnueabihf/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_INSTALL_TARGET_DIR = targets/sbsa-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_INSTALL_TARGET_DIR = targets/ARMv7-linux-QNX/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-qnx/
else ifeq ($(TARGET_ARCH),ppc64le)
CUDA_INSTALL_TARGET_DIR = targets/ppc64le-linux/
endif
# Debug build flags
ifeq ($(dbg),1)
NVCCFLAGS += -g -G
BUILD_TYPE := debug
else
BUILD_TYPE := release
endif
ALL_CCFLAGS :=
ALL_CCFLAGS += $(NVCCFLAGS)
ALL_CCFLAGS += $(EXTRA_NVCCFLAGS)
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(EXTRA_CCFLAGS))
SAMPLE_ENABLED := 1
# This sample is not supported on Linux x86_64
ifeq ($(TARGET_OS),linux)
ifeq ($(TARGET_ARCH),x86_64)
$(info >>> WARNING - cuDLALayerwiseStatsHybrid is not supported on Linux x86_64 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
endif
# This sample is not supported on Mac OSX
ifeq ($(TARGET_OS),darwin)
$(info >>> WARNING - cuDLALayerwiseStatsHybrid is not supported on Mac OSX - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on ARMv7
ifeq ($(TARGET_ARCH),armv7l)
$(info >>> WARNING - cuDLALayerwiseStatsHybrid is not supported on ARMv7 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on sbsa
ifeq ($(TARGET_ARCH),sbsa)
$(info >>> WARNING - cuDLALayerwiseStatsHybrid is not supported on sbsa - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ALL_LDFLAGS :=
ALL_LDFLAGS += $(ALL_CCFLAGS)
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
ALL_LDFLAGS += $(addprefix -Xlinker ,$(EXTRA_LDFLAGS))
# Common includes and paths for CUDA
INCLUDES := -I../../../Common
LIBRARIES :=
################################################################################
#Detect if installed version of GCC supports required C++11
ifeq ($(TARGET_OS),linux)
empty :=
space := $(empty) $(empty)
GCCVERSIONSTRING := $(shell expr `$(HOST_COMPILER) -dumpversion`)
#Create version number without "."
GCCVERSION := $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f1 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f2 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f3 -d.)
# Make sure the version number has at least 3 decimals
GCCVERSION += 00
# Remove spaces from the version number
GCCVERSION := $(subst $(space),$(empty),$(GCCVERSION))
#$(warning $(GCCVERSION))
IS_MIN_VERSION := $(shell expr `echo $(GCCVERSION)` \>= 47000)
ifneq ($(CUSTOM_HOST_COMPILER), 1)
ifeq ($(IS_MIN_VERSION), 1)
$(info >>> GCC Version is greater or equal to 4.7.0 <<<)
else
$(info >>> Waiving build. Minimum GCC version required is 4.7.0<<<)
SAMPLE_ENABLED := 0
endif
else
$(warning >>> Custom HOST_COMPILER set; skipping GCC version check. This may lead to unintended behavior. Please note the minimum equivalent GCC version is 4.7.0 <<<)
endif
endif
# Gencode arguments
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),armv7l aarch64 sbsa))
SMS ?= 53 61 70 72 75 80 86 87 90
else
SMS ?= 50 52 60 61 70 75 80 86 89 90
endif
ifeq ($(SMS),)
$(info >>> WARNING - no SM architectures have been specified - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ($(GENCODE_FLAGS),)
# Generate SASS code for each SM architecture listed in $(SMS)
$(foreach sm,$(SMS),$(eval GENCODE_FLAGS += -gencode arch=compute_$(sm),code=sm_$(sm)))
# Generate PTX code from the highest SM architecture in $(SMS) to guarantee forward-compatibility
HIGHEST_SM := $(lastword $(sort $(SMS)))
ifneq ($(HIGHEST_SM),)
GENCODE_FLAGS += -gencode arch=compute_$(HIGHEST_SM),code=compute_$(HIGHEST_SM)
endif
endif
ALL_CCFLAGS += --std=c++11 --threads 0
LIBRARIES += -lcudla
ifeq ($(SAMPLE_ENABLED),0)
EXEC ?= @echo "[@]"
endif
################################################################################
# Target rules
all: build
build: cuDLALayerwiseStatsHybrid
check.deps:
ifeq ($(SAMPLE_ENABLED),0)
@echo "Sample will be waived due to the above missing dependencies"
else
@echo "Sample is ready - all dependencies have been met"
endif
main.o:main.cu
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
cuDLALayerwiseStatsHybrid: main.o
$(EXEC) $(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
$(EXEC) mkdir -p ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
$(EXEC) cp $@ ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
run: build
$(EXEC) ./cuDLALayerwiseStatsHybrid
testrun: build
clean:
rm -f cuDLALayerwiseStatsHybrid main.o
rm -rf ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)/cuDLALayerwiseStatsHybrid
clobber: clean

View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entry SYSTEM "SamplesInfo.dtd">
<entry>
<name>cuDLALayerwiseStatsHybrid</name>
<cflags>
<flag>--std=c++11</flag>
</cflags>
<cuda_api_list>
<toolkit>cudaStreamCreateWithFlags</toolkit>
<toolkit>cudaStreamDestroy</toolkit>
<toolkit>cudaFree</toolkit>
<toolkit>cudaGetErrorName</toolkit>
<toolkit>cudaSetDevice</toolkit>
<toolkit>cudaStreamSynchronize</toolkit>
<toolkit>cudaMalloc</toolkit>
<toolkit>cudaMemsetAsync</toolkit>
<toolkit>cudaMemcpyAsync</toolkit>
</cuda_api_list>
<description><![CDATA[This sample is used to provide layerwise statistics to the application in the cuDLA hybrid mode wherein DLA is programmed using CUDA.]]></description>
<devicecompilation>whole</devicecompilation>
<includepaths>
<path>./</path>
<path>../</path>
<path>../../../Common</path>
</includepaths>
<keyconcepts>
<concept level="basic">cuDLA</concept>
<concept level="advanced">Data Parallel Algorithms</concept>
<concept level="advanced">Image Processing</concept>
</keyconcepts>
<keywords>
<keyword>CUDA</keyword>
<keyword>CPP11</keyword>
</keywords>
<libraries>
<library>cudla</library>
</libraries>
<librarypaths>
</librarypaths>
<nsight_eclipse>true</nsight_eclipse>
<primary_file>main.cu</primary_file>
<scopes>
<scope>1:CUDA Advanced Topics</scope>
<scope>1:cuDLA</scope>
</scopes>
<sm-arch>sm60</sm-arch>
<sm-arch>sm61</sm-arch>
<sm-arch>sm70</sm-arch>
<sm-arch>sm72</sm-arch>
<sm-arch>sm75</sm-arch>
<sm-arch>sm80</sm-arch>
<sm-arch>sm86</sm-arch>
<sm-arch>sm87</sm-arch>
<sm-arch>sm89</sm-arch>
<sm-arch>sm90</sm-arch>
<supported_envs>
<env>
<arch>aarch64</arch>
<platform>linux</platform>
</env>
<env>
<arch>aarch64</arch>
<platform>qnx</platform>
</env>
</supported_envs>
<supported_sm_architectures>
<from>6.0</from>
</supported_sm_architectures>
<title>cuDLA Layerwise statistics HybridMode</title>
<type>exe</type>
</entry>

View File

@@ -0,0 +1,60 @@
# cuDLALayerwiseStatsHybrid - cuDLA Layerwise statistics HybridMode
## Description
This sample is used to provide layerwise statistics to the application in the cuDLA hybrid mode wherein DLA is programmed using CUDA.
## Key Concepts
cuDLA, Data Parallel Algorithms, Image Processing
## Supported SM Architectures
[SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
## Supported OSes
Linux, QNX
## Supported CPU Architecture
aarch64
## CUDA APIs involved
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
cudaStreamCreateWithFlags, cudaStreamDestroy, cudaFree, cudaGetErrorName, cudaSetDevice, cudaStreamSynchronize, cudaMalloc, cudaMemsetAsync, cudaMemcpyAsync
## Prerequisites
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
## Build and Run
### Linux
The Linux samples are built using makefiles. To use the makefiles, change the current directory to the sample directory you wish to build, and run make:
```
$ cd <sample_dir>
$ make
```
The samples makefiles can take advantage of certain options:
* **TARGET_ARCH=<arch>** - cross-compile targeting a specific architecture. Allowed architectures are aarch64.
By default, TARGET_ARCH is set to HOST_ARCH. On a x86_64 machine, not setting TARGET_ARCH is the equivalent of setting TARGET_ARCH=x86_64.<br/>
`$ make TARGET_ARCH=aarch64` <br/>
See [here](http://docs.nvidia.com/cuda/cuda-samples/index.html#cross-samples) for more details.
* **dbg=1** - build with debug symbols
```
$ make dbg=1
```
* **SMS="A B ..."** - override the SM architectures for which the sample will be built, where `"A B ..."` is a space-delimited list of SM architectures. For example, to generate SASS for SM 50 and SM 60, use `SMS="50 60"`.
```
$ make SMS="50 60"
```
* **HOST_COMPILER=<host_compiler>** - override the default g++ host compiler. See the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements) for a list of supported host compilers.
```
$ make HOST_COMPILER=g++
```
## References (for more details)

View File

@@ -0,0 +1,898 @@
/* Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cudla.h"
#include "cuda_runtime.h"
#include "cudlaExternalEtbl.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <fstream>
#include <sstream>
#define MAX_FILENAME_LEN 200
#define RESERVED_SUFFIX_LEN 10
#define DPRINTF(...) printf(__VA_ARGS__)
static void printTensorDesc(cudlaModuleTensorDescriptor* tensorDesc) {
DPRINTF("\tTENSOR NAME : %s\n", tensorDesc->name);
DPRINTF("\tsize: %lu\n", tensorDesc->size);
DPRINTF("\tdims: [%lu, %lu, %lu, %lu]\n", tensorDesc->n, tensorDesc->c,
tensorDesc->h, tensorDesc->w);
DPRINTF("\tdata fmt: %d\n", tensorDesc->dataFormat);
DPRINTF("\tdata type: %d\n", tensorDesc->dataType);
DPRINTF("\tdata category: %d\n", tensorDesc->dataCategory);
DPRINTF("\tpixel fmt: %d\n", tensorDesc->pixelFormat);
DPRINTF("\tpixel mapping: %d\n", tensorDesc->pixelMapping);
DPRINTF("\tstride[0]: %d\n", tensorDesc->stride[0]);
DPRINTF("\tstride[1]: %d\n", tensorDesc->stride[1]);
DPRINTF("\tstride[2]: %d\n", tensorDesc->stride[2]);
DPRINTF("\tstride[3]: %d\n", tensorDesc->stride[3]);
}
typedef struct {
cudlaDevHandle devHandle;
cudlaModule moduleHandle;
unsigned char* loadableData;
cudaStream_t stream;
uint32_t numInputTensors;
uint32_t numOutputTensors;
uint32_t numOutputTaskStatistics;
unsigned char** inputBuffer;
unsigned char** outputBuffer;
unsigned char** statisticsOutputBuffer;
void** inputBufferGPU;
void** outputBufferGPU;
void** outputTaskStatisticsGPU;
void **csv;
cudlaModuleTensorDescriptor* inputTensorDesc;
cudlaModuleTensorDescriptor* outputTensorDesc;
cudlaModuleTensorDescriptor* outputTaskStatisticsDesc;
uint64_t** inputBufferRegisteredPtr;
uint64_t** outputBufferRegisteredPtr;
uint64_t** outputTaskStatisticsRegisteredPtr;
uint64_t** outputStatisticsBufferRegisteredPtr;
} ResourceList;
void cleanUp(ResourceList* resourceList);
void cleanUp(ResourceList* resourceList) {
uint32_t ii = 0;
if (resourceList->inputTensorDesc != NULL) {
free(resourceList->inputTensorDesc);
resourceList->inputTensorDesc = NULL;
}
if (resourceList->outputTensorDesc != NULL) {
free(resourceList->outputTensorDesc);
resourceList->outputTensorDesc = NULL;
}
if (resourceList->outputTaskStatisticsDesc != NULL) {
free(resourceList->outputTaskStatisticsDesc);
resourceList->outputTaskStatisticsDesc = NULL;
}
if (resourceList->loadableData != NULL) {
free(resourceList->loadableData);
resourceList->loadableData = NULL;
}
if (resourceList->moduleHandle != NULL) {
cudlaModuleUnload(resourceList->moduleHandle, 0);
resourceList->moduleHandle = NULL;
}
if (resourceList->devHandle != NULL) {
cudlaDestroyDevice(resourceList->devHandle);
resourceList->devHandle = NULL;
}
if (resourceList->inputBufferGPU != NULL) {
for (ii = 0; ii < resourceList->numInputTensors; ii++) {
if ((resourceList->inputBufferGPU)[ii] != NULL) {
cudaFree((resourceList->inputBufferGPU)[ii]);
(resourceList->inputBufferGPU)[ii] = NULL;
}
}
free(resourceList->inputBufferGPU);
resourceList->inputBufferGPU = NULL;
}
if (resourceList->outputBufferGPU != NULL) {
for (ii = 0; ii < resourceList->numOutputTensors; ii++) {
if ((resourceList->outputBufferGPU)[ii] != NULL) {
cudaFree((resourceList->outputBufferGPU)[ii]);
(resourceList->outputBufferGPU)[ii] = NULL;
}
}
free(resourceList->outputBufferGPU);
resourceList->outputBufferGPU = NULL;
}
if (resourceList->outputTaskStatisticsGPU != NULL) {
for (ii = 0; ii < resourceList->numOutputTaskStatistics; ii++) {
if ((resourceList->outputTaskStatisticsGPU)[ii] != NULL) {
cudaFree((resourceList->outputTaskStatisticsGPU)[ii]);
(resourceList->outputTaskStatisticsGPU)[ii] = NULL;
}
}
free(resourceList->outputTaskStatisticsGPU);
resourceList->outputTaskStatisticsGPU = NULL;
}
if (resourceList->csv != NULL) {
for (ii = 0; ii < resourceList->numOutputTaskStatistics; ii++) {
if ((resourceList->csv)[ii] != NULL)
{
free((resourceList->csv)[ii]);
(resourceList->csv)[ii] = NULL;
}
}
free(resourceList->csv);
resourceList->csv = NULL;
}
if (resourceList->inputBuffer != NULL) {
for (ii = 0; ii < resourceList->numInputTensors; ii++) {
if ((resourceList->inputBuffer)[ii] != NULL) {
free((resourceList->inputBuffer)[ii]);
(resourceList->inputBuffer)[ii] = NULL;
}
}
free(resourceList->inputBuffer);
resourceList->inputBuffer = NULL;
}
if (resourceList->outputBuffer != NULL) {
for (ii = 0; ii < resourceList->numOutputTensors; ii++) {
if ((resourceList->outputBuffer)[ii] != NULL)
{
free((resourceList->outputBuffer)[ii]);
(resourceList->outputBuffer)[ii] = NULL;
}
}
free(resourceList->outputBuffer);
resourceList->outputBuffer = NULL;
}
if (resourceList->statisticsOutputBuffer != NULL) {
for (ii = 0; ii < resourceList->numOutputTaskStatistics; ii++) {
if ((resourceList->statisticsOutputBuffer)[ii] != NULL) {
free((resourceList->statisticsOutputBuffer)[ii]);
(resourceList->statisticsOutputBuffer)[ii] = NULL;
}
}
free(resourceList->statisticsOutputBuffer);
resourceList->statisticsOutputBuffer = NULL;
}
if (resourceList->stream != NULL) {
cudaStreamDestroy(resourceList->stream);
resourceList->stream = NULL;
}
if (resourceList->inputBufferRegisteredPtr != NULL) {
free(resourceList->inputBufferRegisteredPtr);
resourceList->inputBufferRegisteredPtr = NULL;
}
if (resourceList->outputBufferRegisteredPtr != NULL) {
free(resourceList->outputBufferRegisteredPtr);
resourceList->outputBufferRegisteredPtr = NULL;
}
if (resourceList->outputTaskStatisticsRegisteredPtr != NULL) {
free(resourceList->outputTaskStatisticsRegisteredPtr);
resourceList->outputTaskStatisticsRegisteredPtr = NULL;
}
if (resourceList->outputStatisticsBufferRegisteredPtr != NULL) {
free(resourceList->outputStatisticsBufferRegisteredPtr);
resourceList->outputStatisticsBufferRegisteredPtr = NULL;
}
resourceList->numInputTensors = 0;
resourceList->numOutputTensors = 0;
resourceList->numOutputTaskStatistics = 0;
}
int main(int argc, char** argv) {
cudlaDevHandle devHandle;
cudlaModule moduleHandle;
cudlaStatus err;
uint32_t statSupport = 0;
uint32_t dlaFreqInMHz = 0;
FILE* fp = NULL;
struct stat st;
size_t file_size;
size_t actually_read = 0;
unsigned char *loadableData = NULL;
char filename[MAX_FILENAME_LEN];
const char* suffix = ".csv";
cudaStream_t stream;
cudaError_t result;
const char* errPtr = NULL;
ResourceList resourceList;
memset(&resourceList, 0x00, sizeof(ResourceList));
if ((argc != 4) && (argc != 5)) {
DPRINTF("Usage : ./test_cudla_layerwise_stats_L0_hybrid_test1 <loadable> <freqMHZ> <statSupport> <filename prefix>\n");
return 1;
}
if (argc == 5) {
if((strlen(argv[4])) > (MAX_FILENAME_LEN - RESERVED_SUFFIX_LEN))
{
DPRINTF("Filename prefix length is too big, greater than maximum permissible prefix length of %u \n",(MAX_FILENAME_LEN - RESERVED_SUFFIX_LEN));
return 1;
}
}
// Read loadable into buffer.
fp = fopen(argv[1], "rb");
if (fp == NULL) {
DPRINTF("Cannot open file %s\n", argv[1]);
return 1;
}
if (stat(argv[1], &st) != 0) {
DPRINTF("Cannot stat file\n");
return 1;
}
file_size = st.st_size;
DPRINTF("The file size = %ld\n", file_size);
dlaFreqInMHz = atoi(argv[2]);
statSupport = atoi(argv[3]);
loadableData = (unsigned char *)malloc(file_size);
if (loadableData == NULL) {
DPRINTF("Cannot Allocate memory for loadable\n");
return 1;
}
actually_read = fread(loadableData, 1, file_size, fp);
if ( actually_read != file_size ) {
free(loadableData);
DPRINTF("Read wrong size\n");
return 1;
}
fclose(fp);
resourceList.loadableData = loadableData;
// Initialize CUDA.
result = cudaFree(0);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cudaFree = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
result = cudaSetDevice(0);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cudaSetDevice = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
err = cudlaCreateDevice(0, &devHandle, CUDLA_CUDA_DLA);
if (err != cudlaSuccess) {
DPRINTF("Error in cuDLA create device = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Device created successfully\n");
resourceList.devHandle = devHandle;
err = cudlaModuleLoadFromMemory(devHandle, loadableData, file_size, &moduleHandle, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in cudlaModuleLoadFromMemory = %d\n", err);
cleanUp(&resourceList);
return 1;
} else {
DPRINTF("Successfully loaded module\n");
}
resourceList.moduleHandle = moduleHandle;
// Create CUDA stream.
result = cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in creating cuda stream = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
resourceList.stream = stream;
// Get tensor attributes.
uint32_t numInputTensors = 0;
uint32_t numOutputTensors = 0;
uint32_t numOutputTaskStatistics = 0;
cudlaModuleAttribute attribute;
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_NUM_INPUT_TENSORS, &attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting numInputTensors = %d\n", err);
cleanUp(&resourceList);
return 1;
}
numInputTensors = attribute.numInputTensors;
DPRINTF("numInputTensors = %d\n", numInputTensors);
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_NUM_OUTPUT_TENSORS, &attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting numOutputTensors = %d\n", err);
cleanUp(&resourceList);
return 1;
}
numOutputTensors = attribute.numOutputTensors;
DPRINTF("numOutputTensors = %d\n", numOutputTensors);
// using the same attributes to get num_output_task_statistics_tensors
attribute.numOutputTensors = 0;
err = cudlaModuleGetAttributes(moduleHandle, CUDLA_NUM_OUTPUT_TASK_STATISTICS, &attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting numOutputTensors = %d\n", err);
cleanUp(&resourceList);
return 1;
}
numOutputTaskStatistics = attribute.numOutputTensors;
DPRINTF("numOutputTaskStatistics = %d\n", numOutputTaskStatistics);
if(numOutputTaskStatistics == 0) {
DPRINTF("Layerwise stats is not supported for this Loadable \n");
cleanUp(&resourceList);
return 1;
}
resourceList.numInputTensors = numInputTensors;
resourceList.numOutputTensors = numOutputTensors;
resourceList.numOutputTaskStatistics = numOutputTaskStatistics;
cudlaModuleTensorDescriptor* inputTensorDesc =
(cudlaModuleTensorDescriptor*)malloc(sizeof(cudlaModuleTensorDescriptor)*numInputTensors);
cudlaModuleTensorDescriptor* outputTensorDesc =
(cudlaModuleTensorDescriptor*)malloc(sizeof(cudlaModuleTensorDescriptor)*numOutputTensors);
if ((inputTensorDesc == NULL) || (outputTensorDesc == NULL)) {
if (inputTensorDesc != NULL) {
free(inputTensorDesc);
inputTensorDesc = NULL;
}
if (outputTensorDesc != NULL) {
free(outputTensorDesc);
outputTensorDesc = NULL;
}
cleanUp(&resourceList);
return 1;
}
resourceList.inputTensorDesc = inputTensorDesc;
resourceList.outputTensorDesc = outputTensorDesc;
cudlaModuleTensorDescriptor* outputTaskStatisticsDesc =
(cudlaModuleTensorDescriptor*)malloc(sizeof(cudlaModuleTensorDescriptor)*numOutputTaskStatistics);
if (outputTaskStatisticsDesc == NULL) {
free(outputTaskStatisticsDesc);
outputTaskStatisticsDesc = NULL;
cleanUp(&resourceList);
return 1;
}
resourceList.outputTaskStatisticsDesc = outputTaskStatisticsDesc;
attribute.inputTensorDesc = inputTensorDesc;
err = cudlaModuleGetAttributes(moduleHandle,
CUDLA_INPUT_TENSOR_DESCRIPTORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting input tensor descriptor = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Printing input tensor descriptor\n");
printTensorDesc(inputTensorDesc);
attribute.outputTensorDesc = outputTensorDesc;
err = cudlaModuleGetAttributes(moduleHandle,
CUDLA_OUTPUT_TENSOR_DESCRIPTORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting output tensor descriptor = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Printing output tensor descriptor\n");
printTensorDesc(outputTensorDesc);
attribute.outputTensorDesc = outputTaskStatisticsDesc;
err = cudlaModuleGetAttributes(moduleHandle,
CUDLA_OUTPUT_TASK_STATISTICS_DESCRIPTORS,
&attribute);
if (err != cudlaSuccess) {
DPRINTF("Error in getting task statistics descriptor = %d\n", err);
cleanUp(&resourceList);
return 1;
}
DPRINTF("Printing output task statistics descriptor size\n");
for (uint32_t ii = 0; ii < numOutputTaskStatistics; ii++) {
DPRINTF("The size of %u descriptor is %lu\n", ii,outputTaskStatisticsDesc[ii].size);
}
// Setup the input and output buffers which will be used as an input to CUDA.
unsigned char** inputBuffer = (unsigned char **)malloc(sizeof(unsigned char *)*numInputTensors);
if (inputBuffer == NULL) {
DPRINTF("Error in allocating memory for input buffer array\n");
cleanUp(&resourceList);
return 1;
}
memset(inputBuffer, 0x00, sizeof(unsigned char *)*numInputTensors);
resourceList.inputBuffer = inputBuffer;
for (uint32_t ii = 0; ii < numInputTensors; ii++) {
inputBuffer[ii] = (unsigned char* )malloc(inputTensorDesc[ii].size);
if (inputBuffer[ii] == NULL) {
DPRINTF("Error in allocating input memory\n");
cleanUp(&resourceList);
return 1;
}
memset(inputBuffer[ii], 0x01, inputTensorDesc[ii].size);
}
unsigned char** outputBuffer = (unsigned char **)malloc(sizeof(unsigned char *)*numOutputTensors);
if (outputBuffer == NULL) {
DPRINTF("Error in allocating memory for output buffer array\n");
cleanUp(&resourceList);
return 1;
}
memset(outputBuffer, 0x00, sizeof(unsigned char *)*numOutputTensors);
resourceList.outputBuffer = outputBuffer;
for (uint32_t ii = 0; ii < numOutputTensors; ii++) {
outputBuffer[ii] = (unsigned char* )malloc(outputTensorDesc[ii].size);
if (outputBuffer[ii] == NULL) {
DPRINTF("Error in allocating output memory\n");
cleanUp(&resourceList);
return 1;
}
memset(outputBuffer[ii], 0x00, outputTensorDesc[ii].size);
}
unsigned char** statisticsOutputBuffer = (unsigned char **)malloc(sizeof(unsigned char *)*numOutputTaskStatistics);
if (statisticsOutputBuffer == NULL) {
DPRINTF("Error in allocating memory for output buffer array\n");
cleanUp(&resourceList);
return 1;
}
memset(statisticsOutputBuffer, 0x00, sizeof(unsigned char *)*numOutputTaskStatistics);
resourceList.statisticsOutputBuffer = statisticsOutputBuffer;
for (uint32_t ii = 0; ii < numOutputTaskStatistics; ii++) {
statisticsOutputBuffer[ii] = (unsigned char* )malloc(outputTaskStatisticsDesc[ii].size);
if (outputBuffer[ii] == NULL) {
DPRINTF("Error in allocating output memory\n");
cleanUp(&resourceList);
return 1;
}
memset(statisticsOutputBuffer[ii], 0x00, outputTaskStatisticsDesc[ii].size);
}
// Allocate memory on GPU.
void** inputBufferGPU = (void **)malloc(sizeof(void *)*numInputTensors);
if (inputBufferGPU == NULL) {
DPRINTF("Error in allocating memory for input buffer GPU array\n");
cleanUp(&resourceList);
return 1;
}
memset(inputBufferGPU, 0x00, sizeof(void *)*numInputTensors);
resourceList.inputBufferGPU = inputBufferGPU;
for (uint32_t ii = 0; ii < numInputTensors; ii++) {
result = cudaMalloc(&(inputBufferGPU[ii]), inputTensorDesc[ii].size);
if (result != cudaSuccess)
{
DPRINTF("Error in allocating input memory on GPU\n");
cleanUp(&resourceList);
return 1;
}
}
void** outputBufferGPU = (void **)malloc(sizeof(void *)*numOutputTensors);
if (outputBufferGPU == NULL) {
DPRINTF("Error in allocating memory for output buffer GPU array\n");
cleanUp(&resourceList);
return 1;
}
memset(outputBufferGPU, 0x00, sizeof(void *)*numOutputTensors);
resourceList.outputBufferGPU = outputBufferGPU;
for (uint32_t ii = 0; ii < numOutputTensors; ii++) {
result = cudaMalloc(&(outputBufferGPU[ii]), outputTensorDesc[ii].size);
if (result != cudaSuccess) {
DPRINTF("Error in allocating output memory on GPU\n");
cleanUp(&resourceList);
return 1;
}
}
void** outputTaskStatisticsGPU = (void **)malloc(sizeof(void *)*numOutputTaskStatistics);
if (outputTaskStatisticsGPU == NULL) {
DPRINTF("Error in allocating memory for output task statistics GPU array\n");
cleanUp(&resourceList);
return 1;
}
memset(outputTaskStatisticsGPU, 0x00, sizeof(void *)*numOutputTaskStatistics);
resourceList.outputTaskStatisticsGPU = outputTaskStatisticsGPU;
for (uint32_t ii = 0; ii < numOutputTaskStatistics; ii++) {
result = cudaMalloc(&(outputTaskStatisticsGPU[ii]), outputTaskStatisticsDesc[ii].size);
if (result != cudaSuccess) {
DPRINTF("Error in allocating task statistics memory on GPU\n");
cleanUp(&resourceList);
return 1;
}
}
uint64_t** inputBufferRegisteredPtr = (uint64_t **)malloc(sizeof(uint64_t*)*numInputTensors);
uint64_t** outputBufferRegisteredPtr = (uint64_t **)malloc(sizeof(uint64_t*)*numOutputTensors);
uint64_t** outputTaskStatisticsRegisteredPtr = (uint64_t **)malloc(sizeof(uint64_t*)*numOutputTaskStatistics);
if ((inputBufferRegisteredPtr == NULL) || (outputBufferRegisteredPtr == NULL) || (outputTaskStatisticsRegisteredPtr == NULL)) {
if (inputBufferRegisteredPtr != NULL) {
free(inputBufferRegisteredPtr);
inputBufferRegisteredPtr = NULL;
}
if (outputBufferRegisteredPtr != NULL) {
free(outputBufferRegisteredPtr);
outputBufferRegisteredPtr = NULL;
}
if (outputTaskStatisticsRegisteredPtr != NULL) {
free(outputTaskStatisticsRegisteredPtr);
outputTaskStatisticsRegisteredPtr = NULL;
}
cleanUp(&resourceList);
return 1;
}
resourceList.inputBufferRegisteredPtr = inputBufferRegisteredPtr;
resourceList.outputBufferRegisteredPtr = outputBufferRegisteredPtr;
resourceList.outputTaskStatisticsRegisteredPtr = outputTaskStatisticsRegisteredPtr;
// Register the CUDA-allocated buffers.
for (uint32_t ii = 0; ii < numInputTensors; ii++) {
err = cudlaMemRegister(devHandle,
(uint64_t* )(inputBufferGPU[ii]),
inputTensorDesc[ii].size,
&(inputBufferRegisteredPtr[ii]),
0);
if (err != cudlaSuccess) {
DPRINTF("Error in registering input memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
}
for (uint32_t ii = 0; ii < numOutputTensors; ii++) {
err = cudlaMemRegister(devHandle,
(uint64_t* )(outputBufferGPU[ii]),
outputTensorDesc[ii].size,
&(outputBufferRegisteredPtr[ii]),
0);
if (err != cudlaSuccess) {
DPRINTF("Error in registering output memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
}
for (uint32_t ii = 0; ii < numOutputTaskStatistics; ii++) {
err = cudlaMemRegister(devHandle,
(uint64_t* )(outputTaskStatisticsGPU[ii]),
outputTaskStatisticsDesc[ii].size,
&(outputTaskStatisticsRegisteredPtr[ii]),
CUDLA_TASK_STATISTICS);
if (err != cudlaSuccess) {
DPRINTF("Error in registering statistics output memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
}
DPRINTF("ALL MEMORY REGISTERED SUCCESSFULLY\n");
// Copy data from CPU buffers to GPU buffers.
for (uint32_t ii = 0; ii < numInputTensors; ii++) {
result = cudaMemcpyAsync(inputBufferGPU[ii], inputBuffer[ii], inputTensorDesc[ii].size, cudaMemcpyHostToDevice, stream);
if (result != cudaSuccess) {
DPRINTF("Error in enqueueing memcpy for input\n");
cleanUp(&resourceList);
return 1;
}
}
for (uint32_t ii = 0; ii < numOutputTensors; ii++) {
result = cudaMemsetAsync(outputBufferGPU[ii], 0, outputTensorDesc[ii].size, stream);
if (result != cudaSuccess) {
DPRINTF("Error in enqueueing memset for output\n");
cleanUp(&resourceList);
return 1;
}
}
for (uint32_t ii = 0; ii < numOutputTaskStatistics; ii++) {
result = cudaMemsetAsync(outputTaskStatisticsGPU[ii], 0, outputTaskStatisticsDesc[ii].size, stream);
if (result != cudaSuccess) {
DPRINTF("Error in enqueueing memset for statistics output\n");
cleanUp(&resourceList);
return 1;
}
}
uint64_t *outputStatisticsBufferRegisteredPtr[numOutputTensors + numOutputTaskStatistics] = {0};
uint32_t index = 0;
for (; index < numOutputTensors ; index++) {
outputStatisticsBufferRegisteredPtr[index] = ((outputBufferRegisteredPtr[index]));
}
for (uint32_t jj=0; jj < numOutputTaskStatistics ; jj++) {
outputStatisticsBufferRegisteredPtr[index++] = ((outputTaskStatisticsRegisteredPtr[jj]));
}
// Enqueue a cuDLA task.
cudlaTask task;
task.moduleHandle = moduleHandle;
task.outputTensor = (uint64_t * const*)&outputStatisticsBufferRegisteredPtr;
if(statSupport == 1) {
task.numOutputTensors = (numOutputTensors + numOutputTaskStatistics);
DPRINTF("Layerwise profiling is requested \n");
} else {
task.numOutputTensors = numOutputTensors;
DPRINTF("Layerwise profiling is not requested \n");
}
task.numInputTensors = numInputTensors;
task.inputTensor = inputBufferRegisteredPtr;
task.waitEvents = NULL;
task.signalEvents = NULL;
err = cudlaSubmitTask(devHandle, &task, 1, stream, 0);
if (err != cudlaSuccess) {
DPRINTF("no of output tensor %u \n",(task.numOutputTensors));
DPRINTF("Error in submitting task\n");
cleanUp(&resourceList);
return 1;
}
DPRINTF("SUBMIT IS DONE !!!\n");
result = cudaStreamSynchronize(stream);
if (result != cudaSuccess) {
DPRINTF("Error in synchronizing stream = %s\n", cudaGetErrorName(result));
cleanUp(&resourceList);
return 1;
}
// Wait for stream operations to finish and bring output buffer to CPU.
for (uint32_t ii = 0; ii < numOutputTensors; ii++) {
result = cudaMemcpyAsync(outputBuffer[ii], outputBufferGPU[ii],
outputTensorDesc[ii].size, cudaMemcpyDeviceToHost, stream);
if (result != cudaSuccess) {
DPRINTF("Error in bringing result back to CPU\n");
cleanUp(&resourceList);
return 1;
}
}
result = cudaStreamSynchronize(stream);
if (result != cudaSuccess) {
DPRINTF("Error in synchronizing stream\n");
cleanUp(&resourceList);
return 1;
}
if(statSupport == 1) {
// copy statistics data to cpu
for (uint32_t ii = 0; ii < numOutputTaskStatistics; ii++) {
result = cudaMemcpyAsync(statisticsOutputBuffer[ii], outputTaskStatisticsGPU[ii],
outputTaskStatisticsDesc[ii].size, cudaMemcpyDeviceToHost, stream);
if (result != cudaSuccess) {
DPRINTF("Error in bringing result back to CPU\n");
cleanUp(&resourceList);
return 1;
}
}
result = cudaStreamSynchronize(stream);
if (result != cudaSuccess) {
DPRINTF("Error in synchronizing stream\n");
cleanUp(&resourceList);
return 1;
}
// To get the last index of the filename prefix in which statistics will be dumped
uint32_t index = 0;
if (argc == 5) {
while(argv[4][index]!='\0') {
index++;
}
}
const cudlaExternalEtbl* etbl = NULL;
if (cudlaGetExternalExportTable(&etbl,0) != cudlaSuccess) {
DPRINTF("Error in getting export table\n");
cleanUp(&resourceList);
return 1;
}
void** csv = (void **)malloc(sizeof(void *)*numOutputTaskStatistics);
if (csv == NULL) {
DPRINTF("Error in allocating memory for csv stream\n");
cleanUp(&resourceList);
return 1;
}
memset(csv, 0x00, sizeof(void *)*numOutputTaskStatistics);
resourceList.csv = csv;
for (uint32_t ii = 0; ii < numOutputTaskStatistics; ii++) {
cudlaTranslateCsvAttribute csvAttribute;
uint64_t csvStreamLength = 0;
err = etbl->etiTranslateStats(devHandle,statisticsOutputBuffer[ii],dlaFreqInMHz,ii,CUDLA_GET_CSV_LENGTH,&csvAttribute);
csv[ii] = (void* )malloc(csvAttribute.csvStreamLength);
csvStreamLength = csvAttribute.csvStreamLength;
DPRINTF("size for statistics buffer %u is %lu \n",ii,csvStreamLength);
if (csv[ii] == NULL) {
DPRINTF("Error in allocating memory for csv stream\n");
cleanUp(&resourceList);
return 1;
}
memset(csv[ii], 0x00, csvAttribute.csvStreamLength);
csvAttribute.csvStreamStats = csv[ii];
err = etbl->etiTranslateStats(devHandle,statisticsOutputBuffer[ii],dlaFreqInMHz,ii,CUDLA_GET_CSV_STATS,&csvAttribute);
if (err != cudlaSuccess) {
DPRINTF("Error in translating stats\n");
cleanUp(&resourceList);
return 1;
}
if (argc == 5) {
sprintf(filename,"%s%u%s", argv[4],(ii+1),suffix);
fp = fopen(filename, "w+");
if (fp == NULL) {
DPRINTF("Cannot open file %s\n", filename);
cleanUp(&resourceList);
return 1;
}
uint32_t ret_val = fwrite(csv[ii],sizeof(char),csvStreamLength,fp);
if(ret_val != csvStreamLength) {
DPRINTF("number of elements written to file is %u \n", ret_val);
cleanUp(&resourceList);
return 1;
}
fclose(fp);
} else {
DPRINTF("%s \n",(char *)csv[ii]);
}
}
}
// unregister the CUDA-allocated buffers.
for (uint32_t ii = 0; ii < numInputTensors; ii++) {
err = cudlaMemUnregister(devHandle,
(inputBufferRegisteredPtr[ii]));
if (err != cudlaSuccess) {
DPRINTF("Error in registering input memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
}
for (uint32_t ii = 0; ii < numOutputTensors; ii++) {
err = cudlaMemUnregister(devHandle,
(outputBufferRegisteredPtr[ii]));
if (err != cudlaSuccess) {
DPRINTF("Error in registering output memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
}
for (uint32_t ii = 0; ii < numOutputTaskStatistics; ii++) {
err = cudlaMemUnregister(devHandle,
(outputTaskStatisticsRegisteredPtr[ii]));
if (err != cudlaSuccess) {
DPRINTF("Error in registering output memory = %d\n", err);
cleanUp(&resourceList);
return 1;
}
}
DPRINTF("ALL MEMORY UNREGISTERED SUCCESSFULLY\n");
result = cudaStreamDestroy(stream);
if (result != cudaSuccess) {
errPtr = cudaGetErrorName(result);
DPRINTF("Error in destroying cuda stream = %s\n", errPtr);
cleanUp(&resourceList);
return 1;
}
resourceList.stream = NULL;
err = cudlaModuleUnload(moduleHandle, 0);
if (err != cudlaSuccess) {
DPRINTF("Error in cudlaModuleUnload = %d\n", err);
cleanUp(&resourceList);
return 1;
} else {
DPRINTF("Successfully unloaded module\n");
}
resourceList.moduleHandle = NULL;
err = cudlaDestroyDevice(devHandle);
if (err != cudlaSuccess) {
DPRINTF("Error in cuDLA destroy device = %d\n", err);
return 1;
}
DPRINTF("Device destroyed successfully\n");
resourceList.devHandle = NULL;
cleanUp(&resourceList);
DPRINTF("cuDLALayerwiseStatsHybrid DONE !!!\n");
return 0;
}

View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../../../Common"
],
"defines": [],
"compilerPath": "/usr/local/cuda/bin/nvcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@@ -0,0 +1,7 @@
{
"recommendations": [
"nvidia.nsight-vscode-edition",
"ms-vscode.cpptools",
"ms-vscode.makefile-tools"
]
}

View File

@@ -0,0 +1,10 @@
{
"configurations": [
{
"name": "CUDA C++: Launch",
"type": "cuda-gdb",
"request": "launch",
"program": "${workspaceFolder}/cuDLALayerwiseStatsStandalone"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "sample",
"type": "shell",
"command": "make dbg=1",
"problemMatcher": ["$nvcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -0,0 +1,416 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################
#
# Makefile project only supported on Mac OS X and Linux Platforms)
#
################################################################################
# Location of the CUDA Toolkit
CUDA_PATH ?= /usr/local/cuda
##############################
# start deprecated interface #
##############################
ifeq ($(x86_64),1)
$(info WARNING - x86_64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=x86_64 instead)
TARGET_ARCH ?= x86_64
endif
ifeq ($(ARMv7),1)
$(info WARNING - ARMv7 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=armv7l instead)
TARGET_ARCH ?= armv7l
endif
ifeq ($(aarch64),1)
$(info WARNING - aarch64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=aarch64 instead)
TARGET_ARCH ?= aarch64
endif
ifeq ($(ppc64le),1)
$(info WARNING - ppc64le variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=ppc64le instead)
TARGET_ARCH ?= ppc64le
endif
ifneq ($(GCC),)
$(info WARNING - GCC variable has been deprecated)
$(info WARNING - please use HOST_COMPILER=$(GCC) instead)
HOST_COMPILER ?= $(GCC)
endif
ifneq ($(abi),)
$(error ERROR - abi variable has been removed)
endif
############################
# end deprecated interface #
############################
# architecture
HOST_ARCH := $(shell uname -m)
TARGET_ARCH ?= $(HOST_ARCH)
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l))
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le))
TARGET_SIZE := 64
else ifneq (,$(filter $(TARGET_ARCH),armv7l))
TARGET_SIZE := 32
endif
else
TARGET_SIZE := $(shell getconf LONG_BIT)
endif
else
$(error ERROR - unsupported value $(TARGET_ARCH) for TARGET_ARCH!)
endif
# sbsa and aarch64 systems look similar. Need to differentiate them at host level for now.
ifeq ($(HOST_ARCH),aarch64)
ifeq ($(CUDA_PATH)/targets/sbsa-linux,$(shell ls -1d $(CUDA_PATH)/targets/sbsa-linux 2>/dev/null))
HOST_ARCH := sbsa
TARGET_ARCH := sbsa
endif
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq (,$(filter $(HOST_ARCH)-$(TARGET_ARCH),aarch64-armv7l x86_64-armv7l x86_64-aarch64 x86_64-sbsa x86_64-ppc64le))
$(error ERROR - cross compiling from $(HOST_ARCH) to $(TARGET_ARCH) is not supported!)
endif
endif
# When on native aarch64 system with userspace of 32-bit, change TARGET_ARCH to armv7l
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_SIZE),aarch64-aarch64-32)
TARGET_ARCH = armv7l
endif
# operating system
HOST_OS := $(shell uname -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
TARGET_OS ?= $(HOST_OS)
ifeq (,$(filter $(TARGET_OS),linux darwin qnx android))
$(error ERROR - unsupported value $(TARGET_OS) for TARGET_OS!)
endif
# host compiler
ifdef HOST_COMPILER
CUSTOM_HOST_COMPILER = 1
endif
ifeq ($(TARGET_OS),darwin)
ifeq ($(shell expr `xcodebuild -version | grep -i xcode | awk '{print $$2}' | cut -d'.' -f1` \>= 5),1)
HOST_COMPILER ?= clang++
endif
else ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
ifeq ($(TARGET_OS),linux)
HOST_COMPILER ?= arm-linux-gnueabihf-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/arm-unknown-nto-qnx6.6.0eabi-g++
else ifeq ($(TARGET_OS),android)
HOST_COMPILER ?= arm-linux-androideabi-g++
endif
else ifeq ($(TARGET_ARCH),aarch64)
ifeq ($(TARGET_OS), linux)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/q++
else ifeq ($(TARGET_OS), android)
HOST_COMPILER ?= aarch64-linux-android-clang++
endif
else ifeq ($(TARGET_ARCH),sbsa)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_ARCH),ppc64le)
HOST_COMPILER ?= powerpc64le-linux-gnu-g++
endif
endif
HOST_COMPILER ?= g++
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(HOST_COMPILER)
# internal flags
NVCCFLAGS := -m${TARGET_SIZE}
CCFLAGS :=
LDFLAGS :=
# build flags
# Link flag for customized HOST_COMPILER with gcc realpath
GCC_PATH := $(shell which gcc)
ifeq ($(CUSTOM_HOST_COMPILER),1)
ifneq ($(filter /%,$(HOST_COMPILER)),)
ifneq ($(findstring gcc,$(HOST_COMPILER)),)
ifneq ($(GCC_PATH),$(HOST_COMPILER))
LDFLAGS += -lstdc++
endif
endif
endif
endif
ifeq ($(TARGET_OS),darwin)
LDFLAGS += -rpath $(CUDA_PATH)/lib
CCFLAGS += -arch $(HOST_ARCH)
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
LDFLAGS += --dynamic-linker=/lib/ld-linux-armhf.so.3
CCFLAGS += -mfloat-abi=hard
else ifeq ($(TARGET_OS),android)
LDFLAGS += -pie
CCFLAGS += -fpie -fpic -fexceptions
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib -L$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/lib/aarch64-linux-gnu -L$(TARGET_FS)/lib/aarch64-linux-gnu
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib -L$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/aarch64-linux-gnu -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
LDFLAGS += --unresolved-symbols=ignore-in-shared-libs
CCFLAGS += -isystem=$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include/libdrm
CCFLAGS += -isystem=$(TARGET_FS)/usr/include/aarch64-linux-gnu -I$(TARGET_FS)/usr/include/aarch64-linux-gnu
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
NVCCFLAGS += -D_QNX_SOURCE
NVCCFLAGS += --qpp-config 8.3.0,gcc_ntoaarch64le
CCFLAGS += -DWIN_INTERFACE_CUSTOM -I/usr/include/aarch64-qnx-gnu
LDFLAGS += -lsocket
LDFLAGS += -L/usr/lib/aarch64-qnx-gnu
CCFLAGS += "-Wl\,-rpath-link\,/usr/lib/aarch64-qnx-gnu"
ifdef TARGET_OVERRIDE
LDFLAGS += -lslog2
endif
ifneq ($(TARGET_FS),)
LDFLAGS += -L$(TARGET_FS)/usr/lib
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/lib"
LDFLAGS += -L$(TARGET_FS)/usr/libnvidia
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/libnvidia"
CCFLAGS += -I$(TARGET_FS)/../include
endif
endif
endif
ifdef TARGET_OVERRIDE # cuda toolkit targets override
NVCCFLAGS += -target-dir $(TARGET_OVERRIDE)
endif
# Install directory of different arch
CUDA_INSTALL_TARGET_DIR :=
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-gnueabihf/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_INSTALL_TARGET_DIR = targets/sbsa-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_INSTALL_TARGET_DIR = targets/ARMv7-linux-QNX/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-qnx/
else ifeq ($(TARGET_ARCH),ppc64le)
CUDA_INSTALL_TARGET_DIR = targets/ppc64le-linux/
endif
# Debug build flags
ifeq ($(dbg),1)
NVCCFLAGS += -g -G
BUILD_TYPE := debug
else
BUILD_TYPE := release
endif
ALL_CCFLAGS :=
ALL_CCFLAGS += $(NVCCFLAGS)
ALL_CCFLAGS += $(EXTRA_NVCCFLAGS)
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(EXTRA_CCFLAGS))
SAMPLE_ENABLED := 1
# This sample is not supported on Linux x86_64
ifeq ($(TARGET_OS),linux)
ifeq ($(TARGET_ARCH),x86_64)
$(info >>> WARNING - cuDLALayerwiseStatsStandalone is not supported on Linux x86_64 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
endif
# This sample is not supported on Mac OSX
ifeq ($(TARGET_OS),darwin)
$(info >>> WARNING - cuDLALayerwiseStatsStandalone is not supported on Mac OSX - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on ARMv7
ifeq ($(TARGET_ARCH),armv7l)
$(info >>> WARNING - cuDLALayerwiseStatsStandalone is not supported on ARMv7 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on sbsa
ifeq ($(TARGET_ARCH),sbsa)
$(info >>> WARNING - cuDLALayerwiseStatsStandalone is not supported on sbsa - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ALL_LDFLAGS :=
ALL_LDFLAGS += $(ALL_CCFLAGS)
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
ALL_LDFLAGS += $(addprefix -Xlinker ,$(EXTRA_LDFLAGS))
# Common includes and paths for CUDA
INCLUDES := -I../../../Common
LIBRARIES :=
################################################################################
# Makefile include to help find NVSCI Libraries
include ./findnvsci.mk
#Detect if installed version of GCC supports required C++11
ifeq ($(TARGET_OS),linux)
empty :=
space := $(empty) $(empty)
GCCVERSIONSTRING := $(shell expr `$(HOST_COMPILER) -dumpversion`)
#Create version number without "."
GCCVERSION := $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f1 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f2 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f3 -d.)
# Make sure the version number has at least 3 decimals
GCCVERSION += 00
# Remove spaces from the version number
GCCVERSION := $(subst $(space),$(empty),$(GCCVERSION))
#$(warning $(GCCVERSION))
IS_MIN_VERSION := $(shell expr `echo $(GCCVERSION)` \>= 47000)
ifneq ($(CUSTOM_HOST_COMPILER), 1)
ifeq ($(IS_MIN_VERSION), 1)
$(info >>> GCC Version is greater or equal to 4.7.0 <<<)
else
$(info >>> Waiving build. Minimum GCC version required is 4.7.0<<<)
SAMPLE_ENABLED := 0
endif
else
$(warning >>> Custom HOST_COMPILER set; skipping GCC version check. This may lead to unintended behavior. Please note the minimum equivalent GCC version is 4.7.0 <<<)
endif
endif
# Gencode arguments
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),armv7l aarch64 sbsa))
SMS ?= 53 61 70 72 75 80 86 87 90
else
SMS ?= 50 52 60 61 70 75 80 86 89 90
endif
ifeq ($(SMS),)
$(info >>> WARNING - no SM architectures have been specified - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ($(GENCODE_FLAGS),)
# Generate SASS code for each SM architecture listed in $(SMS)
$(foreach sm,$(SMS),$(eval GENCODE_FLAGS += -gencode arch=compute_$(sm),code=sm_$(sm)))
# Generate PTX code from the highest SM architecture in $(SMS) to guarantee forward-compatibility
HIGHEST_SM := $(lastword $(sort $(SMS)))
ifneq ($(HIGHEST_SM),)
GENCODE_FLAGS += -gencode arch=compute_$(HIGHEST_SM),code=compute_$(HIGHEST_SM)
endif
endif
ALL_CCFLAGS += --std=c++11 --threads 0
LIBRARIES += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu/nvidia -lcudla -lnvscibuf -lnvscisync
ifeq ($(SAMPLE_ENABLED),0)
EXEC ?= @echo "[@]"
endif
################################################################################
# Target rules
all: build
build: cuDLALayerwiseStatsStandalone
check.deps:
ifeq ($(SAMPLE_ENABLED),0)
@echo "Sample will be waived due to the above missing dependencies"
else
@echo "Sample is ready - all dependencies have been met"
endif
main.o:main.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
cuDLALayerwiseStatsStandalone: main.o
$(EXEC) $(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
$(EXEC) mkdir -p ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
$(EXEC) cp $@ ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
run: build
$(EXEC) ./cuDLALayerwiseStatsStandalone
testrun: build
clean:
rm -f cuDLALayerwiseStatsStandalone main.o
rm -rf ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)/cuDLALayerwiseStatsStandalone
clobber: clean

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entry SYSTEM "SamplesInfo.dtd">
<entry>
<name>cuDLALayerwiseStatsStandalone</name>
<cflags>
<flag>--std=c++11</flag>
</cflags>
<description><![CDATA[This sample is used to provide layerwise statistics to the application in cuDLA standalone mode where DLA is programmed without using CUDA.]]></description>
<devicecompilation>whole</devicecompilation>
<includepaths>
<path>./</path>
<path>../</path>
<path>../../../Common</path>
</includepaths>
<keyconcepts>
<concept level="basic">cuDLA</concept>
<concept level="advanced">Data Parallel Algorithms</concept>
<concept level="advanced">Image Processing</concept>
</keyconcepts>
<keywords>
<keyword>CUDA</keyword>
<keyword>CPP11</keyword>
</keywords>
<libraries>
<library>cudla</library>
<library>nvscibuf</library>
<library>nvscisync</library>
</libraries>
<librarypaths>
<path>$(TARGET_FS)/usr/lib/aarch64-linux-gnu/nvidia</path>
</librarypaths>
<nsight_eclipse>true</nsight_eclipse>
<primary_file>main.cpp</primary_file>
<required_dependencies>
<dependency>NVSCI</dependency>
</required_dependencies>
<scopes>
<scope>1:CUDA Advanced Topics</scope>
<scope>1:cuDLA</scope>
</scopes>
<sm-arch>sm60</sm-arch>
<sm-arch>sm61</sm-arch>
<sm-arch>sm70</sm-arch>
<sm-arch>sm72</sm-arch>
<sm-arch>sm75</sm-arch>
<sm-arch>sm80</sm-arch>
<sm-arch>sm86</sm-arch>
<sm-arch>sm87</sm-arch>
<sm-arch>sm89</sm-arch>
<sm-arch>sm90</sm-arch>
<supported_envs>
<env>
<arch>aarch64</arch>
<platform>linux</platform>
</env>
<env>
<arch>aarch64</arch>
<platform>qnx</platform>
</env>
</supported_envs>
<supported_sm_architectures>
<from>6.0</from>
</supported_sm_architectures>
<title>cuDLA Layerwise Statistics Standalone Mode</title>
<type>exe</type>
</entry>

View File

@@ -0,0 +1,61 @@
# cuDLALayerwiseStatsStandalone - cuDLA Layerwise Statistics Standalone Mode
## Description
This sample is used to provide layerwise statistics to the application in cuDLA standalone mode where DLA is programmed without using CUDA.
## Key Concepts
cuDLA, Data Parallel Algorithms, Image Processing
## Supported SM Architectures
[SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
## Supported OSes
Linux, QNX
## Supported CPU Architecture
aarch64
## CUDA APIs involved
## Dependencies needed to build/run
[NVSCI](../../../README.md#nvsci)
## Prerequisites
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
## Build and Run
### Linux
The Linux samples are built using makefiles. To use the makefiles, change the current directory to the sample directory you wish to build, and run make:
```
$ cd <sample_dir>
$ make
```
The samples makefiles can take advantage of certain options:
* **TARGET_ARCH=<arch>** - cross-compile targeting a specific architecture. Allowed architectures are aarch64.
By default, TARGET_ARCH is set to HOST_ARCH. On a x86_64 machine, not setting TARGET_ARCH is the equivalent of setting TARGET_ARCH=x86_64.<br/>
`$ make TARGET_ARCH=aarch64` <br/>
See [here](http://docs.nvidia.com/cuda/cuda-samples/index.html#cross-samples) for more details.
* **dbg=1** - build with debug symbols
```
$ make dbg=1
```
* **SMS="A B ..."** - override the SM architectures for which the sample will be built, where `"A B ..."` is a space-delimited list of SM architectures. For example, to generate SASS for SM 50 and SM 60, use `SMS="50 60"`.
```
$ make SMS="50 60"
```
* **HOST_COMPILER=<host_compiler>** - override the default g++ host compiler. See the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements) for a list of supported host compilers.
```
$ make HOST_COMPILER=g++
```
## References (for more details)

View File

@@ -0,0 +1,144 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#################################################################################
# findnvsci.mk is used to find the NVSCI Libraries and headers
#
################################################################################
# Determine OS platform and unix distribution
ifeq ("$(TARGET_OS)","linux")
# first search lsb_release
DISTRO = $(shell lsb_release -i -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
ifeq ("$(DISTRO)","")
# second search and parse /etc/issue
DISTRO = $(shell more /etc/issue | awk '{print $$1}' | sed '1!d' | sed -e "/^$$/d" 2>/dev/null | tr "[:upper:]" "[:lower:]")
# ensure data from /etc/issue is valid
ifeq (,$(filter $(DISTRO),ubuntu fedora red rhel centos suse))
DISTRO =
endif
ifeq ("$(DISTRO)","")
# third, we can search in /etc/os-release or /etc/{distro}-release
DISTRO = $(shell awk '/ID/' /etc/*-release | sed 's/ID=//' | grep -v "VERSION" | grep -v "ID" | grep -v "DISTRIB")
endif
endif
endif
ifeq ("$(TARGET_OS)","linux")
# $(info) >> findegl.mk -> LINUX path <<<)
# Each set of Linux Distros have different paths for where to find their OpenGL libraries reside
UBUNTU = $(shell echo $(DISTRO) | grep -i ubuntu >/dev/null 2>&1; echo $$?)
FEDORA = $(shell echo $(DISTRO) | grep -i fedora >/dev/null 2>&1; echo $$?)
RHEL = $(shell echo $(DISTRO) | grep -i 'red\|rhel' >/dev/null 2>&1; echo $$?)
CENTOS = $(shell echo $(DISTRO) | grep -i centos >/dev/null 2>&1; echo $$?)
SUSE = $(shell echo $(DISTRO) | grep -i 'suse\|sles' >/dev/null 2>&1; echo $$?)
KYLIN = $(shell echo $(DISTRO) | grep -i kylin >/dev/null 2>&1; echo $$?)
ifeq ("$(UBUNTU)","0")
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
GLPATH := /usr/arm-linux-gnueabihf/lib
GLLINK := -L/usr/arm-linux-gnueabihf/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib/arm-linux-gnueabihf
GLLINK += -L$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-aarch64)
GLPATH := /usr/aarch64-linux-gnu/lib
GLLINK := -L/usr/aarch64-linux-gnu/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib
GLPATH += $(TARGET_FS)/usr/lib/aarch64-linux-gnu
GLLINK += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
endif
else
UBUNTU_PKG_NAME = $(shell which dpkg >/dev/null 2>&1 && dpkg -l 'nvidia-*' | grep '^ii' | awk '{print $$2}' | head -1)
ifneq ("$(UBUNTU_PKG_NAME)","")
GLPATH ?= /usr/lib/$(UBUNTU_PKG_NAME)
GLLINK ?= -L/usr/lib/$(UBUNTU_PKG_NAME)
endif
DFLT_PATH ?= /usr/lib
endif
endif
ifeq ("$(SUSE)","0")
GLPATH ?= /usr/X11R6/lib64
GLLINK ?= -L/usr/X11R6/lib64
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(FEDORA)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(RHEL)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(CENTOS)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(KYLIN)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
NVSCIBUFLIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvscibuf.so -print 2>/dev/null)
NVSCISYNCLIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvscisync.so -print 2>/dev/null)
ifeq ("$(NVSCIBUFLIB)","")
$(info >>> WARNING - libnvscibuf.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ("$(NVSCISYNCLIB)","")
$(info >>> WARNING - libnvscisync.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
HEADER_SEARCH_PATH ?= $(TARGET_FS)/usr/include
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
HEADER_SEARCH_PATH += /usr/arm-linux-gnueabihf/include
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-aarch64-linux)
HEADER_SEARCH_PATH += /usr/aarch64-linux-gnu/include
endif
NVSCIBUFHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvscibuf.h -print 2>/dev/null)
NVSCISYNCHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvscisync.h -print 2>/dev/null)
ifeq ("$(NVSCIBUFHEADER)","")
$(info >>> WARNING - nvscibuf.h not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ("$(NVSCISYNCHEADER)","")
$(info >>> WARNING - nvscisync.h not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
else
endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../../../Common"
],
"defines": [],
"compilerPath": "/usr/local/cuda/bin/nvcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@@ -0,0 +1,7 @@
{
"recommendations": [
"nvidia.nsight-vscode-edition",
"ms-vscode.cpptools",
"ms-vscode.makefile-tools"
]
}

View File

@@ -0,0 +1,10 @@
{
"configurations": [
{
"name": "CUDA C++: Launch",
"type": "cuda-gdb",
"request": "launch",
"program": "${workspaceFolder}/cuDLAStandaloneMode"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "sample",
"type": "shell",
"command": "make dbg=1",
"problemMatcher": ["$nvcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -0,0 +1,416 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################
#
# Makefile project only supported on Mac OS X and Linux Platforms)
#
################################################################################
# Location of the CUDA Toolkit
CUDA_PATH ?= /usr/local/cuda
##############################
# start deprecated interface #
##############################
ifeq ($(x86_64),1)
$(info WARNING - x86_64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=x86_64 instead)
TARGET_ARCH ?= x86_64
endif
ifeq ($(ARMv7),1)
$(info WARNING - ARMv7 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=armv7l instead)
TARGET_ARCH ?= armv7l
endif
ifeq ($(aarch64),1)
$(info WARNING - aarch64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=aarch64 instead)
TARGET_ARCH ?= aarch64
endif
ifeq ($(ppc64le),1)
$(info WARNING - ppc64le variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=ppc64le instead)
TARGET_ARCH ?= ppc64le
endif
ifneq ($(GCC),)
$(info WARNING - GCC variable has been deprecated)
$(info WARNING - please use HOST_COMPILER=$(GCC) instead)
HOST_COMPILER ?= $(GCC)
endif
ifneq ($(abi),)
$(error ERROR - abi variable has been removed)
endif
############################
# end deprecated interface #
############################
# architecture
HOST_ARCH := $(shell uname -m)
TARGET_ARCH ?= $(HOST_ARCH)
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l))
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le))
TARGET_SIZE := 64
else ifneq (,$(filter $(TARGET_ARCH),armv7l))
TARGET_SIZE := 32
endif
else
TARGET_SIZE := $(shell getconf LONG_BIT)
endif
else
$(error ERROR - unsupported value $(TARGET_ARCH) for TARGET_ARCH!)
endif
# sbsa and aarch64 systems look similar. Need to differentiate them at host level for now.
ifeq ($(HOST_ARCH),aarch64)
ifeq ($(CUDA_PATH)/targets/sbsa-linux,$(shell ls -1d $(CUDA_PATH)/targets/sbsa-linux 2>/dev/null))
HOST_ARCH := sbsa
TARGET_ARCH := sbsa
endif
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq (,$(filter $(HOST_ARCH)-$(TARGET_ARCH),aarch64-armv7l x86_64-armv7l x86_64-aarch64 x86_64-sbsa x86_64-ppc64le))
$(error ERROR - cross compiling from $(HOST_ARCH) to $(TARGET_ARCH) is not supported!)
endif
endif
# When on native aarch64 system with userspace of 32-bit, change TARGET_ARCH to armv7l
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_SIZE),aarch64-aarch64-32)
TARGET_ARCH = armv7l
endif
# operating system
HOST_OS := $(shell uname -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
TARGET_OS ?= $(HOST_OS)
ifeq (,$(filter $(TARGET_OS),linux darwin qnx android))
$(error ERROR - unsupported value $(TARGET_OS) for TARGET_OS!)
endif
# host compiler
ifdef HOST_COMPILER
CUSTOM_HOST_COMPILER = 1
endif
ifeq ($(TARGET_OS),darwin)
ifeq ($(shell expr `xcodebuild -version | grep -i xcode | awk '{print $$2}' | cut -d'.' -f1` \>= 5),1)
HOST_COMPILER ?= clang++
endif
else ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
ifeq ($(TARGET_OS),linux)
HOST_COMPILER ?= arm-linux-gnueabihf-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/arm-unknown-nto-qnx6.6.0eabi-g++
else ifeq ($(TARGET_OS),android)
HOST_COMPILER ?= arm-linux-androideabi-g++
endif
else ifeq ($(TARGET_ARCH),aarch64)
ifeq ($(TARGET_OS), linux)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/q++
else ifeq ($(TARGET_OS), android)
HOST_COMPILER ?= aarch64-linux-android-clang++
endif
else ifeq ($(TARGET_ARCH),sbsa)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_ARCH),ppc64le)
HOST_COMPILER ?= powerpc64le-linux-gnu-g++
endif
endif
HOST_COMPILER ?= g++
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(HOST_COMPILER)
# internal flags
NVCCFLAGS := -m${TARGET_SIZE}
CCFLAGS :=
LDFLAGS :=
# build flags
# Link flag for customized HOST_COMPILER with gcc realpath
GCC_PATH := $(shell which gcc)
ifeq ($(CUSTOM_HOST_COMPILER),1)
ifneq ($(filter /%,$(HOST_COMPILER)),)
ifneq ($(findstring gcc,$(HOST_COMPILER)),)
ifneq ($(GCC_PATH),$(HOST_COMPILER))
LDFLAGS += -lstdc++
endif
endif
endif
endif
ifeq ($(TARGET_OS),darwin)
LDFLAGS += -rpath $(CUDA_PATH)/lib
CCFLAGS += -arch $(HOST_ARCH)
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
LDFLAGS += --dynamic-linker=/lib/ld-linux-armhf.so.3
CCFLAGS += -mfloat-abi=hard
else ifeq ($(TARGET_OS),android)
LDFLAGS += -pie
CCFLAGS += -fpie -fpic -fexceptions
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib -L$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/lib/aarch64-linux-gnu -L$(TARGET_FS)/lib/aarch64-linux-gnu
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib -L$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/aarch64-linux-gnu -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
LDFLAGS += --unresolved-symbols=ignore-in-shared-libs
CCFLAGS += -isystem=$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include/libdrm
CCFLAGS += -isystem=$(TARGET_FS)/usr/include/aarch64-linux-gnu -I$(TARGET_FS)/usr/include/aarch64-linux-gnu
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
NVCCFLAGS += -D_QNX_SOURCE
NVCCFLAGS += --qpp-config 8.3.0,gcc_ntoaarch64le
CCFLAGS += -DWIN_INTERFACE_CUSTOM -I/usr/include/aarch64-qnx-gnu
LDFLAGS += -lsocket
LDFLAGS += -L/usr/lib/aarch64-qnx-gnu
CCFLAGS += "-Wl\,-rpath-link\,/usr/lib/aarch64-qnx-gnu"
ifdef TARGET_OVERRIDE
LDFLAGS += -lslog2
endif
ifneq ($(TARGET_FS),)
LDFLAGS += -L$(TARGET_FS)/usr/lib
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/lib"
LDFLAGS += -L$(TARGET_FS)/usr/libnvidia
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/libnvidia"
CCFLAGS += -I$(TARGET_FS)/../include
endif
endif
endif
ifdef TARGET_OVERRIDE # cuda toolkit targets override
NVCCFLAGS += -target-dir $(TARGET_OVERRIDE)
endif
# Install directory of different arch
CUDA_INSTALL_TARGET_DIR :=
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-gnueabihf/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_INSTALL_TARGET_DIR = targets/sbsa-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_INSTALL_TARGET_DIR = targets/ARMv7-linux-QNX/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-qnx/
else ifeq ($(TARGET_ARCH),ppc64le)
CUDA_INSTALL_TARGET_DIR = targets/ppc64le-linux/
endif
# Debug build flags
ifeq ($(dbg),1)
NVCCFLAGS += -g -G
BUILD_TYPE := debug
else
BUILD_TYPE := release
endif
ALL_CCFLAGS :=
ALL_CCFLAGS += $(NVCCFLAGS)
ALL_CCFLAGS += $(EXTRA_NVCCFLAGS)
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(EXTRA_CCFLAGS))
SAMPLE_ENABLED := 1
# This sample is not supported on Linux x86_64
ifeq ($(TARGET_OS),linux)
ifeq ($(TARGET_ARCH),x86_64)
$(info >>> WARNING - cuDLAStandaloneMode is not supported on Linux x86_64 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
endif
# This sample is not supported on Mac OSX
ifeq ($(TARGET_OS),darwin)
$(info >>> WARNING - cuDLAStandaloneMode is not supported on Mac OSX - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on ARMv7
ifeq ($(TARGET_ARCH),armv7l)
$(info >>> WARNING - cuDLAStandaloneMode is not supported on ARMv7 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on sbsa
ifeq ($(TARGET_ARCH),sbsa)
$(info >>> WARNING - cuDLAStandaloneMode is not supported on sbsa - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ALL_LDFLAGS :=
ALL_LDFLAGS += $(ALL_CCFLAGS)
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
ALL_LDFLAGS += $(addprefix -Xlinker ,$(EXTRA_LDFLAGS))
# Common includes and paths for CUDA
INCLUDES := -I../../../Common
LIBRARIES :=
################################################################################
# Makefile include to help find NVSCI Libraries
include ./findnvsci.mk
#Detect if installed version of GCC supports required C++11
ifeq ($(TARGET_OS),linux)
empty :=
space := $(empty) $(empty)
GCCVERSIONSTRING := $(shell expr `$(HOST_COMPILER) -dumpversion`)
#Create version number without "."
GCCVERSION := $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f1 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f2 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f3 -d.)
# Make sure the version number has at least 3 decimals
GCCVERSION += 00
# Remove spaces from the version number
GCCVERSION := $(subst $(space),$(empty),$(GCCVERSION))
#$(warning $(GCCVERSION))
IS_MIN_VERSION := $(shell expr `echo $(GCCVERSION)` \>= 47000)
ifneq ($(CUSTOM_HOST_COMPILER), 1)
ifeq ($(IS_MIN_VERSION), 1)
$(info >>> GCC Version is greater or equal to 4.7.0 <<<)
else
$(info >>> Waiving build. Minimum GCC version required is 4.7.0<<<)
SAMPLE_ENABLED := 0
endif
else
$(warning >>> Custom HOST_COMPILER set; skipping GCC version check. This may lead to unintended behavior. Please note the minimum equivalent GCC version is 4.7.0 <<<)
endif
endif
# Gencode arguments
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),armv7l aarch64 sbsa))
SMS ?= 53 61 70 72 75 80 86 87 90
else
SMS ?= 50 52 60 61 70 75 80 86 89 90
endif
ifeq ($(SMS),)
$(info >>> WARNING - no SM architectures have been specified - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ($(GENCODE_FLAGS),)
# Generate SASS code for each SM architecture listed in $(SMS)
$(foreach sm,$(SMS),$(eval GENCODE_FLAGS += -gencode arch=compute_$(sm),code=sm_$(sm)))
# Generate PTX code from the highest SM architecture in $(SMS) to guarantee forward-compatibility
HIGHEST_SM := $(lastword $(sort $(SMS)))
ifneq ($(HIGHEST_SM),)
GENCODE_FLAGS += -gencode arch=compute_$(HIGHEST_SM),code=compute_$(HIGHEST_SM)
endif
endif
ALL_CCFLAGS += --std=c++11 --threads 0
LIBRARIES += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu/nvidia -lcudla -lnvscibuf -lnvscisync
ifeq ($(SAMPLE_ENABLED),0)
EXEC ?= @echo "[@]"
endif
################################################################################
# Target rules
all: build
build: cuDLAStandaloneMode
check.deps:
ifeq ($(SAMPLE_ENABLED),0)
@echo "Sample will be waived due to the above missing dependencies"
else
@echo "Sample is ready - all dependencies have been met"
endif
main.o:main.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
cuDLAStandaloneMode: main.o
$(EXEC) $(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
$(EXEC) mkdir -p ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
$(EXEC) cp $@ ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
run: build
$(EXEC) ./cuDLAStandaloneMode
testrun: build
clean:
rm -f cuDLAStandaloneMode main.o
rm -rf ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)/cuDLAStandaloneMode
clobber: clean

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entry SYSTEM "SamplesInfo.dtd">
<entry>
<name>cuDLAStandaloneMode</name>
<cflags>
<flag>--std=c++11</flag>
</cflags>
<description><![CDATA[This sample demonstrates cuDLA standalone mode wherein DLA can be programmed without using CUDA.]]></description>
<devicecompilation>whole</devicecompilation>
<includepaths>
<path>./</path>
<path>../</path>
<path>../../../Common</path>
</includepaths>
<keyconcepts>
<concept level="basic">cuDLA</concept>
<concept level="advanced">Data Parallel Algorithms</concept>
<concept level="advanced">Image Processing</concept>
</keyconcepts>
<keywords>
<keyword>CUDA</keyword>
<keyword>CPP11</keyword>
</keywords>
<libraries>
<library>cudla</library>
<library>nvscibuf</library>
<library>nvscisync</library>
</libraries>
<librarypaths>
<path>$(TARGET_FS)/usr/lib/aarch64-linux-gnu/nvidia</path>
</librarypaths>
<nsight_eclipse>true</nsight_eclipse>
<primary_file>main.cpp</primary_file>
<required_dependencies>
<dependency>NVSCI</dependency>
</required_dependencies>
<scopes>
<scope>1:CUDA Advanced Topics</scope>
<scope>1:cuDLA</scope>
</scopes>
<sm-arch>sm60</sm-arch>
<sm-arch>sm61</sm-arch>
<sm-arch>sm70</sm-arch>
<sm-arch>sm72</sm-arch>
<sm-arch>sm75</sm-arch>
<sm-arch>sm80</sm-arch>
<sm-arch>sm86</sm-arch>
<sm-arch>sm87</sm-arch>
<sm-arch>sm89</sm-arch>
<sm-arch>sm90</sm-arch>
<supported_envs>
<env>
<arch>aarch64</arch>
<platform>linux</platform>
</env>
<env>
<arch>aarch64</arch>
<platform>qnx</platform>
</env>
</supported_envs>
<supported_sm_architectures>
<from>6.0</from>
</supported_sm_architectures>
<title>cuDLA Standalone Mode</title>
<type>exe</type>
</entry>

View File

@@ -0,0 +1,61 @@
# cuDLAStandaloneMode - cuDLA Standalone Mode
## Description
This sample demonstrates cuDLA standalone mode wherein DLA can be programmed without using CUDA.
## Key Concepts
cuDLA, Data Parallel Algorithms, Image Processing
## Supported SM Architectures
[SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
## Supported OSes
Linux, QNX
## Supported CPU Architecture
aarch64
## CUDA APIs involved
## Dependencies needed to build/run
[NVSCI](../../../README.md#nvsci)
## Prerequisites
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
## Build and Run
### Linux
The Linux samples are built using makefiles. To use the makefiles, change the current directory to the sample directory you wish to build, and run make:
```
$ cd <sample_dir>
$ make
```
The samples makefiles can take advantage of certain options:
* **TARGET_ARCH=<arch>** - cross-compile targeting a specific architecture. Allowed architectures are aarch64.
By default, TARGET_ARCH is set to HOST_ARCH. On a x86_64 machine, not setting TARGET_ARCH is the equivalent of setting TARGET_ARCH=x86_64.<br/>
`$ make TARGET_ARCH=aarch64` <br/>
See [here](http://docs.nvidia.com/cuda/cuda-samples/index.html#cross-samples) for more details.
* **dbg=1** - build with debug symbols
```
$ make dbg=1
```
* **SMS="A B ..."** - override the SM architectures for which the sample will be built, where `"A B ..."` is a space-delimited list of SM architectures. For example, to generate SASS for SM 50 and SM 60, use `SMS="50 60"`.
```
$ make SMS="50 60"
```
* **HOST_COMPILER=<host_compiler>** - override the default g++ host compiler. See the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements) for a list of supported host compilers.
```
$ make HOST_COMPILER=g++
```
## References (for more details)

View File

@@ -0,0 +1,144 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#################################################################################
# findnvsci.mk is used to find the NVSCI Libraries and headers
#
################################################################################
# Determine OS platform and unix distribution
ifeq ("$(TARGET_OS)","linux")
# first search lsb_release
DISTRO = $(shell lsb_release -i -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
ifeq ("$(DISTRO)","")
# second search and parse /etc/issue
DISTRO = $(shell more /etc/issue | awk '{print $$1}' | sed '1!d' | sed -e "/^$$/d" 2>/dev/null | tr "[:upper:]" "[:lower:]")
# ensure data from /etc/issue is valid
ifeq (,$(filter $(DISTRO),ubuntu fedora red rhel centos suse))
DISTRO =
endif
ifeq ("$(DISTRO)","")
# third, we can search in /etc/os-release or /etc/{distro}-release
DISTRO = $(shell awk '/ID/' /etc/*-release | sed 's/ID=//' | grep -v "VERSION" | grep -v "ID" | grep -v "DISTRIB")
endif
endif
endif
ifeq ("$(TARGET_OS)","linux")
# $(info) >> findegl.mk -> LINUX path <<<)
# Each set of Linux Distros have different paths for where to find their OpenGL libraries reside
UBUNTU = $(shell echo $(DISTRO) | grep -i ubuntu >/dev/null 2>&1; echo $$?)
FEDORA = $(shell echo $(DISTRO) | grep -i fedora >/dev/null 2>&1; echo $$?)
RHEL = $(shell echo $(DISTRO) | grep -i 'red\|rhel' >/dev/null 2>&1; echo $$?)
CENTOS = $(shell echo $(DISTRO) | grep -i centos >/dev/null 2>&1; echo $$?)
SUSE = $(shell echo $(DISTRO) | grep -i 'suse\|sles' >/dev/null 2>&1; echo $$?)
KYLIN = $(shell echo $(DISTRO) | grep -i kylin >/dev/null 2>&1; echo $$?)
ifeq ("$(UBUNTU)","0")
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
GLPATH := /usr/arm-linux-gnueabihf/lib
GLLINK := -L/usr/arm-linux-gnueabihf/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib/arm-linux-gnueabihf
GLLINK += -L$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-aarch64)
GLPATH := /usr/aarch64-linux-gnu/lib
GLLINK := -L/usr/aarch64-linux-gnu/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib
GLPATH += $(TARGET_FS)/usr/lib/aarch64-linux-gnu
GLLINK += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
endif
else
UBUNTU_PKG_NAME = $(shell which dpkg >/dev/null 2>&1 && dpkg -l 'nvidia-*' | grep '^ii' | awk '{print $$2}' | head -1)
ifneq ("$(UBUNTU_PKG_NAME)","")
GLPATH ?= /usr/lib/$(UBUNTU_PKG_NAME)
GLLINK ?= -L/usr/lib/$(UBUNTU_PKG_NAME)
endif
DFLT_PATH ?= /usr/lib
endif
endif
ifeq ("$(SUSE)","0")
GLPATH ?= /usr/X11R6/lib64
GLLINK ?= -L/usr/X11R6/lib64
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(FEDORA)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(RHEL)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(CENTOS)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(KYLIN)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
NVSCIBUFLIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvscibuf.so -print 2>/dev/null)
NVSCISYNCLIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvscisync.so -print 2>/dev/null)
ifeq ("$(NVSCIBUFLIB)","")
$(info >>> WARNING - libnvscibuf.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ("$(NVSCISYNCLIB)","")
$(info >>> WARNING - libnvscisync.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
HEADER_SEARCH_PATH ?= $(TARGET_FS)/usr/include
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
HEADER_SEARCH_PATH += /usr/arm-linux-gnueabihf/include
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-aarch64-linux)
HEADER_SEARCH_PATH += /usr/aarch64-linux-gnu/include
endif
NVSCIBUFHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvscibuf.h -print 2>/dev/null)
NVSCISYNCHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvscisync.h -print 2>/dev/null)
ifeq ("$(NVSCIBUFHEADER)","")
$(info >>> WARNING - nvscibuf.h not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ("$(NVSCISYNCHEADER)","")
$(info >>> WARNING - nvscisync.h not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
else
endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../../../Common"
],
"defines": [],
"compilerPath": "/usr/local/cuda/bin/nvcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@@ -0,0 +1,7 @@
{
"recommendations": [
"nvidia.nsight-vscode-edition",
"ms-vscode.cpptools",
"ms-vscode.makefile-tools"
]
}

View File

@@ -0,0 +1,10 @@
{
"configurations": [
{
"name": "CUDA C++: Launch",
"type": "cuda-gdb",
"request": "launch",
"program": "${workspaceFolder}/cudaNvSci"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "sample",
"type": "shell",
"command": "make dbg=1",
"problemMatcher": ["$nvcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -0,0 +1,474 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################
#
# Makefile project only supported on Mac OS X and Linux Platforms)
#
################################################################################
# Location of the CUDA Toolkit
CUDA_PATH ?= /usr/local/cuda
##############################
# start deprecated interface #
##############################
ifeq ($(x86_64),1)
$(info WARNING - x86_64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=x86_64 instead)
TARGET_ARCH ?= x86_64
endif
ifeq ($(ARMv7),1)
$(info WARNING - ARMv7 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=armv7l instead)
TARGET_ARCH ?= armv7l
endif
ifeq ($(aarch64),1)
$(info WARNING - aarch64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=aarch64 instead)
TARGET_ARCH ?= aarch64
endif
ifeq ($(ppc64le),1)
$(info WARNING - ppc64le variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=ppc64le instead)
TARGET_ARCH ?= ppc64le
endif
ifneq ($(GCC),)
$(info WARNING - GCC variable has been deprecated)
$(info WARNING - please use HOST_COMPILER=$(GCC) instead)
HOST_COMPILER ?= $(GCC)
endif
ifneq ($(abi),)
$(error ERROR - abi variable has been removed)
endif
############################
# end deprecated interface #
############################
# architecture
HOST_ARCH := $(shell uname -m)
TARGET_ARCH ?= $(HOST_ARCH)
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l))
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le))
TARGET_SIZE := 64
else ifneq (,$(filter $(TARGET_ARCH),armv7l))
TARGET_SIZE := 32
endif
else
TARGET_SIZE := $(shell getconf LONG_BIT)
endif
else
$(error ERROR - unsupported value $(TARGET_ARCH) for TARGET_ARCH!)
endif
# sbsa and aarch64 systems look similar. Need to differentiate them at host level for now.
ifeq ($(HOST_ARCH),aarch64)
ifeq ($(CUDA_PATH)/targets/sbsa-linux,$(shell ls -1d $(CUDA_PATH)/targets/sbsa-linux 2>/dev/null))
HOST_ARCH := sbsa
TARGET_ARCH := sbsa
endif
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq (,$(filter $(HOST_ARCH)-$(TARGET_ARCH),aarch64-armv7l x86_64-armv7l x86_64-aarch64 x86_64-sbsa x86_64-ppc64le))
$(error ERROR - cross compiling from $(HOST_ARCH) to $(TARGET_ARCH) is not supported!)
endif
endif
# When on native aarch64 system with userspace of 32-bit, change TARGET_ARCH to armv7l
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_SIZE),aarch64-aarch64-32)
TARGET_ARCH = armv7l
endif
# operating system
HOST_OS := $(shell uname -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
TARGET_OS ?= $(HOST_OS)
ifeq (,$(filter $(TARGET_OS),linux darwin qnx android))
$(error ERROR - unsupported value $(TARGET_OS) for TARGET_OS!)
endif
# host compiler
ifdef HOST_COMPILER
CUSTOM_HOST_COMPILER = 1
endif
ifeq ($(TARGET_OS),darwin)
ifeq ($(shell expr `xcodebuild -version | grep -i xcode | awk '{print $$2}' | cut -d'.' -f1` \>= 5),1)
HOST_COMPILER ?= clang++
endif
else ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
ifeq ($(TARGET_OS),linux)
HOST_COMPILER ?= arm-linux-gnueabihf-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/arm-unknown-nto-qnx6.6.0eabi-g++
else ifeq ($(TARGET_OS),android)
HOST_COMPILER ?= arm-linux-androideabi-g++
endif
else ifeq ($(TARGET_ARCH),aarch64)
ifeq ($(TARGET_OS), linux)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/q++
else ifeq ($(TARGET_OS), android)
HOST_COMPILER ?= aarch64-linux-android-clang++
endif
else ifeq ($(TARGET_ARCH),sbsa)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_ARCH),ppc64le)
HOST_COMPILER ?= powerpc64le-linux-gnu-g++
endif
endif
HOST_COMPILER ?= g++
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(HOST_COMPILER)
# internal flags
NVCCFLAGS := -m${TARGET_SIZE}
CCFLAGS :=
LDFLAGS :=
# build flags
# Link flag for customized HOST_COMPILER with gcc realpath
GCC_PATH := $(shell which gcc)
ifeq ($(CUSTOM_HOST_COMPILER),1)
ifneq ($(filter /%,$(HOST_COMPILER)),)
ifneq ($(findstring gcc,$(HOST_COMPILER)),)
ifneq ($(GCC_PATH),$(HOST_COMPILER))
LDFLAGS += -lstdc++
endif
endif
endif
endif
ifeq ($(TARGET_OS),darwin)
LDFLAGS += -rpath $(CUDA_PATH)/lib
CCFLAGS += -arch $(HOST_ARCH)
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
LDFLAGS += --dynamic-linker=/lib/ld-linux-armhf.so.3
CCFLAGS += -mfloat-abi=hard
else ifeq ($(TARGET_OS),android)
LDFLAGS += -pie
CCFLAGS += -fpie -fpic -fexceptions
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib -L$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/lib/aarch64-linux-gnu -L$(TARGET_FS)/lib/aarch64-linux-gnu
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib -L$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/aarch64-linux-gnu -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
LDFLAGS += --unresolved-symbols=ignore-in-shared-libs
CCFLAGS += -isystem=$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include/libdrm
CCFLAGS += -isystem=$(TARGET_FS)/usr/include/aarch64-linux-gnu -I$(TARGET_FS)/usr/include/aarch64-linux-gnu
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
NVCCFLAGS += -D_QNX_SOURCE
NVCCFLAGS += --qpp-config 8.3.0,gcc_ntoaarch64le
CCFLAGS += -DWIN_INTERFACE_CUSTOM -I/usr/include/aarch64-qnx-gnu
LDFLAGS += -lsocket
LDFLAGS += -L/usr/lib/aarch64-qnx-gnu
CCFLAGS += "-Wl\,-rpath-link\,/usr/lib/aarch64-qnx-gnu"
ifdef TARGET_OVERRIDE
LDFLAGS += -lslog2
endif
ifneq ($(TARGET_FS),)
LDFLAGS += -L$(TARGET_FS)/usr/lib
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/lib"
LDFLAGS += -L$(TARGET_FS)/usr/libnvidia
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/libnvidia"
CCFLAGS += -I$(TARGET_FS)/../include
endif
endif
endif
ifdef TARGET_OVERRIDE # cuda toolkit targets override
NVCCFLAGS += -target-dir $(TARGET_OVERRIDE)
endif
# Install directory of different arch
CUDA_INSTALL_TARGET_DIR :=
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-gnueabihf/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_INSTALL_TARGET_DIR = targets/sbsa-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_INSTALL_TARGET_DIR = targets/ARMv7-linux-QNX/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-qnx/
else ifeq ($(TARGET_ARCH),ppc64le)
CUDA_INSTALL_TARGET_DIR = targets/ppc64le-linux/
endif
# Debug build flags
ifeq ($(dbg),1)
NVCCFLAGS += -g -G
BUILD_TYPE := debug
else
BUILD_TYPE := release
endif
ALL_CCFLAGS :=
ALL_CCFLAGS += $(NVCCFLAGS)
ALL_CCFLAGS += $(EXTRA_NVCCFLAGS)
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(EXTRA_CCFLAGS))
UBUNTU = $(shell lsb_release -i -s 2>/dev/null | grep -i ubuntu)
SAMPLE_ENABLED := 1
# This sample is not supported on Mac OSX
ifeq ($(TARGET_OS),darwin)
$(info >>> WARNING - cudaNvSci is not supported on Mac OSX - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on ARMv7
ifeq ($(TARGET_ARCH),armv7l)
$(info >>> WARNING - cudaNvSci is not supported on ARMv7 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on sbsa
ifeq ($(TARGET_ARCH),sbsa)
$(info >>> WARNING - cudaNvSci is not supported on sbsa - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ALL_LDFLAGS :=
ALL_LDFLAGS += $(ALL_CCFLAGS)
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
ALL_LDFLAGS += $(addprefix -Xlinker ,$(EXTRA_LDFLAGS))
# Common includes and paths for CUDA
INCLUDES := -I../../../Common
LIBRARIES :=
################################################################################
# Makefile include to help find NVSCI Libraries
include ./findnvsci.mk
#Detect if installed version of GCC supports required C++11
ifeq ($(TARGET_OS),linux)
empty :=
space := $(empty) $(empty)
GCCVERSIONSTRING := $(shell expr `$(HOST_COMPILER) -dumpversion`)
#Create version number without "."
GCCVERSION := $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f1 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f2 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f3 -d.)
# Make sure the version number has at least 3 decimals
GCCVERSION += 00
# Remove spaces from the version number
GCCVERSION := $(subst $(space),$(empty),$(GCCVERSION))
#$(warning $(GCCVERSION))
IS_MIN_VERSION := $(shell expr `echo $(GCCVERSION)` \>= 47000)
ifneq ($(CUSTOM_HOST_COMPILER), 1)
ifeq ($(IS_MIN_VERSION), 1)
$(info >>> GCC Version is greater or equal to 4.7.0 <<<)
else
$(info >>> Waiving build. Minimum GCC version required is 4.7.0<<<)
SAMPLE_ENABLED := 0
endif
else
$(warning >>> Custom HOST_COMPILER set; skipping GCC version check. This may lead to unintended behavior. Please note the minimum equivalent GCC version is 4.7.0 <<<)
endif
endif
# Gencode arguments
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),armv7l aarch64 sbsa))
SMS ?= 53 61 70 72 75 80 86 87 90
else
SMS ?= 50 52 60 61 70 75 80 86 89 90
endif
ifeq ($(SMS),)
$(info >>> WARNING - no SM architectures have been specified - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ($(GENCODE_FLAGS),)
# Generate SASS code for each SM architecture listed in $(SMS)
$(foreach sm,$(SMS),$(eval GENCODE_FLAGS += -gencode arch=compute_$(sm),code=sm_$(sm)))
# Generate PTX code from the highest SM architecture in $(SMS) to guarantee forward-compatibility
HIGHEST_SM := $(lastword $(sort $(SMS)))
ifneq ($(HIGHEST_SM),)
GENCODE_FLAGS += -gencode arch=compute_$(HIGHEST_SM),code=compute_$(HIGHEST_SM)
endif
endif
ifeq ($(TARGET_OS),darwin)
ALL_LDFLAGS += -Xcompiler -F/Library/Frameworks -Xlinker -framework -Xlinker CUDA
else
ifeq ($(TARGET_ARCH),x86_64)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/lib64/stubs
CUDA_SEARCH_PATH += $(CUDA_PATH)/lib/stubs
CUDA_SEARCH_PATH += $(CUDA_PATH)/targets/x86_64-linux/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/armv7-linux-gnueabihf/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/aarch64-linux/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/sbsa-linux/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/armv7-linux-androideabi/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/aarch64-linux-androideabi/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/ARMv7-linux-QNX/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/aarch64-qnx/lib/stubs
ifdef TARGET_OVERRIDE
CUDA_SEARCH_PATH := $(CUDA_PATH)/targets/$(TARGET_OVERRIDE)/lib/stubs
endif
endif
ifeq ($(TARGET_ARCH),ppc64le)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/ppc64le-linux/lib/stubs
endif
ifeq ($(HOST_ARCH),ppc64le)
CUDA_SEARCH_PATH += $(CUDA_PATH)/lib64/stubs
endif
CUDALIB ?= $(shell find -L $(CUDA_SEARCH_PATH) -maxdepth 1 -name libcuda.so 2> /dev/null)
ifeq ("$(CUDALIB)","")
$(info >>> WARNING - libcuda.so not found, CUDA Driver is not installed. Please re-install the driver. <<<)
SAMPLE_ENABLED := 0
else
CUDALIB := $(shell echo $(CUDALIB) | sed "s/ .*//" | sed "s/\/libcuda.so//" )
LIBRARIES += -L$(CUDALIB) -lcuda
endif
endif
ALL_CCFLAGS += --std=c++11 --threads 0
LIBRARIES += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu/nvidia -lnvscibuf -lnvscisync
ifeq ($(SAMPLE_ENABLED),0)
EXEC ?= @echo "[@]"
endif
################################################################################
# Target rules
all: build
build: cudaNvSci
check.deps:
ifeq ($(SAMPLE_ENABLED),0)
@echo "Sample will be waived due to the above missing dependencies"
else
@echo "Sample is ready - all dependencies have been met"
endif
cudaNvSci.o:cudaNvSci.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
imageKernels.o:imageKernels.cu
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
main.o:main.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
cudaNvSci: cudaNvSci.o imageKernels.o main.o
$(EXEC) $(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
$(EXEC) mkdir -p ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
$(EXEC) cp $@ ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
run: build
$(EXEC) ./cudaNvSci
testrun: build
clean:
rm -f cudaNvSci cudaNvSci.o imageKernels.o main.o
rm -rf ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)/cudaNvSci
clobber: clean

View File

@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entry SYSTEM "SamplesInfo.dtd">
<entry>
<name>cudaNvSci</name>
<cflags>
<flag>--std=c++11</flag>
</cflags>
<cuda_api_list>
<driver>cuDeviceGetUuid</driver>
<toolkit>cudaExternalMemoryGetMappedBuffer</toolkit>
<toolkit>cudaImportExternalSemaphore</toolkit>
<toolkit>cudaDeviceGetAttribute</toolkit>
<toolkit>cudaNvSciSignal</toolkit>
<toolkit>cudaGetMipmappedArrayLevel</toolkit>
<toolkit>cudaImportNvSciRawBuf</toolkit>
<toolkit>cudaSetDevice</toolkit>
<toolkit>cudaImportNvSciImage</toolkit>
<toolkit>cudaNvSciApp</toolkit>
<toolkit>cudaDeviceId</toolkit>
<toolkit>cudaMallocHost</toolkit>
<toolkit>cudaSignalExternalSemaphoresAsync</toolkit>
<toolkit>cudaCreateTextureObject</toolkit>
<toolkit>cudaFreeHost</toolkit>
<toolkit>cudaNvSci</toolkit>
<toolkit>cudaNvSciWait</toolkit>
<toolkit>cudaGetDeviceCount</toolkit>
<toolkit>cudaMemcpyAsync</toolkit>
<toolkit>cudaStreamCreateWithFlags</toolkit>
<toolkit>cudaExternalMemoryGetMappedMipmappedArray</toolkit>
<toolkit>cudaStreamDestroy</toolkit>
<toolkit>cudaDeviceGetNvSciSyncAttributes</toolkit>
<toolkit>cudaDestroyTextureObject</toolkit>
<toolkit>cudaDestroyExternalMemory</toolkit>
<toolkit>cudaImportExternalMemory</toolkit>
<toolkit>cudaDestroyExternalSemaphore</toolkit>
<toolkit>cudaFreeMipmappedArray</toolkit>
<toolkit>cudaFree</toolkit>
<toolkit>cudaStreamSynchronize</toolkit>
<toolkit>cudaWaitExternalSemaphoresAsync</toolkit>
<toolkit>cudaImportNvSciSemaphore</toolkit>
</cuda_api_list>
<description><![CDATA[This sample demonstrates CUDA-NvSciBuf/NvSciSync Interop. Two CPU threads import the NvSciBuf and NvSciSync into CUDA to perform two image processing algorithms on a ppm image - image rotation in 1st thread &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; rgba to grayscale conversion of rotated image in 2nd thread. Currently only supported on Ubuntu 18.04]]></description>
<devicecompilation>whole</devicecompilation>
<includepaths>
<path>./</path>
<path>../</path>
<path>../../../Common</path>
</includepaths>
<keyconcepts>
<concept level="basic">CUDA NvSci Interop</concept>
<concept level="advanced">Data Parallel Algorithms</concept>
<concept level="advanced">Image Processing</concept>
</keyconcepts>
<keywords>
<keyword>CUDA</keyword>
<keyword>CPP11</keyword>
</keywords>
<libraries>
<library os="linux">cuda</library>
<library framework="true" os="macosx">CUDA</library>
<library>nvscibuf</library>
<library>nvscisync</library>
</libraries>
<librarypaths>
<path>$(TARGET_FS)/usr/lib/aarch64-linux-gnu/nvidia</path>
</librarypaths>
<nsight_eclipse>true</nsight_eclipse>
<primary_file>main.cpp</primary_file>
<required_dependencies>
<dependency>NVSCI</dependency>
</required_dependencies>
<scopes>
<scope>1:CUDA Advanced Topics</scope>
<scope>1:CUDA NvSciBuf/NvSciSync Interop</scope>
</scopes>
<sm-arch>sm60</sm-arch>
<sm-arch>sm61</sm-arch>
<sm-arch>sm70</sm-arch>
<sm-arch>sm72</sm-arch>
<sm-arch>sm75</sm-arch>
<sm-arch>sm80</sm-arch>
<sm-arch>sm86</sm-arch>
<sm-arch>sm87</sm-arch>
<sm-arch>sm89</sm-arch>
<sm-arch>sm90</sm-arch>
<supported_envs>
<env>
<platform>aarch64</platform>
</env>
<env>
<arch>x86_64</arch>
<platform>linux</platform>
</env>
</supported_envs>
<supported_sm_architectures>
<from>6.0</from>
</supported_sm_architectures>
<title>CUDA NvSciBuf/NvSciSync Interop</title>
<type>exe</type>
</entry>

View File

@@ -0,0 +1,67 @@
# cudaNvSci - CUDA NvSciBuf/NvSciSync Interop
## Description
This sample demonstrates CUDA-NvSciBuf/NvSciSync Interop. Two CPU threads import the NvSciBuf and NvSciSync into CUDA to perform two image processing algorithms on a ppm image - image rotation in 1st thread &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; rgba to grayscale conversion of rotated image in 2nd thread. Currently only supported on Ubuntu 18.04
## Key Concepts
CUDA NvSci Interop, Data Parallel Algorithms, Image Processing
## Supported SM Architectures
[SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
## Supported OSes
Linux
## Supported CPU Architecture
x86_64, aarch64
## CUDA APIs involved
### [CUDA Driver API](http://docs.nvidia.com/cuda/cuda-driver-api/index.html)
cuDeviceGetUuid
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
cudaExternalMemoryGetMappedBuffer, cudaImportExternalSemaphore, cudaDeviceGetAttribute, cudaNvSciSignal, cudaGetMipmappedArrayLevel, cudaImportNvSciRawBuf, cudaSetDevice, cudaImportNvSciImage, cudaNvSciApp, cudaDeviceId, cudaMallocHost, cudaSignalExternalSemaphoresAsync, cudaCreateTextureObject, cudaFreeHost, cudaNvSci, cudaNvSciWait, cudaGetDeviceCount, cudaMemcpyAsync, cudaStreamCreateWithFlags, cudaExternalMemoryGetMappedMipmappedArray, cudaStreamDestroy, cudaDeviceGetNvSciSyncAttributes, cudaDestroyTextureObject, cudaDestroyExternalMemory, cudaImportExternalMemory, cudaDestroyExternalSemaphore, cudaFreeMipmappedArray, cudaFree, cudaStreamSynchronize, cudaWaitExternalSemaphoresAsync, cudaImportNvSciSemaphore
## Dependencies needed to build/run
[NVSCI](../../../README.md#nvsci)
## Prerequisites
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
## Build and Run
### Linux
The Linux samples are built using makefiles. To use the makefiles, change the current directory to the sample directory you wish to build, and run make:
```
$ cd <sample_dir>
$ make
```
The samples makefiles can take advantage of certain options:
* **TARGET_ARCH=<arch>** - cross-compile targeting a specific architecture. Allowed architectures are x86_64, aarch64.
By default, TARGET_ARCH is set to HOST_ARCH. On a x86_64 machine, not setting TARGET_ARCH is the equivalent of setting TARGET_ARCH=x86_64.<br/>
`$ make TARGET_ARCH=x86_64` <br/> `$ make TARGET_ARCH=aarch64` <br/>
See [here](http://docs.nvidia.com/cuda/cuda-samples/index.html#cross-samples) for more details.
* **dbg=1** - build with debug symbols
```
$ make dbg=1
```
* **SMS="A B ..."** - override the SM architectures for which the sample will be built, where `"A B ..."` is a space-delimited list of SM architectures. For example, to generate SASS for SM 50 and SM 60, use `SMS="50 60"`.
```
$ make SMS="50 60"
```
* **HOST_COMPILER=<host_compiler>** - override the default g++ host compiler. See the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements) for a list of supported host compilers.
```
$ make HOST_COMPILER=g++
```
## References (for more details)

View File

@@ -0,0 +1,610 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cudaNvSci.h"
#include <cuda.h>
#include <condition_variable>
#include <iostream>
#include <thread>
std::mutex m_mutex;
std::condition_variable m_condVar;
bool workSubmitted = false;
class cudaNvSciSignal {
private:
NvSciSyncModule m_syncModule;
NvSciBufModule m_bufModule;
NvSciSyncAttrList m_syncAttrList;
NvSciSyncFence *m_fence;
NvSciBufAttrList m_rawBufAttrList;
NvSciBufAttrList m_imageBufAttrList;
NvSciBufAttrList m_buffAttrListOut[2];
NvSciBufAttrKeyValuePair pairArrayOut[10];
cudaExternalMemory_t extMemRawBuf, extMemImageBuf;
cudaMipmappedArray_t d_mipmapArray;
cudaArray_t d_mipLevelArray;
cudaTextureObject_t texObject;
cudaExternalSemaphore_t signalSem;
cudaStream_t streamToRun;
int m_cudaDeviceId;
CUuuid m_devUUID;
uint64_t m_imageWidth;
uint64_t m_imageHeight;
void *d_outputBuf;
size_t m_bufSize;
public:
cudaNvSciSignal(NvSciBufModule bufModule, NvSciSyncModule syncModule,
int cudaDeviceId, int bufSize, uint64_t imageWidth,
uint64_t imageHeight, NvSciSyncFence *fence)
: m_syncModule(syncModule),
m_bufModule(bufModule),
m_cudaDeviceId(cudaDeviceId),
d_outputBuf(NULL),
m_bufSize(bufSize),
m_imageWidth(imageWidth),
m_imageHeight(imageHeight),
m_fence(fence) {
initCuda();
checkNvSciErrors(NvSciSyncAttrListCreate(m_syncModule, &m_syncAttrList));
checkNvSciErrors(NvSciBufAttrListCreate(m_bufModule, &m_rawBufAttrList));
checkNvSciErrors(NvSciBufAttrListCreate(m_bufModule, &m_imageBufAttrList));
setRawBufAttrList(m_bufSize);
setImageBufAttrList(m_imageWidth, m_imageHeight);
checkCudaErrors(cudaDeviceGetNvSciSyncAttributes(
m_syncAttrList, m_cudaDeviceId, cudaNvSciSyncAttrSignal));
}
~cudaNvSciSignal() {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
checkCudaErrors(cudaFreeMipmappedArray(d_mipmapArray));
checkCudaErrors(cudaFree(d_outputBuf));
checkCudaErrors(cudaDestroyExternalSemaphore(signalSem));
checkCudaErrors(cudaDestroyExternalMemory(extMemRawBuf));
checkCudaErrors(cudaDestroyExternalMemory(extMemImageBuf));
checkCudaErrors(cudaDestroyTextureObject(texObject));
checkCudaErrors(cudaStreamDestroy(streamToRun));
}
void initCuda() {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
checkCudaErrors(
cudaStreamCreateWithFlags(&streamToRun, cudaStreamNonBlocking));
int major = 0, minor = 0;
checkCudaErrors(cudaDeviceGetAttribute(
&major, cudaDevAttrComputeCapabilityMajor, m_cudaDeviceId));
checkCudaErrors(cudaDeviceGetAttribute(
&minor, cudaDevAttrComputeCapabilityMinor, m_cudaDeviceId));
printf(
"[cudaNvSciSignal] GPU Device %d: \"%s\" with compute capability "
"%d.%d\n\n",
m_cudaDeviceId, _ConvertSMVer2ArchName(major, minor), major, minor);
#ifdef cuDeviceGetUuid_v2
CUresult res = cuDeviceGetUuid_v2(&m_devUUID, m_cudaDeviceId);
#else
CUresult res = cuDeviceGetUuid(&m_devUUID, m_cudaDeviceId);
#endif
if (res != CUDA_SUCCESS) {
fprintf(stderr, "Driver API error = %04d \n", res);
exit(EXIT_FAILURE);
}
}
void setRawBufAttrList(uint64_t size) {
NvSciBufType bufType = NvSciBufType_RawBuffer;
bool cpuAccess = false;
NvSciBufAttrValAccessPerm perm = NvSciBufAccessPerm_ReadWrite;
NvSciBufAttrKeyValuePair rawBufAttrs[] = {
{NvSciBufRawBufferAttrKey_Size, &size, sizeof(size)},
{NvSciBufGeneralAttrKey_Types, &bufType, sizeof(bufType)},
{NvSciBufGeneralAttrKey_NeedCpuAccess, &cpuAccess, sizeof(cpuAccess)},
{NvSciBufGeneralAttrKey_RequiredPerm, &perm, sizeof(perm)},
{NvSciBufGeneralAttrKey_GpuId, &m_devUUID, sizeof(m_devUUID)},
};
checkNvSciErrors(NvSciBufAttrListSetAttrs(
m_rawBufAttrList, rawBufAttrs,
sizeof(rawBufAttrs) / sizeof(NvSciBufAttrKeyValuePair)));
}
void setImageBufAttrList(uint32_t width, uint32_t height) {
NvSciBufType bufType = NvSciBufType_Image;
NvSciBufAttrValImageLayoutType layout = NvSciBufImage_BlockLinearType;
NvSciBufAttrValAccessPerm perm = NvSciBufAccessPerm_ReadWrite;
uint32_t planeCount = 1;
uint32_t planeWidths[] = {width};
uint32_t planeHeights[] = {height};
uint64_t lrpad = 0, tbpad = 100;
bool cpuAccessFlag = false;
NvSciBufAttrValColorFmt planecolorfmts[] = {NvSciColor_B8G8R8A8};
NvSciBufAttrValColorStd planecolorstds[] = {NvSciColorStd_SRGB};
NvSciBufAttrValImageScanType planescantype[] = {NvSciBufScan_InterlaceType};
NvSciBufAttrKeyValuePair imgBufAttrs[] = {
{NvSciBufGeneralAttrKey_Types, &bufType, sizeof(bufType)},
{NvSciBufImageAttrKey_PlaneCount, &planeCount, sizeof(planeCount)},
{NvSciBufImageAttrKey_Layout, &layout, sizeof(layout)},
{NvSciBufImageAttrKey_TopPadding, &tbpad, sizeof(tbpad)},
{NvSciBufImageAttrKey_BottomPadding, &tbpad, sizeof(tbpad)},
{NvSciBufImageAttrKey_LeftPadding, &lrpad, sizeof(lrpad)},
{NvSciBufImageAttrKey_RightPadding, &lrpad, sizeof(lrpad)},
{NvSciBufImageAttrKey_PlaneColorFormat, planecolorfmts,
sizeof(planecolorfmts)},
{NvSciBufImageAttrKey_PlaneColorStd, planecolorstds,
sizeof(planecolorstds)},
{NvSciBufImageAttrKey_PlaneWidth, planeWidths, sizeof(planeWidths)},
{NvSciBufImageAttrKey_PlaneHeight, planeHeights, sizeof(planeHeights)},
{NvSciBufGeneralAttrKey_NeedCpuAccess, &cpuAccessFlag,
sizeof(cpuAccessFlag)},
{NvSciBufGeneralAttrKey_RequiredPerm, &perm, sizeof(perm)},
{NvSciBufImageAttrKey_PlaneScanType, planescantype,
sizeof(planescantype)},
{NvSciBufGeneralAttrKey_GpuId, &m_devUUID, sizeof(m_devUUID)},
};
checkNvSciErrors(NvSciBufAttrListSetAttrs(
m_imageBufAttrList, imgBufAttrs,
sizeof(imgBufAttrs) / sizeof(NvSciBufAttrKeyValuePair)));
}
NvSciSyncAttrList getNvSciSyncAttrList() { return m_syncAttrList; }
NvSciBufAttrList getNvSciRawBufAttrList() { return m_rawBufAttrList; }
NvSciBufAttrList getNvSciImageBufAttrList() { return m_imageBufAttrList; }
void runRotateImageAndSignal(unsigned char *imageData) {
int numOfGPUs = 0;
checkCudaErrors(cudaGetDeviceCount(&numOfGPUs)); // For cuda init purpose
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
copyDataToImageArray(imageData);
createTexture();
float angle = 0.5f; // angle to rotate image by (in radians)
rotateKernel(texObject, angle, (unsigned int *)d_outputBuf, m_imageWidth,
m_imageHeight, streamToRun);
signalExternalSemaphore();
}
void cudaImportNvSciSemaphore(NvSciSyncObj syncObj) {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
cudaExternalSemaphoreHandleDesc extSemDesc;
memset(&extSemDesc, 0, sizeof(extSemDesc));
extSemDesc.type = cudaExternalSemaphoreHandleTypeNvSciSync;
extSemDesc.handle.nvSciSyncObj = (void *)syncObj;
checkCudaErrors(cudaImportExternalSemaphore(&signalSem, &extSemDesc));
}
void signalExternalSemaphore() {
cudaExternalSemaphoreSignalParams signalParams;
memset(&signalParams, 0, sizeof(signalParams));
// For cross-process signaler-waiter applications need to use NvSciIpc
// and NvSciSync[Export|Import] utilities to share the NvSciSyncFence
// across process. This step is optional in single-process.
signalParams.params.nvSciSync.fence = (void *)m_fence;
signalParams.flags = 0;
checkCudaErrors(cudaSignalExternalSemaphoresAsync(&signalSem, &signalParams,
1, streamToRun));
}
void cudaImportNvSciRawBuf(NvSciBufObj inputBufObj) {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
checkNvSciErrors(
NvSciBufObjGetAttrList(inputBufObj, &m_buffAttrListOut[0]));
memset(pairArrayOut, 0, sizeof(NvSciBufAttrKeyValuePair) * 10);
pairArrayOut[0].key = NvSciBufRawBufferAttrKey_Size;
checkNvSciErrors(
NvSciBufAttrListGetAttrs(m_buffAttrListOut[0], pairArrayOut, 1));
uint64_t size = *(uint64_t *)pairArrayOut[0].value;
cudaExternalMemoryHandleDesc memHandleDesc;
memset(&memHandleDesc, 0, sizeof(memHandleDesc));
memHandleDesc.type = cudaExternalMemoryHandleTypeNvSciBuf;
memHandleDesc.handle.nvSciBufObject = inputBufObj;
memHandleDesc.size = size;
checkCudaErrors(cudaImportExternalMemory(&extMemRawBuf, &memHandleDesc));
cudaExternalMemoryBufferDesc bufferDesc;
memset(&bufferDesc, 0, sizeof(bufferDesc));
bufferDesc.offset = 0;
bufferDesc.size = size;
m_bufSize = size;
checkCudaErrors(cudaExternalMemoryGetMappedBuffer(
&d_outputBuf, extMemRawBuf, &bufferDesc));
}
void cudaImportNvSciImage(NvSciBufObj inputBufObj) {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
checkNvSciErrors(
NvSciBufObjGetAttrList(inputBufObj, &m_buffAttrListOut[1]));
memset(pairArrayOut, 0, sizeof(NvSciBufAttrKeyValuePair) * 10);
pairArrayOut[0].key = NvSciBufImageAttrKey_Size;
pairArrayOut[1].key = NvSciBufImageAttrKey_Alignment;
pairArrayOut[2].key = NvSciBufImageAttrKey_PlaneCount;
pairArrayOut[3].key = NvSciBufImageAttrKey_PlaneWidth;
pairArrayOut[4].key = NvSciBufImageAttrKey_PlaneHeight;
checkNvSciErrors(
NvSciBufAttrListGetAttrs(m_buffAttrListOut[1], pairArrayOut, 5));
uint64_t size = *(uint64_t *)pairArrayOut[0].value;
uint64_t alignment = *(uint64_t *)pairArrayOut[1].value;
uint64_t planeCount = *(uint64_t *)pairArrayOut[2].value;
uint64_t imageWidth = *(uint64_t *)pairArrayOut[3].value;
uint64_t imageHeight = *(uint64_t *)pairArrayOut[4].value;
cudaExternalMemoryHandleDesc memHandleDesc;
memset(&memHandleDesc, 0, sizeof(memHandleDesc));
memHandleDesc.type = cudaExternalMemoryHandleTypeNvSciBuf;
memHandleDesc.handle.nvSciBufObject = inputBufObj;
memHandleDesc.size = size;
checkCudaErrors(cudaImportExternalMemory(&extMemImageBuf, &memHandleDesc));
cudaExtent extent = {};
memset(&extent, 0, sizeof(extent));
extent.width = imageWidth;
extent.height = imageHeight;
extent.depth = 0;
cudaChannelFormatDesc desc;
desc.x = 8;
desc.y = 8;
desc.z = 8;
desc.w = 8;
desc.f = cudaChannelFormatKindUnsigned;
cudaExternalMemoryMipmappedArrayDesc mipmapDesc = {0};
mipmapDesc.offset = 0;
mipmapDesc.formatDesc = desc;
mipmapDesc.extent = extent;
mipmapDesc.flags = 0;
mipmapDesc.numLevels = 1;
checkCudaErrors(cudaExternalMemoryGetMappedMipmappedArray(
&d_mipmapArray, extMemImageBuf, &mipmapDesc));
}
void copyDataToImageArray(unsigned char *imageData) {
uint32_t mipLevelId = 0;
checkCudaErrors(cudaGetMipmappedArrayLevel(&d_mipLevelArray, d_mipmapArray,
mipLevelId));
checkCudaErrors(cudaMemcpy2DToArrayAsync(
d_mipLevelArray, 0, 0, imageData, m_imageWidth * sizeof(unsigned int),
m_imageWidth * sizeof(unsigned int), m_imageHeight,
cudaMemcpyHostToDevice, streamToRun));
}
void createTexture() {
cudaResourceDesc texRes;
memset(&texRes, 0, sizeof(cudaResourceDesc));
texRes.resType = cudaResourceTypeArray;
texRes.res.array.array = d_mipLevelArray;
cudaTextureDesc texDescr;
memset(&texDescr, 0, sizeof(cudaTextureDesc));
texDescr.normalizedCoords = true;
texDescr.filterMode = cudaFilterModeLinear;
texDescr.addressMode[0] = cudaAddressModeWrap;
texDescr.addressMode[1] = cudaAddressModeWrap;
texDescr.readMode = cudaReadModeNormalizedFloat;
checkCudaErrors(
cudaCreateTextureObject(&texObject, &texRes, &texDescr, NULL));
}
};
class cudaNvSciWait {
private:
NvSciSyncModule m_syncModule;
NvSciBufModule m_bufModule;
NvSciSyncAttrList m_syncAttrList;
NvSciBufAttrList m_rawBufAttrList;
NvSciBufAttrList m_buffAttrListOut;
NvSciBufAttrKeyValuePair pairArrayOut[10];
NvSciSyncFence *m_fence;
cudaExternalMemory_t extMemRawBuf;
cudaExternalSemaphore_t waitSem;
cudaStream_t streamToRun;
int m_cudaDeviceId;
CUuuid m_devUUID;
void *d_outputBuf;
size_t m_bufSize;
size_t imageWidth;
size_t imageHeight;
public:
cudaNvSciWait(NvSciBufModule bufModule, NvSciSyncModule syncModule,
int cudaDeviceId, int bufSize, NvSciSyncFence *fence)
: m_bufModule(bufModule),
m_syncModule(syncModule),
m_cudaDeviceId(cudaDeviceId),
m_bufSize(bufSize),
m_fence(fence) {
initCuda();
checkNvSciErrors(NvSciSyncAttrListCreate(m_syncModule, &m_syncAttrList));
checkNvSciErrors(NvSciBufAttrListCreate(m_bufModule, &m_rawBufAttrList));
setRawBufAttrList(m_bufSize);
checkCudaErrors(cudaDeviceGetNvSciSyncAttributes(
m_syncAttrList, m_cudaDeviceId, cudaNvSciSyncAttrWait));
}
~cudaNvSciWait() {
checkCudaErrors(cudaStreamDestroy(streamToRun));
checkCudaErrors(cudaDestroyExternalSemaphore(waitSem));
checkCudaErrors(cudaDestroyExternalMemory(extMemRawBuf));
checkCudaErrors(cudaFree(d_outputBuf));
}
void initCuda() {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
checkCudaErrors(
cudaStreamCreateWithFlags(&streamToRun, cudaStreamNonBlocking));
#ifdef cuDeviceGetUuid_v2
CUresult res = cuDeviceGetUuid_v2(&m_devUUID, m_cudaDeviceId);
#else
CUresult res = cuDeviceGetUuid(&m_devUUID, m_cudaDeviceId);
#endif
if (res != CUDA_SUCCESS) {
fprintf(stderr, "Driver API error = %04d \n", res);
exit(EXIT_FAILURE);
}
int major = 0, minor = 0;
checkCudaErrors(cudaDeviceGetAttribute(
&major, cudaDevAttrComputeCapabilityMajor, m_cudaDeviceId));
checkCudaErrors(cudaDeviceGetAttribute(
&minor, cudaDevAttrComputeCapabilityMinor, m_cudaDeviceId));
printf(
"[cudaNvSciWait] GPU Device %d: \"%s\" with compute capability "
"%d.%d\n\n",
m_cudaDeviceId, _ConvertSMVer2ArchName(major, minor), major, minor);
}
void setRawBufAttrList(uint64_t size) {
NvSciBufType bufType = NvSciBufType_RawBuffer;
bool cpuAccess = false;
NvSciBufAttrValAccessPerm perm = NvSciBufAccessPerm_ReadWrite;
NvSciBufAttrKeyValuePair rawBufAttrs[] = {
{NvSciBufRawBufferAttrKey_Size, &size, sizeof(size)},
{NvSciBufGeneralAttrKey_Types, &bufType, sizeof(bufType)},
{NvSciBufGeneralAttrKey_NeedCpuAccess, &cpuAccess, sizeof(cpuAccess)},
{NvSciBufGeneralAttrKey_RequiredPerm, &perm, sizeof(perm)},
{NvSciBufGeneralAttrKey_GpuId, &m_devUUID, sizeof(m_devUUID)},
};
checkNvSciErrors(NvSciBufAttrListSetAttrs(
m_rawBufAttrList, rawBufAttrs,
sizeof(rawBufAttrs) / sizeof(NvSciBufAttrKeyValuePair)));
}
NvSciSyncAttrList getNvSciSyncAttrList() { return m_syncAttrList; }
NvSciBufAttrList getNvSciRawBufAttrList() { return m_rawBufAttrList; }
void runImageGrayscale(std::string image_filename, size_t imageWidth,
size_t imageHeight) {
int numOfGPUs = 0;
checkCudaErrors(cudaGetDeviceCount(&numOfGPUs)); // For cuda init purpose
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
waitExternalSemaphore();
launchGrayScaleKernel((unsigned int *)d_outputBuf, image_filename,
imageWidth, imageHeight, streamToRun);
}
void cudaImportNvSciSemaphore(NvSciSyncObj syncObj) {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
cudaExternalSemaphoreHandleDesc extSemDesc;
memset(&extSemDesc, 0, sizeof(extSemDesc));
extSemDesc.type = cudaExternalSemaphoreHandleTypeNvSciSync;
extSemDesc.handle.nvSciSyncObj = (void *)syncObj;
checkCudaErrors(cudaImportExternalSemaphore(&waitSem, &extSemDesc));
}
void waitExternalSemaphore() {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
cudaExternalSemaphoreWaitParams waitParams;
memset(&waitParams, 0, sizeof(waitParams));
// For cross-process signaler-waiter applications need to use NvSciIpc
// and NvSciSync[Export|Import] utilities to share the NvSciSyncFence
// across process. This step is optional in single-process.
waitParams.params.nvSciSync.fence = (void *)m_fence;
waitParams.flags = 0;
checkCudaErrors(
cudaWaitExternalSemaphoresAsync(&waitSem, &waitParams, 1, streamToRun));
}
void cudaImportNvSciRawBuf(NvSciBufObj inputBufObj) {
checkCudaErrors(cudaSetDevice(m_cudaDeviceId));
checkNvSciErrors(NvSciBufObjGetAttrList(inputBufObj, &m_buffAttrListOut));
memset(pairArrayOut, 0, sizeof(NvSciBufAttrKeyValuePair) * 10);
pairArrayOut[0].key = NvSciBufRawBufferAttrKey_Size;
checkNvSciErrors(
NvSciBufAttrListGetAttrs(m_buffAttrListOut, pairArrayOut, 1));
uint64_t size = *(uint64_t *)pairArrayOut[0].value;
cudaExternalMemoryHandleDesc memHandleDesc;
memset(&memHandleDesc, 0, sizeof(memHandleDesc));
memHandleDesc.type = cudaExternalMemoryHandleTypeNvSciBuf;
memHandleDesc.handle.nvSciBufObject = inputBufObj;
memHandleDesc.size = size;
checkCudaErrors(cudaImportExternalMemory(&extMemRawBuf, &memHandleDesc));
cudaExternalMemoryBufferDesc bufferDesc;
memset(&bufferDesc, 0, sizeof(bufferDesc));
bufferDesc.offset = 0;
bufferDesc.size = size;
m_bufSize = size;
checkCudaErrors(cudaExternalMemoryGetMappedBuffer(
&d_outputBuf, extMemRawBuf, &bufferDesc));
}
};
void thread_rotateAndSignal(cudaNvSciSignal *cudaNvSciSignalObj,
unsigned char *imageData) {
std::lock_guard<std::mutex> guard(m_mutex);
cudaNvSciSignalObj->runRotateImageAndSignal(imageData);
workSubmitted = true;
m_condVar.notify_one();
}
void thread_waitAndGrayscale(cudaNvSciWait *cudaNvSciWaitObj,
std::string image_filename, size_t imageWidth,
size_t imageHeight) {
// Acquire the lock
std::unique_lock<std::mutex> mlock(m_mutex);
m_condVar.wait(mlock, [] { return workSubmitted; });
cudaNvSciWaitObj->runImageGrayscale(image_filename, imageWidth, imageHeight);
}
cudaNvSci::cudaNvSci(int isMultiGPU, std::vector<int> &deviceIds,
unsigned char *imageData, size_t width, size_t height)
: m_isMultiGPU(isMultiGPU),
image_data(imageData),
imageWidth(width),
imageHeight(height) {
if (isMultiGPU) {
m_cudaNvSciSignalDeviceId = deviceIds[0];
m_cudaNvSciWaitDeviceId = deviceIds[1];
} else {
m_cudaNvSciSignalDeviceId = m_cudaNvSciWaitDeviceId = deviceIds[0];
}
m_bufSize = imageWidth * imageHeight * sizeof(unsigned int);
}
void cudaNvSci::initNvSci() {
checkNvSciErrors(NvSciSyncModuleOpen(&syncModule));
checkNvSciErrors(NvSciBufModuleOpen(&buffModule));
fence = (NvSciSyncFence *)calloc(1, sizeof(NvSciSyncFence));
}
void cudaNvSci::runCudaNvSci(std::string &image_filename) {
initNvSci();
cudaNvSciSignal rotateAndSignal(buffModule, syncModule,
m_cudaNvSciSignalDeviceId, m_bufSize,
imageWidth, imageHeight, fence);
cudaNvSciWait waitAndGrayscale(buffModule, syncModule,
m_cudaNvSciWaitDeviceId, m_bufSize, fence);
rawBufUnreconciledList[0] = rotateAndSignal.getNvSciRawBufAttrList();
rawBufUnreconciledList[1] = waitAndGrayscale.getNvSciRawBufAttrList();
createNvSciRawBufObj();
imageBufUnreconciledList[0] = rotateAndSignal.getNvSciImageBufAttrList();
createNvSciBufImageObj();
rotateAndSignal.cudaImportNvSciRawBuf(rawBufObj);
rotateAndSignal.cudaImportNvSciImage(imageBufObj);
waitAndGrayscale.cudaImportNvSciRawBuf(rawBufObj);
syncUnreconciledList[0] = rotateAndSignal.getNvSciSyncAttrList();
syncUnreconciledList[1] = waitAndGrayscale.getNvSciSyncAttrList();
createNvSciSyncObj();
rotateAndSignal.cudaImportNvSciSemaphore(syncObj);
waitAndGrayscale.cudaImportNvSciSemaphore(syncObj);
std::thread rotateThread(&thread_rotateAndSignal, &rotateAndSignal,
image_data);
std::thread grayscaleThread(&thread_waitAndGrayscale, &waitAndGrayscale,
image_filename, imageWidth, imageHeight);
rotateThread.join();
grayscaleThread.join();
}
void cudaNvSci::createNvSciRawBufObj() {
int numAttrList = 2;
checkNvSciErrors(NvSciBufAttrListReconcile(rawBufUnreconciledList,
numAttrList, &rawBufReconciledList,
&buffConflictList));
checkNvSciErrors(NvSciBufObjAlloc(rawBufReconciledList, &rawBufObj));
printf("created NvSciBufObj\n");
}
void cudaNvSci::createNvSciBufImageObj() {
int numAttrList = 1;
checkNvSciErrors(NvSciBufAttrListReconcile(
imageBufUnreconciledList, numAttrList, &imageBufReconciledList,
&imageBufConflictList));
checkNvSciErrors(NvSciBufObjAlloc(imageBufReconciledList, &imageBufObj));
printf("created NvSciBufImageObj\n");
}
void cudaNvSci::createNvSciSyncObj() {
int numAttrList = 2;
checkNvSciErrors(NvSciSyncAttrListReconcile(syncUnreconciledList, numAttrList,
&syncReconciledList,
&syncConflictList));
checkNvSciErrors(NvSciSyncObjAlloc(syncReconciledList, &syncObj));
printf("created NvSciSyncObj\n");
}

View File

@@ -0,0 +1,101 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CUDANVSCI_H
#define CUDANVSCI_H
#include <cuda_runtime.h>
#include <helper_cuda.h>
#include <nvscibuf.h>
#include <nvscisync.h>
#include <vector>
#define checkNvSciErrors(call) \
do { \
NvSciError _status = call; \
if (NvSciError_Success != _status) { \
printf( \
"NVSCI call in file '%s' in line %i returned" \
" %d, expected %d\n", \
__FILE__, __LINE__, _status, NvSciError_Success); \
fflush(stdout); \
exit(EXIT_FAILURE); \
} \
} while (0)
extern void rotateKernel(cudaTextureObject_t &texObj, const float angle,
unsigned int *d_outputData, const int imageWidth,
const int imageHeight, cudaStream_t stream);
extern void launchGrayScaleKernel(unsigned int *d_rgbaImage,
std::string image_filename, size_t imageWidth,
size_t imageHeight, cudaStream_t stream);
class cudaNvSci {
private:
int m_isMultiGPU;
int m_cudaNvSciSignalDeviceId;
int m_cudaNvSciWaitDeviceId;
unsigned char *image_data;
size_t m_bufSize;
size_t imageWidth;
size_t imageHeight;
public:
NvSciSyncModule syncModule;
NvSciBufModule buffModule;
NvSciSyncAttrList syncUnreconciledList[2];
NvSciSyncAttrList syncReconciledList;
NvSciSyncAttrList syncConflictList;
NvSciBufAttrList rawBufUnreconciledList[2];
NvSciBufAttrList imageBufUnreconciledList[2];
NvSciBufAttrList rawBufReconciledList;
NvSciBufAttrList buffConflictList;
NvSciBufAttrList imageBufReconciledList;
NvSciBufAttrList imageBufConflictList;
NvSciBufAttrList buffAttrListOut;
NvSciSyncObj syncObj;
NvSciBufObj rawBufObj;
NvSciBufObj imageBufObj;
NvSciSyncFence *fence;
cudaNvSci(int isMultiGPU, std::vector<int> &deviceIds,
unsigned char *image_data, size_t imageWidth, size_t imageHeight);
void initNvSci();
void runCudaNvSci(std::string &image_filename);
void createNvSciRawBufObj();
void createNvSciSyncObj();
void createNvSciBufImageObj();
};
#endif // CUDANVSCI_H

View File

@@ -0,0 +1,144 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#################################################################################
# findnvsci.mk is used to find the NVSCI Libraries and headers
#
################################################################################
# Determine OS platform and unix distribution
ifeq ("$(TARGET_OS)","linux")
# first search lsb_release
DISTRO = $(shell lsb_release -i -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
ifeq ("$(DISTRO)","")
# second search and parse /etc/issue
DISTRO = $(shell more /etc/issue | awk '{print $$1}' | sed '1!d' | sed -e "/^$$/d" 2>/dev/null | tr "[:upper:]" "[:lower:]")
# ensure data from /etc/issue is valid
ifeq (,$(filter $(DISTRO),ubuntu fedora red rhel centos suse))
DISTRO =
endif
ifeq ("$(DISTRO)","")
# third, we can search in /etc/os-release or /etc/{distro}-release
DISTRO = $(shell awk '/ID/' /etc/*-release | sed 's/ID=//' | grep -v "VERSION" | grep -v "ID" | grep -v "DISTRIB")
endif
endif
endif
ifeq ("$(TARGET_OS)","linux")
# $(info) >> findegl.mk -> LINUX path <<<)
# Each set of Linux Distros have different paths for where to find their OpenGL libraries reside
UBUNTU = $(shell echo $(DISTRO) | grep -i ubuntu >/dev/null 2>&1; echo $$?)
FEDORA = $(shell echo $(DISTRO) | grep -i fedora >/dev/null 2>&1; echo $$?)
RHEL = $(shell echo $(DISTRO) | grep -i 'red\|rhel' >/dev/null 2>&1; echo $$?)
CENTOS = $(shell echo $(DISTRO) | grep -i centos >/dev/null 2>&1; echo $$?)
SUSE = $(shell echo $(DISTRO) | grep -i 'suse\|sles' >/dev/null 2>&1; echo $$?)
KYLIN = $(shell echo $(DISTRO) | grep -i kylin >/dev/null 2>&1; echo $$?)
ifeq ("$(UBUNTU)","0")
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
GLPATH := /usr/arm-linux-gnueabihf/lib
GLLINK := -L/usr/arm-linux-gnueabihf/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib/arm-linux-gnueabihf
GLLINK += -L$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-aarch64)
GLPATH := /usr/aarch64-linux-gnu/lib
GLLINK := -L/usr/aarch64-linux-gnu/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib
GLPATH += $(TARGET_FS)/usr/lib/aarch64-linux-gnu
GLLINK += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
endif
else
UBUNTU_PKG_NAME = $(shell which dpkg >/dev/null 2>&1 && dpkg -l 'nvidia-*' | grep '^ii' | awk '{print $$2}' | head -1)
ifneq ("$(UBUNTU_PKG_NAME)","")
GLPATH ?= /usr/lib/$(UBUNTU_PKG_NAME)
GLLINK ?= -L/usr/lib/$(UBUNTU_PKG_NAME)
endif
DFLT_PATH ?= /usr/lib
endif
endif
ifeq ("$(SUSE)","0")
GLPATH ?= /usr/X11R6/lib64
GLLINK ?= -L/usr/X11R6/lib64
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(FEDORA)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(RHEL)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(CENTOS)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(KYLIN)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
NVSCIBUFLIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvscibuf.so -print 2>/dev/null)
NVSCISYNCLIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvscisync.so -print 2>/dev/null)
ifeq ("$(NVSCIBUFLIB)","")
$(info >>> WARNING - libnvscibuf.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ("$(NVSCISYNCLIB)","")
$(info >>> WARNING - libnvscisync.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
HEADER_SEARCH_PATH ?= $(TARGET_FS)/usr/include
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
HEADER_SEARCH_PATH += /usr/arm-linux-gnueabihf/include
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-aarch64-linux)
HEADER_SEARCH_PATH += /usr/aarch64-linux-gnu/include
endif
NVSCIBUFHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvscibuf.h -print 2>/dev/null)
NVSCISYNCHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvscisync.h -print 2>/dev/null)
ifeq ("$(NVSCIBUFHEADER)","")
$(info >>> WARNING - nvscibuf.h not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ("$(NVSCISYNCHEADER)","")
$(info >>> WARNING - nvscisync.h not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
else
endif

View File

@@ -0,0 +1,120 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cuda.h>
#include <helper_cuda.h>
#include <helper_image.h>
// convert floating point rgba color to 32-bit integer
__device__ unsigned int rgbaFloatToInt(float4 rgba) {
rgba.x = __saturatef(rgba.x); // clamp to [0.0, 1.0]
rgba.y = __saturatef(rgba.y);
rgba.z = __saturatef(rgba.z);
rgba.w = __saturatef(rgba.w);
return ((unsigned int)(rgba.w * 255.0f) << 24) |
((unsigned int)(rgba.z * 255.0f) << 16) |
((unsigned int)(rgba.y * 255.0f) << 8) |
((unsigned int)(rgba.x * 255.0f));
}
////////////////////////////////////////////////////////////////////////////////
//! Rotate an image using texture lookups
//! @param outputData output data in global memory
////////////////////////////////////////////////////////////////////////////////
static __global__ void transformKernel(unsigned int *outputData, int width,
int height, float theta,
cudaTextureObject_t tex) {
// calculate normalized texture coordinates
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
float u = (float)x - (float)width / 2;
float v = (float)y - (float)height / 2;
float tu = u * cosf(theta) - v * sinf(theta);
float tv = v * cosf(theta) + u * sinf(theta);
tu /= (float)width;
tv /= (float)height;
// read from texture and write to global memory
float4 pix = tex2D<float4>(tex, tu + 0.5f, tv + 0.5f);
unsigned int pixelInt = rgbaFloatToInt(pix);
outputData[y * width + x] = pixelInt;
}
static __global__ void rgbToGrayscaleKernel(unsigned int *rgbaImage,
size_t imageWidth,
size_t imageHeight) {
size_t gidX = blockDim.x * blockIdx.x + threadIdx.x;
uchar4 *pixArray = (uchar4 *)rgbaImage;
for (int pixId = gidX; pixId < imageWidth * imageHeight;
pixId += gridDim.x * blockDim.x) {
uchar4 dataA = pixArray[pixId];
unsigned char grayscale =
(unsigned char)(dataA.x * 0.3 + dataA.y * 0.59 + dataA.z * 0.11);
uchar4 dataB = make_uchar4(grayscale, grayscale, grayscale, 0);
pixArray[pixId] = dataB;
}
}
void launchGrayScaleKernel(unsigned int *d_rgbaImage,
std::string image_filename, size_t imageWidth,
size_t imageHeight, cudaStream_t stream) {
int numThreadsPerBlock = 1024;
int numOfBlocks = (imageWidth * imageHeight) / numThreadsPerBlock;
rgbToGrayscaleKernel<<<numOfBlocks, numThreadsPerBlock, 0, stream>>>(
d_rgbaImage, imageWidth, imageHeight);
unsigned int *outputData;
checkCudaErrors(cudaMallocHost((void**)&outputData, sizeof(unsigned int) * imageWidth * imageHeight));
checkCudaErrors(cudaMemcpyAsync(
outputData, d_rgbaImage, sizeof(unsigned int) * imageWidth * imageHeight,
cudaMemcpyDeviceToHost, stream));
checkCudaErrors(cudaStreamSynchronize(stream));
char outputFilename[1024];
strcpy(outputFilename, image_filename.c_str());
strcpy(outputFilename + image_filename.length() - 4, "_out.ppm");
sdkSavePPM4ub(outputFilename, (unsigned char *)outputData, imageWidth,
imageHeight);
printf("Wrote '%s'\n", outputFilename);
checkCudaErrors(cudaFreeHost(outputData));
}
void rotateKernel(cudaTextureObject_t &texObj, const float angle,
unsigned int *d_outputData, const int imageWidth,
const int imageHeight, cudaStream_t stream) {
dim3 dimBlock(8, 8, 1);
dim3 dimGrid(imageWidth / dimBlock.x, imageHeight / dimBlock.y, 1);
transformKernel<<<dimGrid, dimBlock, 0, stream>>>(d_outputData, imageWidth,
imageHeight, angle, texObj);
}

View File

@@ -0,0 +1,101 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cuda.h>
#include <vector>
#include "cudaNvSci.h"
#include <helper_cuda.h>
#include <helper_image.h>
void loadImageData(const std::string &filename, const char **argv,
unsigned char **image_data, uint32_t &imageWidth,
uint32_t &imageHeight) {
// load image (needed so we can get the width and height before we create
// the window
char *image_path = sdkFindFilePath(filename.c_str(), argv[0]);
if (image_path == 0) {
printf("Error finding image file '%s'\n", filename.c_str());
exit(EXIT_FAILURE);
}
sdkLoadPPM4(image_path, image_data, &imageWidth, &imageHeight);
if (!image_data) {
printf("Error opening file '%s'\n", image_path);
exit(EXIT_FAILURE);
}
printf("Loaded '%s', %d x %d pixels\n", image_path, imageWidth, imageHeight);
}
int main(int argc, const char **argv) {
int numOfGPUs = 0;
std::vector<int> deviceIds;
checkCudaErrors(cudaGetDeviceCount(&numOfGPUs));
printf("%d GPUs found\n", numOfGPUs);
if (!numOfGPUs) {
exit(EXIT_WAIVED);
} else {
for (int devID = 0; devID < numOfGPUs; devID++) {
int major = 0, minor = 0;
checkCudaErrors(cudaDeviceGetAttribute(
&major, cudaDevAttrComputeCapabilityMajor, devID));
checkCudaErrors(cudaDeviceGetAttribute(
&minor, cudaDevAttrComputeCapabilityMinor, devID));
if (major >= 6) {
deviceIds.push_back(devID);
}
}
if (deviceIds.size() == 0) {
printf(
"cudaNvSci requires one or more GPUs of Pascal(SM 6.0) or higher "
"archs\nWaiving..\n");
exit(EXIT_WAIVED);
}
}
std::string image_filename = "teapot1024.ppm";
if (checkCmdLineFlag(argc, (const char **)argv, "file")) {
getCmdLineArgumentString(argc, (const char **)argv, "file",
(char **)&image_filename);
}
uint32_t imageWidth = 0;
uint32_t imageHeight = 0;
unsigned char *image_data = NULL;
loadImageData(image_filename, argv, &image_data, imageWidth, imageHeight);
cudaNvSci cudaNvSciApp(deviceIds.size() > 1, deviceIds, image_data,
imageWidth, imageHeight);
cudaNvSciApp.runCudaNvSci(image_filename);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../../../Common"
],
"defines": [],
"compilerPath": "/usr/local/cuda/bin/nvcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}

View File

@@ -0,0 +1,7 @@
{
"recommendations": [
"nvidia.nsight-vscode-edition",
"ms-vscode.cpptools",
"ms-vscode.makefile-tools"
]
}

View File

@@ -0,0 +1,10 @@
{
"configurations": [
{
"name": "CUDA C++: Launch",
"type": "cuda-gdb",
"request": "launch",
"program": "${workspaceFolder}/cudaNvSciNvMedia"
}
]
}

View File

@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "sample",
"type": "shell",
"command": "make dbg=1",
"problemMatcher": ["$nvcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@@ -0,0 +1,503 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
################################################################################
#
# Makefile project only supported on Mac OS X and Linux Platforms)
#
################################################################################
# Location of the CUDA Toolkit
CUDA_PATH ?= /usr/local/cuda
##############################
# start deprecated interface #
##############################
ifeq ($(x86_64),1)
$(info WARNING - x86_64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=x86_64 instead)
TARGET_ARCH ?= x86_64
endif
ifeq ($(ARMv7),1)
$(info WARNING - ARMv7 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=armv7l instead)
TARGET_ARCH ?= armv7l
endif
ifeq ($(aarch64),1)
$(info WARNING - aarch64 variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=aarch64 instead)
TARGET_ARCH ?= aarch64
endif
ifeq ($(ppc64le),1)
$(info WARNING - ppc64le variable has been deprecated)
$(info WARNING - please use TARGET_ARCH=ppc64le instead)
TARGET_ARCH ?= ppc64le
endif
ifneq ($(GCC),)
$(info WARNING - GCC variable has been deprecated)
$(info WARNING - please use HOST_COMPILER=$(GCC) instead)
HOST_COMPILER ?= $(GCC)
endif
ifneq ($(abi),)
$(error ERROR - abi variable has been removed)
endif
############################
# end deprecated interface #
############################
# architecture
HOST_ARCH := $(shell uname -m)
TARGET_ARCH ?= $(HOST_ARCH)
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le armv7l))
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifneq (,$(filter $(TARGET_ARCH),x86_64 aarch64 sbsa ppc64le))
TARGET_SIZE := 64
else ifneq (,$(filter $(TARGET_ARCH),armv7l))
TARGET_SIZE := 32
endif
else
TARGET_SIZE := $(shell getconf LONG_BIT)
endif
else
$(error ERROR - unsupported value $(TARGET_ARCH) for TARGET_ARCH!)
endif
# sbsa and aarch64 systems look similar. Need to differentiate them at host level for now.
ifeq ($(HOST_ARCH),aarch64)
ifeq ($(CUDA_PATH)/targets/sbsa-linux,$(shell ls -1d $(CUDA_PATH)/targets/sbsa-linux 2>/dev/null))
HOST_ARCH := sbsa
TARGET_ARCH := sbsa
endif
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq (,$(filter $(HOST_ARCH)-$(TARGET_ARCH),aarch64-armv7l x86_64-armv7l x86_64-aarch64 x86_64-sbsa x86_64-ppc64le))
$(error ERROR - cross compiling from $(HOST_ARCH) to $(TARGET_ARCH) is not supported!)
endif
endif
# When on native aarch64 system with userspace of 32-bit, change TARGET_ARCH to armv7l
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_SIZE),aarch64-aarch64-32)
TARGET_ARCH = armv7l
endif
# operating system
HOST_OS := $(shell uname -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
TARGET_OS ?= $(HOST_OS)
ifeq (,$(filter $(TARGET_OS),linux darwin qnx android))
$(error ERROR - unsupported value $(TARGET_OS) for TARGET_OS!)
endif
# host compiler
ifdef HOST_COMPILER
CUSTOM_HOST_COMPILER = 1
endif
ifeq ($(TARGET_OS),darwin)
ifeq ($(shell expr `xcodebuild -version | grep -i xcode | awk '{print $$2}' | cut -d'.' -f1` \>= 5),1)
HOST_COMPILER ?= clang++
endif
else ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
ifeq ($(TARGET_OS),linux)
HOST_COMPILER ?= arm-linux-gnueabihf-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/arm-unknown-nto-qnx6.6.0eabi-g++
else ifeq ($(TARGET_OS),android)
HOST_COMPILER ?= arm-linux-androideabi-g++
endif
else ifeq ($(TARGET_ARCH),aarch64)
ifeq ($(TARGET_OS), linux)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_OS),qnx)
ifeq ($(QNX_HOST),)
$(error ERROR - QNX_HOST must be passed to the QNX host toolchain)
endif
ifeq ($(QNX_TARGET),)
$(error ERROR - QNX_TARGET must be passed to the QNX target toolchain)
endif
export QNX_HOST
export QNX_TARGET
HOST_COMPILER ?= $(QNX_HOST)/usr/bin/q++
else ifeq ($(TARGET_OS), android)
HOST_COMPILER ?= aarch64-linux-android-clang++
endif
else ifeq ($(TARGET_ARCH),sbsa)
HOST_COMPILER ?= aarch64-linux-gnu-g++
else ifeq ($(TARGET_ARCH),ppc64le)
HOST_COMPILER ?= powerpc64le-linux-gnu-g++
endif
endif
HOST_COMPILER ?= g++
NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(HOST_COMPILER)
# internal flags
NVCCFLAGS := -m${TARGET_SIZE}
CCFLAGS :=
LDFLAGS :=
# build flags
# Link flag for customized HOST_COMPILER with gcc realpath
GCC_PATH := $(shell which gcc)
ifeq ($(CUSTOM_HOST_COMPILER),1)
ifneq ($(filter /%,$(HOST_COMPILER)),)
ifneq ($(findstring gcc,$(HOST_COMPILER)),)
ifneq ($(GCC_PATH),$(HOST_COMPILER))
LDFLAGS += -lstdc++
endif
endif
endif
endif
ifeq ($(TARGET_OS),darwin)
LDFLAGS += -rpath $(CUDA_PATH)/lib
CCFLAGS += -arch $(HOST_ARCH)
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
LDFLAGS += --dynamic-linker=/lib/ld-linux-armhf.so.3
CCFLAGS += -mfloat-abi=hard
else ifeq ($(TARGET_OS),android)
LDFLAGS += -pie
CCFLAGS += -fpie -fpic -fexceptions
endif
ifneq ($(TARGET_ARCH),$(HOST_ARCH))
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
ifneq ($(TARGET_FS),)
GCCVERSIONLTEQ46 := $(shell expr `$(HOST_COMPILER) -dumpversion` \<= 4.6)
ifeq ($(GCCVERSIONLTEQ46),1)
CCFLAGS += --sysroot=$(TARGET_FS)
endif
LDFLAGS += --sysroot=$(TARGET_FS)
LDFLAGS += -rpath-link=$(TARGET_FS)/lib -L$(TARGET_FS)/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/lib/aarch64-linux-gnu -L$(TARGET_FS)/lib/aarch64-linux-gnu
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib -L$(TARGET_FS)/usr/lib
LDFLAGS += -rpath-link=$(TARGET_FS)/usr/lib/aarch64-linux-gnu -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
LDFLAGS += --unresolved-symbols=ignore-in-shared-libs
CCFLAGS += -isystem=$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include -I$(TARGET_FS)/usr/include/libdrm
CCFLAGS += -isystem=$(TARGET_FS)/usr/include/aarch64-linux-gnu -I$(TARGET_FS)/usr/include/aarch64-linux-gnu
endif
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
NVCCFLAGS += -D_QNX_SOURCE
NVCCFLAGS += --qpp-config 8.3.0,gcc_ntoaarch64le
CCFLAGS += -DWIN_INTERFACE_CUSTOM -I/usr/include/aarch64-qnx-gnu
LDFLAGS += -lsocket
LDFLAGS += -L/usr/lib/aarch64-qnx-gnu
CCFLAGS += "-Wl\,-rpath-link\,/usr/lib/aarch64-qnx-gnu"
ifdef TARGET_OVERRIDE
LDFLAGS += -lslog2
endif
ifneq ($(TARGET_FS),)
LDFLAGS += -L$(TARGET_FS)/usr/lib
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/lib"
LDFLAGS += -L$(TARGET_FS)/usr/libnvidia
CCFLAGS += "-Wl\,-rpath-link\,$(TARGET_FS)/usr/libnvidia"
CCFLAGS += -I$(TARGET_FS)/../include
endif
endif
endif
ifdef TARGET_OVERRIDE # cuda toolkit targets override
NVCCFLAGS += -target-dir $(TARGET_OVERRIDE)
endif
# Install directory of different arch
CUDA_INSTALL_TARGET_DIR :=
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-gnueabihf/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_INSTALL_TARGET_DIR = targets/sbsa-linux/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_INSTALL_TARGET_DIR = targets/armv7-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-linux-androideabi/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_INSTALL_TARGET_DIR = targets/ARMv7-linux-QNX/
else ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_INSTALL_TARGET_DIR = targets/aarch64-qnx/
else ifeq ($(TARGET_ARCH),ppc64le)
CUDA_INSTALL_TARGET_DIR = targets/ppc64le-linux/
endif
# Debug build flags
ifeq ($(dbg),1)
NVCCFLAGS += -g -G
BUILD_TYPE := debug
else
BUILD_TYPE := release
endif
ALL_CCFLAGS :=
ALL_CCFLAGS += $(NVCCFLAGS)
ALL_CCFLAGS += $(EXTRA_NVCCFLAGS)
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(CCFLAGS))
ALL_CCFLAGS += $(addprefix -Xcompiler ,$(EXTRA_CCFLAGS))
UBUNTU = $(shell lsb_release -i -s 2>/dev/null | grep -i ubuntu)
SAMPLE_ENABLED := 1
# This sample is not supported on Linux x86_64
ifeq ($(TARGET_OS),linux)
ifeq ($(TARGET_ARCH),x86_64)
$(info >>> WARNING - cudaNvSciNvMedia is not supported on Linux x86_64 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
endif
# This sample is not supported on Mac OSX
ifeq ($(TARGET_OS),darwin)
$(info >>> WARNING - cudaNvSciNvMedia is not supported on Mac OSX - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on ARMv7
ifeq ($(TARGET_ARCH),armv7l)
$(info >>> WARNING - cudaNvSciNvMedia is not supported on ARMv7 - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
# This sample is not supported on sbsa
ifeq ($(TARGET_ARCH),sbsa)
$(info >>> WARNING - cudaNvSciNvMedia is not supported on sbsa - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ALL_LDFLAGS :=
ALL_LDFLAGS += $(ALL_CCFLAGS)
ALL_LDFLAGS += $(addprefix -Xlinker ,$(LDFLAGS))
ALL_LDFLAGS += $(addprefix -Xlinker ,$(EXTRA_LDFLAGS))
# Common includes and paths for CUDA
INCLUDES := -I../../../Common
LIBRARIES :=
################################################################################
# Makefile include to help find NVSCI Libraries
include ./findnvsci.mk
# Makefile include to help find NvMedia Libraries
include ./findnvmedia.mk
#Detect if installed version of GCC supports required C++11
ifeq ($(TARGET_OS),linux)
empty :=
space := $(empty) $(empty)
GCCVERSIONSTRING := $(shell expr `$(HOST_COMPILER) -dumpversion`)
#Create version number without "."
GCCVERSION := $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f1 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f2 -d.)
GCCVERSION += $(shell expr `echo $(GCCVERSIONSTRING)` | cut -f3 -d.)
# Make sure the version number has at least 3 decimals
GCCVERSION += 00
# Remove spaces from the version number
GCCVERSION := $(subst $(space),$(empty),$(GCCVERSION))
#$(warning $(GCCVERSION))
IS_MIN_VERSION := $(shell expr `echo $(GCCVERSION)` \>= 47000)
ifneq ($(CUSTOM_HOST_COMPILER), 1)
ifeq ($(IS_MIN_VERSION), 1)
$(info >>> GCC Version is greater or equal to 4.7.0 <<<)
else
$(info >>> Waiving build. Minimum GCC version required is 4.7.0<<<)
SAMPLE_ENABLED := 0
endif
else
$(warning >>> Custom HOST_COMPILER set; skipping GCC version check. This may lead to unintended behavior. Please note the minimum equivalent GCC version is 4.7.0 <<<)
endif
endif
# Gencode arguments
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),armv7l aarch64 sbsa))
SMS ?= 53 61 70 72 75 80 86 87 90
else
SMS ?= 50 52 60 61 70 75 80 86 89 90
endif
ifeq ($(SMS),)
$(info >>> WARNING - no SM architectures have been specified - waiving sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ($(GENCODE_FLAGS),)
# Generate SASS code for each SM architecture listed in $(SMS)
$(foreach sm,$(SMS),$(eval GENCODE_FLAGS += -gencode arch=compute_$(sm),code=sm_$(sm)))
# Generate PTX code from the highest SM architecture in $(SMS) to guarantee forward-compatibility
HIGHEST_SM := $(lastword $(sort $(SMS)))
ifneq ($(HIGHEST_SM),)
GENCODE_FLAGS += -gencode arch=compute_$(HIGHEST_SM),code=compute_$(HIGHEST_SM)
endif
endif
ifeq ($(TARGET_OS),darwin)
ALL_LDFLAGS += -Xcompiler -F/Library/Frameworks -Xlinker -framework -Xlinker CUDA
else
ifeq ($(TARGET_ARCH),x86_64)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/lib64/stubs
CUDA_SEARCH_PATH += $(CUDA_PATH)/lib/stubs
CUDA_SEARCH_PATH += $(CUDA_PATH)/targets/x86_64-linux/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-linux)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/armv7-linux-gnueabihf/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-linux)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/aarch64-linux/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),sbsa-linux)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/sbsa-linux/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-android)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/armv7-linux-androideabi/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-android)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/aarch64-linux-androideabi/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),armv7l-qnx)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/ARMv7-linux-QNX/lib/stubs
endif
ifeq ($(TARGET_ARCH)-$(TARGET_OS),aarch64-qnx)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/aarch64-qnx/lib/stubs
ifdef TARGET_OVERRIDE
CUDA_SEARCH_PATH := $(CUDA_PATH)/targets/$(TARGET_OVERRIDE)/lib/stubs
endif
endif
ifeq ($(TARGET_ARCH),ppc64le)
CUDA_SEARCH_PATH ?= $(CUDA_PATH)/targets/ppc64le-linux/lib/stubs
endif
ifeq ($(HOST_ARCH),ppc64le)
CUDA_SEARCH_PATH += $(CUDA_PATH)/lib64/stubs
endif
CUDALIB ?= $(shell find -L $(CUDA_SEARCH_PATH) -maxdepth 1 -name libcuda.so 2> /dev/null)
ifeq ("$(CUDALIB)","")
$(info >>> WARNING - libcuda.so not found, CUDA Driver is not installed. Please re-install the driver. <<<)
SAMPLE_ENABLED := 0
else
CUDALIB := $(shell echo $(CUDALIB) | sed "s/ .*//" | sed "s/\/libcuda.so//" )
LIBRARIES += -L$(CUDALIB) -lcuda
endif
endif
ALL_CCFLAGS += --std=c++11 --threads 0
LIBRARIES += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu/nvidia -lnvscibuf -lnvscisync -lnvmedia
ifeq ($(SAMPLE_ENABLED),0)
EXEC ?= @echo "[@]"
endif
################################################################################
# Target rules
all: build
build: cudaNvSciNvMedia
check.deps:
ifeq ($(SAMPLE_ENABLED),0)
@echo "Sample will be waived due to the above missing dependencies"
else
@echo "Sample is ready - all dependencies have been met"
endif
cuda_consumer.o:cuda_consumer.cu
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
main.o:main.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
nvmedia_producer.o:nvmedia_producer.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
cmdline.o:nvmedia_utils/cmdline.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
config_parser.o:nvmedia_utils/config_parser.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
image_utils.o:nvmedia_utils/image_utils.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
log_utils.o:nvmedia_utils/log_utils.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
misc_utils.o:nvmedia_utils/misc_utils.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
nvsci_setup.o:nvsci_setup.cpp
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
cudaNvSciNvMedia: cuda_consumer.o main.o nvmedia_producer.o cmdline.o config_parser.o image_utils.o log_utils.o misc_utils.o nvsci_setup.o
$(EXEC) $(NVCC) $(ALL_LDFLAGS) $(GENCODE_FLAGS) -o $@ $+ $(LIBRARIES)
$(EXEC) mkdir -p ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
$(EXEC) cp $@ ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)
run: build
$(EXEC) ./cudaNvSciNvMedia
testrun: build
clean:
rm -f cudaNvSciNvMedia cuda_consumer.o main.o nvmedia_producer.o cmdline.o config_parser.o image_utils.o log_utils.o misc_utils.o nvsci_setup.o
rm -rf ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)/cudaNvSciNvMedia
clobber: clean

View File

@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE entry SYSTEM "SamplesInfo.dtd">
<entry>
<name>cudaNvSciNvMedia</name>
<cflags>
<flag>--std=c++11</flag>
</cflags>
<cuda_api_list>
<driver>cuDeviceGetUuid</driver>
<toolkit>cudaImportExternalSemaphore</toolkit>
<toolkit>cudaGetMipmappedArrayLevel</toolkit>
<toolkit>cudaSetDevice</toolkit>
<toolkit>cudaDestroySurfaceObject</toolkit>
<toolkit>cudaCreateSurfaceObject</toolkit>
<toolkit>cudaImportNvSciImage</toolkit>
<toolkit>cudaCreateChannelDesc</toolkit>
<toolkit>cudaMallocHost</toolkit>
<toolkit>cudaSignalExternalSemaphoresAsync</toolkit>
<toolkit>cudaFreeHost</toolkit>
<toolkit>cudaMemcpyAsync</toolkit>
<toolkit>cudaStreamCreateWithFlags</toolkit>
<toolkit>cudaExternalMemoryGetMappedMipmappedArray</toolkit>
<toolkit>cudaMallocArray</toolkit>
<toolkit>cudaFreeArray</toolkit>
<toolkit>cudaStreamDestroy</toolkit>
<toolkit>cudaDeviceGetNvSciSyncAttributes</toolkit>
<toolkit>cudaDestroyExternalMemory</toolkit>
<toolkit>cudaImportExternalMemory</toolkit>
<toolkit>cudaDestroyExternalSemaphore</toolkit>
<toolkit>cudaFreeMipmappedArray</toolkit>
<toolkit>cudaImportNvSciSync</toolkit>
<toolkit>cudaFree</toolkit>
<toolkit>cudaStreamSynchronize</toolkit>
<toolkit>cudaMalloc</toolkit>
<toolkit>cudaWaitExternalSemaphoresAsync</toolkit>
</cuda_api_list>
<description><![CDATA[This sample demonstrates CUDA-NvMedia interop via NvSciBuf/NvSciSync APIs. Note that this sample only supports cross build from x86_64 to aarch64, aarch64 native build is not supported. For detailed workflow of the sample please check cudaNvSciNvMedia_Readme.pdf in the sample directory.]]></description>
<devicecompilation>whole</devicecompilation>
<includepaths>
<path>./</path>
<path>../</path>
<path>../../../Common</path>
</includepaths>
<keyconcepts>
<concept level="basic">CUDA NvSci Interop</concept>
<concept level="advanced">Data Parallel Algorithms</concept>
<concept level="advanced">Image Processing</concept>
</keyconcepts>
<keywords>
<keyword>CUDA</keyword>
<keyword>CPP11</keyword>
</keywords>
<libraries>
<library os="linux">cuda</library>
<library framework="true" os="macosx">CUDA</library>
<library>nvscibuf</library>
<library>nvscisync</library>
<library>nvmedia</library>
</libraries>
<librarypaths>
<path>$(TARGET_FS)/usr/lib/aarch64-linux-gnu/nvidia</path>
</librarypaths>
<nsight_eclipse>true</nsight_eclipse>
<primary_file>main.cpp</primary_file>
<required_dependencies>
<dependency>NVSCI</dependency>
<dependency>NvMedia</dependency>
</required_dependencies>
<scopes>
<scope>1:CUDA Advanced Topics</scope>
<scope>1:CUDA NvSciBuf/NvSciSync Interop</scope>
</scopes>
<sm-arch>sm60</sm-arch>
<sm-arch>sm61</sm-arch>
<sm-arch>sm70</sm-arch>
<sm-arch>sm72</sm-arch>
<sm-arch>sm75</sm-arch>
<sm-arch>sm80</sm-arch>
<sm-arch>sm86</sm-arch>
<sm-arch>sm87</sm-arch>
<sm-arch>sm89</sm-arch>
<sm-arch>sm90</sm-arch>
<supported_envs>
<env>
<arch>aarch64</arch>
<platform>linux</platform>
</env>
<env>
<arch>aarch64</arch>
<platform>qnx</platform>
</env>
</supported_envs>
<supported_sm_architectures>
<from>6.0</from>
</supported_sm_architectures>
<title>NvMedia CUDA Interop</title>
<type>exe</type>
</entry>

View File

@@ -0,0 +1,67 @@
# cudaNvSciNvMedia - NvMedia CUDA Interop
## Description
This sample demonstrates CUDA-NvMedia interop via NvSciBuf/NvSciSync APIs. Note that this sample only supports cross build from x86_64 to aarch64, aarch64 native build is not supported. For detailed workflow of the sample please check cudaNvSciNvMedia_Readme.pdf in the sample directory.
## Key Concepts
CUDA NvSci Interop, Data Parallel Algorithms, Image Processing
## Supported SM Architectures
[SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
## Supported OSes
Linux, QNX
## Supported CPU Architecture
aarch64
## CUDA APIs involved
### [CUDA Driver API](http://docs.nvidia.com/cuda/cuda-driver-api/index.html)
cuDeviceGetUuid
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
cudaImportExternalSemaphore, cudaGetMipmappedArrayLevel, cudaSetDevice, cudaDestroySurfaceObject, cudaCreateSurfaceObject, cudaImportNvSciImage, cudaCreateChannelDesc, cudaMallocHost, cudaSignalExternalSemaphoresAsync, cudaFreeHost, cudaMemcpyAsync, cudaStreamCreateWithFlags, cudaExternalMemoryGetMappedMipmappedArray, cudaMallocArray, cudaFreeArray, cudaStreamDestroy, cudaDeviceGetNvSciSyncAttributes, cudaDestroyExternalMemory, cudaImportExternalMemory, cudaDestroyExternalSemaphore, cudaFreeMipmappedArray, cudaImportNvSciSync, cudaFree, cudaStreamSynchronize, cudaMalloc, cudaWaitExternalSemaphoresAsync
## Dependencies needed to build/run
[NVSCI](../../../README.md#nvsci), [NvMedia](../../../README.md#nvmedia)
## Prerequisites
Download and install the [CUDA Toolkit 12.5](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
Make sure the dependencies mentioned in [Dependencies]() section above are installed.
## Build and Run
### Linux
The Linux samples are built using makefiles. To use the makefiles, change the current directory to the sample directory you wish to build, and run make:
```
$ cd <sample_dir>
$ make
```
The samples makefiles can take advantage of certain options:
* **TARGET_ARCH=<arch>** - cross-compile targeting a specific architecture. Allowed architectures are aarch64.
By default, TARGET_ARCH is set to HOST_ARCH. On a x86_64 machine, not setting TARGET_ARCH is the equivalent of setting TARGET_ARCH=x86_64.<br/>
`$ make TARGET_ARCH=aarch64` <br/>
See [here](http://docs.nvidia.com/cuda/cuda-samples/index.html#cross-samples) for more details.
* **dbg=1** - build with debug symbols
```
$ make dbg=1
```
* **SMS="A B ..."** - override the SM architectures for which the sample will be built, where `"A B ..."` is a space-delimited list of SM architectures. For example, to generate SASS for SM 50 and SM 60, use `SMS="50 60"`.
```
$ make SMS="50 60"
```
* **HOST_COMPILER=<host_compiler>** - override the default g++ host compiler. See the [Linux Installation Guide](http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements) for a list of supported host compilers.
```
$ make HOST_COMPILER=g++
```
## References (for more details)

View File

@@ -0,0 +1,428 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <cuda_runtime.h>
#include "cuda_consumer.h"
#include <helper_image.h>
#include "nvmedia_image_nvscibuf.h"
#include "nvmedia_utils/cmdline.h"
// Enable this to 1 if require cuda processed output to ppm file.
#define WRITE_OUTPUT_IMAGE 0
#define checkNvSciErrors(call) \
do { \
NvSciError _status = call; \
if (NvSciError_Success != _status) { \
printf( \
"NVSCI call in file '%s' in line %i returned" \
" %d, expected %d\n", \
__FILE__, __LINE__, _status, NvSciError_Success); \
fflush(stdout); \
exit(EXIT_FAILURE); \
} \
} while (0)
__global__ static void yuvToGrayscale(cudaSurfaceObject_t surfaceObject,
unsigned int *dstImage,
int32_t imageWidth, int32_t imageHeight) {
size_t x = blockIdx.x * blockDim.x + threadIdx.x;
size_t y = blockIdx.y * blockDim.y + threadIdx.y;
uchar4 *dstImageUchar4 = (uchar4 *)dstImage;
for (; x < imageWidth && y < imageHeight;
x += gridDim.x * blockDim.x, y += gridDim.y * blockDim.y) {
int colInBytes = x * sizeof(unsigned char);
unsigned char luma =
surf2Dread<unsigned char>(surfaceObject, colInBytes, y);
uchar4 grayscalePix = make_uchar4(luma, luma, luma, 0);
dstImageUchar4[y * imageWidth + x] = grayscalePix;
}
}
static void cudaImportNvSciSync(cudaExternalSemaphore_t &extSem,
NvSciSyncObj &syncObj) {
cudaExternalSemaphoreHandleDesc extSemDesc;
memset(&extSemDesc, 0, sizeof(extSemDesc));
extSemDesc.type = cudaExternalSemaphoreHandleTypeNvSciSync;
extSemDesc.handle.nvSciSyncObj = (void *)syncObj;
checkCudaErrors(cudaImportExternalSemaphore(&extSem, &extSemDesc));
}
static void waitExternalSemaphore(cudaExternalSemaphore_t &waitSem,
NvSciSyncFence *fence, cudaStream_t stream) {
cudaExternalSemaphoreWaitParams waitParams;
memset(&waitParams, 0, sizeof(waitParams));
// For cross-process signaler-waiter applications need to use NvSciIpc
// and NvSciSync[Export|Import] utilities to share the NvSciSyncFence
// across process. This step is optional in single-process.
waitParams.params.nvSciSync.fence = (void *)fence;
waitParams.flags = 0;
checkCudaErrors(
cudaWaitExternalSemaphoresAsync(&waitSem, &waitParams, 1, stream));
}
static void signalExternalSemaphore(cudaExternalSemaphore_t &signalSem,
NvSciSyncFence *fence,
cudaStream_t stream) {
cudaExternalSemaphoreSignalParams signalParams;
memset(&signalParams, 0, sizeof(signalParams));
// For cross-process signaler-waiter applications need to use NvSciIpc
// and NvSciSync[Export|Import] utilities to share the NvSciSyncFence
// across process. This step is optional in single-process.
signalParams.params.nvSciSync.fence = (void *)fence;
signalParams.flags = 0;
checkCudaErrors(
cudaSignalExternalSemaphoresAsync(&signalSem, &signalParams, 1, stream));
}
static void yuvToGrayscaleCudaKernel(cudaExternalResInterop &cudaExtResObj,
int32_t imageWidth, int32_t imageHeight) {
#if WRITE_OUTPUT_IMAGE
unsigned int *h_dstImage;
checkCudaErrors(cudaMallocHost(
&h_dstImage, sizeof(unsigned int) * imageHeight * imageWidth));
#endif
dim3 block(16, 16, 1);
dim3 grid((imageWidth / block.x) + 1, (imageHeight / block.y) + 1, 1);
yuvToGrayscale<<<grid, block, 0, cudaExtResObj.stream>>>(
cudaExtResObj.cudaSurfaceNvmediaBuf[0], cudaExtResObj.d_outputImage,
imageWidth, imageHeight);
#if WRITE_OUTPUT_IMAGE
checkCudaErrors(
cudaMemcpyAsync(h_dstImage, cudaExtResObj.d_outputImage,
sizeof(unsigned int) * imageHeight * imageWidth,
cudaMemcpyDeviceToHost, cudaExtResObj.stream));
checkCudaErrors(cudaStreamSynchronize(cudaExtResObj.stream));
char outputFilename[1024];
std::string image_filename = "Grayscale";
strcpy(outputFilename, image_filename.c_str());
strcpy(outputFilename + image_filename.length(), "_nvsci_out.ppm");
sdkSavePPM4ub(outputFilename, (unsigned char *)h_dstImage, imageWidth,
imageHeight);
printf("Wrote '%s'\n", outputFilename);
checkCudaErrors(cudaFreeHost(h_dstImage));
#endif
}
static void cudaImportNvSciImage(cudaExternalResInterop &cudaExtResObj,
NvSciBufObj &inputBufObj) {
NvSciBufModule module = NULL;
NvSciBufAttrList attrlist = NULL;
NvSciBufAttrKeyValuePair pairArrayOut[10];
checkNvSciErrors(NvSciBufModuleOpen(&module));
checkNvSciErrors(NvSciBufAttrListCreate(module, &attrlist));
checkNvSciErrors(NvSciBufObjGetAttrList(inputBufObj, &attrlist));
memset(pairArrayOut, 0, sizeof(NvSciBufAttrKeyValuePair) * 10);
int numAttrs = 0;
pairArrayOut[numAttrs++].key = NvSciBufImageAttrKey_Size;
pairArrayOut[numAttrs++].key = NvSciBufImageAttrKey_PlaneChannelCount;
pairArrayOut[numAttrs++].key = NvSciBufImageAttrKey_PlaneCount;
pairArrayOut[numAttrs++].key = NvSciBufImageAttrKey_PlaneWidth;
pairArrayOut[numAttrs++].key = NvSciBufImageAttrKey_PlaneHeight;
pairArrayOut[numAttrs++].key = NvSciBufImageAttrKey_Layout;
pairArrayOut[numAttrs++].key = NvSciBufImageAttrKey_PlaneBitsPerPixel;
pairArrayOut[numAttrs++].key = NvSciBufImageAttrKey_PlaneOffset;
checkNvSciErrors(NvSciBufAttrListGetAttrs(attrlist, pairArrayOut, numAttrs));
uint64_t size = *(uint64_t *)pairArrayOut[0].value;
uint8_t channelCount = *(uint8_t *)pairArrayOut[1].value;
cudaExtResObj.planeCount = *(int32_t *)pairArrayOut[2].value;
cudaExtResObj.imageWidth =
(int32_t *)malloc(sizeof(int32_t) * cudaExtResObj.planeCount);
cudaExtResObj.imageHeight =
(int32_t *)malloc(sizeof(int32_t) * cudaExtResObj.planeCount);
cudaExtResObj.planeOffset =
(uint64_t *)malloc(sizeof(uint64_t) * cudaExtResObj.planeCount);
memcpy(cudaExtResObj.imageWidth, (int32_t *)pairArrayOut[3].value,
cudaExtResObj.planeCount * sizeof(int32_t));
memcpy(cudaExtResObj.imageHeight, (int32_t *)pairArrayOut[4].value,
cudaExtResObj.planeCount * sizeof(int32_t));
memcpy(cudaExtResObj.planeOffset, (uint64_t *)pairArrayOut[7].value,
cudaExtResObj.planeCount * sizeof(uint64_t));
NvSciBufAttrValImageLayoutType layout =
*(NvSciBufAttrValImageLayoutType *)pairArrayOut[5].value;
uint32_t bitsPerPixel = *(uint32_t *)pairArrayOut[6].value;
if (layout != NvSciBufImage_BlockLinearType) {
printf("Image layout is not block linear.. waiving execution\n");
exit(EXIT_WAIVED);
}
cudaExternalMemoryHandleDesc memHandleDesc;
memset(&memHandleDesc, 0, sizeof(memHandleDesc));
memHandleDesc.type = cudaExternalMemoryHandleTypeNvSciBuf;
memHandleDesc.handle.nvSciBufObject = inputBufObj;
memHandleDesc.size = size;
checkCudaErrors(
cudaImportExternalMemory(&cudaExtResObj.extMemImageBuf, &memHandleDesc));
cudaExtResObj.d_mipmapArray = (cudaMipmappedArray_t *)malloc(
sizeof(cudaMipmappedArray_t) * cudaExtResObj.planeCount);
for (int i = 0; i < cudaExtResObj.planeCount; i++) {
cudaExtent extent = {};
memset(&extent, 0, sizeof(extent));
extent.width = cudaExtResObj.imageWidth[i];
extent.height = cudaExtResObj.imageHeight[i];
extent.depth = 0;
cudaChannelFormatDesc desc;
switch (channelCount) {
case 1:
default:
desc = cudaCreateChannelDesc(bitsPerPixel, 0, 0, 0,
cudaChannelFormatKindUnsigned);
break;
case 2:
desc = cudaCreateChannelDesc(bitsPerPixel, bitsPerPixel, 0, 0,
cudaChannelFormatKindUnsigned);
break;
case 3:
desc = cudaCreateChannelDesc(bitsPerPixel, bitsPerPixel, bitsPerPixel,
0, cudaChannelFormatKindUnsigned);
break;
case 4:
desc =
cudaCreateChannelDesc(bitsPerPixel, bitsPerPixel, bitsPerPixel,
bitsPerPixel, cudaChannelFormatKindUnsigned);
break;
}
cudaExternalMemoryMipmappedArrayDesc mipmapDesc = {0};
mipmapDesc.offset = cudaExtResObj.planeOffset[i];
mipmapDesc.formatDesc = desc;
mipmapDesc.extent = extent;
mipmapDesc.flags = 0;
mipmapDesc.numLevels = 1;
checkCudaErrors(cudaExternalMemoryGetMappedMipmappedArray(
&cudaExtResObj.d_mipmapArray[i], cudaExtResObj.extMemImageBuf,
&mipmapDesc));
}
}
static cudaSurfaceObject_t createCudaSurface(cudaArray_t &d_mipLevelArray) {
cudaResourceDesc resourceDesc;
memset(&resourceDesc, 0, sizeof(resourceDesc));
resourceDesc.resType = cudaResourceTypeArray;
resourceDesc.res.array.array = d_mipLevelArray;
cudaSurfaceObject_t surfaceObject;
checkCudaErrors(cudaCreateSurfaceObject(&surfaceObject, &resourceDesc));
return surfaceObject;
}
static cudaStream_t createCudaStream(int deviceId) {
checkCudaErrors(cudaSetDevice(deviceId));
cudaStream_t stream;
checkCudaErrors(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
return stream;
}
// CUDA setup buffers/synchronization objects for interop via NvSci API.
void setupCuda(cudaExternalResInterop &cudaExtResObj, NvSciBufObj &inputBufObj,
NvSciSyncObj &syncObj, NvSciSyncObj &cudaSignalerSyncObj,
int deviceId) {
checkCudaErrors(cudaSetDevice(deviceId));
cudaImportNvSciSync(cudaExtResObj.waitSem, syncObj);
cudaImportNvSciSync(cudaExtResObj.signalSem, cudaSignalerSyncObj);
cudaImportNvSciImage(cudaExtResObj, inputBufObj);
cudaExtResObj.d_mipLevelArray =
(cudaArray_t *)malloc(sizeof(cudaArray_t) * cudaExtResObj.planeCount);
cudaExtResObj.cudaSurfaceNvmediaBuf = (cudaSurfaceObject_t *)malloc(
sizeof(cudaSurfaceObject_t) * cudaExtResObj.planeCount);
for (int i = 0; i < cudaExtResObj.planeCount; ++i) {
uint32_t mipLevelId = 0;
checkCudaErrors(
cudaGetMipmappedArrayLevel(&cudaExtResObj.d_mipLevelArray[i],
cudaExtResObj.d_mipmapArray[i], mipLevelId));
cudaExtResObj.cudaSurfaceNvmediaBuf[i] =
createCudaSurface(cudaExtResObj.d_mipLevelArray[i]);
}
cudaExtResObj.stream = createCudaStream(deviceId);
checkCudaErrors(cudaMalloc(&cudaExtResObj.d_outputImage,
sizeof(unsigned int) *
cudaExtResObj.imageWidth[0] *
cudaExtResObj.imageHeight[0]));
}
// CUDA clean up buffers used **with** NvSci API.
void cleanupCuda(cudaExternalResInterop &cudaExtResObj) {
for (int i = 0; i < cudaExtResObj.planeCount; i++) {
checkCudaErrors(
cudaDestroySurfaceObject(cudaExtResObj.cudaSurfaceNvmediaBuf[i]));
checkCudaErrors(cudaFreeMipmappedArray(cudaExtResObj.d_mipmapArray[i]));
}
free(cudaExtResObj.d_mipmapArray);
free(cudaExtResObj.d_mipLevelArray);
free(cudaExtResObj.cudaSurfaceNvmediaBuf);
free(cudaExtResObj.imageWidth);
free(cudaExtResObj.imageHeight);
checkCudaErrors(cudaDestroyExternalSemaphore(cudaExtResObj.waitSem));
checkCudaErrors(cudaDestroyExternalSemaphore(cudaExtResObj.signalSem));
checkCudaErrors(cudaDestroyExternalMemory(cudaExtResObj.extMemImageBuf));
checkCudaErrors(cudaStreamDestroy(cudaExtResObj.stream));
checkCudaErrors(cudaFree(cudaExtResObj.d_outputImage));
}
void runCudaOperation(cudaExternalResInterop &cudaExtResObj,
NvSciSyncFence *cudaWaitFence,
NvSciSyncFence *cudaSignalFence, int deviceId,
int iterations) {
checkCudaErrors(cudaSetDevice(deviceId));
static int64_t launch = 0;
waitExternalSemaphore(cudaExtResObj.waitSem, cudaWaitFence,
cudaExtResObj.stream);
// run cuda kernel over surface object of the LUMA surface part to extract
// grayscale.
yuvToGrayscaleCudaKernel(cudaExtResObj, cudaExtResObj.imageWidth[0],
cudaExtResObj.imageHeight[0]);
// signal fence till the second last iterations for NvMedia2DBlit to wait for
// cuda signal and for final iteration as there is no corresponding NvMedia
// operation pending therefore we end with cudaStreamSynchronize()
if (launch < iterations - 1) {
signalExternalSemaphore(cudaExtResObj.signalSem, cudaSignalFence,
cudaExtResObj.stream);
} else {
checkCudaErrors(cudaStreamSynchronize(cudaExtResObj.stream));
}
launch++;
}
// CUDA imports and operates on NvSci buffer/synchronization objects
void setupCuda(Blit2DTest *ctx, cudaResources &cudaResObj, int deviceId) {
checkCudaErrors(cudaSetDevice(deviceId));
cudaResObj.d_yuvArray =
(cudaArray_t *)malloc(sizeof(cudaArray_t) * ctx->numSurfaces);
cudaResObj.cudaSurfaceNvmediaBuf = (cudaSurfaceObject_t *)malloc(
sizeof(cudaSurfaceObject_t) * ctx->numSurfaces);
cudaChannelFormatDesc channelDesc;
switch (ctx->bytesPerPixel) {
case 1:
default:
channelDesc =
cudaCreateChannelDesc(8, 0, 0, 0, cudaChannelFormatKindUnsigned);
break;
}
for (int k = 0; k < ctx->numSurfaces; k++) {
checkCudaErrors(cudaMallocArray(
&cudaResObj.d_yuvArray[k], &channelDesc,
ctx->widthSurface * ctx->xScalePtr[k] * ctx->bytesPerPixel,
ctx->heightSurface * ctx->yScalePtr[k]));
cudaResObj.cudaSurfaceNvmediaBuf[k] =
createCudaSurface(cudaResObj.d_yuvArray[k]);
}
checkCudaErrors(cudaMalloc(
&cudaResObj.d_outputImage,
sizeof(unsigned int) * ctx->widthSurface * ctx->heightSurface));
cudaResObj.stream = createCudaStream(deviceId);
}
// CUDA clean up buffers used **without** NvSci API.
void cleanupCuda(Blit2DTest *ctx, cudaResources &cudaResObj) {
for (int k = 0; k < ctx->numSurfaces; k++) {
checkCudaErrors(
cudaDestroySurfaceObject(cudaResObj.cudaSurfaceNvmediaBuf[k]));
checkCudaErrors(cudaFreeArray(cudaResObj.d_yuvArray[k]));
}
free(cudaResObj.cudaSurfaceNvmediaBuf);
checkCudaErrors(cudaStreamDestroy(cudaResObj.stream));
checkCudaErrors(cudaFree(cudaResObj.d_outputImage));
}
static void yuvToGrayscaleCudaKernelNonNvSci(cudaResources &cudaResObj,
int deviceId, int32_t imageWidth,
int32_t imageHeight) {
#if WRITE_OUTPUT_IMAGE
unsigned int *h_dstImage;
checkCudaErrors(cudaMallocHost(
&h_dstImage, sizeof(unsigned int) * imageHeight * imageWidth));
#endif
dim3 block(16, 16, 1);
dim3 grid((imageWidth / block.x) + 1, (imageHeight / block.y) + 1, 1);
yuvToGrayscale<<<grid, block, 0, cudaResObj.stream>>>(
cudaResObj.cudaSurfaceNvmediaBuf[0], cudaResObj.d_outputImage, imageWidth,
imageHeight);
#if WRITE_OUTPUT_IMAGE
checkCudaErrors(
cudaMemcpyAsync(h_dstImage, cudaResObj.d_outputImage,
sizeof(unsigned int) * imageHeight * imageWidth,
cudaMemcpyDeviceToHost, cudaResObj.stream));
checkCudaErrors(cudaStreamSynchronize(cudaResObj.stream));
char outputFilename[1024];
std::string image_filename = "Grayscale";
strcpy(outputFilename, image_filename.c_str());
strcpy(outputFilename + image_filename.length(), "_non-nvsci_out.ppm");
sdkSavePPM4ub(outputFilename, (unsigned char *)h_dstImage, imageWidth,
imageHeight);
printf("Wrote '%s'\n", outputFilename);
checkCudaErrors(cudaFreeHost(h_dstImage));
#else
checkCudaErrors(cudaStreamSynchronize(cudaResObj.stream));
#endif
}
// CUDA operates **without** NvSci APIs buffer/synchronization objects.
void runCudaOperation(Blit2DTest *ctx, cudaResources &cudaResObj,
int deviceId) {
for (int k = 0; k < ctx->numSurfaces; k++) {
checkCudaErrors(cudaMemcpy2DToArray(
cudaResObj.d_yuvArray[k], 0, 0, ctx->dstBuff[k],
ctx->widthSurface * ctx->xScalePtr[k] * ctx->bytesPerPixel,
ctx->widthSurface * ctx->xScalePtr[k] * ctx->bytesPerPixel,
ctx->heightSurface * ctx->yScalePtr[k], cudaMemcpyHostToDevice));
}
// run cuda kernel over surface object of the LUMA surface part to extract
// grayscale.
yuvToGrayscaleCudaKernelNonNvSci(cudaResObj, deviceId, ctx->widthSurface,
ctx->heightSurface);
}

View File

@@ -0,0 +1,72 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __CUDA_BUFIMPORT_KERNEL_H__
#define __CUDA_BUFIMPORT_KERNEL_H__
#include <cuda_runtime.h>
#include "helper_cuda.h"
#include "nvmedia_image_nvscibuf.h"
#include "nvscisync.h"
#include "nvmedia_utils/cmdline.h"
struct cudaExternalResInterop {
cudaMipmappedArray_t *d_mipmapArray;
cudaArray_t *d_mipLevelArray;
cudaSurfaceObject_t *cudaSurfaceNvmediaBuf;
cudaStream_t stream;
cudaExternalMemory_t extMemImageBuf;
cudaExternalSemaphore_t waitSem;
cudaExternalSemaphore_t signalSem;
int32_t planeCount;
uint64_t *planeOffset;
int32_t *imageWidth;
int32_t *imageHeight;
unsigned int *d_outputImage;
};
struct cudaResources {
cudaArray_t *d_yuvArray;
cudaStream_t stream;
cudaSurfaceObject_t *cudaSurfaceNvmediaBuf;
unsigned int *d_outputImage;
};
void runCudaOperation(cudaExternalResInterop &cudaExtResObj,
NvSciSyncFence *fence, NvSciSyncFence *cudaSignalfence,
int deviceId, int iterations);
void runCudaOperation(Blit2DTest *ctx, cudaResources &cudaResObj, int deviceId);
void setupCuda(cudaExternalResInterop &cudaExtResObj, NvSciBufObj &inputBufObj,
NvSciSyncObj &syncObj, NvSciSyncObj &cudaSignalerSyncObj,
int deviceId);
void setupCuda(Blit2DTest *ctx, cudaResources &cudaResObj, int deviceId);
void cleanupCuda(cudaExternalResInterop &cudaObjs);
void cleanupCuda(Blit2DTest *ctx, cudaResources &cudaResObj);
#endif

View File

@@ -0,0 +1,133 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#################################################################################
# findnvmedia.mk is used to find the NvMedia Libraries
#
################################################################################
# Determine OS platform and unix distribution
ifeq ("$(TARGET_OS)","linux")
# first search lsb_release
DISTRO = $(shell lsb_release -i -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
ifeq ("$(DISTRO)","")
# second search and parse /etc/issue
DISTRO = $(shell more /etc/issue | awk '{print $$1}' | sed '1!d' | sed -e "/^$$/d" 2>/dev/null | tr "[:upper:]" "[:lower:]")
# ensure data from /etc/issue is valid
ifeq (,$(filter $(DISTRO),ubuntu fedora red rhel centos suse))
DISTRO =
endif
ifeq ("$(DISTRO)","")
# third, we can search in /etc/os-release or /etc/{distro}-release
DISTRO = $(shell awk '/ID/' /etc/*-release | sed 's/ID=//' | grep -v "VERSION" | grep -v "ID" | grep -v "DISTRIB")
endif
endif
endif
ifeq ("$(TARGET_OS)","linux")
# $(info) >> findegl.mk -> LINUX path <<<)
# Each set of Linux Distros have different paths for where to find their OpenGL libraries reside
UBUNTU = $(shell echo $(DISTRO) | grep -i ubuntu >/dev/null 2>&1; echo $$?)
FEDORA = $(shell echo $(DISTRO) | grep -i fedora >/dev/null 2>&1; echo $$?)
RHEL = $(shell echo $(DISTRO) | grep -i 'red\|rhel' >/dev/null 2>&1; echo $$?)
CENTOS = $(shell echo $(DISTRO) | grep -i centos >/dev/null 2>&1; echo $$?)
SUSE = $(shell echo $(DISTRO) | grep -i 'suse\|sles' >/dev/null 2>&1; echo $$?)
KYLIN = $(shell echo $(DISTRO) | grep -i kylin >/dev/null 2>&1; echo $$?)
ifeq ("$(UBUNTU)","0")
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
GLPATH := /usr/arm-linux-gnueabihf/lib
GLLINK := -L/usr/arm-linux-gnueabihf/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib/arm-linux-gnueabihf
GLLINK += -L$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-aarch64)
GLPATH := /usr/aarch64-linux-gnu/lib
GLLINK := -L/usr/aarch64-linux-gnu/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib
GLPATH += $(TARGET_FS)/usr/lib/aarch64-linux-gnu
GLLINK += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
endif
else
UBUNTU_PKG_NAME = $(shell which dpkg >/dev/null 2>&1 && dpkg -l 'nvidia-*' | grep '^ii' | awk '{print $$2}' | head -1)
ifneq ("$(UBUNTU_PKG_NAME)","")
GLPATH ?= /usr/lib/$(UBUNTU_PKG_NAME)
GLLINK ?= -L/usr/lib/$(UBUNTU_PKG_NAME)
endif
DFLT_PATH ?= /usr/lib
endif
endif
ifeq ("$(SUSE)","0")
GLPATH ?= /usr/X11R6/lib64
GLLINK ?= -L/usr/X11R6/lib64
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(FEDORA)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(RHEL)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(CENTOS)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(KYLIN)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
NVMEDIALIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvmedia.so -print 2>/dev/null)
ifeq ("$(NVMEDIALIB)","")
$(info >>> WARNING - libnvmedia.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
HEADER_SEARCH_PATH ?= $(TARGET_FS)/usr/include
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
HEADER_SEARCH_PATH += /usr/arm-linux-gnueabihf/include
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-aarch64-linux)
HEADER_SEARCH_PATH += /usr/aarch64-linux-gnu/include
endif
NVMEDIAHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvmedia_core.h -print 2>/dev/null)
ifeq ("$(NVMEDIAHEADER)","")
$(info >>> WARNING - nvmedia headers not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
else
endif

View File

@@ -0,0 +1,144 @@
################################################################################
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#################################################################################
# findnvsci.mk is used to find the NVSCI Libraries and headers
#
################################################################################
# Determine OS platform and unix distribution
ifeq ("$(TARGET_OS)","linux")
# first search lsb_release
DISTRO = $(shell lsb_release -i -s 2>/dev/null | tr "[:upper:]" "[:lower:]")
ifeq ("$(DISTRO)","")
# second search and parse /etc/issue
DISTRO = $(shell more /etc/issue | awk '{print $$1}' | sed '1!d' | sed -e "/^$$/d" 2>/dev/null | tr "[:upper:]" "[:lower:]")
# ensure data from /etc/issue is valid
ifeq (,$(filter $(DISTRO),ubuntu fedora red rhel centos suse))
DISTRO =
endif
ifeq ("$(DISTRO)","")
# third, we can search in /etc/os-release or /etc/{distro}-release
DISTRO = $(shell awk '/ID/' /etc/*-release | sed 's/ID=//' | grep -v "VERSION" | grep -v "ID" | grep -v "DISTRIB")
endif
endif
endif
ifeq ("$(TARGET_OS)","linux")
# $(info) >> findegl.mk -> LINUX path <<<)
# Each set of Linux Distros have different paths for where to find their OpenGL libraries reside
UBUNTU = $(shell echo $(DISTRO) | grep -i ubuntu >/dev/null 2>&1; echo $$?)
FEDORA = $(shell echo $(DISTRO) | grep -i fedora >/dev/null 2>&1; echo $$?)
RHEL = $(shell echo $(DISTRO) | grep -i 'red\|rhel' >/dev/null 2>&1; echo $$?)
CENTOS = $(shell echo $(DISTRO) | grep -i centos >/dev/null 2>&1; echo $$?)
SUSE = $(shell echo $(DISTRO) | grep -i 'suse\|sles' >/dev/null 2>&1; echo $$?)
KYLIN = $(shell echo $(DISTRO) | grep -i kylin >/dev/null 2>&1; echo $$?)
ifeq ("$(UBUNTU)","0")
ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-armv7l)
GLPATH := /usr/arm-linux-gnueabihf/lib
GLLINK := -L/usr/arm-linux-gnueabihf/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib/arm-linux-gnueabihf
GLLINK += -L$(TARGET_FS)/usr/lib/arm-linux-gnueabihf
endif
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH),x86_64-aarch64)
GLPATH := /usr/aarch64-linux-gnu/lib
GLLINK := -L/usr/aarch64-linux-gnu/lib
ifneq ($(TARGET_FS),)
GLPATH += $(TARGET_FS)/usr/lib
GLPATH += $(TARGET_FS)/usr/lib/aarch64-linux-gnu
GLLINK += -L$(TARGET_FS)/usr/lib/aarch64-linux-gnu
endif
else
UBUNTU_PKG_NAME = $(shell which dpkg >/dev/null 2>&1 && dpkg -l 'nvidia-*' | grep '^ii' | awk '{print $$2}' | head -1)
ifneq ("$(UBUNTU_PKG_NAME)","")
GLPATH ?= /usr/lib/$(UBUNTU_PKG_NAME)
GLLINK ?= -L/usr/lib/$(UBUNTU_PKG_NAME)
endif
DFLT_PATH ?= /usr/lib
endif
endif
ifeq ("$(SUSE)","0")
GLPATH ?= /usr/X11R6/lib64
GLLINK ?= -L/usr/X11R6/lib64
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(FEDORA)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(RHEL)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(CENTOS)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
ifeq ("$(KYLIN)","0")
GLPATH ?= /usr/lib64/nvidia
GLLINK ?= -L/usr/lib64/nvidia
DFLT_PATH ?= /usr/lib64
endif
NVSCIBUFLIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvscibuf.so -print 2>/dev/null)
NVSCISYNCLIB := $(shell find -L $(GLPATH) $(DFLT_PATH) -name libnvscisync.so -print 2>/dev/null)
ifeq ("$(NVSCIBUFLIB)","")
$(info >>> WARNING - libnvscibuf.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ("$(NVSCISYNCLIB)","")
$(info >>> WARNING - libnvscisync.so not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
HEADER_SEARCH_PATH ?= $(TARGET_FS)/usr/include
ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-armv7l-linux)
HEADER_SEARCH_PATH += /usr/arm-linux-gnueabihf/include
else ifeq ($(HOST_ARCH)-$(TARGET_ARCH)-$(TARGET_OS),x86_64-aarch64-linux)
HEADER_SEARCH_PATH += /usr/aarch64-linux-gnu/include
endif
NVSCIBUFHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvscibuf.h -print 2>/dev/null)
NVSCISYNCHEADER := $(shell find -L $(HEADER_SEARCH_PATH) -name nvscisync.h -print 2>/dev/null)
ifeq ("$(NVSCIBUFHEADER)","")
$(info >>> WARNING - nvscibuf.h not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
ifeq ("$(NVSCISYNCHEADER)","")
$(info >>> WARNING - nvscisync.h not found, Waiving the sample <<<)
SAMPLE_ENABLED := 0
endif
else
endif

View File

@@ -0,0 +1,210 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* standard headers */
#include <string.h>
#include <iostream>
#include <signal.h>
#include <thread>
/* Nvidia headers */
#include <nvscisync.h>
#include "nvmedia_utils/cmdline.h"
#include "nvmedia_image.h"
#include "nvmedia_2d.h"
#include "nvmedia_2d_nvscisync.h"
#include "nvmedia_surface.h"
#include "nvmedia_utils/image_utils.h"
#include "nvmedia_image_nvscibuf.h"
#include "cuda_consumer.h"
#include "nvmedia_producer.h"
#include "nvsci_setup.h"
#define checkNvSciErrors(call) \
do { \
NvSciError _status = call; \
if (NvSciError_Success != _status) { \
printf( \
"NVSCI call in file '%s' in line %i returned" \
" %d, expected %d\n", \
__FILE__, __LINE__, _status, NvSciError_Success); \
fflush(stdout); \
exit(EXIT_FAILURE); \
} \
} while (0)
static void cleanup(Blit2DTest* ctx, NvMediaStatus status) {
if (ctx->i2d != NULL) {
NvMedia2DDestroy(ctx->i2d);
}
if (ctx->device != NULL) {
NvMediaDeviceDestroy(ctx->device);
}
if (status != NVMEDIA_STATUS_OK) {
exit(EXIT_FAILURE);
}
}
int main(int argc, char* argv[]) {
TestArgs args;
Blit2DTest ctx;
NvMediaStatus status = NVMEDIA_STATUS_ERROR;
NvSciSyncFence nvMediaSignalerFence = NvSciSyncFenceInitializer;
NvSciSyncFence cudaSignalerFence = NvSciSyncFenceInitializer;
int cudaDeviceId;
uint64_t startTime, endTime;
uint64_t operationStartTime, operationEndTime;
double processingTime;
/* Read configuration from command line and config file */
memset(&args, 0, sizeof(TestArgs));
memset(&ctx, 0, sizeof(Blit2DTest));
/* ParseArgs parses the command line and the 2D configuration file and
* populates all initParams and run time configuration in to appropriate
* structures within args
*/
if (ParseArgs(argc, argv, &args)) {
PrintUsage();
return -1;
}
/* Check version */
NvMediaVersion version;
status = NvMedia2DGetVersion(&version);
if (status == NVMEDIA_STATUS_OK) {
printf("Library version: %u.%u\n", version.major, version.minor);
printf("Header version: %u.%u\n", NVMEDIA_2D_VERSION_MAJOR,
NVMEDIA_2D_VERSION_MINOR);
if ((version.major != NVMEDIA_2D_VERSION_MAJOR) ||
(version.minor != NVMEDIA_2D_VERSION_MINOR)) {
printf("Library and Header mismatch!\n");
cleanup(&ctx, status);
}
}
// Create NvMedia device
ctx.device = NvMediaDeviceCreate();
if (!ctx.device) {
printf("%s: Failed to create NvMedia device\n", __func__);
cleanup(&ctx, status);
}
// Create 2D blitter
ctx.i2d = NvMedia2DCreate(ctx.device);
if (!ctx.i2d) {
printf("%s: Failed to create NvMedia 2D i2d\n", __func__);
cleanup(&ctx, status);
}
cudaDeviceId = findCudaDevice(argc, (const char**)argv);
// NvMedia-CUDA operations without NvSCI APIs starts
cudaResources cudaResObj;
GetTimeMicroSec(&startTime);
setupNvMedia(&args, &ctx);
setupCuda(&ctx, cudaResObj, cudaDeviceId);
GetTimeMicroSec(&operationStartTime);
for (int i = 0; i < args.iterations; i++) {
runNvMediaBlit2D(&args, &ctx);
runCudaOperation(&ctx, cudaResObj, cudaDeviceId);
}
GetTimeMicroSec(&operationEndTime);
cleanupNvMedia(&ctx);
cleanupCuda(&ctx, cudaResObj);
GetTimeMicroSec(&endTime);
// NvMedia-CUDA operations without NvSCI APIs ends
processingTime = (double)(operationEndTime - operationStartTime) / 1000.0;
printf(
"Overall Processing time of NvMedia-CUDA Operations without NvSCI APIs "
"%.4f ms with %zu iterations\n",
processingTime, args.iterations);
processingTime = (double)(endTime - startTime) / 1000.0;
printf(
"Overall Processing time of NvMedia-CUDA Operations + allocation/cleanup "
"without NvSCI APIs %.4f ms with %zu iterations\n",
processingTime, args.iterations);
NvSciBufObj dstNvSciBufobj, srcNvSciBufobj;
NvSciSyncObj nvMediaSignalerSyncObj, cudaSignalerSyncObj;
cudaExternalResInterop cudaExtResObj;
// NvMedia-CUDA operations via interop with NvSCI APIs starts
GetTimeMicroSec(&startTime);
setupNvMediaSignalerNvSciSync(&ctx, nvMediaSignalerSyncObj, cudaDeviceId);
setupCudaSignalerNvSciSync(&ctx, cudaSignalerSyncObj, cudaDeviceId);
setupNvMedia(&args, &ctx, srcNvSciBufobj, dstNvSciBufobj,
nvMediaSignalerSyncObj, cudaSignalerSyncObj, cudaDeviceId);
setupCuda(cudaExtResObj, dstNvSciBufobj, nvMediaSignalerSyncObj,
cudaSignalerSyncObj, cudaDeviceId);
GetTimeMicroSec(&operationStartTime);
for (int i = 0; i < args.iterations; i++) {
runNvMediaBlit2D(&args, &ctx, nvMediaSignalerSyncObj, &cudaSignalerFence,
&nvMediaSignalerFence);
runCudaOperation(cudaExtResObj, &nvMediaSignalerFence, &cudaSignalerFence,
cudaDeviceId, args.iterations);
}
GetTimeMicroSec(&operationEndTime);
cleanupNvMedia(&ctx, nvMediaSignalerSyncObj, cudaSignalerSyncObj);
cleanupCuda(cudaExtResObj);
cleanupNvSciSync(nvMediaSignalerSyncObj);
cleanupNvSciSync(cudaSignalerSyncObj);
cleanupNvSciBuf(srcNvSciBufobj);
cleanupNvSciBuf(dstNvSciBufobj);
GetTimeMicroSec(&endTime);
// NvMedia-CUDA operations via interop with NvSCI APIs ends
processingTime = (double)(operationEndTime - operationStartTime) / 1000.0;
printf(
"Overall Processing time of NvMedia-CUDA Operations with NvSCI APIs %.4f "
"ms with %zu iterations\n",
processingTime, args.iterations);
processingTime = (double)(endTime - startTime) / 1000.0;
printf(
"Overall Processing time of NvMedia-CUDA Operations + allocation/cleanup "
"with NvSCI APIs %.4f ms with %zu iterations\n",
processingTime, args.iterations);
if (ctx.i2d != NULL) {
NvMedia2DDestroy(ctx.i2d);
}
if (ctx.device != NULL) {
NvMediaDeviceDestroy(ctx.device);
}
if (status == NVMEDIA_STATUS_OK) {
return 0;
} else {
return 1;
}
}

View File

@@ -0,0 +1,471 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <iostream>
/* Nvidia headers */
#include "nvmedia_utils/cmdline.h"
#include "nvmedia_image.h"
#include "nvmedia_2d.h"
#include "nvmedia_surface.h"
#include "nvmedia_utils/image_utils.h"
#include "nvmedia_image_nvscibuf.h"
#include "nvmedia_producer.h"
#include "nvmedia_2d_nvscisync.h"
#include "nvsci_setup.h"
NvMediaImage *NvMediaImageCreateUsingNvScibuf(NvMediaDevice *device,
NvMediaSurfaceType type,
const NvMediaSurfAllocAttr *attrs,
uint32_t numAttrs, uint32_t flags,
NvSciBufObj &bufobj,
int cudaDeviceId) {
NvSciBufModule module = NULL;
NvSciError err = NvSciError_Success;
NvMediaStatus status = NVMEDIA_STATUS_OK;
NvSciBufAttrList attrlist = NULL;
NvSciBufAttrList conflictlist = NULL;
NvSciBufAttrValAccessPerm access_perm = NvSciBufAccessPerm_ReadWrite;
NvSciBufAttrKeyValuePair attr_kvp = {NvSciBufGeneralAttrKey_RequiredPerm,
&access_perm, sizeof(access_perm)};
NvSciBufAttrKeyValuePair pairArrayOut[10];
NvMediaImage *image = NULL;
err = NvSciBufModuleOpen(&module);
if (err != NvSciError_Success) {
printf("%s: NvSciBuffModuleOpen failed. Error: %d \n", __func__, err);
goto fail_cleanup;
}
err = NvSciBufAttrListCreate(module, &attrlist);
if (err != NvSciError_Success) {
printf("%s: SciBufAttrListCreate failed. Error: %d \n", __func__, err);
goto fail_cleanup;
}
err = NvSciBufAttrListSetAttrs(attrlist, &attr_kvp, 1);
if (err != NvSciError_Success) {
printf("%s: AccessPermSetAttr failed. Error: %d \n", __func__, err);
goto fail_cleanup;
}
status =
NvMediaImageFillNvSciBufAttrs(device, type, attrs, numAttrs, 0, attrlist);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: ImageFillSciBufAttrs failed. Error: %d \n", __func__, err);
goto fail_cleanup;
}
setupNvSciBuf(bufobj, attrlist, cudaDeviceId);
status = NvMediaImageCreateFromNvSciBuf(device, bufobj, &image);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: ImageCreatefromSciBuf failed. Error: %d \n", __func__, err);
goto fail_cleanup;
}
NvSciBufAttrListFree(attrlist);
if (module != NULL) {
NvSciBufModuleClose(module);
}
return image;
fail_cleanup:
if (attrlist != NULL) {
NvSciBufAttrListFree(attrlist);
}
if (bufobj != NULL) {
NvSciBufObjFree(bufobj);
bufobj = NULL;
}
if (module != NULL) {
NvSciBufModuleClose(module);
}
NvMediaImageDestroy(image);
return NULL;
}
/* Create NvMediaImage surface based on the input attributes.
* Returns NVMEDIA_STATUS_OK on success
*/
static NvMediaStatus createSurface(Blit2DTest *ctx,
NvMediaSurfFormatAttr *surfFormatAttrs,
NvMediaSurfAllocAttr *surfAllocAttrs,
uint32_t numSurfAllocAttrs,
NvMediaImage **image, NvSciBufObj &bufObj,
int cudaDeviceId) {
NvMediaSurfaceType surfType;
/* create source image */
surfType =
NvMediaSurfaceFormatGetType(surfFormatAttrs, NVM_SURF_FMT_ATTR_MAX);
*image = NvMediaImageCreateUsingNvScibuf(ctx->device, /* device */
surfType, /* surface type */
surfAllocAttrs, numSurfAllocAttrs, 0,
bufObj, cudaDeviceId);
if (*image == NULL) {
printf("Unable to create image\n");
return NVMEDIA_STATUS_ERROR;
}
InitImage(*image, surfAllocAttrs[0].value, surfAllocAttrs[1].value);
/* printf("%s: NvMediaImageCreate:: Image size: %ux%u Image type: %d\n",
__func__, surfAllocAttrs[0].value, surfAllocAttrs[1].value,
surfType);*/
return NVMEDIA_STATUS_OK;
}
/* Create NvMediaImage surface based on the input attributes.
* Returns NVMEDIA_STATUS_OK on success
*/
static NvMediaStatus createSurfaceNonNvSCI(
Blit2DTest *ctx, NvMediaSurfFormatAttr *surfFormatAttrs,
NvMediaSurfAllocAttr *surfAllocAttrs, uint32_t numSurfAllocAttrs,
NvMediaImage **image) {
NvMediaSurfaceType surfType;
/* create source image */
surfType =
NvMediaSurfaceFormatGetType(surfFormatAttrs, NVM_SURF_FMT_ATTR_MAX);
*image = NvMediaImageCreateNew(ctx->device, surfType, surfAllocAttrs,
numSurfAllocAttrs, 0);
if (*image == NULL) {
printf("Unable to create image\n");
return NVMEDIA_STATUS_ERROR;
}
InitImage(*image, surfAllocAttrs[0].value, surfAllocAttrs[1].value);
/* printf("%s: NvMediaImageCreate:: Image size: %ux%u Image type: %d\n",
__func__, surfAllocAttrs[0].value, surfAllocAttrs[1].value,
surfType);*/
return NVMEDIA_STATUS_OK;
}
static void destroySurface(NvMediaImage *image) { NvMediaImageDestroy(image); }
static NvMediaStatus blit2DImage(Blit2DTest *ctx, TestArgs *args,
NvSciSyncObj &nvMediaSignalerSyncObj,
NvSciSyncFence *preSyncFence,
NvSciSyncFence *fence) {
NvMediaStatus status;
NvMediaImageSurfaceMap surfaceMap;
status = ReadImage(args->inputFileName, /* fileName */
0, /* frameNum */
args->srcSurfAllocAttrs[0].value, /* source image width */
args->srcSurfAllocAttrs[1].value, /* source image height */
ctx->srcImage, /* srcImage */
NVMEDIA_TRUE, /* uvOrderFlag */
1, /* bytesPerPixel */
MSB_ALIGNED); /* pixelAlignment */
if (status != NVMEDIA_STATUS_OK) {
printf("%s: ReadImage failed for input buffer: %d\n", __func__, status);
return status;
}
if ((args->srcRect.x1 <= args->srcRect.x0) ||
(args->srcRect.y1 <= args->srcRect.y0)) {
ctx->srcRect = NULL;
} else {
ctx->srcRect = &(args->srcRect);
}
if ((args->dstRect.x1 <= args->dstRect.x0) ||
(args->dstRect.y1 <= args->dstRect.y0)) {
ctx->dstRect = NULL;
} else {
ctx->dstRect = &(args->dstRect);
}
static int64_t launch = 0;
// Start inserting pre-fence from second launch inorder to for NvMedia2Blit to
// wait
// for cuda signal on fence.
if (launch) {
status = NvMedia2DInsertPreNvSciSyncFence(ctx->i2d, preSyncFence);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMedia2DSetNvSciSyncObjforEOF failed: %d\n", __func__,
status);
return status;
}
NvSciSyncFenceClear(preSyncFence);
}
launch++;
status = NvMedia2DSetNvSciSyncObjforEOF(ctx->i2d, nvMediaSignalerSyncObj);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMedia2DSetNvSciSyncObjforEOF failed: %d\n", __func__,
status);
return status;
}
/* 2DBlit processing on input image */
status = NvMedia2DBlitEx(ctx->i2d, /* i2d */
ctx->dstImage, /* dstSurface */
ctx->dstRect, /* dstRect */
ctx->srcImage, /* srcSurface */
ctx->srcRect, /* srcRect */
&args->blitParams, /* params */
NULL); /* paramsOut */
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMedia2DBlitEx failed: %d\n", __func__, status);
return status;
}
status =
NvMedia2DGetEOFNvSciSyncFence(ctx->i2d, nvMediaSignalerSyncObj, fence);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMedia2DGetEOFNvSciSyncFence failed: %d\n", __func__, status);
return status;
}
return NVMEDIA_STATUS_OK;
}
static NvMediaStatus blit2DImageNonNvSCI(Blit2DTest *ctx, TestArgs *args) {
NvMediaStatus status;
NvMediaImageSurfaceMap surfaceMap;
status = ReadImage(args->inputFileName, /* fileName */
0, /* frameNum */
args->srcSurfAllocAttrs[0].value, /* source image width */
args->srcSurfAllocAttrs[1].value, /* source image height */
ctx->srcImage, /* srcImage */
NVMEDIA_TRUE, /* uvOrderFlag */
1, /* bytesPerPixel */
MSB_ALIGNED); /* pixelAlignment */
if (status != NVMEDIA_STATUS_OK) {
printf("%s: ReadImage failed for input buffer: %d\n", __func__, status);
return status;
}
if ((args->srcRect.x1 <= args->srcRect.x0) ||
(args->srcRect.y1 <= args->srcRect.y0)) {
ctx->srcRect = NULL;
} else {
ctx->srcRect = &(args->srcRect);
}
if ((args->dstRect.x1 <= args->dstRect.x0) ||
(args->dstRect.y1 <= args->dstRect.y0)) {
ctx->dstRect = NULL;
} else {
ctx->dstRect = &(args->dstRect);
}
/* 2DBlit processing on input image */
status = NvMedia2DBlitEx(ctx->i2d, /* i2d */
ctx->dstImage, /* dstSurface */
ctx->dstRect, /* dstRect */
ctx->srcImage, /* srcSurface */
ctx->srcRect, /* srcRect */
&args->blitParams, /* params */
NULL); /* paramsOut */
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMedia2DBlitEx failed: %d\n", __func__, status);
return status;
}
/* Write output image into buffer */
ctx->bytesPerPixel = 1;
WriteImageToAllocatedBuffer(ctx, ctx->dstImage, NVMEDIA_TRUE, NVMEDIA_FALSE,
ctx->bytesPerPixel);
return NVMEDIA_STATUS_OK;
}
static void cleanup(Blit2DTest *ctx, NvMediaStatus status = NVMEDIA_STATUS_OK) {
if (ctx->srcImage != NULL) {
NvMedia2DImageUnRegister(ctx->i2d, ctx->srcImage);
destroySurface(ctx->srcImage);
}
if (ctx->dstImage != NULL) {
NvMedia2DImageUnRegister(ctx->i2d, ctx->dstImage);
destroySurface(ctx->dstImage);
}
if (status != NVMEDIA_STATUS_OK) {
exit(EXIT_FAILURE);
}
}
void cleanupNvMedia(Blit2DTest *ctx, NvSciSyncObj &syncObj,
NvSciSyncObj &preSyncObj) {
NvMediaStatus status;
cleanup(ctx);
status = NvMedia2DUnregisterNvSciSyncObj(ctx->i2d, syncObj);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageSciBufInit failed\n", __func__);
exit(EXIT_FAILURE);
}
status = NvMedia2DUnregisterNvSciSyncObj(ctx->i2d, preSyncObj);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageSciBufInit failed\n", __func__);
exit(EXIT_FAILURE);
}
NvMediaImageNvSciBufDeinit();
}
void cleanupNvMedia(Blit2DTest *ctx) {
cleanup(ctx);
free(ctx->dstBuffPitches);
free(ctx->dstBuffer);
free(ctx->dstBuff);
}
void setupNvMedia(TestArgs *args, Blit2DTest *ctx, NvSciBufObj &srcNvSciBufobj,
NvSciBufObj &dstNvSciBufobj, NvSciSyncObj &syncObj,
NvSciSyncObj &preSyncObj, int cudaDeviceId) {
NvMediaStatus status;
status = NvMediaImageNvSciBufInit();
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageSciBufInit failed\n", __func__);
cleanup(ctx, status);
}
// Create source surface
status = createSurface(ctx, args->srcSurfFormatAttrs, args->srcSurfAllocAttrs,
args->numSurfAllocAttrs, &ctx->srcImage,
srcNvSciBufobj, cudaDeviceId);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to create buffer pools\n", __func__);
cleanup(ctx, status);
}
// Create destination surface
status = createSurface(ctx, args->dstSurfFormatAttrs, args->dstSurfAllocAttrs,
args->numSurfAllocAttrs, &ctx->dstImage,
dstNvSciBufobj, cudaDeviceId);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to create buffer pools\n", __func__);
cleanup(ctx, status);
}
// Register source Surface
status =
NvMedia2DImageRegister(ctx->i2d, ctx->srcImage, NVMEDIA_ACCESS_MODE_READ);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to register source surface\n", __func__);
cleanup(ctx, status);
}
// Register destination Surface
status = NvMedia2DImageRegister(ctx->i2d, ctx->dstImage,
NVMEDIA_ACCESS_MODE_READ_WRITE);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to register destination surface\n", __func__);
cleanup(ctx, status);
}
status = NvMedia2DRegisterNvSciSyncObj(ctx->i2d, NVMEDIA_EOFSYNCOBJ, syncObj);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to NvMedia2DRegisterNvSciSyncObj\n", __func__);
}
status =
NvMedia2DRegisterNvSciSyncObj(ctx->i2d, NVMEDIA_PRESYNCOBJ, preSyncObj);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to NvMedia2DRegisterNvSciSyncObj\n", __func__);
}
}
// Create NvMedia src & dst image without NvSciBuf
void setupNvMedia(TestArgs *args, Blit2DTest *ctx) {
NvMediaStatus status;
// Create source surface
status = createSurfaceNonNvSCI(ctx, args->srcSurfFormatAttrs,
args->srcSurfAllocAttrs,
args->numSurfAllocAttrs, &ctx->srcImage);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to create buffer pools\n", __func__);
cleanup(ctx, status);
}
// Create destination surface
status = createSurfaceNonNvSCI(ctx, args->dstSurfFormatAttrs,
args->dstSurfAllocAttrs,
args->numSurfAllocAttrs, &ctx->dstImage);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to create buffer pools\n", __func__);
cleanup(ctx, status);
}
// Register source Surface
status =
NvMedia2DImageRegister(ctx->i2d, ctx->srcImage, NVMEDIA_ACCESS_MODE_READ);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to register source surface\n", __func__);
cleanup(ctx, status);
}
// Register destination Surface
status = NvMedia2DImageRegister(ctx->i2d, ctx->dstImage,
NVMEDIA_ACCESS_MODE_READ_WRITE);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Unable to register destination surface\n", __func__);
cleanup(ctx, status);
}
// Allocate buffer for writing image & set image parameters in Blit2DTest.
ctx->bytesPerPixel = 1;
AllocateBufferToWriteImage(ctx, ctx->dstImage, NVMEDIA_TRUE, /* uvOrderFlag */
NVMEDIA_FALSE); /* appendFlag */
}
void runNvMediaBlit2D(TestArgs *args, Blit2DTest *ctx) {
// Blit2D function
NvMediaStatus status = blit2DImageNonNvSCI(ctx, args);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Blit2D failed\n", __func__);
cleanup(ctx, status);
}
}
void runNvMediaBlit2D(TestArgs *args, Blit2DTest *ctx,
NvSciSyncObj &nvMediaSignalerSyncObj,
NvSciSyncFence *preSyncFence, NvSciSyncFence *fence) {
// Blit2D function
NvMediaStatus status =
blit2DImage(ctx, args, nvMediaSignalerSyncObj, preSyncFence, fence);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: Blit2D failed\n", __func__);
cleanup(ctx, status);
}
}

View File

@@ -0,0 +1,48 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __NVMEDIA_PRODUCER_H__
#define __NVMEDIA_PRODUCER_H__
#include "nvmedia_utils/cmdline.h"
#include "nvmedia_image.h"
#include "nvmedia_2d.h"
#include "nvmedia_surface.h"
#include "nvmedia_utils/image_utils.h"
#include "nvmedia_image_nvscibuf.h"
#include "nvscisync.h"
void runNvMediaBlit2D(TestArgs* args, Blit2DTest* ctx, NvSciSyncObj& syncObj,
NvSciSyncFence* preSyncFence, NvSciSyncFence* fence);
void runNvMediaBlit2D(TestArgs* args, Blit2DTest* ctx);
void setupNvMedia(TestArgs* args, Blit2DTest* ctx, NvSciBufObj& srcNvSciBufobj,
NvSciBufObj& dstNvSciBufobj, NvSciSyncObj& syncObj,
NvSciSyncObj& preSyncObj, int cudaDeviceId);
void setupNvMedia(TestArgs* args, Blit2DTest* ctx);
void cleanupNvMedia(Blit2DTest* ctx, NvSciSyncObj& syncObj,
NvSciSyncObj& preSyncObj);
void cleanupNvMedia(Blit2DTest* ctx);
#endif

View File

@@ -0,0 +1,192 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
/* Nvidia headers */
#include "cmdline.h"
#include "log_utils.h"
#include "misc_utils.h"
#include "config_parser.h"
#include "helper_cuda.h"
/* see cmdline.h for details */
void PrintUsage()
{
printf("cudaNvSciNvMedia\n");
printf("Usage: cudaNvSciNvMedia [options]\n");
printf("Options:\n");
printf("-h Prints usage\n");
printf("-device=n (n >= 0 for cuda device ID)\n");
printf("-cf=[config] 2d config file. Path length limited to %u chars\n", FILE_NAME_SIZE);
printf("-iterations=n (n > 0 - num of iterations of NvMedia-CUDA operations to be launched)\n");
}
SectionMap sectionsMap[] = {
{SECTION_NONE, "", 0, 0} /* Has to be the last item - specifies the end of array */
};
/* see cmdline.h for details */
int ParseArgs(int argc, char *argv[], TestArgs *args)
{
NvMediaBool bLastArg = NVMEDIA_FALSE;
NvMediaBool bDataAvailable = NVMEDIA_FALSE;
NvMediaStatus status = NVMEDIA_STATUS_OK;
const char* filename = NULL;
int i;
args->srcSurfAllocAttrs[0].type = args->dstSurfAllocAttrs[0].type = NVM_SURF_ATTR_WIDTH;
args->srcSurfAllocAttrs[1].type = args->dstSurfAllocAttrs[1].type = NVM_SURF_ATTR_HEIGHT;
args->srcSurfAllocAttrs[2].type = args->dstSurfAllocAttrs[2].type = NVM_SURF_ATTR_EMB_LINES_TOP;
args->srcSurfAllocAttrs[3].type = args->dstSurfAllocAttrs[3].type = NVM_SURF_ATTR_EMB_LINES_BOTTOM;
args->srcSurfAllocAttrs[4].type = args->dstSurfAllocAttrs[4].type = NVM_SURF_ATTR_CPU_ACCESS;
args->srcSurfAllocAttrs[5].type = args->dstSurfAllocAttrs[5].type = NVM_SURF_ATTR_ALLOC_TYPE;
args->srcSurfAllocAttrs[6].type = args->dstSurfAllocAttrs[6].type = NVM_SURF_ATTR_SCAN_TYPE;
args->srcSurfAllocAttrs[7].type = args->dstSurfAllocAttrs[7].type = NVM_SURF_ATTR_COLOR_STD_TYPE;
args->numSurfAllocAttrs = 8;
args->srcSurfFormatAttrs[0].type = args->dstSurfFormatAttrs[0].type = NVM_SURF_ATTR_SURF_TYPE;
args->srcSurfFormatAttrs[1].type = args->dstSurfFormatAttrs[1].type = NVM_SURF_ATTR_LAYOUT;
args->srcSurfFormatAttrs[2].type = args->dstSurfFormatAttrs[2].type = NVM_SURF_ATTR_DATA_TYPE;
args->srcSurfFormatAttrs[3].type = args->dstSurfFormatAttrs[3].type = NVM_SURF_ATTR_MEMORY;
args->srcSurfFormatAttrs[4].type = args->dstSurfFormatAttrs[4].type = NVM_SURF_ATTR_SUB_SAMPLING_TYPE;
args->srcSurfFormatAttrs[5].type = args->dstSurfFormatAttrs[5].type = NVM_SURF_ATTR_BITS_PER_COMPONENT;
args->srcSurfFormatAttrs[6].type = args->dstSurfFormatAttrs[6].type = NVM_SURF_ATTR_COMPONENT_ORDER;
/* ConfigParamsMap
* See nvmedia_2d.h and sample config file(s) for details.
*/
ConfigParamsMap paramsMap[] = {
/*ParamName, &args->variableName, paramType, D, LimitType, Mn, Mx, CharSize, p2C, section */
{"transformMode", &args->blitParams.dstTransform, TYPE_UINT, 0, LIMITS_BOTH, 0, 7, 0, 0, SECTION_NONE},
{"filterMode", &args->blitParams.filter, TYPE_UINT, 1, LIMITS_BOTH, 1, 4, 0, 0, SECTION_NONE},
{"colorStd", &args->blitParams.colorStandard, TYPE_UINT, 0, LIMITS_MIN, 0, 3, 0, 0, SECTION_NONE},
{"validOperations", &args->blitParams.validFields, TYPE_UINT, 0, LIMITS_BOTH, 0, 15, 0, 0, SECTION_NONE},
{"inputfile", &args->inputFileName, TYPE_CHAR_ARR, 0, LIMITS_NONE, 0, 0, FILE_NAME_SIZE, 0, SECTION_NONE},
/*src surface alloc attributes*/
{"srcWidth", &args->srcSurfAllocAttrs[0].value, TYPE_UINT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcHeight", &args->srcSurfAllocAttrs[1].value, TYPE_UINT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcCPUAccess", &args->srcSurfAllocAttrs[4].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"srcAllocType", &args->srcSurfAllocAttrs[5].value, TYPE_UINT, 0, LIMITS_BOTH, 0, 1, 0, 0, SECTION_NONE},
{"srcScanType", &args->srcSurfAllocAttrs[6].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 1, 0, 0, SECTION_NONE},
{"srcColorStd", &args->srcSurfAllocAttrs[7].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 12, 0, 0, SECTION_NONE},
/*src surface format attributes*/
{"srcSurfType", &args->srcSurfFormatAttrs[0].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"srcLayout", &args->srcSurfFormatAttrs[1].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 2, 0, 0, SECTION_NONE},
{"srcDataType", &args->srcSurfFormatAttrs[2].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 4, 0, 0, SECTION_NONE},
{"srcMemory", &args->srcSurfFormatAttrs[3].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"srcSubSamplingType", &args->srcSurfFormatAttrs[4].value, TYPE_UINT, 1, LIMITS_BOTH, 0, 4, 0, 0, SECTION_NONE},
{"srcBitsPerComponent", &args->srcSurfFormatAttrs[5].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 10, 0, 0, SECTION_NONE},
{"srcComponentOrder", &args->srcSurfFormatAttrs[6].value, TYPE_UINT, 2, LIMITS_BOTH, 1, 45, 0, 0, SECTION_NONE},
/*srcRect*/
{"srcRectx0", &args->srcRect.x0, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcRecty0", &args->srcRect.y0, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcRectx1", &args->srcRect.x1, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"srcRecty1", &args->srcRect.y1, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
/*dst surface alloc attributes*/
{"dstWidth", &args->dstSurfAllocAttrs[0].value, TYPE_UINT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstHeight", &args->dstSurfAllocAttrs[1].value, TYPE_UINT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstCPUAccess", &args->dstSurfAllocAttrs[4].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"dstAllocType", &args->dstSurfAllocAttrs[5].value, TYPE_UINT, 0, LIMITS_BOTH, 0, 1, 0, 0, SECTION_NONE},
{"dstScanType", &args->dstSurfAllocAttrs[6].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 1, 0, 0, SECTION_NONE},
{"dstColorStd", &args->dstSurfAllocAttrs[7].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 12, 0, 0, SECTION_NONE},
/*dst surface format attributes*/
{"dstSurfType", &args->dstSurfFormatAttrs[0].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"dstLayout", &args->dstSurfFormatAttrs[1].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 2, 0, 0, SECTION_NONE},
{"dstDataType", &args->dstSurfFormatAttrs[2].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 4, 0, 0, SECTION_NONE},
{"dstMemory", &args->dstSurfFormatAttrs[3].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 3, 0, 0, SECTION_NONE},
{"dstSubSamplingType", &args->dstSurfFormatAttrs[4].value, TYPE_UINT, 1, LIMITS_BOTH, 0, 4, 0, 0, SECTION_NONE},
{"dstBitsPerComponent", &args->dstSurfFormatAttrs[5].value, TYPE_UINT, 1, LIMITS_BOTH, 1, 10, 0, 0, SECTION_NONE},
{"dstComponentOrder", &args->dstSurfFormatAttrs[6].value, TYPE_UINT, 2, LIMITS_BOTH, 1, 45, 0, 0, SECTION_NONE},
/*dstRect*/
{"dstRectx0", &args->dstRect.x0, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstRecty0", &args->dstRect.y0, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstRectx1", &args->dstRect.x1, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
{"dstRecty1", &args->dstRect.y1, TYPE_USHORT, 0, LIMITS_MIN, 0, 0, 0, 0, SECTION_NONE},
/*End of the array */
{NULL, NULL, TYPE_UINT, 0, LIMITS_NONE, 0, 0, 0, 0, SECTION_NONE}
};
args->iterations = 100; // Set default iterations value.
if (checkCmdLineFlag(argc, (const char **)argv, "-h")) {
return -1;
}
if (checkCmdLineFlag(argc, (const char **)argv, "cf")) {
char *inputFileName = NULL;
getCmdLineArgumentString(argc, (const char **)argv, "cf", (char**)&inputFileName);
if (!inputFileName) {
printf("ERR: Invalid config file name\n");
return -1;
}
filename = sdkFindFilePath(inputFileName, ".");
}
if (checkCmdLineFlag(argc, (const char **)argv, "iterations")) {
args->iterations = getCmdLineArgumentInt(argc, (const char **)argv, "iterations");
}
if (filename == NULL) {
// Set default file to use if no config file given.
filename = sdkFindFilePath("sample.cfg", ".");
}
if (filename != NULL)
{
printf("Using config file %s\n", filename);
/* Init Parser Map*/
status = ConfigParser_InitParamsMap(paramsMap);
if (status != NVMEDIA_STATUS_OK) {
printf("ERR: ConfigParser_InitParamsMap failed! status:%x\n", status);
return -1;
}
status = ConfigParser_ParseFile(paramsMap, 1, sectionsMap, (char*)filename);
if (status != NVMEDIA_STATUS_OK) {
printf("ERR: Failed to parse config file. status:%x\n", status);
return -1;
}
}
LOG_INFO("ParseArgs: Validating params from config file\n");
status = ConfigParser_ValidateParams(paramsMap, sectionsMap);
if (status != NVMEDIA_STATUS_OK) {
printf("ERR: Some of the params in config file are invalid.\n");
return -1;
}
return 0;
}

View File

@@ -0,0 +1,90 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _NVMEDIA_2D_CMD_LINE_H_
#define _NVMEDIA_2D_CMD_LINE_H_
/* Include header containing NvMedia2D declarations */
#include "nvmedia_2d.h"
/* Maximum length of the path including file name */
#define FILE_NAME_SIZE 1024
/* TestArgs contains all arguments required to run the 2D test */
typedef struct _TestArgs {
char inputFileName[FILE_NAME_SIZE];
NvMediaSurfAllocAttr srcSurfAllocAttrs[NVM_SURF_ALLOC_ATTR_MAX];
NvMediaSurfAllocAttr dstSurfAllocAttrs[NVM_SURF_ALLOC_ATTR_MAX];
uint32_t numSurfAllocAttrs;
NvMediaSurfFormatAttr srcSurfFormatAttrs[NVM_SURF_FMT_ATTR_MAX];
NvMediaSurfFormatAttr dstSurfFormatAttrs[NVM_SURF_FMT_ATTR_MAX];
NvMediaRect srcRect;
NvMediaRect dstRect;
NvMedia2DBlitParameters blitParams;
size_t iterations;
} TestArgs;
typedef struct {
NvMediaDevice *device;
/* I2D for 2D blit processing */
NvMedia2D *i2d;
NvMediaImage *srcImage;
NvMediaImage *dstImage;
NvMediaRect *srcRect;
NvMediaRect *dstRect;
uint8_t **dstBuff;
uint32_t *dstBuffPitches;
uint8_t *dstBuffer;
uint32_t numSurfaces;
uint32_t bytesPerPixel;
uint32_t heightSurface;
uint32_t widthSurface;
float *xScalePtr;
float *yScalePtr;
} Blit2DTest;
/* Prints application usage options */
void PrintUsage (void);
/* Parses command line arguments.
* Also parses any configuration files supplied in the command line arguments.
* Arguments:
* argc
* (in) Number of tokens in the command line
* argv
* (in) Command line tokens
* args
* (out) Pointer to test arguments structure
*/
int ParseArgs(int argc, char **argv, TestArgs *args);
#endif /* _NVMEDIA_2D_CMD_LINE_H_ */

View File

@@ -0,0 +1,558 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include "config_parser.h"
#include "log_utils.h"
#if defined(__QNX__)
#include <strings.h>
#endif
static NvMediaStatus GetParamIndex(ConfigParamsMap *paramsMap, char *paramName, unsigned int *index)
{
int i = 0;
while(paramsMap[i].paramName != NULL) {
if (strcasecmp(paramsMap[i].paramName, paramName) == 0) {
*index = i;
return NVMEDIA_STATUS_OK;
} else {
i++;
}
}
return NVMEDIA_STATUS_BAD_PARAMETER;
}
NvMediaStatus ConfigParser_GetSectionIndexByName(SectionMap *sectionsMap, char *sectionName, unsigned int *index)
{
unsigned int i = 0;
while(sectionsMap[i].secType != SECTION_NONE) {
if(strcmp(sectionsMap[i].name, sectionName) == 0) {
*index = i;
return NVMEDIA_STATUS_OK;
} else {
i++;
}
}
return NVMEDIA_STATUS_BAD_PARAMETER;
}
NvMediaStatus ConfigParser_GetSectionIndexByType(SectionMap *sectionsMap, SectionType sectionType, unsigned int *index)
{
unsigned int i = 0;
while(sectionsMap[i].secType != SECTION_NONE) {
if(sectionsMap[i].secType == sectionType) {
*index = i;
return NVMEDIA_STATUS_OK;
} else {
i++;
}
}
*index = i;
return NVMEDIA_STATUS_OK;
}
static NvMediaStatus GetFileContent(char *filename, char **fileContentOut)
{
FILE *file;
char *fileCotent;
long fileSize;
file = fopen(filename, "r");
if(file == NULL) {
printf("Parser_GetFileContent: Cannot open configuration file %s\n", filename);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
if (fseek(file, 0, SEEK_END) != 0) {
printf("Parser_GetFileContent: Cannot fseek in configuration file %s\n", filename);
return NVMEDIA_STATUS_ERROR;
}
fileSize = ftell(file);
if(fileSize < 0 || fileSize > 150000) {
printf("Parser_GetFileContent: Unreasonable Filesize %ld encountered for file %s\n", fileSize, filename);
return NVMEDIA_STATUS_ERROR;
}
if(fseek (file, 0, SEEK_SET) != 0) {
printf("Parser_GetFileContent: Cannot fseek in configuration file %s\n", filename);
return NVMEDIA_STATUS_ERROR;
}
fileCotent = (char*)malloc(fileSize + 1);
if(fileCotent == NULL) {
printf("Parser_GetFileContent: Failed allocating buffer for file Content\n");
return NVMEDIA_STATUS_OUT_OF_MEMORY;
}
fileSize = (long)fread(fileCotent, 1, fileSize, file);
fileCotent[fileSize] = '\0';
*fileContentOut = fileCotent;
fclose(file);
return NVMEDIA_STATUS_OK;
}
NvMediaStatus ConfigParser_ParseFile(ConfigParamsMap *paramsMap, unsigned int numParams, SectionMap *sectionsMap, char *fileName)
{
char *items[MAX_ITEMS_TO_PARSE] = {NULL};
int intValue, itemsCount = 0, i = 0, sectionIndex = 0;
double doubleValue;
float floatValue;
unsigned int currItemIndex, uintValue, sectionId = 0, currSectionId = 0, charValue, paramDefaultLength;
unsigned short ushortValue;
short shortValue;
unsigned long long ullValue;
NvMediaBool isInString = NVMEDIA_FALSE, isInItem = NVMEDIA_FALSE;
char *buffer, *bufferEnd, *param, *pParamLength;
char sectionName[100];
char currDigit;
char *configContentBuf = NULL;
unsigned int numSetsInSection = 0;
if(GetFileContent(fileName, &configContentBuf) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_ParseFile: Failed reading file %s", fileName);
return NVMEDIA_STATUS_ERROR;
}
buffer = configContentBuf;
bufferEnd = &configContentBuf[strlen(configContentBuf)];
// Stage one: Create items mapping in the content using "items" pointers array. For each parameter we have 3 items: param name, '=' char and the param value.
while(buffer < bufferEnd) {
if(itemsCount >= MAX_ITEMS_TO_PARSE) {
LOG_WARN("ConfigParser_ParseFile: Number of items in configuration file exceeded the maximum allowed (%d). Only %d items will be parsed.\n",
MAX_ITEMS_TO_PARSE, MAX_ITEMS_TO_PARSE);
itemsCount = MAX_ITEMS_TO_PARSE;
break;
}
switch(*buffer) {
// Carriage return
case 13:
++buffer;
break;
case '#':
*buffer = '\0'; // Replace '#' with '\0' in case of comment immediately following integer or string
while(*buffer != '\n' && buffer < bufferEnd) { // Skip till EOL or EOF
++buffer;
}
isInString = NVMEDIA_FALSE;
isInItem = NVMEDIA_FALSE;
break;
case '\n':
isInItem = NVMEDIA_FALSE;
isInString = NVMEDIA_FALSE;
*buffer++='\0';
break;
case ' ':
case '\t': // Skip whitespace, leave state unchanged
if(isInString)
buffer++;
else { // Terminate non-strings once whitespace is found
*buffer++ = '\0';
isInItem = NVMEDIA_FALSE;
}
break;
case '"': // Begin/End of String
*buffer++ = '\0';
if(!isInString) {
items[itemsCount++] = buffer;
isInItem = ~isInItem;
} else {
isInItem = NVMEDIA_FALSE;
}
isInString = ~isInString; // Toggle
break;
case '[':
*(buffer++) = '\0';
items[itemsCount++] = buffer;
while(*buffer != ' ' && *buffer != '\n' && buffer < bufferEnd) { // Skip till whitespace (after which is located the parsed section number) or EOL or EOF
sectionName[i++] = *(buffer++);
}
sectionName[i] = '\0';
i = 0;
while(*buffer == ' ') {
*(buffer++) = '\0';
}
items[itemsCount++] = buffer;
while(*buffer != ']' && *buffer != '\n' && buffer < bufferEnd) { // Read the section number
currDigit = *buffer;
sectionIndex = sectionIndex * 10 + (currDigit - '0');
buffer++;
}
*(buffer++) = '\0';
sectionIndex--;
if(ConfigParser_GetSectionIndexByName(sectionsMap, sectionName, &sectionId) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_ParseFile: SectionName couldn't be found in section map: '%s'.\n", sectionName);
}
numSetsInSection++;
sectionsMap[sectionId].lastSectionIndex = sectionIndex;
sectionIndex = 0;
isInString = NVMEDIA_FALSE;
isInItem = NVMEDIA_FALSE;
break;
default:
if(!isInItem) {
items[itemsCount++] = buffer;
isInItem = ~isInItem;
}
buffer++;
}
}
itemsCount--;
if(numSetsInSection > numParams) {
printf("%s: Not enough buffers allocated for parsing. Number of sets allocated: %d. Number of sets in config file: %d \n",
__func__, numParams, numSetsInSection);
if(configContentBuf) {
free(configContentBuf);
}
return NVMEDIA_STATUS_ERROR;
}
// Stage 2: Go through the list of items and save their values in parameters map
for(i = 0; i < itemsCount; i += 3) {
if(ConfigParser_GetSectionIndexByName(sectionsMap, items[i], &currItemIndex) == NVMEDIA_STATUS_OK) {
currSectionId = atoi(items[i + 1]);
currSectionId--;
LOG_DBG("ConfigParser_ParseFile: Parsing section %s index %d\n", items[i], currSectionId);
i -= 1;
continue;
}
if(GetParamIndex(paramsMap, items[i], &currItemIndex) != NVMEDIA_STATUS_OK) {
LOG_WARN("ConfigParser_ParseFile: Parameter Name '%s' is not recognized. Dismissing this parameter.\n", items[i]);
continue;
}
if(strcmp("=", items[i + 1])) {
printf("ConfigParser_ParseFile: '=' expected as the second token in each line. Error caught while parsing parameter '%s'.\n", items[i]);
i -= 2;
continue;
}
if(ConfigParser_GetSectionIndexByType(sectionsMap, paramsMap[currItemIndex].sectionType, &sectionId) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_ParseFile: Section index couldn't be found in section map by type. Param Name: '%s'.\n", paramsMap[currItemIndex].paramName);
}
if(sectionsMap[sectionId].lastSectionIndex == 0) {
// Param is not part of a collection or collection includes only one item
currSectionId = 0;
}
param = (char *)paramsMap[currItemIndex].mappedLocation + currSectionId * sectionsMap[sectionId].sizeOfStruct;
pParamLength = (char *)paramsMap[currItemIndex].stringLengthAddr + currSectionId * sectionsMap[sectionId].sizeOfStruct;
paramDefaultLength = paramsMap[currItemIndex].stringLength;
// Interpret the Value
LOG_DBG("ConfigParser_ParseFile: Interpreting parameter %s\n", items[i]);
switch(paramsMap[currItemIndex].type) {
case TYPE_INT:
if(sscanf(items[i + 2], "%d", &intValue) != 1) {
printf("ConfigParser_ParseFile: Expected numerical value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(int *)(void *)param = intValue;
break;
case TYPE_UINT:
if(sscanf(items[i + 2], "%u", &uintValue) != 1) {
printf("ConfigParser_ParseFile: Expected numerical value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned int *)(void *)param = uintValue;
break;
case TYPE_UINT_HEX:
if(sscanf(items[i + 2], "%x", &uintValue) != 1) {
printf("ConfigParser_ParseFile: Expected unsigned char value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned int *)(void *)param = uintValue;
break;
case TYPE_CHAR_ARR:
if(items[i + 2] == NULL)
memset(param, 0, (pParamLength != NULL && *pParamLength != 0) ? *pParamLength : paramDefaultLength);
else {
strncpy(param, items[i + 2], paramsMap[currItemIndex].stringLength);
param[strlen(items[i + 2])] = '\0';
}
break;
case TYPE_DOUBLE:
if(sscanf(items[i + 2], "%lf", &doubleValue) != 1) {
printf("ConfigParser_ParseFile: Expected double value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(double *)(void *)param = doubleValue;
break;
case TYPE_FLOAT:
if(sscanf(items[i + 2], "%f", &floatValue) != 1) {
printf("ConfigParser_ParseFile: Expected double value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(float *)(void *)param = floatValue;
break;
case TYPE_UCHAR:
if(sscanf(items[i + 2], "%u", &charValue) != 1) {
printf("ConfigParser_ParseFile: Expected unsigned char value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned char *)(void *)param = charValue;
break;
case TYPE_USHORT:
if(sscanf(items[i + 2], "%hu", &ushortValue) != 1) {
printf("ConfigParser_ParseFile: Expected unsigned short value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned short *)(void *)param = ushortValue;
break;
case TYPE_SHORT:
if(sscanf(items[i + 2], "%hd", &shortValue) != 1) {
printf("ConfigParser_ParseFile: Expected short value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(short *)(void *)param = shortValue;
break;
case TYPE_UCHAR_ARR:
if(items[i + 2] == NULL)
memset(param, 0, (pParamLength != NULL && *pParamLength != 0) ? *pParamLength : paramDefaultLength);
else {
strncpy(param, items[i + 2], paramsMap[currItemIndex].stringLength);
param[strlen(items[i + 2])] = '\0';
}
break;
case TYPE_ULLONG:
if(sscanf(items[i + 2], "%llu", &ullValue) != 1) {
printf("ConfigParser_ParseFile: Expected numerical value for Parameter %s, found value '%s'\n", items[i], items[i + 2]);
}
*(unsigned long long *)(void *)param = ullValue;
break;
default:
printf("ConfigParser_ParseFile: Encountered unknown value type in the map\n");
}
}
if (configContentBuf)
free(configContentBuf);
return NVMEDIA_STATUS_OK;
}
NvMediaStatus ConfigParser_InitParamsMap(ConfigParamsMap *paramsMap)
{
int i = 0;
while(paramsMap[i].paramName != NULL) {
if (paramsMap[i].mappedLocation == NULL) {
i++;
continue;
}
switch(paramsMap[i].type) {
case TYPE_UINT:
case TYPE_UINT_HEX:
*(unsigned int *)(paramsMap[i].mappedLocation) = (unsigned int)paramsMap[i].defaultValue;
break;
case TYPE_INT:
*(int *)(paramsMap[i].mappedLocation) = (int)paramsMap[i].defaultValue;
break;
case TYPE_DOUBLE:
*(double *)(paramsMap[i].mappedLocation) = (double)paramsMap[i].defaultValue;
break;
case TYPE_FLOAT:
*(float *)(paramsMap[i].mappedLocation) = (float)paramsMap[i].defaultValue;
break;
case TYPE_UCHAR:
*(unsigned char *)(paramsMap[i].mappedLocation) = (NvMediaBool)paramsMap[i].defaultValue;
break;
case TYPE_USHORT:
*(unsigned short *)(paramsMap[i].mappedLocation) = (unsigned short)paramsMap[i].defaultValue;
break;
case TYPE_SHORT:
*(short *)(paramsMap[i].mappedLocation) = (short)paramsMap[i].defaultValue;
break;
case TYPE_ULLONG:
*(unsigned long long *)(paramsMap[i].mappedLocation) = (unsigned long long)paramsMap[i].defaultValue;
break;
case TYPE_CHAR_ARR:
case TYPE_UCHAR_ARR:
default:
break;
}
i++;
}
return NVMEDIA_STATUS_OK;
}
NvMediaStatus ConfigParser_ValidateParams(ConfigParamsMap *paramsMap, SectionMap *sectionsMap)
{
NvMediaStatus status = NVMEDIA_STATUS_OK;
unsigned int sectionId = 0, i = 0, j;
char *param;
while(paramsMap[i].paramName != NULL) {
if(ConfigParser_GetSectionIndexByType(sectionsMap, paramsMap[i].sectionType, &sectionId) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_ValidateParams: Section index couldn't be found in section map. Param Name: '%s'.\n", paramsMap[i].paramName);
}
for(j = 0; j <= sectionsMap[sectionId].lastSectionIndex; j++) {
if(paramsMap[i].paramLimits == 1 || paramsMap[i].paramLimits == 2) {
param = (char *)paramsMap[i].mappedLocation + j * sectionsMap[sectionId].sizeOfStruct;
if (param == NULL) {
i++;
continue;
}
switch (paramsMap[i].type) {
case TYPE_UINT:
case TYPE_UINT_HEX:
if(*(unsigned int *)(void *)param < (unsigned int)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(unsigned int *)(void *)param > (unsigned int)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_DOUBLE:
if(*(double *)(void *)param < (double)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(double *)(void *)param > (double)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_FLOAT:
if(*(float *)(void *)param < (float)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(float *)(void *)param > (float)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_INT:
if(*(int *)(void *)param < (int)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(int *)(void *)param > (int)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_USHORT:
if(*(unsigned short *)(void *)param < (unsigned short)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(unsigned short *)(void *)param > (unsigned short)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_SHORT:
if(*(short *)(void *)param < (short)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(short *)(void *)param > (short)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
case TYPE_ULLONG:
if(*(unsigned long long *)(void *)param < (unsigned long long)paramsMap[i].minLimit ||
(paramsMap[i].paramLimits == 2 && *(unsigned long long *)(void *)param > (unsigned long long)paramsMap[i].maxLimit )) {
printf("ConfigParser_ValidateParams: Error in input parameter %s\n", paramsMap[i].paramName);
printf("Check configuration file for parameter limits\n");
status = NVMEDIA_STATUS_BAD_PARAMETER;
}
break;
default:
break;
}
}
}
i++;
}
return status;
}
NvMediaStatus ConfigParser_DisplayParams(ConfigParamsMap *pParamsMap, SectionMap *pSectionsMap)
{
unsigned int i = 0, j, sectionId = 0;
char *param;
while(pParamsMap[i].paramName != NULL) {
if(ConfigParser_GetSectionIndexByType(pSectionsMap, pParamsMap[i].sectionType, &sectionId) != NVMEDIA_STATUS_OK) {
printf("ConfigParser_DisplayParams: Section index couldn't be found in section map by type. Param Name: '%s'.\n", pParamsMap[i].paramName);
}
for(j = 0; j <= pSectionsMap[sectionId].lastSectionIndex; j++) {
param = (char *)pParamsMap[i].mappedLocation + j * pSectionsMap[sectionId].sizeOfStruct;
if (param == NULL) {
i++;
continue;
}
switch(pParamsMap[i].type) {
case TYPE_UINT:
printf("(%d) %s = %u\n", j, pParamsMap[i].paramName, *(unsigned int *)(void *)param);
break;
case TYPE_DOUBLE:
printf("(%d) %s = %.2lf\n", j, pParamsMap[i].paramName, *(double *)(void *)param);
break;
case TYPE_FLOAT:
printf("(%d) %s = %.2f\n", j, pParamsMap[i].paramName, *(float *)(void *)param);
break;
case TYPE_UCHAR:
printf("(%d) %s = %d\n", j, pParamsMap[i].paramName, *(unsigned char *)(void *)param);
break;
case TYPE_USHORT:
printf("(%d) %s = %hu\n", j, pParamsMap[i].paramName, *(unsigned short *)(void *)param);
break;
case TYPE_SHORT:
printf("(%d) %s = %hd\n", j, pParamsMap[i].paramName, *(short *)(void *)param);
break;
case TYPE_ULLONG:
printf("(%d) %s = %llu\n", j, pParamsMap[i].paramName, *(unsigned long long *)(void *)param);
break;
case TYPE_CHAR_ARR:
printf("(%d) %s = ""%s""\n", j, pParamsMap[i].paramName, param);
break;
case TYPE_UCHAR_ARR:
printf("(%d) %s = ""%s""\n", j, pParamsMap[i].paramName, (unsigned char *)(void *)param);
break;
case TYPE_INT:
printf("(%d) %s = %d\n", j, pParamsMap[i].paramName, *(int *)(void *)param);
break;
case TYPE_UINT_HEX:
printf("(%d) %s = %x\n", j, pParamsMap[i].paramName, *(unsigned int *)(void *)param);
break;
default:
// Do nothing
break;
}
}
i++;
}
return NVMEDIA_STATUS_OK;
}

View File

@@ -0,0 +1,109 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _NVMEDIA_TEST_CONFIG_PARSER_H_
#define _NVMEDIA_TEST_CONFIG_PARSER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "nvmedia_core.h"
#include "nvmedia_surface.h"
#define MAX_ITEMS_TO_PARSE 10000
typedef enum _ParamType {
TYPE_UINT = 0,
TYPE_UINT_HEX,
TYPE_INT,
TYPE_DOUBLE,
TYPE_FLOAT,
TYPE_UCHAR,
TYPE_ULLONG,
TYPE_USHORT,
TYPE_CHAR_ARR,
TYPE_UCHAR_ARR,
TYPE_SHORT
} ParamType;
typedef enum {
LIMITS_NONE = 0,
LIMITS_MIN = 1,
LIMITS_BOTH = 2
} LimitsType;
typedef enum {
SECTION_NONE,
SECTION_CAPTURE,
SECTION_QP,
SECTION_RC,
SECTION_ENCODE_PIC,
SECTION_ENCODE_PIC_H264,
SECTION_ENCODE_PIC_H265,
SECTION_MVC,
SECTION_PAYLOAD,
SECTION_2DPROCESSOR
} SectionType;
typedef struct {
SectionType secType;
const char *name;
unsigned int lastSectionIndex;
size_t sizeOfStruct;
} SectionMap;
typedef struct {
const char *paramName;
void *mappedLocation;
ParamType type;
double defaultValue;
LimitsType paramLimits;
double minLimit;
double maxLimit;
unsigned int stringLength; // string param size
unsigned int *stringLengthAddr; // address of string param size
SectionType sectionType;
} ConfigParamsMap;
NvMediaStatus ConfigParser_InitParamsMap(ConfigParamsMap *paramsMap);
NvMediaStatus ConfigParser_ParseFile(ConfigParamsMap *paramsMap, unsigned int numParams, SectionMap *sectionsMap, char *file);
NvMediaStatus ConfigParser_ValidateParams(ConfigParamsMap *paramsMap, SectionMap *sectionsMap);
NvMediaStatus ConfigParser_DisplayParams(ConfigParamsMap *paramsMap, SectionMap *sectionsMap);
NvMediaStatus ConfigParser_GetSectionIndexByName(SectionMap *sectionsMap, char *sectionName, unsigned int *index);
NvMediaStatus ConfigParser_GetSectionIndexByType(SectionMap *sectionsMap, SectionType sectionType, unsigned int *index);
#ifdef __cplusplus
}
#endif
#endif /* _NVMEDIA_TEST_CONFIG_PARSER_H_ */

View File

@@ -0,0 +1,808 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "image_utils.h"
#include "misc_utils.h"
#include "nvmedia_surface.h"
#define MAXM_NUM_SURFACES 6
typedef struct {
float heightFactor[6];
float widthFactor[6];
unsigned int numSurfaces;
} ImgUtilSurfParams;
ImgUtilSurfParams ImgSurfParamsTable_RGBA = {
.heightFactor = {1, 0, 0, 0, 0, 0},
.widthFactor = {1, 0, 0, 0, 0, 0},
.numSurfaces = 1,
};
ImgUtilSurfParams ImgSurfParamsTable_RAW = {
.heightFactor = {1, 0, 0, 0, 0, 0},
.widthFactor = {1, 0, 0, 0, 0, 0},
.numSurfaces = 1,
};
ImgUtilSurfParams ImgSurfParamsTable_YUV[][4] = {
{ /* PLANAR */
{ /* 420 */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 0.5, 0.5, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 422 */
.heightFactor = {1, 1, 1, 0, 0, 0},
.widthFactor = {1, 0.5, 0.5, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 444 */
.heightFactor = {1, 1, 1, 0, 0, 0},
.widthFactor = {1, 1, 1, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 422R */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 1, 1, 0, 0, 0},
.numSurfaces = 3,
},
},
{ /* SEMI_PLANAR */
{ /* 420 */
.heightFactor = {1, 0.5, 0, 0, 0, 0},
.widthFactor = {1, 0.5, 0, 0, 0, 0},
.numSurfaces = 2,
},
{ /* 422 */
.heightFactor = {1, 1, 0, 0, 0, 0},
.widthFactor = {1, 0.5, 0, 0, 0, 0},
.numSurfaces = 2,
},
{ /* 444 */
.heightFactor = {1, 1, 0.5, 0, 0, 0},
.widthFactor = {1, 1, 0.5, 0, 0, 0},
.numSurfaces = 2,
},
{ /* 422R */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 1, 0.5, 0, 0, 0},
.numSurfaces = 2,
},
},
{ /* PACKED */
{ /* 420 */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 0.5, 0.5, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 422 */
.heightFactor = {1, 1, 1, 0, 0, 0},
.widthFactor = {1, 0.5, 0.5, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 444 */
.heightFactor = {1, 1, 1, 0, 0, 0},
.widthFactor = {1, 1, 1, 0, 0, 0},
.numSurfaces = 3,
},
{ /* 422R */
.heightFactor = {1, 0.5, 0.5, 0, 0, 0},
.widthFactor = {1, 1, 1, 0, 0, 0},
.numSurfaces = 3,
},
},
};
ImgUtilSurfParams ImgSurfParamsTable_Packed = {
.heightFactor = {1, 0, 0, 0, 0, 0},
.widthFactor = {1, 0, 0, 0, 0, 0},
.numSurfaces = 1,
};
unsigned int ImgBytesPerPixelTable_RGBA[][6] = {
{4, 0, 0, 0, 0, 0}, /* 8 */
};
unsigned int ImgBytesPerPixelTable_RGBA16[][6] = {
{8, 0, 0, 0, 0, 0}, /* 16 */
};
unsigned int ImgBytesPerPixelTable_RG16[6] =
{4, 0, 0, 0, 0, 0};
unsigned int ImgBytesPerPixelTable_Alpha[][6] = {
{1, 0, 0, 0, 0, 0}, /* 8 */
{2, 0, 0, 0, 0, 0}, /* 10 */
{2, 0, 0, 0, 0, 0}, /* 12 */
{2, 0, 0, 0, 0, 0}, /* 14 */
{2, 0, 0, 0, 0, 0}, /* 16 */
{4, 0, 0, 0, 0, 0}, /* 32 */
};
unsigned int ImgBytesPerPixelTable_RAW[][6] = {
{1, 0, 0, 0, 0, 0}, /* 8 */
{2, 0, 0, 0, 0, 0}, /* 10 */
{2, 0, 0, 0, 0, 0}, /* 12 */
{2, 0, 0, 0, 0, 0}, /* 14 */
{2, 0, 0, 0, 0, 0}, /* 16 */
{4, 0, 0, 0, 0, 0}, /* 32 */
{4, 0, 0, 0, 0, 0}, /* 16_8_8 */
{4, 0, 0, 0, 0, 0}, /* 10_8_8 */
{4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
{4, 0, 0, 0, 0, 0}, /* 20 */
};
unsigned int ImgBytesPerPixelTable_YUV[][9][6] = {
{ /* PLANAR */
{1, 1, 1, 0, 0, 0}, /* 8 */
{2, 2, 2, 0, 0, 0}, /* 10 */
{2, 2, 2, 0, 0, 0}, /* 12 */
{2, 2, 2, 0, 0, 0}, /* 14 */
{2, 2, 2, 0, 0, 0}, /* 16 */
{4, 4, 4, 0, 0, 0}, /* 32 */
{2, 1, 1, 0, 0, 0}, /* 16_8_8 */
{2, 1, 1, 0, 0, 0}, /* 10_8_8 */
{4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
},
{ /* SEMI_PLANAR */
{1, 2, 0, 0, 0, 0}, /* 8 */
{2, 4, 0, 0, 0, 0}, /* 10 */
{2, 4, 0, 0, 0, 0}, /* 12 */
{2, 4, 0, 0, 0, 0}, /* 14 */
{2, 4, 0, 0, 0, 0}, /* 16 */
{4, 8, 0, 0, 0, 0}, /* 32 */
{2, 2, 0, 0, 0, 0}, /* 16_8_8 */
{2, 2, 0, 0, 0, 0}, /* 10_8_8 */
{4, 0, 0, 0, 0, 0}, /* 2_10_10_10 */
}
};
static NvMediaStatus
GetBytesPerCompForPackedYUV(unsigned int surfBPCidx,
unsigned int *bytespercomp
)
{
switch(surfBPCidx) {
case NVM_SURF_ATTR_BITS_PER_COMPONENT_8:
case NVM_SURF_ATTR_BITS_PER_COMPONENT_LAYOUT_2_10_10_10:
*bytespercomp = 1;
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_10:
case NVM_SURF_ATTR_BITS_PER_COMPONENT_12:
case NVM_SURF_ATTR_BITS_PER_COMPONENT_14:
case NVM_SURF_ATTR_BITS_PER_COMPONENT_16:
*bytespercomp = 2;
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_20:
*bytespercomp = 3;
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_32:
*bytespercomp = 4;
break;
default:
return NVMEDIA_STATUS_ERROR;
}
return NVMEDIA_STATUS_OK;
}
static NvMediaStatus
GetSurfParams(unsigned int surfaceType,
float **xScale,
float **yScale,
unsigned int **bytePerPixel,
uint32_t *numSurfacesVal)
{
NvMediaStatus status;
unsigned int surfType, surfMemoryType, surfSubSamplingType, surfBPC, surfCompOrder;
float *xScalePtr = NULL, *yScalePtr = NULL;
unsigned int *bytePerPixelPtr = NULL;
NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
uint32_t numSurfaces = 1;
static unsigned int yuvpackedtbl[6] = {1, 0, 0, 0, 0, 0};
unsigned int numcomps = 1;
status = NvMediaSurfaceFormatGetAttrs(surfaceType,
srcAttr,
NVM_SURF_FMT_ATTR_MAX);
if (status != NVMEDIA_STATUS_OK) {
printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
return NVMEDIA_STATUS_ERROR;
}
surfType = srcAttr[NVM_SURF_ATTR_SURF_TYPE].value;
surfMemoryType = srcAttr[NVM_SURF_ATTR_MEMORY].value;
surfSubSamplingType = srcAttr[NVM_SURF_ATTR_SUB_SAMPLING_TYPE].value;
surfBPC = srcAttr[NVM_SURF_ATTR_BITS_PER_COMPONENT].value;
surfCompOrder = srcAttr[NVM_SURF_ATTR_COMPONENT_ORDER].value;
switch(surfType) {
case NVM_SURF_ATTR_SURF_TYPE_YUV:
if (surfSubSamplingType == NVM_SURF_ATTR_SUB_SAMPLING_TYPE_NONE &&
surfMemoryType == NVM_SURF_ATTR_MEMORY_PACKED) {
xScalePtr = &ImgSurfParamsTable_Packed.widthFactor[0];
yScalePtr = &ImgSurfParamsTable_Packed.heightFactor[0];
numSurfaces = ImgSurfParamsTable_Packed.numSurfaces;
if (NVMEDIA_STATUS_OK != GetBytesPerCompForPackedYUV(surfBPC, &yuvpackedtbl[0])) {
printf("Invalid Bits per component and Packed YUV combination\n");
return NVMEDIA_STATUS_ERROR;
}
switch(surfCompOrder) {
case NVM_SURF_ATTR_COMPONENT_ORDER_VUYX:
case NVM_SURF_ATTR_COMPONENT_ORDER_XYUV:
case NVM_SURF_ATTR_COMPONENT_ORDER_XUYV:
numcomps = 4;
break;
case NVM_SURF_ATTR_COMPONENT_ORDER_UYVY:
case NVM_SURF_ATTR_COMPONENT_ORDER_VYUY:
case NVM_SURF_ATTR_COMPONENT_ORDER_YVYU:
case NVM_SURF_ATTR_COMPONENT_ORDER_YUYV:
numcomps = 2;
break;
case NVM_SURF_ATTR_COMPONENT_ORDER_LUMA:
numcomps = 1;
break;
default:
printf("Invalid component Order and Packed YUV combination\n");
return NVMEDIA_STATUS_ERROR;
}
yuvpackedtbl[0] = yuvpackedtbl[0] * numcomps;
bytePerPixelPtr = &yuvpackedtbl[0];
} else {
xScalePtr = &ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].widthFactor[0];
yScalePtr = &ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].heightFactor[0];
numSurfaces = ImgSurfParamsTable_YUV[0][surfSubSamplingType - NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420].numSurfaces;
bytePerPixelPtr = &ImgBytesPerPixelTable_YUV[0][surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
}
break;
case NVM_SURF_ATTR_SURF_TYPE_RGBA:
if (surfCompOrder == NVM_SURF_ATTR_COMPONENT_ORDER_ALPHA) {
bytePerPixelPtr = &ImgBytesPerPixelTable_Alpha[surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
} else if (surfCompOrder == NVM_SURF_ATTR_COMPONENT_ORDER_RG) {
if(surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_16) {
bytePerPixelPtr = &ImgBytesPerPixelTable_RG16[0];
} else {
printf("Invalid RGorder & Bitspercomp combination.Only RG16 is supported\n");
return NVMEDIA_STATUS_ERROR;
}
} else { /* RGBA, ARGB, BGRA */
if (surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_16) {
bytePerPixelPtr = &ImgBytesPerPixelTable_RGBA16[0][0];
} else if (surfBPC == NVM_SURF_ATTR_BITS_PER_COMPONENT_8) {
bytePerPixelPtr = &ImgBytesPerPixelTable_RGBA[0][0];
} else {
printf("RGBA orders with 8 and 16bits only is supported \n");
return NVMEDIA_STATUS_ERROR;
}
}
xScalePtr = &ImgSurfParamsTable_RGBA.widthFactor[0];
yScalePtr = &ImgSurfParamsTable_RGBA.heightFactor[0];
numSurfaces = ImgSurfParamsTable_RGBA.numSurfaces;
break;
case NVM_SURF_ATTR_SURF_TYPE_RAW:
bytePerPixelPtr = &ImgBytesPerPixelTable_RAW[surfBPC - NVM_SURF_ATTR_BITS_PER_COMPONENT_8][0];
xScalePtr = &ImgSurfParamsTable_RAW.widthFactor[0];
yScalePtr = &ImgSurfParamsTable_RAW.heightFactor[0];
numSurfaces = ImgSurfParamsTable_RAW.numSurfaces;
break;
default:
printf("%s: Unsupported Pixel Format %d", __func__, surfType);
return NVMEDIA_STATUS_ERROR;
}
if (xScale) {
*xScale = xScalePtr;
}
if (yScale) {
*yScale = yScalePtr;
}
if (bytePerPixel) {
*bytePerPixel = bytePerPixelPtr;
}
if (numSurfacesVal) {
*numSurfacesVal = numSurfaces;
}
return NVMEDIA_STATUS_OK;
}
NvMediaStatus
AllocateBufferToWriteImage(
Blit2DTest *ctx,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
NvMediaBool appendFlag)
{
uint32_t imageSize = 0;
unsigned int size[3] ={0};
uint8_t *buffer = NULL;
uint32_t i, k, newk = 0;
unsigned int *bytePerPixelPtr = NULL;
ctx->numSurfaces = 1;
NvMediaImageSurfaceMap surfaceMap;
NvMediaStatus status = NVMEDIA_STATUS_ERROR;
uint32_t lineWidth, numRows, startOffset;
if(!image) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock() failed\n", __func__);
return status;
}
NvMediaImageUnlock(image);
ctx->dstBuff = (uint8_t**) malloc(sizeof(uint8_t*)*MAXM_NUM_SURFACES);
if(!ctx->dstBuff) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
ctx->dstBuffPitches = (uint32_t*) calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
if(!ctx->dstBuffPitches) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
ctx->heightSurface = surfaceMap.height;
ctx->widthSurface = surfaceMap.width;
status = GetSurfParams(image->type,
&ctx->xScalePtr,
&ctx->yScalePtr,
&bytePerPixelPtr,
&ctx->numSurfaces);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: GetSurfParams() failed\n", __func__);
goto done;
}
imageSize = 0;
for(i = 0; i < ctx->numSurfaces; i++) {
size[i] = (ctx->widthSurface * ctx->xScalePtr[i] * ctx->heightSurface * ctx->yScalePtr[i] * bytePerPixelPtr[i]);
imageSize += size[i];
ctx->dstBuffPitches[i] = (uint32_t)((float)ctx->widthSurface * ctx->xScalePtr[i]) * bytePerPixelPtr[i];
}
// Embedded data size needs to be included for RAW surftype
size[0] += image->embeddedDataTopSize;
size[0] += image->embeddedDataBottomSize;
imageSize += image->embeddedDataTopSize;
imageSize += image->embeddedDataBottomSize;
buffer = (uint8_t *) calloc(1, imageSize);
if(!buffer) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
ctx->dstBuffer = buffer;
memset(buffer, 0xFF, imageSize);
for(i = 0; i < ctx->numSurfaces; i++) {
ctx->dstBuff[i] = buffer;
buffer = buffer + (uint32_t)(ctx->heightSurface * ctx->yScalePtr[i] * ctx->dstBuffPitches[i]);
}
done:
return status;
}
NvMediaStatus
WriteImageToAllocatedBuffer(
Blit2DTest *ctx,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
NvMediaBool appendFlag,
uint32_t bytesPerPixel)
{
NvMediaImageSurfaceMap surfaceMap;
NvMediaStatus status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock() failed\n", __func__);
goto done;
}
status = NvMediaImageGetBits(image, NULL, (void **)ctx->dstBuff, ctx->dstBuffPitches);
NvMediaImageUnlock(image);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaVideoSurfaceGetBits() failed \n", __func__);
goto done;
}
done:
return status;
}
static NvMediaStatus
ReadImageNew(
char *fileName,
uint32_t frameNum,
uint32_t width,
uint32_t height,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
uint32_t bytesPerPixel,
uint32_t pixelAlignment)
{
uint8_t **pBuff = NULL;
uint32_t *pBuffPitches = NULL;
uint32_t imageSize = 0,surfaceSize = 0;
uint8_t *buffer = NULL;
uint8_t *pBuffer = NULL;
uint32_t i, j, k, newk = 0;
float *xScalePtr = NULL, *yScalePtr = NULL;
unsigned int *bytePerPixelPtr = NULL;
uint32_t numSurfaces = 1;
unsigned int uHeightSurface, uWidthSurface;
NvMediaImageSurfaceMap surfaceMap;
NvMediaStatus status = NVMEDIA_STATUS_ERROR;
FILE *file = NULL;
unsigned int count, index;
NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
unsigned int surfType, surfBPC;
if(!image || !fileName) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock() failed\n", __func__);
return status;
}
NvMediaImageUnlock(image);
uHeightSurface = surfaceMap.height;
uWidthSurface = surfaceMap.width;
if(width > uWidthSurface || height > uHeightSurface) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
pBuff = (uint8_t **) malloc(sizeof(uint8_t*)*MAXM_NUM_SURFACES);
if(!pBuff) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
pBuffPitches = (uint32_t *)calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
if(!pBuffPitches) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
status = GetSurfParams(image->type,
&xScalePtr,
&yScalePtr,
&bytePerPixelPtr,
&numSurfaces);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: GetSurfParams() failed\n", __func__);
goto done;
}
status = NvMediaSurfaceFormatGetAttrs(image->type,
srcAttr,
NVM_SURF_FMT_ATTR_MAX);
if (status != NVMEDIA_STATUS_OK) {
printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
goto done;
}
surfType = srcAttr[NVM_SURF_ATTR_SURF_TYPE].value;
surfBPC = srcAttr[NVM_SURF_ATTR_BITS_PER_COMPONENT].value;
surfaceSize = 0;
imageSize = 0;
for(i = 0; i < numSurfaces; i++) {
surfaceSize += (uWidthSurface * xScalePtr[i] * uHeightSurface * yScalePtr[i] * bytePerPixelPtr[i]);
imageSize += (width * xScalePtr[i] * height * yScalePtr[i] * bytePerPixelPtr[i]);
pBuffPitches[i] = (uint32_t)((float)uWidthSurface * xScalePtr[i]) * bytePerPixelPtr[i];
}
buffer = (uint8_t *)calloc(1, surfaceSize);
if(!buffer) {
printf("%s: Out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
pBuffer = buffer;
memset(buffer,0x10,surfaceSize);
for(i = 0; i < numSurfaces; i++) {
pBuff[i] = buffer;
if (i) {
memset(pBuff[i], 0x80, (uHeightSurface * yScalePtr[i] * pBuffPitches[i]));
}
buffer = buffer + (uint32_t)(uHeightSurface * yScalePtr[i] * pBuffPitches[i]);
}
file = fopen(fileName, "rb");
if(!file) {
printf("%s: Error opening file: %s\n", __func__, fileName);
status = NVMEDIA_STATUS_ERROR;
goto done;
}
if(frameNum > 0) {
if(fseeko(file, frameNum * (off_t)imageSize, SEEK_SET)) {
printf("ReadImage: Error seeking file: %s\n", fileName);
status = NVMEDIA_STATUS_ERROR;
goto done;
}
}
if((surfType == NVM_SURF_ATTR_SURF_TYPE_RGBA ) && strstr(fileName, ".png")) {
printf("ReadImage: Does not support png format\n");
status = NVMEDIA_STATUS_ERROR;
goto done;
}
for(k = 0; k < numSurfaces; k++) {
for(j = 0; j < height*yScalePtr[k]; j++) {
newk = (!uvOrderFlag && k ) ? (numSurfaces - k) : k;
index = j * pBuffPitches[newk];
count = width * xScalePtr[newk] * bytePerPixelPtr[newk];
if (fread(pBuff[newk] + index, count, 1, file) != 1) {
status = NVMEDIA_STATUS_ERROR;
printf("ReadImage: Error reading file: %s\n", fileName);
goto done;
}
if((surfType == NVM_SURF_ATTR_SURF_TYPE_YUV) && (pixelAlignment == LSB_ALIGNED)) {
uint16_t *psrc = (uint16_t*)(pBuff[newk] + index);
switch(surfBPC) {
case NVM_SURF_ATTR_BITS_PER_COMPONENT_10:
for(i = 0; i < count/2; i++) {
*(psrc + i) = (*(psrc + i)) << (16 - 10);
}
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_12:
for(i = 0; i < count/2; i++) {
*(psrc + i) = (*(psrc + i)) << (16 - 12);
}
break;
case NVM_SURF_ATTR_BITS_PER_COMPONENT_14:
for(i = 0; i < count/2; i++) {
*(psrc + i) = (*(psrc + i)) << (16 - 14);
}
break;
default:
break;
}
}
}
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock() failed\n", __func__);
goto done;
}
status = NvMediaImagePutBits(image, NULL, (void **)pBuff, pBuffPitches);
NvMediaImageUnlock(image);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: Failed to put bits\n", __func__);
}
done:
if(pBuff) {
free(pBuff);
}
if (pBuffPitches) {
free(pBuffPitches);
}
if (pBuffer) {
free(pBuffer);
}
if(file) {
fclose(file);
}
return status;
}
NvMediaStatus
ReadImage(
char *fileName,
uint32_t frameNum,
uint32_t width,
uint32_t height,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
uint32_t bytesPerPixel,
uint32_t pixelAlignment)
{
NvMediaStatus status;
NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
status = NvMediaSurfaceFormatGetAttrs(image->type,
srcAttr,
NVM_SURF_FMT_ATTR_MAX);
if (status == NVMEDIA_STATUS_OK) {
return ReadImageNew(
fileName,
frameNum,
width,
height,
image,
uvOrderFlag,
bytesPerPixel,
pixelAlignment);
} else {
printf("%s:NvMediaSurfaceFormatGetAttrs failed\n", __func__);
return status;
}
}
NvMediaStatus
InitImage(
NvMediaImage *image,
uint32_t width,
uint32_t height)
{
uint8_t **pBuff = NULL;
uint32_t *pBuffPitches = NULL;
uint32_t imageSize = 0,surfaceSize = 0;
uint8_t *buffer = NULL;
uint8_t *pBuffer = NULL;
float *xScalePtr = NULL, *yScalePtr = NULL;
unsigned int *bytePerPixelPtr = NULL;
uint32_t numSurfaces = 1;
uint32_t i;
unsigned int uHeightSurface, uWidthSurface;
NvMediaImageSurfaceMap surfaceMap;
NvMediaStatus status = NVMEDIA_STATUS_ERROR;
NVM_SURF_FMT_DEFINE_ATTR(srcAttr);
if(!image) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock failed\n", __func__);
return status;
}
NvMediaImageUnlock(image);
uHeightSurface = surfaceMap.height;
uWidthSurface = surfaceMap.width;
if(width > uWidthSurface || height > uHeightSurface) {
printf("%s: Bad parameter\n", __func__);
return NVMEDIA_STATUS_BAD_PARAMETER;
}
pBuff = (uint8_t **) calloc(1,sizeof(uint8_t*)*MAXM_NUM_SURFACES);
if(!pBuff) {
printf("%s: out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
pBuffPitches = (uint32_t *) calloc(1,sizeof(uint32_t) * MAXM_NUM_SURFACES);
if(!pBuffPitches) {
printf("%s: out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
status = GetSurfParams(image->type,
&xScalePtr,
&yScalePtr,
&bytePerPixelPtr,
&numSurfaces);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: GetSurfParams failed\n", __func__);
goto done;
}
status = NvMediaSurfaceFormatGetAttrs(image->type,
srcAttr,
NVM_SURF_FMT_ATTR_MAX);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaSurfaceFormatGetAttrs failed\n", __func__);
goto done;
}
surfaceSize = 0;
imageSize = 0;
for(i = 0; i < numSurfaces; i++) {
surfaceSize += (uWidthSurface * xScalePtr[i] * uHeightSurface * yScalePtr[i] * bytePerPixelPtr[i]);
imageSize += (width * xScalePtr[i] * height * yScalePtr[i] * bytePerPixelPtr[i]);
pBuffPitches[i] = (uint32_t)((float)uWidthSurface * xScalePtr[i]) * bytePerPixelPtr[i];
}
buffer = (uint8_t *)calloc(1, surfaceSize);
if(!buffer) {
printf("%s: out of memory\n", __func__);
status = NVMEDIA_STATUS_OUT_OF_MEMORY;
goto done;
}
pBuffer = buffer;
memset(buffer,0x00,surfaceSize);
for(i = 0; i < numSurfaces; i++) {
pBuff[i] = buffer;
buffer = buffer + (uint32_t)(uHeightSurface * yScalePtr[i] * pBuffPitches[i]);
}
status = NvMediaImageLock(image, NVMEDIA_IMAGE_ACCESS_WRITE, &surfaceMap);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImageLock failed\n", __func__);
goto done;
}
status = NvMediaImagePutBits(image, NULL, (void **)pBuff, pBuffPitches);
NvMediaImageUnlock(image);
if(status != NVMEDIA_STATUS_OK) {
printf("%s: NvMediaImagePutBits failed\n", __func__);
}
done:
if(pBuff) {
free(pBuff);
}
if (pBuffPitches) {
free(pBuffPitches);
}
if (pBuffer) {
free(pBuffer);
}
return status;
}

View File

@@ -0,0 +1,141 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _NVMEDIA_TEST_IMAGE_UTILS_H_
#define _NVMEDIA_TEST_IMAGE_UTILS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "misc_utils.h"
#include "nvmedia_core.h"
#include "nvmedia_surface.h"
#include "nvmedia_image.h"
#include "cmdline.h"
#if (NV_IS_SAFETY == 1)
#include "nvmedia_image_internal.h"
#endif
#define PACK_RGBA(R, G, B, A) (((uint32_t)(A) << 24) | ((uint32_t)(B) << 16) | \
((uint32_t)(G) << 8) | (uint32_t)(R))
#define DEFAULT_ALPHA 0x80
// ReadImage
//
// ReadImage() Read image from file
//
// Arguments:
//
// filename
// (in) Input file name
//
// frameNum
// (in) Frame number to read. Use for stream input files.
//
// width
// (in) Surface width
//
// height
// (in) Surface height
//
// image
// (out) Pointer to pre-allocated output surface
//
// uvOrderFlag
// (in) Flag for UV order. If true - UV; If false - VU;
//
// bytesPerPixel
// (in) Bytes per pixel. Nedded for RAW image types handling.
// RAW8 - 1 byte per pixel
// RAW10, RAW12, RAW14 - 2 bytes per pixel
//
// pixelAlignment
// (in) Alignment of bits in pixel.
// 0 - LSB Aligned
// 1 - MSB Aligned
NvMediaStatus
ReadImage(
char *fileName,
uint32_t frameNum,
uint32_t width,
uint32_t height,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
uint32_t bytesPerPixel,
uint32_t pixelAlignment);
// InitImage
//
// InitImage() Init image data to zeros
//
// Arguments:
//
// image
// (in) image to initialize
//
// width
// (in) Surface width
//
// height
// (in) Surface height
NvMediaStatus
InitImage(
NvMediaImage *image,
uint32_t width,
uint32_t height);
NvMediaStatus
AllocateBufferToWriteImage(
Blit2DTest *ctx,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
NvMediaBool appendFlag);
// WriteImageToBuffer
//
// WriteImageToBuffer() Save RGB or YUV image
//
NvMediaStatus
WriteImageToAllocatedBuffer(
Blit2DTest *ctx,
NvMediaImage *image,
NvMediaBool uvOrderFlag,
NvMediaBool appendFlag,
uint32_t bytesPerPixel);
#ifdef __cplusplus
}
#endif
#endif /* _NVMEDIA_TEST_IMAGE_UTILS_H_ */

View File

@@ -0,0 +1,154 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#ifdef NVMEDIA_ANDROID
#define LOG_TAG "nvmedia_common"
#define LOG_NDEBUG 1
#include <utils/Log.h>
#endif
#ifdef NVMEDIA_QNX
#include <sys/slog.h>
#endif
#include "log_utils.h"
#ifdef NVMEDIA_QNX
#define NV_SLOGCODE 0xAAAA
#endif
#define MAX_STATS_LEN 500
#define LOG_BUFFER_BYTES 1024
static enum LogLevel msg_level = LEVEL_ERR;
static enum LogStyle msg_style = LOG_STYLE_NORMAL;
static FILE *msg_file = NULL;
void SetLogLevel(enum LogLevel level)
{
if (level > LEVEL_DBG)
return;
msg_level = level;
}
void SetLogStyle(enum LogStyle style)
{
if (style > LOG_STYLE_FUNCTION_LINE)
return;
msg_style = style;
}
void SetLogFile(FILE *logFileHandle)
{
if(!logFileHandle)
return;
msg_file = logFileHandle;
}
void LogLevelMessage(enum LogLevel level, const char *functionName,
int lineNumber, const char *format, ...)
{
va_list ap;
char str[LOG_BUFFER_BYTES] = {'\0',};
FILE *logFile = msg_file ? msg_file : stdout;
if (level > msg_level)
return;
#ifndef NVMEDIA_ANDROID
/** In the case of Android ADB log, if LOG_TAG is defined,
* before 'Log.h' is included in source file,
* LOG_TAG is automatically concatenated at the beginning of log message,
* so, we don't copy 'nvmedia: ' into 'str'.
*/
strcpy(str, "nvmedia: ");
/** As LOG_TAG is concatednated, log level is also automatically concatenated,
* by calling different ADB log function such as ALOGE(for eror log message),
* ALOGW(for warning log message).
*/
switch (level) {
case LEVEL_ERR:
strcat(str, "ERROR: ");
break;
case LEVEL_WARN:
strcat(str, "WARNING: ");
break;
case LEVEL_INFO:
case LEVEL_DBG:
// Empty
break;
}
#endif
va_start(ap, format);
vsnprintf(str + strlen(str), sizeof(str) - strlen(str), format, ap);
if(msg_style == LOG_STYLE_NORMAL) {
// Add trailing new line char
if(strlen(str) && str[strlen(str) - 1] != '\n')
strcat(str, "\n");
} else if(msg_style == LOG_STYLE_FUNCTION_LINE) {
// Remove trailing new line char
if(strlen(str) && str[strlen(str) - 1] == '\n')
str[strlen(str) - 1] = 0;
// Add function and line info
snprintf(str + + strlen(str), sizeof(str) - strlen(str), " at %s():%d\n", functionName, lineNumber);
}
#ifdef NVMEDIA_ANDROID
switch (msg_level) {
case LEVEL_ERR:
ALOGE("%s", str);
break;
case LEVEL_WARN:
ALOGW("%s", str);
break;
case LEVEL_INFO:
ALOGI("%s", str);
break;
case LEVEL_DBG:
ALOGD("%s", str);
break;
}
#else
fprintf(logFile, "%s", str);
#endif
#ifdef NVMEDIA_QNX
/* send to system logger */
slogf(_SLOG_SETCODE(NV_SLOGCODE, 0), _SLOG_ERROR, str);
#endif
va_end(ap);
}

View File

@@ -0,0 +1,121 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _NVMEDIA_TEST_LOG_UTILS_H_
#define _NVMEDIA_TEST_LOG_UTILS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <stdio.h>
enum LogLevel {
LEVEL_ERR = 0,
LEVEL_WARN = 1,
LEVEL_INFO = 2,
LEVEL_DBG = 3,
};
enum LogStyle {
LOG_STYLE_NORMAL = 0,
LOG_STYLE_FUNCTION_LINE
};
#define LINE_INFO __FUNCTION__, __LINE__
#define LOG_DBG(...) LogLevelMessage(LEVEL_DBG, LINE_INFO, __VA_ARGS__)
#define LOG_INFO(...) LogLevelMessage(LEVEL_INFO, LINE_INFO, __VA_ARGS__)
#define LOG_WARN(...) LogLevelMessage(LEVEL_WARN, LINE_INFO, __VA_ARGS__)
// SetLogLevel
//
// SetLogLevel() Set logging level
//
// Arguments:
//
// level
// (in) Logging level
void
SetLogLevel(
enum LogLevel level);
// SetLogStyle
//
// SetLogStyle() Set logging print slyle
//
// Arguments:
//
// level
// (in) Logging style
void
SetLogStyle(
enum LogStyle style);
// SetLogFile
//
// SetLogFile() Set logging file handle
//
// Arguments:
//
// level
// (in) Logging file handle
void
SetLogFile(
FILE *logFileHandle);
// LogLevelMessage
//
// LogLevelMessage() Print message if logging level is higher than message level
//
// Arguments:
//
// LogLevel
// (in) Message level
//
// format
// (in) Message format
//
// ...
// (in) Parameters list
void
LogLevelMessage(
enum LogLevel level,
const char *functionName,
int lineNumber,
const char *format,
...);
#ifdef __cplusplus
}
#endif
#endif /* _NVMEDIA_TEST_LOG_UTILS_H_ */

View File

@@ -0,0 +1,67 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#if defined (__QNX__)
#include <sys/time.h>
#endif
#include "misc_utils.h"
uint32_t
u32(const uint8_t* ptr)
{
return ptr[0] | (ptr[1]<<8) | (ptr[2]<<16) | (ptr[3]<<24);
}
NvMediaStatus
GetTimeMicroSec(
uint64_t *uTime)
{
struct timespec t;
#if !(defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 && _POSIX_TIMERS > 0)
struct timeval tv;
#endif
if(!uTime)
return NVMEDIA_STATUS_BAD_PARAMETER;
#if !(defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 && _POSIX_TIMERS > 0)
gettimeofday(&tv, NULL);
t.tv_sec = tv.tv_sec;
t.tv_nsec = tv.tv_usec*1000L;
#else
clock_gettime(CLOCK_MONOTONIC, &t);
#endif
*uTime = (uint64_t)t.tv_sec * 1000000LL + (uint64_t)t.tv_nsec / 1000LL;
return NVMEDIA_STATUS_OK;
}

View File

@@ -0,0 +1,77 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _NVMEDIA_TEST_MISC_UTILS_H_
#define _NVMEDIA_TEST_MISC_UTILS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "nvmedia_core.h"
#include "nvmedia_common.h"
#ifndef __INTEGRITY
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif
typedef enum {
LSB_ALIGNED,
MSB_ALIGNED
} PixelAlignment;
// u32
//
// u32() Reads 4 bytes from buffer and returns the read value
//
// Arguments:
//
// ptr
// (in) Input buffer
uint32_t u32(const uint8_t* ptr);
// GetTimeMicroSec
//
// GetTimeMicroSec() Returns current time in microseconds
//
// Arguments:
//
// uTime
// (out) Pointer to current time in microseconds
NvMediaStatus
GetTimeMicroSec(
uint64_t *uTime);
#ifdef __cplusplus
}
#endif
#endif /* _NVMEDIA_TEST_MISC_UTILS_H_ */

View File

@@ -0,0 +1,157 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "nvmedia_utils/cmdline.h"
#include <cuda.h>
#include <cuda_runtime.h>
#include "helper_cuda.h"
#include "nvsci_setup.h"
#include "nvmedia_2d_nvscisync.h"
#define checkNvSciErrors(call) \
do { \
NvSciError _status = call; \
if (NvSciError_Success != _status) { \
printf( \
"NVSCI call in file '%s' in line %i returned" \
" %d, expected %d\n", \
__FILE__, __LINE__, _status, NvSciError_Success); \
fflush(stdout); \
exit(EXIT_FAILURE); \
} \
} while (0)
void setupNvMediaSignalerNvSciSync(Blit2DTest *ctx, NvSciSyncObj &syncObj,
int cudaDeviceId) {
NvSciSyncModule sciSyncModule;
checkNvSciErrors(NvSciSyncModuleOpen(&sciSyncModule));
NvSciSyncAttrList signalerAttrList, waiterAttrList;
NvSciSyncAttrList syncUnreconciledList[2];
NvSciSyncAttrList syncReconciledList, syncConflictList;
checkNvSciErrors(NvSciSyncAttrListCreate(sciSyncModule, &signalerAttrList));
checkNvSciErrors(NvSciSyncAttrListCreate(sciSyncModule, &waiterAttrList));
NvMediaStatus status = NvMedia2DFillNvSciSyncAttrList(
ctx->i2d, signalerAttrList, NVMEDIA_SIGNALER);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMedia2DFillNvSciSyncAttrList failed\n", __func__);
exit(EXIT_FAILURE);
}
checkCudaErrors(cudaSetDevice(cudaDeviceId));
checkCudaErrors(cudaDeviceGetNvSciSyncAttributes(waiterAttrList, cudaDeviceId,
cudaNvSciSyncAttrWait));
syncUnreconciledList[0] = signalerAttrList;
syncUnreconciledList[1] = waiterAttrList;
checkNvSciErrors(NvSciSyncAttrListReconcile(
syncUnreconciledList, 2, &syncReconciledList, &syncConflictList));
checkNvSciErrors(NvSciSyncObjAlloc(syncReconciledList, &syncObj));
NvSciSyncAttrListFree(signalerAttrList);
NvSciSyncAttrListFree(waiterAttrList);
if (syncConflictList != nullptr) {
NvSciSyncAttrListFree(syncConflictList);
}
}
void setupCudaSignalerNvSciSync(Blit2DTest *ctx, NvSciSyncObj &syncObj,
int cudaDeviceId) {
NvSciSyncModule sciSyncModule;
checkNvSciErrors(NvSciSyncModuleOpen(&sciSyncModule));
NvSciSyncAttrList signalerAttrList, waiterAttrList;
NvSciSyncAttrList syncUnreconciledList[2];
NvSciSyncAttrList syncReconciledList, syncConflictList;
checkNvSciErrors(NvSciSyncAttrListCreate(sciSyncModule, &signalerAttrList));
checkNvSciErrors(NvSciSyncAttrListCreate(sciSyncModule, &waiterAttrList));
NvMediaStatus status =
NvMedia2DFillNvSciSyncAttrList(ctx->i2d, waiterAttrList, NVMEDIA_WAITER);
if (status != NVMEDIA_STATUS_OK) {
printf("%s: NvMedia2DFillNvSciSyncAttrList failed\n", __func__);
exit(EXIT_FAILURE);
}
checkCudaErrors(cudaSetDevice(cudaDeviceId));
checkCudaErrors(cudaDeviceGetNvSciSyncAttributes(
signalerAttrList, cudaDeviceId, cudaNvSciSyncAttrSignal));
syncUnreconciledList[0] = signalerAttrList;
syncUnreconciledList[1] = waiterAttrList;
checkNvSciErrors(NvSciSyncAttrListReconcile(
syncUnreconciledList, 2, &syncReconciledList, &syncConflictList));
checkNvSciErrors(NvSciSyncObjAlloc(syncReconciledList, &syncObj));
NvSciSyncAttrListFree(signalerAttrList);
NvSciSyncAttrListFree(waiterAttrList);
if (syncConflictList != nullptr) {
NvSciSyncAttrListFree(syncConflictList);
}
}
void setupNvSciBuf(NvSciBufObj &bufobj, NvSciBufAttrList &nvmediaAttrlist,
int cudaDeviceId) {
CUuuid devUUID;
NvSciBufAttrList conflictlist;
NvSciBufAttrList bufUnreconciledAttrlist[1];
CUresult res = cuDeviceGetUuid(&devUUID, cudaDeviceId);
if (res != CUDA_SUCCESS) {
fprintf(stderr, "Driver API error = %04d \n", res);
exit(EXIT_FAILURE);
}
NvSciBufAttrKeyValuePair attr_gpuid[] = {NvSciBufGeneralAttrKey_GpuId,
&devUUID, sizeof(devUUID)};
// set CUDA GPU ID to attribute list
checkNvSciErrors(NvSciBufAttrListSetAttrs(
nvmediaAttrlist, attr_gpuid,
sizeof(attr_gpuid) / sizeof(NvSciBufAttrKeyValuePair)));
bufUnreconciledAttrlist[0] = nvmediaAttrlist;
checkNvSciErrors(NvSciBufAttrListReconcileAndObjAlloc(
bufUnreconciledAttrlist, 1, &bufobj, &conflictlist));
if (conflictlist != NULL) {
NvSciBufAttrListFree(conflictlist);
}
}
void cleanupNvSciBuf(NvSciBufObj &Bufobj) {
if (Bufobj != NULL) {
NvSciBufObjFree(Bufobj);
}
}
void cleanupNvSciSync(NvSciSyncObj &syncObj) {
if (NvSciSyncObjFree != NULL) {
NvSciSyncObjFree(syncObj);
}
}

View File

@@ -0,0 +1,42 @@
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __NVSCI_SETUP_H__
#define __NVSCI_SETUP_H__
#include "nvmedia_utils/cmdline.h"
#include <nvscibuf.h>
#include <nvscisync.h>
void setupNvMediaSignalerNvSciSync(Blit2DTest *ctx, NvSciSyncObj &syncObj,
int cudaDeviceId);
void setupCudaSignalerNvSciSync(Blit2DTest *ctx, NvSciSyncObj &syncObj,
int cudaDeviceId);
void setupNvSciBuf(NvSciBufObj &bufobj, NvSciBufAttrList &nvmediaAttrlist,
int cudaDeviceId);
void cleanupNvSciBuf(NvSciBufObj &Bufobj);
void cleanupNvSciSync(NvSciSyncObj &syncObj);
#endif

View File

@@ -0,0 +1,198 @@
###################################################################################################
#
# Copyright (c) 2022, NVIDIA CORPORATION. All Rights Reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
#
# 2D configuration file for nvimg_2d application.
#
# Plese see nvmedia_2d.h for detailed information about parameters and datatypes
#
###################################################################################################
###################################################################################################
# Top level application parameters
#
# inputFile name limited to 1024 chars
inputfile = "teapot.rgba"
# The following bit-masks can be ORed:
# 1 => If the user wants to enable Filtering mode
# 2 => Reserved for future use
# 4 => If the user wants to enable Transformation mode
# 8 => If the user wants to enable color space conversion standard
validOperations = 0
# Transformation Mode for Blit2D
# 0 => IDENTITY
# 1 => ROTATE_90
# 2 => ROTATE_180
# 3 => ROTATE_270
# 4 => FLIP_HORIZONTAL
# 5 => INV_TRANSPOSE
# 6 => FLIP_VERTICAL
# 7 => TRANSPOSE
transformMode = 0
# Filtering mode for Blit2D
# 1 => FILTER_OFF
# 2 => FILTER_LOW
# 3 => FILTER_MEDIUM
# 4 => FILTER_HIGH
filterMode = 1
# color space conversion standard
# 0 => ITU BT.601
# 1 => ITU BT.709
# 2 => SMTE 240M
# 3 => BT.601 Extended Range
colorStd = 0
####################### source image properties##########################
srcWidth = 1024
srcHeight = 1024
# 1 => Uncached (mapped) access type flag
# 2 => Cached (mapped) access type flag
# 3 => Unmapped access type flag
srcCPUAccess = 3
# Allocation type
# 0 => none
# 1 => isochronous
srcAllocType = 1
# Surface scan type
# Only progressive scan is supported
# 1 => Progressive
srcScanType = 1
# Color Standard type
# 1 => sRGB
# 2 => YCbCr Rec.601 (Studio Range)
# 3 => YCbCr Rec.601 (Extended Range)
# 4 => YCbCr Rec.709 (Studio Range)
# 5 => YCbCr Rec.709 (Extended Range)
# 11 => Sensor RGBA
srcColorStd = 1
# 1 => YUV
# 2 => RGBA
# 3 => RAW
srcSurfType = 2
# 1 => Block Linear
# 2 => Pitch Linear
srcLayout = 2
# 1 => uint
# 2 => int
srcDataType = 1
# 1 => planar
# 2 => Semi Planar
# 3 => Packed
srcMemory = 3
# Sub-Sampling type of the input surface
# 1 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420
# 2 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_422
# 3 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_444
# 4 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_422R
# 0 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_NONE
srcSubSamplingType = 0
# Bits per component of the input surface
# 1 => NVM_SURF_ATTR_BITS_PER_COMPONENT_8
# 2 => NVM_SURF_ATTR_BITS_PER_COMPONENT_10
# 3 => NVM_SURF_ATTR_BITS_PER_COMPONENT_12
# 5 => NVM_SURF_ATTR_BITS_PER_COMPONENT_16
srcBitsPerComponent = 1
# 1 => NVM_SURF_ATTR_COMPONENT_ORDER_LUMA
# 2 => NVM_SURF_ATTR_COMPONENT_ORDER_YUV
# 3 => NVM_SURF_ATTR_COMPONENT_ORDER_YVU
# 4 => NVM_SURF_ATTR_COMPONENT_ORDER_YUYV
# 5 => NVM_SURF_ATTR_COMPONENT_ORDER_YVYU
# 6 => NVM_SURF_ATTR_COMPONENT_ORDER_VYUY
# 10 => NVM_SURF_ATTR_COMPONENT_ORDER_VUYX
# 18 => NVM_SURF_ATTR_COMPONENT_ORDER_RGBA
# 20 => NVM_SURF_ATTR_COMPONENT_ORDER_BGRA
srcComponentOrder = 18
# srcRect Structure containing co-ordinates of the rectangle in the source image.
# Left X co-ordinate.
srcRectx0 = 0
# Top Y co-ordinate.
srcRecty0 = 0
# Right X co-ordinate.
srcRectx1 = 1024
# Bottom Y co-ordinate.
srcRecty1 = 1024
####################### output image properties##########################
dstWidth = 1024
dstHeight = 1024
# 1 => Uncached (mapped) access type flag
# 2 => Cached (mapped) access type flag
# 3 => Unmapped access type flag
dstCPUAccess = 3
# Allocation type
# 0 => none
# 1 => isochronous
dstAllocType = 1
# Surface scan type
# Only progressive scan is supported
# 1 => Progressive
dstScanType = 1
# Color Standard type
# 1 => sRGB
# 2 => YCbCr Rec.601 (Studio Range)
# 3 => YCbCr Rec.601 (Extended Range)
# 4 => YCbCr Rec.709 (Studio Range)
# 5 => YCbCr Rec.709 (Extended Range)
# 11 => Sensor RGBA
dstColorStd = 2
# 1 => YUV
# 2 => RGBA
# 3 => RAW
dstSurfType = 1
# 1 => Block Linear
# 2 => Pitch Linear
dstLayout = 1
# 1 => uint
# 2 => int
dstDataType = 1
# 1 => planar
# 2 => Semi Planar
# 3 => Packed
dstMemory = 1
# Sub-Sampling type of the output surface
# 1 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_420
# 2 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_422
# 3 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_444
# 4 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_422R
# 0 => NVM_SURF_ATTR_SUB_SAMPLING_TYPE_NONE
dstSubSamplingType = 1
# Bits per component of the output surface
# 1 => NVM_SURF_ATTR_BITS_PER_COMPONENT_8
# 2 => NVM_SURF_ATTR_BITS_PER_COMPONENT_10
# 3 => NVM_SURF_ATTR_BITS_PER_COMPONENT_12
# 5 => NVM_SURF_ATTR_BITS_PER_COMPONENT_16
dstBitsPerComponent = 1
# 1 => NVM_SURF_ATTR_COMPONENT_ORDER_LUMA
# 2 => NVM_SURF_ATTR_COMPONENT_ORDER_YUV
# 3 => NVM_SURF_ATTR_COMPONENT_ORDER_YVU
# 4 => NVM_SURF_ATTR_COMPONENT_ORDER_YUYV
# 5 => NVM_SURF_ATTR_COMPONENT_ORDER_YVYU
# 6 => NVM_SURF_ATTR_COMPONENT_ORDER_VYUY
# 10 => NVM_SURF_ATTR_COMPONENT_ORDER_VUYX
# 18 => NVM_SURF_ATTR_COMPONENT_ORDER_RGBA
# 20 => NVM_SURF_ATTR_COMPONENT_ORDER_BGRA
dstComponentOrder = 2
# dstRect Structure containing co-ordinates of the rectangle in the destination image.
# Left X co-ordinate.
dstRectx0 = 0
# Top Y co-ordinate.
dstRecty0 = 0
# Right X co-ordinate.
dstRectx1 = 1024
# Bottom Y co-ordinate.
dstRecty1 = 1024