add and update samples for CUDA 11.6

This commit is contained in:
Rutwik Choughule
2022-01-13 11:35:24 +05:30
parent 11de19f00c
commit 2e41896e1b
4615 changed files with 178150 additions and 181350 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}/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,454 @@
################################################################################
# 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
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
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)
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
endif
# Gencode arguments
ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),armv7l aarch64 sbsa))
SMS ?= 53 61 70 72 75 80 86 87
else
SMS ?= 35 37 50 52 60 61 70 75 80 86
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 += -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,97 @@
<?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>cudaGetMipmappedArrayLevel</toolkit>
<toolkit>cudaImportNvSciImage</toolkit>
<toolkit>cudaImportExternalSemaphore</toolkit>
<toolkit>cudaNvSciApp</toolkit>
<toolkit>cudaStreamCreateWithFlags</toolkit>
<toolkit>cudaExternalMemoryGetMappedMipmappedArray</toolkit>
<toolkit>cudaNvSciWait</toolkit>
<toolkit>cudaDestroyExternalMemory</toolkit>
<toolkit>cudaMemcpyAsync</toolkit>
<toolkit>cudaStreamDestroy</toolkit>
<toolkit>cudaSignalExternalSemaphoresAsync</toolkit>
<toolkit>cudaDeviceGetNvSciSyncAttributes</toolkit>
<toolkit>cudaFreeMipmappedArray</toolkit>
<toolkit>cudaMallocHost</toolkit>
<toolkit>cudaNvSci</toolkit>
<toolkit>cudaImportExternalMemory</toolkit>
<toolkit>cudaSetDevice</toolkit>
<toolkit>cudaImportNvSciRawBuf</toolkit>
<toolkit>cudaImportNvSciSemaphore</toolkit>
<toolkit>cudaGetDeviceCount</toolkit>
<toolkit>cudaDestroyTextureObject</toolkit>
<toolkit>cudaDeviceGetAttribute</toolkit>
<toolkit>cudaDestroyExternalSemaphore</toolkit>
<toolkit>cudaStreamSynchronize</toolkit>
<toolkit>cudaNvSciSignal</toolkit>
<toolkit>cudaFree</toolkit>
<toolkit>cudaDeviceId</toolkit>
<toolkit>cudaExternalMemoryGetMappedBuffer</toolkit>
<toolkit>cudaCreateTextureObject</toolkit>
<toolkit>cudaFreeHost</toolkit>
<toolkit>cudaWaitExternalSemaphoresAsync</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; 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>
</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>
<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; 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)
## 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)
cudaGetMipmappedArrayLevel, cudaImportNvSciImage, cudaImportExternalSemaphore, cudaNvSciApp, cudaStreamCreateWithFlags, cudaExternalMemoryGetMappedMipmappedArray, cudaNvSciWait, cudaDestroyExternalMemory, cudaMemcpyAsync, cudaStreamDestroy, cudaSignalExternalSemaphoresAsync, cudaDeviceGetNvSciSyncAttributes, cudaFreeMipmappedArray, cudaMallocHost, cudaNvSci, cudaImportExternalMemory, cudaSetDevice, cudaImportNvSciRawBuf, cudaImportNvSciSemaphore, cudaGetDeviceCount, cudaDestroyTextureObject, cudaDeviceGetAttribute, cudaDestroyExternalSemaphore, cudaStreamSynchronize, cudaNvSciSignal, cudaFree, cudaDeviceId, cudaExternalMemoryGetMappedBuffer, cudaCreateTextureObject, cudaFreeHost, cudaWaitExternalSemaphoresAsync
## Dependencies needed to build/run
[NVSCI](../../README.md#nvsci)
## Prerequisites
Download and install the [CUDA Toolkit 11.6](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,601 @@
/* 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);
CUresult res = cuDeviceGetUuid(&m_devUUID, m_cudaDeviceId);
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));
CUresult res = cuDeviceGetUuid(&m_devUUID, m_cudaDeviceId);
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,138 @@
################################################################################
# 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 $$?)
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
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(&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;
}

Binary file not shown.