mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-23 21:28:17 +08:00
add and update samples for CUDA 11.6
This commit is contained in:
18
Samples/3_CUDA_Features/cdpAdvancedQuicksort/.vscode/c_cpp_properties.json
vendored
Normal file
18
Samples/3_CUDA_Features/cdpAdvancedQuicksort/.vscode/c_cpp_properties.json
vendored
Normal 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
|
||||
}
|
||||
7
Samples/3_CUDA_Features/cdpAdvancedQuicksort/.vscode/extensions.json
vendored
Normal file
7
Samples/3_CUDA_Features/cdpAdvancedQuicksort/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
10
Samples/3_CUDA_Features/cdpAdvancedQuicksort/.vscode/launch.json
vendored
Normal file
10
Samples/3_CUDA_Features/cdpAdvancedQuicksort/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "CUDA C++: Launch",
|
||||
"type": "cuda-gdb",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/cdpAdvancedQuicksort"
|
||||
}
|
||||
]
|
||||
}
|
||||
15
Samples/3_CUDA_Features/cdpAdvancedQuicksort/.vscode/tasks.json
vendored
Normal file
15
Samples/3_CUDA_Features/cdpAdvancedQuicksort/.vscode/tasks.json
vendored
Normal 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
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
376
Samples/3_CUDA_Features/cdpAdvancedQuicksort/Makefile
Normal file
376
Samples/3_CUDA_Features/cdpAdvancedQuicksort/Makefile
Normal file
@@ -0,0 +1,376 @@
|
||||
################################################################################
|
||||
# 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))
|
||||
|
||||
SAMPLE_ENABLED := 1
|
||||
|
||||
# This sample is not supported on QNX
|
||||
ifeq ($(TARGET_OS),qnx)
|
||||
$(info >>> WARNING - cdpAdvancedQuicksort is not supported on QNX - 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++14
|
||||
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)` \>= 50000)
|
||||
|
||||
ifeq ($(IS_MIN_VERSION), 1)
|
||||
$(info >>> GCC Version is greater or equal to 5.0.0 <<<)
|
||||
else
|
||||
$(info >>> Waiving build. Minimum GCC version required is 5.0.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
|
||||
|
||||
ALL_CCFLAGS += -dc -maxrregcount=64 --std=c++14 --threads 0
|
||||
|
||||
LIBRARIES += -lcudadevrt
|
||||
|
||||
ifeq ($(SAMPLE_ENABLED),0)
|
||||
EXEC ?= @echo "[@]"
|
||||
endif
|
||||
|
||||
################################################################################
|
||||
|
||||
# Target rules
|
||||
all: build
|
||||
|
||||
build: cdpAdvancedQuicksort
|
||||
|
||||
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
|
||||
|
||||
cdpAdvancedQuicksort.o:cdpAdvancedQuicksort.cu
|
||||
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
|
||||
|
||||
cdpBitonicSort.o:cdpBitonicSort.cu
|
||||
$(EXEC) $(NVCC) $(INCLUDES) $(ALL_CCFLAGS) $(GENCODE_FLAGS) -o $@ -c $<
|
||||
|
||||
cdpAdvancedQuicksort: cdpAdvancedQuicksort.o cdpBitonicSort.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) ./cdpAdvancedQuicksort
|
||||
|
||||
testrun: build
|
||||
|
||||
clean:
|
||||
rm -f cdpAdvancedQuicksort cdpAdvancedQuicksort.o cdpBitonicSort.o
|
||||
rm -rf ../../../bin/$(TARGET_ARCH)/$(TARGET_OS)/$(BUILD_TYPE)/cdpAdvancedQuicksort
|
||||
|
||||
clobber: clean
|
||||
@@ -0,0 +1,94 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE entry SYSTEM "SamplesInfo.dtd">
|
||||
<entry>
|
||||
<name>cdpAdvancedQuicksort</name>
|
||||
<cflags>
|
||||
<flag>-dc</flag>
|
||||
<flag>-maxrregcount=64</flag>
|
||||
<flag>--std=c++14</flag>
|
||||
</cflags>
|
||||
<cuda_api_list>
|
||||
<toolkit>cudaMemset</toolkit>
|
||||
<toolkit>cudaFree</toolkit>
|
||||
<toolkit>cudaEventRecord</toolkit>
|
||||
<toolkit>cudaEventCreate</toolkit>
|
||||
<toolkit>cudaEventElapsedTime</toolkit>
|
||||
<toolkit>cudaDeviceSynchronize</toolkit>
|
||||
<toolkit>cudaPeekAtLastError</toolkit>
|
||||
<toolkit>cudaMalloc</toolkit>
|
||||
<toolkit>cudaStreamCreateWithFlags</toolkit>
|
||||
<toolkit>cudaGetLastError</toolkit>
|
||||
<toolkit>cudaMemcpyAsync</toolkit>
|
||||
<toolkit>cudaMemcpy</toolkit>
|
||||
<toolkit>cudaGetErrorString</toolkit>
|
||||
<toolkit>cudaGetDeviceProperties</toolkit>
|
||||
</cuda_api_list>
|
||||
<description><![CDATA[This sample demonstrates an advanced quicksort implemented using CUDA Dynamic Parallelism. This sample requires devices with compute capability 3.5 or higher.]]></description>
|
||||
<includepaths>
|
||||
<path>./</path>
|
||||
<path>../</path>
|
||||
<path>../../../Common</path>
|
||||
</includepaths>
|
||||
<keyconcepts>
|
||||
<concept level="basic">Cooperative Groups</concept>
|
||||
<concept level="advanced">CUDA Dynamic Parallelism</concept>
|
||||
</keyconcepts>
|
||||
<keywords>
|
||||
<keyword>GPGPU</keyword>
|
||||
<keyword>CPP14</keyword>
|
||||
</keywords>
|
||||
<libraries>
|
||||
<library>cudadevrt</library>
|
||||
</libraries>
|
||||
<librarypaths>
|
||||
</librarypaths>
|
||||
<nsight_eclipse>true</nsight_eclipse>
|
||||
<primary_file>cdpAdvancedQuicksort.cu</primary_file>
|
||||
<required_dependencies>
|
||||
<dependency>CDP</dependency>
|
||||
</required_dependencies>
|
||||
<scopes>
|
||||
<scope>1:CUDA Advanced Topics</scope>
|
||||
</scopes>
|
||||
<sm-arch>sm35</sm-arch>
|
||||
<sm-arch>sm37</sm-arch>
|
||||
<sm-arch>sm50</sm-arch>
|
||||
<sm-arch>sm52</sm-arch>
|
||||
<sm-arch>sm53</sm-arch>
|
||||
<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>
|
||||
<arch>x86_64</arch>
|
||||
<platform>linux</platform>
|
||||
</env>
|
||||
<env>
|
||||
<platform>windows7</platform>
|
||||
</env>
|
||||
<env>
|
||||
<arch>x86_64</arch>
|
||||
<platform>macosx</platform>
|
||||
</env>
|
||||
<env>
|
||||
<arch>arm</arch>
|
||||
</env>
|
||||
<env>
|
||||
<arch>sbsa</arch>
|
||||
</env>
|
||||
<env>
|
||||
<arch>ppc64le</arch>
|
||||
<platform>linux</platform>
|
||||
</env>
|
||||
</supported_envs>
|
||||
<supported_sm_architectures>
|
||||
<from>3.5</from>
|
||||
</supported_sm_architectures>
|
||||
<title>Advanced Quicksort (CUDA Dynamic Parallelism)</title>
|
||||
<type>exe</type>
|
||||
</entry>
|
||||
74
Samples/3_CUDA_Features/cdpAdvancedQuicksort/README.md
Normal file
74
Samples/3_CUDA_Features/cdpAdvancedQuicksort/README.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# cdpAdvancedQuicksort - Advanced Quicksort (CUDA Dynamic Parallelism)
|
||||
|
||||
## Description
|
||||
|
||||
This sample demonstrates an advanced quicksort implemented using CUDA Dynamic Parallelism. This sample requires devices with compute capability 3.5 or higher.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Cooperative Groups, CUDA Dynamic Parallelism
|
||||
|
||||
## Supported SM Architectures
|
||||
|
||||
[SM 3.5 ](https://developer.nvidia.com/cuda-gpus) [SM 3.7 ](https://developer.nvidia.com/cuda-gpus) [SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [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, Windows
|
||||
|
||||
## Supported CPU Architecture
|
||||
|
||||
x86_64, ppc64le, armv7l
|
||||
|
||||
## CUDA APIs involved
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaMemset, cudaFree, cudaEventRecord, cudaEventCreate, cudaEventElapsedTime, cudaDeviceSynchronize, cudaPeekAtLastError, cudaMalloc, cudaStreamCreateWithFlags, cudaGetLastError, cudaMemcpyAsync, cudaMemcpy, cudaGetErrorString, cudaGetDeviceProperties
|
||||
|
||||
## Dependencies needed to build/run
|
||||
[CDP](../../README.md#cdp)
|
||||
|
||||
## 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
|
||||
|
||||
### Windows
|
||||
The Windows samples are built using the Visual Studio IDE. Solution files (.sln) are provided for each supported version of Visual Studio, using the format:
|
||||
```
|
||||
*_vs<version>.sln - for Visual Studio <version>
|
||||
```
|
||||
Each individual sample has its own set of solution files in its directory:
|
||||
|
||||
To build/examine all the samples at once, the complete solution files should be used. To build/examine a single sample, the individual sample solution files should be used.
|
||||
> **Note:** Some samples require that the Microsoft DirectX SDK (June 2010 or newer) be installed and that the VC++ directory paths are properly set up (**Tools > Options...**). Check DirectX Dependencies section for details."
|
||||
|
||||
### 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, ppc64le, armv7l.
|
||||
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=ppc64le` <br/> `$ make TARGET_ARCH=armv7l` <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)
|
||||
|
||||
@@ -0,0 +1,583 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// cdpAdvancedQuicksort.cu
|
||||
//
|
||||
// Implementation of a parallel quicksort in CUDA. It comes in
|
||||
// several parts:
|
||||
//
|
||||
// 1. A small-set insertion sort. We do this on any set with <=32 elements
|
||||
// 2. A partitioning kernel, which - given a pivot - separates an input
|
||||
// array into elements <=pivot, and >pivot. Two quicksorts will then
|
||||
// be launched to resolve each of these.
|
||||
// 3. A quicksort co-ordinator, which figures out what kernels to launch
|
||||
// and when.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include <thrust/random.h>
|
||||
#include <thrust/device_vector.h>
|
||||
#include <cooperative_groups.h>
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_string.h>
|
||||
#include "cdpQuicksort.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Inline PTX call to return index of highest non-zero bit in a word
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static __device__ __forceinline__ unsigned int __qsflo(unsigned int word) {
|
||||
unsigned int ret;
|
||||
asm volatile("bfind.u32 %0, %1;" : "=r"(ret) : "r"(word));
|
||||
return ret;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ringbufAlloc
|
||||
//
|
||||
// Allocates from a ringbuffer. Allows for not failing when we run out
|
||||
// of stack for tracking the offset counts for each sort subsection.
|
||||
//
|
||||
// We use the atomicMax trick to allow out-of-order retirement. If we
|
||||
// hit the size limit on the ringbuffer, then we spin-wait for people
|
||||
// to complete.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
static __device__ T *ringbufAlloc(qsortRingbuf *ringbuf) {
|
||||
// Wait for there to be space in the ring buffer. We'll retry only a fixed
|
||||
// number of times and then fail, to avoid an out-of-memory deadlock.
|
||||
unsigned int loop = 10000;
|
||||
|
||||
while (((ringbuf->head - ringbuf->tail) >= ringbuf->stacksize) &&
|
||||
(loop-- > 0))
|
||||
;
|
||||
|
||||
if (loop == 0) return NULL;
|
||||
|
||||
// Note that the element includes a little index book-keeping, for freeing
|
||||
// later.
|
||||
unsigned int index = atomicAdd((unsigned int *)&ringbuf->head, 1);
|
||||
T *ret = (T *)(ringbuf->stackbase) + (index & (ringbuf->stacksize - 1));
|
||||
ret->index = index;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ringBufFree
|
||||
//
|
||||
// Releases an element from the ring buffer. If every element is released
|
||||
// up to and including this one, we can advance the tail to indicate that
|
||||
// space is now available.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
static __device__ void ringbufFree(qsortRingbuf *ringbuf, T *data) {
|
||||
unsigned int index = data->index; // Non-wrapped index to free
|
||||
unsigned int count = atomicAdd((unsigned int *)&(ringbuf->count), 1) + 1;
|
||||
unsigned int max = atomicMax((unsigned int *)&(ringbuf->max), index + 1);
|
||||
|
||||
// Update the tail if need be. Note we update "max" to be the new value in
|
||||
// ringbuf->max
|
||||
if (max < (index + 1)) max = index + 1;
|
||||
|
||||
if (max == count) atomicMax((unsigned int *)&(ringbuf->tail), count);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// qsort_warp
|
||||
//
|
||||
// Simplest possible implementation, does a per-warp quicksort with no
|
||||
// inter-warp
|
||||
// communication. This has a high atomic issue rate, but the rest should
|
||||
// actually
|
||||
// be fairly quick because of low work per thread.
|
||||
//
|
||||
// A warp finds its section of the data, then writes all data <pivot to one
|
||||
// buffer and all data >pivot to the other. Atomics are used to get a unique
|
||||
// section of the buffer.
|
||||
//
|
||||
// Obvious optimisation: do multiple chunks per warp, to increase in-flight
|
||||
// loads
|
||||
// and cover the instruction overhead.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void qsort_warp(unsigned *indata, unsigned *outdata,
|
||||
unsigned int offset, unsigned int len,
|
||||
qsortAtomicData *atomicData,
|
||||
qsortRingbuf *atomicDataStack,
|
||||
unsigned int source_is_indata, unsigned int depth) {
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
// Find my data offset, based on warp ID
|
||||
unsigned int thread_id = threadIdx.x + (blockIdx.x << QSORT_BLOCKSIZE_SHIFT);
|
||||
// unsigned int warp_id = threadIdx.x >> 5; // Used for debug only
|
||||
unsigned int lane_id = threadIdx.x & (warpSize - 1);
|
||||
|
||||
// Exit if I'm outside the range of sort to be done
|
||||
if (thread_id >= len) return;
|
||||
|
||||
//
|
||||
// First part of the algorithm. Each warp counts the number of elements that
|
||||
// are
|
||||
// greater/less than the pivot.
|
||||
//
|
||||
// When a warp knows its count, it updates an atomic counter.
|
||||
//
|
||||
|
||||
// Read in the data and the pivot. Arbitrary pivot selection for now.
|
||||
unsigned pivot = indata[offset + len / 2];
|
||||
unsigned data = indata[offset + thread_id];
|
||||
|
||||
// Count how many are <= and how many are > pivot.
|
||||
// If all are <= pivot then we adjust the comparison
|
||||
// because otherwise the sort will move nothing and
|
||||
// we'll iterate forever.
|
||||
cg::coalesced_group active = cg::coalesced_threads();
|
||||
unsigned int greater = (data > pivot);
|
||||
unsigned int gt_mask = active.ballot(greater);
|
||||
|
||||
if (gt_mask == 0) {
|
||||
greater = (data >= pivot);
|
||||
gt_mask = active.ballot(greater); // Must re-ballot for adjusted comparator
|
||||
}
|
||||
|
||||
unsigned int lt_mask = active.ballot(!greater);
|
||||
unsigned int gt_count = __popc(gt_mask);
|
||||
unsigned int lt_count = __popc(lt_mask);
|
||||
|
||||
// Atomically adjust the lt_ and gt_offsets by this amount. Only one thread
|
||||
// need do this. Share the result using shfl
|
||||
unsigned int lt_offset, gt_offset;
|
||||
|
||||
if (lane_id == 0) {
|
||||
if (lt_count > 0)
|
||||
lt_offset = atomicAdd((unsigned int *)&atomicData->lt_offset, lt_count);
|
||||
|
||||
if (gt_count > 0)
|
||||
gt_offset =
|
||||
len - (atomicAdd((unsigned int *)&atomicData->gt_offset, gt_count) +
|
||||
gt_count);
|
||||
}
|
||||
|
||||
lt_offset =
|
||||
active.shfl((int)lt_offset, 0); // Everyone pulls the offsets from lane 0
|
||||
gt_offset = active.shfl((int)gt_offset, 0);
|
||||
|
||||
// Now compute my own personal offset within this. I need to know how many
|
||||
// threads with a lane ID less than mine are going to write to the same buffer
|
||||
// as me. We can use popc to implement a single-operation warp scan in this
|
||||
// case.
|
||||
unsigned lane_mask_lt;
|
||||
asm("mov.u32 %0, %%lanemask_lt;" : "=r"(lane_mask_lt));
|
||||
unsigned int my_mask = greater ? gt_mask : lt_mask;
|
||||
unsigned int my_offset = __popc(my_mask & lane_mask_lt);
|
||||
|
||||
// Move data.
|
||||
my_offset += greater ? gt_offset : lt_offset;
|
||||
outdata[offset + my_offset] = data;
|
||||
|
||||
// Count up if we're the last warp in. If so, then Kepler will launch the next
|
||||
// set of sorts directly from here.
|
||||
if (lane_id == 0) {
|
||||
// Count "elements written". If I wrote the last one, then trigger the next
|
||||
// qsorts
|
||||
unsigned int mycount = lt_count + gt_count;
|
||||
|
||||
if (atomicAdd((unsigned int *)&atomicData->sorted_count, mycount) +
|
||||
mycount ==
|
||||
len) {
|
||||
// We're the last warp to do any sorting. Therefore it's up to us to
|
||||
// launch the next stage.
|
||||
unsigned int lt_len = atomicData->lt_offset;
|
||||
unsigned int gt_len = atomicData->gt_offset;
|
||||
|
||||
cudaStream_t lstream, rstream;
|
||||
cudaStreamCreateWithFlags(&lstream, cudaStreamNonBlocking);
|
||||
cudaStreamCreateWithFlags(&rstream, cudaStreamNonBlocking);
|
||||
|
||||
// Begin by freeing our atomicData storage. It's better for the ringbuffer
|
||||
// algorithm
|
||||
// if we free when we're done, rather than re-using (makes for less
|
||||
// fragmentation).
|
||||
ringbufFree<qsortAtomicData>(atomicDataStack, atomicData);
|
||||
|
||||
// Exceptional case: if "lt_len" is zero, then all values in the batch
|
||||
// are equal. We are then done (may need to copy into correct buffer,
|
||||
// though)
|
||||
if (lt_len == 0) {
|
||||
if (source_is_indata)
|
||||
cudaMemcpyAsync(indata + offset, outdata + offset,
|
||||
gt_len * sizeof(unsigned), cudaMemcpyDeviceToDevice,
|
||||
lstream);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Start with lower half first
|
||||
if (lt_len > BITONICSORT_LEN) {
|
||||
// If we've exceeded maximum depth, fall through to backup
|
||||
// big_bitonicsort
|
||||
if (depth >= QSORT_MAXDEPTH) {
|
||||
// The final bitonic stage sorts in-place in "outdata". We therefore
|
||||
// re-use "indata" as the out-of-range tracking buffer. For (2^n)+1
|
||||
// elements we need (2^(n+1)) bytes of oor buffer. The backup qsort
|
||||
// buffer is at least this large when sizeof(QTYPE) >= 2.
|
||||
big_bitonicsort<<<1, BITONICSORT_LEN, 0, lstream>>>(
|
||||
outdata, source_is_indata ? indata : outdata, indata, offset,
|
||||
lt_len);
|
||||
} else {
|
||||
// Launch another quicksort. We need to allocate more storage for the
|
||||
// atomic data.
|
||||
if ((atomicData = ringbufAlloc<qsortAtomicData>(atomicDataStack)) ==
|
||||
NULL)
|
||||
printf("Stack-allocation error. Failing left child launch.\n");
|
||||
else {
|
||||
atomicData->lt_offset = atomicData->gt_offset =
|
||||
atomicData->sorted_count = 0;
|
||||
unsigned int numblocks =
|
||||
(unsigned int)(lt_len + (QSORT_BLOCKSIZE - 1)) >>
|
||||
QSORT_BLOCKSIZE_SHIFT;
|
||||
qsort_warp<<<numblocks, QSORT_BLOCKSIZE, 0, lstream>>>(
|
||||
outdata, indata, offset, lt_len, atomicData, atomicDataStack,
|
||||
!source_is_indata, depth + 1);
|
||||
}
|
||||
}
|
||||
} else if (lt_len > 1) {
|
||||
// Final stage uses a bitonic sort instead. It's important to
|
||||
// make sure the final stage ends up in the correct (original) buffer.
|
||||
// We launch the smallest power-of-2 number of threads that we can.
|
||||
unsigned int bitonic_len = 1 << (__qsflo(lt_len - 1U) + 1);
|
||||
bitonicsort<<<1, bitonic_len, 0, lstream>>>(
|
||||
outdata, source_is_indata ? indata : outdata, offset, lt_len);
|
||||
}
|
||||
// Finally, if we sorted just one single element, we must still make
|
||||
// sure that it winds up in the correct place.
|
||||
else if (source_is_indata && (lt_len == 1))
|
||||
indata[offset] = outdata[offset];
|
||||
|
||||
if (cudaPeekAtLastError() != cudaSuccess)
|
||||
printf("Left-side launch fail: %s\n",
|
||||
cudaGetErrorString(cudaGetLastError()));
|
||||
|
||||
// Now the upper half.
|
||||
if (gt_len > BITONICSORT_LEN) {
|
||||
// If we've exceeded maximum depth, fall through to backup
|
||||
// big_bitonicsort
|
||||
if (depth >= QSORT_MAXDEPTH)
|
||||
big_bitonicsort<<<1, BITONICSORT_LEN, 0, rstream>>>(
|
||||
outdata, source_is_indata ? indata : outdata, indata,
|
||||
offset + lt_len, gt_len);
|
||||
else {
|
||||
// Allocate new atomic storage for this launch
|
||||
if ((atomicData = ringbufAlloc<qsortAtomicData>(atomicDataStack)) ==
|
||||
NULL)
|
||||
printf("Stack allocation error! Failing right-side launch.\n");
|
||||
else {
|
||||
atomicData->lt_offset = atomicData->gt_offset =
|
||||
atomicData->sorted_count = 0;
|
||||
unsigned int numblocks =
|
||||
(unsigned int)(gt_len + (QSORT_BLOCKSIZE - 1)) >>
|
||||
QSORT_BLOCKSIZE_SHIFT;
|
||||
qsort_warp<<<numblocks, QSORT_BLOCKSIZE, 0, rstream>>>(
|
||||
outdata, indata, offset + lt_len, gt_len, atomicData,
|
||||
atomicDataStack, !source_is_indata, depth + 1);
|
||||
}
|
||||
}
|
||||
} else if (gt_len > 1) {
|
||||
unsigned int bitonic_len = 1 << (__qsflo(gt_len - 1U) + 1);
|
||||
bitonicsort<<<1, bitonic_len, 0, rstream>>>(
|
||||
outdata, source_is_indata ? indata : outdata, offset + lt_len,
|
||||
gt_len);
|
||||
} else if (source_is_indata && (gt_len == 1))
|
||||
indata[offset + lt_len] = outdata[offset + lt_len];
|
||||
|
||||
if (cudaPeekAtLastError() != cudaSuccess)
|
||||
printf("Right-side launch fail: %s\n",
|
||||
cudaGetErrorString(cudaGetLastError()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// run_quicksort
|
||||
//
|
||||
// Host-side code to run the Kepler version of quicksort. It's pretty
|
||||
// simple, because all launch control is handled on the device via CDP.
|
||||
//
|
||||
// All parallel quicksorts require an equal-sized scratch buffer. This
|
||||
// must be passed in ahead of time.
|
||||
//
|
||||
// Returns the time elapsed for the sort.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float run_quicksort_cdp(unsigned *gpudata, unsigned *scratchdata,
|
||||
unsigned int count, cudaStream_t stream) {
|
||||
unsigned int stacksize = QSORT_STACK_ELEMS;
|
||||
|
||||
// This is the stack, for atomic tracking of each sort's status
|
||||
qsortAtomicData *gpustack;
|
||||
checkCudaErrors(
|
||||
cudaMalloc((void **)&gpustack, stacksize * sizeof(qsortAtomicData)));
|
||||
checkCudaErrors(cudaMemset(
|
||||
gpustack, 0, sizeof(qsortAtomicData))); // Only need set first entry to 0
|
||||
|
||||
// Create the memory ringbuffer used for handling the stack.
|
||||
// Initialise everything to where it needs to be.
|
||||
qsortRingbuf buf;
|
||||
qsortRingbuf *ringbuf;
|
||||
checkCudaErrors(cudaMalloc((void **)&ringbuf, sizeof(qsortRingbuf)));
|
||||
buf.head = 1; // We start with one allocation
|
||||
buf.tail = 0;
|
||||
buf.count = 0;
|
||||
buf.max = 0;
|
||||
buf.stacksize = stacksize;
|
||||
buf.stackbase = gpustack;
|
||||
checkCudaErrors(
|
||||
cudaMemcpy(ringbuf, &buf, sizeof(buf), cudaMemcpyHostToDevice));
|
||||
|
||||
// Timing events...
|
||||
cudaEvent_t ev1, ev2;
|
||||
checkCudaErrors(cudaEventCreate(&ev1));
|
||||
checkCudaErrors(cudaEventCreate(&ev2));
|
||||
checkCudaErrors(cudaEventRecord(ev1));
|
||||
|
||||
// Now we trivially launch the qsort kernel
|
||||
if (count > BITONICSORT_LEN) {
|
||||
unsigned int numblocks =
|
||||
(unsigned int)(count + (QSORT_BLOCKSIZE - 1)) >> QSORT_BLOCKSIZE_SHIFT;
|
||||
qsort_warp<<<numblocks, QSORT_BLOCKSIZE, 0, stream>>>(
|
||||
gpudata, scratchdata, 0U, count, gpustack, ringbuf, true, 0);
|
||||
} else {
|
||||
bitonicsort<<<1, BITONICSORT_LEN>>>(gpudata, gpudata, 0, count);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaGetLastError());
|
||||
checkCudaErrors(cudaEventRecord(ev2));
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
float elapse = 0.0f;
|
||||
|
||||
if (cudaPeekAtLastError() != cudaSuccess)
|
||||
printf("Launch failure: %s\n", cudaGetErrorString(cudaGetLastError()));
|
||||
else
|
||||
checkCudaErrors(cudaEventElapsedTime(&elapse, ev1, ev2));
|
||||
|
||||
// Sanity check that the stack allocator is doing the right thing
|
||||
checkCudaErrors(
|
||||
cudaMemcpy(&buf, ringbuf, sizeof(*ringbuf), cudaMemcpyDeviceToHost));
|
||||
|
||||
if (count > BITONICSORT_LEN && buf.head != buf.tail) {
|
||||
printf("Stack allocation error!\nRingbuf:\n");
|
||||
printf("\t head = %u\n", buf.head);
|
||||
printf("\t tail = %u\n", buf.tail);
|
||||
printf("\tcount = %u\n", buf.count);
|
||||
printf("\t max = %u\n", buf.max);
|
||||
}
|
||||
|
||||
// Release our stack data once we're done
|
||||
checkCudaErrors(cudaFree(ringbuf));
|
||||
checkCudaErrors(cudaFree(gpustack));
|
||||
|
||||
return elapse;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int run_qsort(unsigned int size, int seed, int debug, int loop, int verbose) {
|
||||
if (seed > 0) srand(seed);
|
||||
|
||||
// Create and set up our test
|
||||
unsigned *gpudata, *scratchdata;
|
||||
checkCudaErrors(cudaMalloc((void **)&gpudata, size * sizeof(unsigned)));
|
||||
checkCudaErrors(cudaMalloc((void **)&scratchdata, size * sizeof(unsigned)));
|
||||
|
||||
// Create CPU data.
|
||||
unsigned *data = new unsigned[size];
|
||||
unsigned int min = loop ? loop : size;
|
||||
unsigned int max = size;
|
||||
loop = (loop == 0) ? 1 : loop;
|
||||
|
||||
for (size = min; size <= max; size += loop) {
|
||||
if (verbose) printf(" Input: ");
|
||||
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
// Build data 8 bits at a time
|
||||
data[i] = 0;
|
||||
char *ptr = (char *)&(data[i]);
|
||||
|
||||
for (unsigned j = 0; j < sizeof(unsigned); j++) {
|
||||
// Easy-to-read data in debug mode
|
||||
if (debug) {
|
||||
*ptr++ = (char)(rand() % 10);
|
||||
break;
|
||||
}
|
||||
|
||||
*ptr++ = (char)(rand() & 255);
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
if (i && !(i % 32)) printf("\n ");
|
||||
|
||||
printf("%u ", data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose) printf("\n");
|
||||
|
||||
checkCudaErrors(cudaMemcpy(gpudata, data, size * sizeof(unsigned),
|
||||
cudaMemcpyHostToDevice));
|
||||
|
||||
// So we're now populated and ready to go! We size our launch as
|
||||
// blocks of up to BLOCKSIZE threads, and appropriate grid size.
|
||||
// One thread is launched per element.
|
||||
float elapse;
|
||||
elapse = run_quicksort_cdp(gpudata, scratchdata, size, NULL);
|
||||
|
||||
// run_bitonicsort<SORTTYPE>(gpudata, scratchdata, size, verbose);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
|
||||
// Copy back the data and verify correct sort
|
||||
checkCudaErrors(cudaMemcpy(data, gpudata, size * sizeof(unsigned),
|
||||
cudaMemcpyDeviceToHost));
|
||||
|
||||
if (verbose) {
|
||||
printf("Output: ");
|
||||
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
if (i && !(i % 32)) printf("\n ");
|
||||
|
||||
printf("%u ", data[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
unsigned int check;
|
||||
|
||||
for (check = 1; check < size; check++) {
|
||||
if (data[check] < data[check - 1]) {
|
||||
printf("FAILED at element: %d\n", check);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (check != size) {
|
||||
printf(" cdpAdvancedQuicksort FAILED\n");
|
||||
exit(EXIT_FAILURE);
|
||||
} else
|
||||
printf(" cdpAdvancedQuicksort PASSED\n");
|
||||
|
||||
// Display the time between event recordings
|
||||
printf("Sorted %u elems in %.3f ms (%.3f Melems/sec)\n", size, elapse,
|
||||
(float)size / (elapse * 1000.0f));
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
// Release everything and we're done
|
||||
checkCudaErrors(cudaFree(scratchdata));
|
||||
checkCudaErrors(cudaFree(gpudata));
|
||||
delete (data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void usage() {
|
||||
printf(
|
||||
"Syntax: cdpAdvancedQuicksort [-size=<num>] [-seed=<num>] [-debug] "
|
||||
"[-loop-step=<num>] [-verbose]\n");
|
||||
printf(
|
||||
"If loop_step is non-zero, will run from 1->array_len in steps of "
|
||||
"loop_step\n");
|
||||
}
|
||||
|
||||
// Host side entry
|
||||
int main(int argc, char *argv[]) {
|
||||
int size = 1000000;
|
||||
unsigned int seed = 0;
|
||||
int debug = 0;
|
||||
int loop = 0;
|
||||
int verbose = 0;
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "help") ||
|
||||
checkCmdLineFlag(argc, (const char **)argv, "h")) {
|
||||
usage();
|
||||
printf("&&&& cdpAdvancedQuicksort WAIVED\n");
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "size")) {
|
||||
size = getCmdLineArgumentInt(argc, (const char **)argv, "size");
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "seed")) {
|
||||
seed = getCmdLineArgumentInt(argc, (const char **)argv, "seed");
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "loop-step")) {
|
||||
loop = getCmdLineArgumentInt(argc, (const char **)argv, "loop-step");
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "debug")) {
|
||||
debug = 1;
|
||||
}
|
||||
|
||||
if (checkCmdLineFlag(argc, (const char **)argv, "verbose")) {
|
||||
verbose = 1;
|
||||
}
|
||||
|
||||
// Get device properties
|
||||
int cuda_device = findCudaDevice(argc, (const char **)argv);
|
||||
cudaDeviceProp properties;
|
||||
checkCudaErrors(cudaGetDeviceProperties(&properties, cuda_device));
|
||||
int cdpCapable =
|
||||
(properties.major == 3 && properties.minor >= 5) || properties.major >= 4;
|
||||
|
||||
printf("GPU device %s has compute capabilities (SM %d.%d)\n", properties.name,
|
||||
properties.major, properties.minor);
|
||||
|
||||
if (!cdpCapable) {
|
||||
printf(
|
||||
"cdpAdvancedQuicksort requires SM 3.5 or higher to use CUDA Dynamic "
|
||||
"Parallelism. Exiting...\n");
|
||||
exit(EXIT_WAIVED);
|
||||
}
|
||||
|
||||
printf("Running qsort on %d elements with seed %d, on %s\n", size, seed,
|
||||
properties.name);
|
||||
|
||||
run_qsort(size, seed, debug, loop, verbose);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2017
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cdpAdvancedQuicksort", "cdpAdvancedQuicksort_vs2017.vcxproj", "{997E0757-EA74-4A4E-A0FC-47D8C8831A15}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Debug|x64.Build.0 = Debug|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Release|x64.ActiveCfg = Release|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,114 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<CUDAPropsPath Condition="'$(CUDAPropsPath)'==''">$(VCTargetsPath)\BuildCustomizations</CUDAPropsPath>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{997E0757-EA74-4A4E-A0FC-47D8C8831A15}</ProjectGuid>
|
||||
<RootNamespace>cdpAdvancedQuicksort_vs2017</RootNamespace>
|
||||
<ProjectName>cdpAdvancedQuicksort</ProjectName>
|
||||
<CudaToolkitCustomDir />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(WindowsTargetPlatformVersion)'==''">
|
||||
<LatestTargetPlatformVersion>$([Microsoft.Build.Utilities.ToolLocationHelper]::GetLatestSDKTargetPlatformVersion('Windows', '10.0'))</LatestTargetPlatformVersion>
|
||||
<WindowsTargetPlatformVersion Condition="'$(WindowsTargetPlatformVersion)' == ''">$(LatestTargetPlatformVersion)</WindowsTargetPlatformVersion>
|
||||
<TargetPlatformVersion>$(WindowsTargetPlatformVersion)</TargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup>
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release'">
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.6.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup>
|
||||
<IntDir>$(Platform)/$(Configuration)/</IntDir>
|
||||
<IncludePath>$(IncludePath)</IncludePath>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<CodeAnalysisRules />
|
||||
<CodeAnalysisRuleAssemblies />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Platform)'=='x64'">
|
||||
<OutDir>../../../bin/win64/$(Configuration)/</OutDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>./;$(CudaToolkitDir)/include;../../../Common;$(CudaToolkitIncludeDir);</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<AdditionalDependencies>cudadevrt.lib;cudart_static.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(CudaToolkitLibDir);</AdditionalLibraryDirectories>
|
||||
<OutputFile>$(OutDir)/cdpAdvancedQuicksort.exe</OutputFile>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<CodeGeneration>compute_35,sm_35;compute_37,sm_37;compute_50,sm_50;compute_52,sm_52;compute_60,sm_60;compute_61,sm_61;compute_70,sm_70;compute_75,sm_75;compute_80,sm_80;compute_86,sm_86;</CodeGeneration>
|
||||
<AdditionalOptions>-Xcompiler "/wd 4819" --threads 0 </AdditionalOptions>
|
||||
<Include>./;../../../Common</Include>
|
||||
<Defines>WIN32</Defines>
|
||||
<GenerateRelocatableDeviceCode>true</GenerateRelocatableDeviceCode>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<ClCompile>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<Runtime>MTd</Runtime>
|
||||
<TargetMachinePlatform>64</TargetMachinePlatform>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
|
||||
<ClCompile>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<Runtime>MT</Runtime>
|
||||
<TargetMachinePlatform>64</TargetMachinePlatform>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<CudaCompile Include="cdpAdvancedQuicksort.cu" />
|
||||
<CudaCompile Include="cdpBitonicSort.cu" />
|
||||
<ClInclude Include="cdpQuicksort.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.6.targets" />
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2019
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cdpAdvancedQuicksort", "cdpAdvancedQuicksort_vs2019.vcxproj", "{997E0757-EA74-4A4E-A0FC-47D8C8831A15}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Debug|x64.Build.0 = Debug|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Release|x64.ActiveCfg = Release|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,110 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<CUDAPropsPath Condition="'$(CUDAPropsPath)'==''">$(VCTargetsPath)\BuildCustomizations</CUDAPropsPath>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{997E0757-EA74-4A4E-A0FC-47D8C8831A15}</ProjectGuid>
|
||||
<RootNamespace>cdpAdvancedQuicksort_vs2019</RootNamespace>
|
||||
<ProjectName>cdpAdvancedQuicksort</ProjectName>
|
||||
<CudaToolkitCustomDir />
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup>
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release'">
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.6.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup>
|
||||
<IntDir>$(Platform)/$(Configuration)/</IntDir>
|
||||
<IncludePath>$(IncludePath)</IncludePath>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<CodeAnalysisRules />
|
||||
<CodeAnalysisRuleAssemblies />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Platform)'=='x64'">
|
||||
<OutDir>../../../bin/win64/$(Configuration)/</OutDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>./;$(CudaToolkitDir)/include;../../../Common;$(CudaToolkitIncludeDir);</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<AdditionalDependencies>cudadevrt.lib;cudart_static.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(CudaToolkitLibDir);</AdditionalLibraryDirectories>
|
||||
<OutputFile>$(OutDir)/cdpAdvancedQuicksort.exe</OutputFile>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<CodeGeneration>compute_35,sm_35;compute_37,sm_37;compute_50,sm_50;compute_52,sm_52;compute_60,sm_60;compute_61,sm_61;compute_70,sm_70;compute_75,sm_75;compute_80,sm_80;compute_86,sm_86;</CodeGeneration>
|
||||
<AdditionalOptions>-Xcompiler "/wd 4819" --threads 0 </AdditionalOptions>
|
||||
<Include>./;../../../Common</Include>
|
||||
<Defines>WIN32</Defines>
|
||||
<GenerateRelocatableDeviceCode>true</GenerateRelocatableDeviceCode>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<ClCompile>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<Runtime>MTd</Runtime>
|
||||
<TargetMachinePlatform>64</TargetMachinePlatform>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
|
||||
<ClCompile>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<Runtime>MT</Runtime>
|
||||
<TargetMachinePlatform>64</TargetMachinePlatform>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<CudaCompile Include="cdpAdvancedQuicksort.cu" />
|
||||
<CudaCompile Include="cdpBitonicSort.cu" />
|
||||
<ClInclude Include="cdpQuicksort.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.6.targets" />
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2022
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cdpAdvancedQuicksort", "cdpAdvancedQuicksort_vs2022.vcxproj", "{997E0757-EA74-4A4E-A0FC-47D8C8831A15}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Debug|x64.Build.0 = Debug|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Release|x64.ActiveCfg = Release|x64
|
||||
{997E0757-EA74-4A4E-A0FC-47D8C8831A15}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,110 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<CUDAPropsPath Condition="'$(CUDAPropsPath)'==''">$(VCTargetsPath)\BuildCustomizations</CUDAPropsPath>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{997E0757-EA74-4A4E-A0FC-47D8C8831A15}</ProjectGuid>
|
||||
<RootNamespace>cdpAdvancedQuicksort_vs2022</RootNamespace>
|
||||
<ProjectName>cdpAdvancedQuicksort</ProjectName>
|
||||
<CudaToolkitCustomDir />
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup>
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release'">
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.6.props" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup>
|
||||
<IntDir>$(Platform)/$(Configuration)/</IntDir>
|
||||
<IncludePath>$(IncludePath)</IncludePath>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
<CodeAnalysisRules />
|
||||
<CodeAnalysisRuleAssemblies />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Platform)'=='x64'">
|
||||
<OutDir>../../../bin/win64/$(Configuration)/</OutDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<PreprocessorDefinitions>WIN32;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>./;$(CudaToolkitDir)/include;../../../Common;$(CudaToolkitIncludeDir);</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<AdditionalDependencies>cudadevrt.lib;cudart_static.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalLibraryDirectories>$(CudaToolkitLibDir);</AdditionalLibraryDirectories>
|
||||
<OutputFile>$(OutDir)/cdpAdvancedQuicksort.exe</OutputFile>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<CodeGeneration>compute_35,sm_35;compute_37,sm_37;compute_50,sm_50;compute_52,sm_52;compute_60,sm_60;compute_61,sm_61;compute_70,sm_70;compute_75,sm_75;compute_80,sm_80;compute_86,sm_86;</CodeGeneration>
|
||||
<AdditionalOptions>-Xcompiler "/wd 4819" --threads 0 </AdditionalOptions>
|
||||
<Include>./;../../../Common</Include>
|
||||
<Defines>WIN32</Defines>
|
||||
<GenerateRelocatableDeviceCode>true</GenerateRelocatableDeviceCode>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<ClCompile>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<Runtime>MTd</Runtime>
|
||||
<TargetMachinePlatform>64</TargetMachinePlatform>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
|
||||
<ClCompile>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
<CudaCompile>
|
||||
<Runtime>MT</Runtime>
|
||||
<TargetMachinePlatform>64</TargetMachinePlatform>
|
||||
</CudaCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<CudaCompile Include="cdpAdvancedQuicksort.cu" />
|
||||
<CudaCompile Include="cdpBitonicSort.cu" />
|
||||
<ClInclude Include="cdpQuicksort.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
<Import Project="$(CUDAPropsPath)\CUDA 11.6.targets" />
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
286
Samples/3_CUDA_Features/cdpAdvancedQuicksort/cdpBitonicSort.cu
Normal file
286
Samples/3_CUDA_Features/cdpAdvancedQuicksort/cdpBitonicSort.cu
Normal file
@@ -0,0 +1,286 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
// This is a basic, recursive bitonic sort taken from
|
||||
// http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/bitonic/oddn.htm
|
||||
//
|
||||
// The parallel code is based on:
|
||||
// http://www.tools-of-computing.com/tc/CS/Sorts/bitonic_sort.htm
|
||||
//
|
||||
// The multithread code is from me.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <cooperative_groups.h>
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
|
||||
#include "cdpQuicksort.h"
|
||||
|
||||
// Inline PTX call to return index of highest non-zero bit in a word
|
||||
static __device__ __forceinline__ unsigned int __btflo(unsigned int word) {
|
||||
unsigned int ret;
|
||||
asm volatile("bfind.u32 %0, %1;" : "=r"(ret) : "r"(word));
|
||||
return ret;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// qcompare
|
||||
//
|
||||
// Comparison function. Note difference from libc standard in
|
||||
// that we take by reference, not by pointer. I can't figure
|
||||
// out how to get a template-to-pointer specialisation working.
|
||||
// Perhaps it requires a class?
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__device__ __forceinline__ int qcompare(unsigned &val1, unsigned &val2) {
|
||||
return (val1 > val2) ? 1 : (val1 == val2) ? 0 : -1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Basic any-N bitonic sort. We sort "len" elements of "indata", starting
|
||||
// from the "offset" elements into the input data array. Note that "outdata"
|
||||
// can safely be the same as "indata" for an in-place sort (we stage through
|
||||
// shared memory).
|
||||
//
|
||||
// We handle non-power-of-2 sizes by padding out to the next largest power of
|
||||
// 2.
|
||||
// This is the fully-generic version, for sorting arbitrary data which does not
|
||||
// have a clear "maximum" value. We track "invalid" entries in a separate array
|
||||
// to make sure that they always sorts as "max value" elements. A template
|
||||
// parameter "OOR" allows specialisation to optimise away the invalid tracking.
|
||||
//
|
||||
// We can do a more specialised version for int/longlong/flat/double, in which
|
||||
// we pad out the array with max-value-of-type elements. That's another
|
||||
// function.
|
||||
//
|
||||
// The last step copies from indata -> outdata... the rest is done in-place.
|
||||
// We use shared memory as temporary storage, which puts an upper limit on
|
||||
// how much data we can sort per block.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static __device__ __forceinline__ void bitonicsort_kernel(
|
||||
unsigned *indata, unsigned *outdata, unsigned int offset, unsigned int len,
|
||||
cg::thread_block cta) {
|
||||
__shared__ unsigned
|
||||
sortbuf[1024]; // Max of 1024 elements - TODO: make this dynamic
|
||||
|
||||
// First copy data into shared memory.
|
||||
unsigned int inside = (threadIdx.x < len);
|
||||
sortbuf[threadIdx.x] = inside ? indata[threadIdx.x + offset] : 0xffffffffu;
|
||||
cg::sync(cta);
|
||||
|
||||
// Now the sort loops
|
||||
// Here, "k" is the sort level (remember bitonic does a multi-level butterfly
|
||||
// style sort)
|
||||
// and "j" is the partner element in the butterfly.
|
||||
// Two threads each work on one butterfly, because the read/write needs to
|
||||
// happen
|
||||
// simultaneously
|
||||
for (unsigned int k = 2; k <= blockDim.x;
|
||||
k *= 2) // Butterfly stride increments in powers of 2
|
||||
{
|
||||
for (unsigned int j = k >> 1; j > 0;
|
||||
j >>= 1) // Strides also in powers of to, up to <k
|
||||
{
|
||||
unsigned int swap_idx =
|
||||
threadIdx.x ^ j; // Index of element we're compare-and-swapping with
|
||||
unsigned my_elem = sortbuf[threadIdx.x];
|
||||
unsigned swap_elem = sortbuf[swap_idx];
|
||||
|
||||
cg::sync(cta);
|
||||
|
||||
// The k'th bit of my threadid (and hence my sort item ID)
|
||||
// determines if we sort ascending or descending.
|
||||
// However, since threads are reading from the top AND the bottom of
|
||||
// the butterfly, if my ID is > swap_idx, then ascending means mine<swap.
|
||||
// Finally, if either my_elem or swap_elem is out of range, then it
|
||||
// ALWAYS acts like it's the largest number.
|
||||
// Confusing? It saves us two writes though.
|
||||
unsigned int ascend = k * (swap_idx < threadIdx.x);
|
||||
unsigned int descend = k * (swap_idx > threadIdx.x);
|
||||
bool swap = false;
|
||||
|
||||
if ((threadIdx.x & k) == ascend) {
|
||||
if (my_elem > swap_elem) swap = true;
|
||||
}
|
||||
|
||||
if ((threadIdx.x & k) == descend) {
|
||||
if (my_elem < swap_elem) swap = true;
|
||||
}
|
||||
|
||||
// If we had to swap, then write my data to the other element's position.
|
||||
// Don't forget to track out-of-range status too!
|
||||
if (swap) {
|
||||
sortbuf[swap_idx] = my_elem;
|
||||
}
|
||||
|
||||
cg::sync(cta);
|
||||
}
|
||||
}
|
||||
|
||||
// Copy the sorted data from shared memory back to the output buffer
|
||||
if (threadIdx.x < len) outdata[threadIdx.x + offset] = sortbuf[threadIdx.x];
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
// This is an emergency-CTA sort, which sorts an arbitrary sized chunk
|
||||
// using a single block. Useful for if qsort runs out of nesting depth.
|
||||
//
|
||||
// Note that bitonic sort needs enough storage to pad up to the nearest
|
||||
// power of 2. This means that the double-buffer is always large enough
|
||||
// (when combined with the main buffer), but we do not get enough space
|
||||
// to keep OOR information.
|
||||
//
|
||||
// This in turn means that this sort does not work with a generic data
|
||||
// type. It must be a directly-comparable (i.e. with max value) type.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static __device__ __forceinline__ void big_bitonicsort_kernel(
|
||||
unsigned *indata, unsigned *outdata, unsigned *backbuf, unsigned int offset,
|
||||
unsigned int len, cg::thread_block cta) {
|
||||
unsigned int len2 =
|
||||
1 << (__btflo(len - 1U) + 1); // Round up len to nearest power-of-2
|
||||
|
||||
if (threadIdx.x >= len2)
|
||||
return; // Early out for case where more threads launched than there is
|
||||
// data
|
||||
|
||||
// First, set up our unused values to be the max data type.
|
||||
for (unsigned int i = len; i < len2; i += blockDim.x) {
|
||||
unsigned int index = i + threadIdx.x;
|
||||
|
||||
if (index < len2) {
|
||||
// Must split our index between two buffers
|
||||
if (index < len)
|
||||
indata[index + offset] = 0xffffffffu;
|
||||
else
|
||||
backbuf[index + offset - len] = 0xffffffffu;
|
||||
}
|
||||
}
|
||||
|
||||
cg::sync(cta);
|
||||
|
||||
// Now the sort loops
|
||||
// Here, "k" is the sort level (remember bitonic does a multi-level butterfly
|
||||
// style sort)
|
||||
// and "j" is the partner element in the butterfly.
|
||||
// Two threads each work on one butterfly, because the read/write needs to
|
||||
// happen
|
||||
// simultaneously
|
||||
for (unsigned int k = 2; k <= len2;
|
||||
k *= 2) // Butterfly stride increments in powers of 2
|
||||
{
|
||||
for (unsigned int j = k >> 1; j > 0;
|
||||
j >>= 1) // Strides also in powers of to, up to <k
|
||||
{
|
||||
for (unsigned int i = 0; i < len2; i += blockDim.x) {
|
||||
unsigned int index = threadIdx.x + i;
|
||||
unsigned int swap_idx =
|
||||
index ^ j; // Index of element we're compare-and-swapping with
|
||||
|
||||
// Only do the swap for index<swap_idx (avoids collision between other
|
||||
// threads)
|
||||
if (swap_idx > index) {
|
||||
unsigned my_elem, swap_elem;
|
||||
|
||||
if (index < len)
|
||||
my_elem = indata[index + offset];
|
||||
else
|
||||
my_elem = backbuf[index + offset - len];
|
||||
|
||||
if (swap_idx < len)
|
||||
swap_elem = indata[swap_idx + offset];
|
||||
else
|
||||
swap_elem = backbuf[swap_idx + offset - len];
|
||||
|
||||
// The k'th bit of my index (and hence my sort item ID)
|
||||
// determines if we sort ascending or descending.
|
||||
// Also, if either my_elem or swap_elem is out of range, then it
|
||||
// ALWAYS acts like it's the largest number.
|
||||
bool swap = false;
|
||||
|
||||
if ((index & k) == 0) {
|
||||
if (my_elem > swap_elem) swap = true;
|
||||
}
|
||||
|
||||
if ((index & k) == k) {
|
||||
if (my_elem < swap_elem) swap = true;
|
||||
}
|
||||
|
||||
// If we had to swap, then write my data to the other element's
|
||||
// position.
|
||||
if (swap) {
|
||||
if (swap_idx < len)
|
||||
indata[swap_idx + offset] = my_elem;
|
||||
else
|
||||
backbuf[swap_idx + offset - len] = my_elem;
|
||||
|
||||
if (index < len)
|
||||
indata[index + offset] = swap_elem;
|
||||
else
|
||||
backbuf[index + offset - len] = swap_elem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cg::sync(cta); // Only need to sync for each "j" pass
|
||||
}
|
||||
}
|
||||
|
||||
// Copy the sorted data from the input to the output buffer, because we sort
|
||||
// in-place
|
||||
if (outdata != indata) {
|
||||
for (unsigned int i = 0; i < len; i += blockDim.x) {
|
||||
unsigned int index = i + threadIdx.x;
|
||||
|
||||
if (index < len) outdata[index + offset] = indata[index + offset];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// KERNELS
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
__global__ void bitonicsort(unsigned *indata, unsigned *outdata,
|
||||
unsigned int offset, unsigned int len) {
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
bitonicsort_kernel(indata, outdata, offset, len, cta);
|
||||
}
|
||||
|
||||
__global__ void big_bitonicsort(unsigned *indata, unsigned *outdata,
|
||||
unsigned *backbuf, unsigned int offset,
|
||||
unsigned int len) {
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
big_bitonicsort_kernel(indata, outdata, backbuf, offset, len, cta);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
79
Samples/3_CUDA_Features/cdpAdvancedQuicksort/cdpQuicksort.h
Normal file
79
Samples/3_CUDA_Features/cdpAdvancedQuicksort/cdpQuicksort.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
// Definitions for GPU quicksort
|
||||
|
||||
#ifndef QUICKSORT_H
|
||||
#define QUICKSORT_H
|
||||
|
||||
#define QSORT_BLOCKSIZE_SHIFT 9
|
||||
#define QSORT_BLOCKSIZE (1 << QSORT_BLOCKSIZE_SHIFT)
|
||||
#define BITONICSORT_LEN 1024 // Must be power of 2!
|
||||
#define QSORT_MAXDEPTH \
|
||||
16 // Will force final bitonic stage at depth QSORT_MAXDEPTH+1
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// The algorithm uses several variables updated by using atomic operations.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
typedef struct __align__(128) qsortAtomicData_t {
|
||||
volatile unsigned int lt_offset; // Current output offset for <pivot
|
||||
volatile unsigned int gt_offset; // Current output offset for >pivot
|
||||
volatile unsigned int sorted_count; // Total count sorted, for deciding when
|
||||
// to launch next wave
|
||||
volatile unsigned int
|
||||
index; // Ringbuf tracking index. Can be ignored if not using ringbuf.
|
||||
}
|
||||
qsortAtomicData;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// A ring-buffer for rapid stack allocation
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
typedef struct qsortRingbuf_t {
|
||||
volatile unsigned int head; // Head pointer - we allocate from here
|
||||
volatile unsigned int
|
||||
tail; // Tail pointer - indicates last still-in-use element
|
||||
volatile unsigned int count; // Total count allocated
|
||||
volatile unsigned int max; // Max index allocated
|
||||
unsigned int stacksize; // Wrap-around size of buffer (must be power of 2)
|
||||
volatile void *stackbase; // Pointer to the stack we're allocating from
|
||||
} qsortRingbuf;
|
||||
|
||||
// Stack elem count must be power of 2!
|
||||
#define QSORT_STACK_ELEMS \
|
||||
1 * 1024 * 1024 // One million stack elements is a HUGE number.
|
||||
|
||||
__global__ void qsort_warp(unsigned *indata, unsigned *outdata,
|
||||
unsigned int len, qsortAtomicData *atomicData,
|
||||
qsortRingbuf *ringbuf, unsigned int source_is_indata,
|
||||
unsigned int depth);
|
||||
__global__ void bitonicsort(unsigned *indata, unsigned *outdata,
|
||||
unsigned int offset, unsigned int len);
|
||||
__global__ void big_bitonicsort(unsigned *indata, unsigned *outdata,
|
||||
unsigned *backbuf, unsigned int offset,
|
||||
unsigned int len);
|
||||
|
||||
#endif // QUICKSORT_H
|
||||
Reference in New Issue
Block a user