mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-13 00:08:26 +08:00
CUDA 13.2 samples update (#432)
- Added Python samples for CUDA Python 1.0 release - Renamed top-level `Samples` directory to `cpp` to accommodate Python samples.
This commit is contained in:
18
cpp/2_Concepts_and_Techniques/convolutionSeparable/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/2_Concepts_and_Techniques/convolutionSeparable/.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
cpp/2_Concepts_and_Techniques/convolutionSeparable/.vscode/extensions.json
vendored
Normal file
7
cpp/2_Concepts_and_Techniques/convolutionSeparable/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(convolutionSeparable LANGUAGES C CXX CUDA)
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 89 90 100 110 120)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||
if(ENABLE_CUDA_DEBUG)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (may significantly affect performance on some targets)
|
||||
else()
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo") # add line information to all builds for debug tools (exclusive to -G option)
|
||||
endif()
|
||||
|
||||
# Include directories and libraries
|
||||
include_directories(../../../Common)
|
||||
|
||||
# Source file
|
||||
# Add target for convolutionSeparable
|
||||
add_executable(convolutionSeparable convolutionSeparable.cu convolutionSeparable_gold.cpp main.cpp)
|
||||
|
||||
target_compile_options(convolutionSeparable PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(convolutionSeparable PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(convolutionSeparable PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
target_include_directories(convolutionSeparable PUBLIC
|
||||
${CUDAToolkit_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
34
cpp/2_Concepts_and_Techniques/convolutionSeparable/README.md
Normal file
34
cpp/2_Concepts_and_Techniques/convolutionSeparable/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# convolutionSeparable - CUDA Separable Convolution
|
||||
|
||||
## Description
|
||||
|
||||
This sample implements a separable convolution filter of a 2D signal with a gaussian kernel.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Image Processing, Data Parallel Algorithms
|
||||
|
||||
## Supported SM Architectures
|
||||
|
||||
[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) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||
|
||||
## Supported OSes
|
||||
|
||||
Linux, Windows
|
||||
|
||||
## Supported CPU Architecture
|
||||
|
||||
x86_64, armv7l
|
||||
|
||||
## CUDA APIs involved
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaMemcpy, cudaFree, cudaDeviceSynchronize, cudaMemcpyToSymbol, cudaMalloc
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
|
||||
## References (for more details)
|
||||
|
||||
[whitepaper](./doc/convolutionSeparable.pdf)
|
||||
@@ -0,0 +1,192 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <cooperative_groups.h>
|
||||
#include <helper_cuda.h>
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
#include "convolutionSeparable_common.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Convolution kernel storage
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__constant__ float c_Kernel[KERNEL_LENGTH];
|
||||
|
||||
extern "C" void setConvolutionKernel(float *h_Kernel)
|
||||
{
|
||||
cudaMemcpyToSymbol(c_Kernel, h_Kernel, KERNEL_LENGTH * sizeof(float));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Row convolution filter
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#define ROWS_BLOCKDIM_X 16
|
||||
#define ROWS_BLOCKDIM_Y 4
|
||||
#define ROWS_RESULT_STEPS 8
|
||||
#define ROWS_HALO_STEPS 1
|
||||
|
||||
__global__ void convolutionRowsKernel(float *d_Dst, float *d_Src, int imageW, int imageH, int pitch)
|
||||
{
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
__shared__ float s_Data[ROWS_BLOCKDIM_Y][(ROWS_RESULT_STEPS + 2 * ROWS_HALO_STEPS) * ROWS_BLOCKDIM_X];
|
||||
|
||||
// Offset to the left halo edge
|
||||
const int baseX = (blockIdx.x * ROWS_RESULT_STEPS - ROWS_HALO_STEPS) * ROWS_BLOCKDIM_X + threadIdx.x;
|
||||
const int baseY = blockIdx.y * ROWS_BLOCKDIM_Y + threadIdx.y;
|
||||
|
||||
d_Src += baseY * pitch + baseX;
|
||||
d_Dst += baseY * pitch + baseX;
|
||||
|
||||
// Load main data
|
||||
#pragma unroll
|
||||
|
||||
for (int i = ROWS_HALO_STEPS; i < ROWS_HALO_STEPS + ROWS_RESULT_STEPS; i++) {
|
||||
s_Data[threadIdx.y][threadIdx.x + i * ROWS_BLOCKDIM_X] = d_Src[i * ROWS_BLOCKDIM_X];
|
||||
}
|
||||
|
||||
// Load left halo
|
||||
#pragma unroll
|
||||
|
||||
for (int i = 0; i < ROWS_HALO_STEPS; i++) {
|
||||
s_Data[threadIdx.y][threadIdx.x + i * ROWS_BLOCKDIM_X] =
|
||||
(baseX >= -i * ROWS_BLOCKDIM_X) ? d_Src[i * ROWS_BLOCKDIM_X] : 0;
|
||||
}
|
||||
|
||||
// Load right halo
|
||||
#pragma unroll
|
||||
|
||||
for (int i = ROWS_HALO_STEPS + ROWS_RESULT_STEPS; i < ROWS_HALO_STEPS + ROWS_RESULT_STEPS + ROWS_HALO_STEPS; i++) {
|
||||
s_Data[threadIdx.y][threadIdx.x + i * ROWS_BLOCKDIM_X] =
|
||||
(imageW - baseX > i * ROWS_BLOCKDIM_X) ? d_Src[i * ROWS_BLOCKDIM_X] : 0;
|
||||
}
|
||||
|
||||
// Compute and store results
|
||||
cg::sync(cta);
|
||||
#pragma unroll
|
||||
|
||||
for (int i = ROWS_HALO_STEPS; i < ROWS_HALO_STEPS + ROWS_RESULT_STEPS; i++) {
|
||||
float sum = 0;
|
||||
|
||||
#pragma unroll
|
||||
|
||||
for (int j = -KERNEL_RADIUS; j <= KERNEL_RADIUS; j++) {
|
||||
sum += c_Kernel[KERNEL_RADIUS - j] * s_Data[threadIdx.y][threadIdx.x + i * ROWS_BLOCKDIM_X + j];
|
||||
}
|
||||
|
||||
d_Dst[i * ROWS_BLOCKDIM_X] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void convolutionRowsGPU(float *d_Dst, float *d_Src, int imageW, int imageH)
|
||||
{
|
||||
assert(ROWS_BLOCKDIM_X * ROWS_HALO_STEPS >= KERNEL_RADIUS);
|
||||
assert(imageW % (ROWS_RESULT_STEPS * ROWS_BLOCKDIM_X) == 0);
|
||||
assert(imageH % ROWS_BLOCKDIM_Y == 0);
|
||||
|
||||
dim3 blocks(imageW / (ROWS_RESULT_STEPS * ROWS_BLOCKDIM_X), imageH / ROWS_BLOCKDIM_Y);
|
||||
dim3 threads(ROWS_BLOCKDIM_X, ROWS_BLOCKDIM_Y);
|
||||
|
||||
convolutionRowsKernel<<<blocks, threads>>>(d_Dst, d_Src, imageW, imageH, imageW);
|
||||
getLastCudaError("convolutionRowsKernel() execution failed\n");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Column convolution filter
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#define COLUMNS_BLOCKDIM_X 16
|
||||
#define COLUMNS_BLOCKDIM_Y 8
|
||||
#define COLUMNS_RESULT_STEPS 8
|
||||
#define COLUMNS_HALO_STEPS 1
|
||||
|
||||
__global__ void convolutionColumnsKernel(float *d_Dst, float *d_Src, int imageW, int imageH, int pitch)
|
||||
{
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
__shared__ float s_Data[COLUMNS_BLOCKDIM_X]
|
||||
[(COLUMNS_RESULT_STEPS + 2 * COLUMNS_HALO_STEPS) * COLUMNS_BLOCKDIM_Y + 1];
|
||||
|
||||
// Offset to the upper halo edge
|
||||
const int baseX = blockIdx.x * COLUMNS_BLOCKDIM_X + threadIdx.x;
|
||||
const int baseY = (blockIdx.y * COLUMNS_RESULT_STEPS - COLUMNS_HALO_STEPS) * COLUMNS_BLOCKDIM_Y + threadIdx.y;
|
||||
d_Src += baseY * pitch + baseX;
|
||||
d_Dst += baseY * pitch + baseX;
|
||||
|
||||
// Main data
|
||||
#pragma unroll
|
||||
|
||||
for (int i = COLUMNS_HALO_STEPS; i < COLUMNS_HALO_STEPS + COLUMNS_RESULT_STEPS; i++) {
|
||||
s_Data[threadIdx.x][threadIdx.y + i * COLUMNS_BLOCKDIM_Y] = d_Src[i * COLUMNS_BLOCKDIM_Y * pitch];
|
||||
}
|
||||
|
||||
// Upper halo
|
||||
#pragma unroll
|
||||
|
||||
for (int i = 0; i < COLUMNS_HALO_STEPS; i++) {
|
||||
s_Data[threadIdx.x][threadIdx.y + i * COLUMNS_BLOCKDIM_Y] =
|
||||
(baseY >= -i * COLUMNS_BLOCKDIM_Y) ? d_Src[i * COLUMNS_BLOCKDIM_Y * pitch] : 0;
|
||||
}
|
||||
|
||||
// Lower halo
|
||||
#pragma unroll
|
||||
|
||||
for (int i = COLUMNS_HALO_STEPS + COLUMNS_RESULT_STEPS;
|
||||
i < COLUMNS_HALO_STEPS + COLUMNS_RESULT_STEPS + COLUMNS_HALO_STEPS;
|
||||
i++) {
|
||||
s_Data[threadIdx.x][threadIdx.y + i * COLUMNS_BLOCKDIM_Y] =
|
||||
(imageH - baseY > i * COLUMNS_BLOCKDIM_Y) ? d_Src[i * COLUMNS_BLOCKDIM_Y * pitch] : 0;
|
||||
}
|
||||
|
||||
// Compute and store results
|
||||
cg::sync(cta);
|
||||
#pragma unroll
|
||||
|
||||
for (int i = COLUMNS_HALO_STEPS; i < COLUMNS_HALO_STEPS + COLUMNS_RESULT_STEPS; i++) {
|
||||
float sum = 0;
|
||||
#pragma unroll
|
||||
|
||||
for (int j = -KERNEL_RADIUS; j <= KERNEL_RADIUS; j++) {
|
||||
sum += c_Kernel[KERNEL_RADIUS - j] * s_Data[threadIdx.x][threadIdx.y + i * COLUMNS_BLOCKDIM_Y + j];
|
||||
}
|
||||
|
||||
d_Dst[i * COLUMNS_BLOCKDIM_Y * pitch] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void convolutionColumnsGPU(float *d_Dst, float *d_Src, int imageW, int imageH)
|
||||
{
|
||||
assert(COLUMNS_BLOCKDIM_Y * COLUMNS_HALO_STEPS >= KERNEL_RADIUS);
|
||||
assert(imageW % COLUMNS_BLOCKDIM_X == 0);
|
||||
assert(imageH % (COLUMNS_RESULT_STEPS * COLUMNS_BLOCKDIM_Y) == 0);
|
||||
|
||||
dim3 blocks(imageW / COLUMNS_BLOCKDIM_X, imageH / (COLUMNS_RESULT_STEPS * COLUMNS_BLOCKDIM_Y));
|
||||
dim3 threads(COLUMNS_BLOCKDIM_X, COLUMNS_BLOCKDIM_Y);
|
||||
|
||||
convolutionColumnsKernel<<<blocks, threads>>>(d_Dst, d_Src, imageW, imageH, imageW);
|
||||
getLastCudaError("convolutionColumnsKernel() execution failed\n");
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/* 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 CONVOLUTIONSEPARABLE_COMMON_H
|
||||
#define CONVOLUTIONSEPARABLE_COMMON_H
|
||||
|
||||
#define KERNEL_RADIUS 8
|
||||
#define KERNEL_LENGTH (2 * KERNEL_RADIUS + 1)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Reference CPU convolution
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void convolutionRowCPU(float *h_Dst, float *h_Src, float *h_Kernel, int imageW, int imageH, int kernelR);
|
||||
|
||||
extern "C" void convolutionColumnCPU(float *h_Dst, float *h_Src, float *h_Kernel, int imageW, int imageH, int kernelR);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GPU convolution
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void setConvolutionKernel(float *h_Kernel);
|
||||
|
||||
extern "C" void convolutionRowsGPU(float *d_Dst, float *d_Src, int imageW, int imageH);
|
||||
|
||||
extern "C" void convolutionColumnsGPU(float *d_Dst, float *d_Src, int imageW, int imageH);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,68 @@
|
||||
/* 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 "convolutionSeparable_common.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Reference row convolution filter
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void convolutionRowCPU(float *h_Dst, float *h_Src, float *h_Kernel, int imageW, int imageH, int kernelR)
|
||||
{
|
||||
for (int y = 0; y < imageH; y++)
|
||||
for (int x = 0; x < imageW; x++) {
|
||||
float sum = 0;
|
||||
|
||||
for (int k = -kernelR; k <= kernelR; k++) {
|
||||
int d = x + k;
|
||||
|
||||
if (d >= 0 && d < imageW)
|
||||
sum += h_Src[y * imageW + d] * h_Kernel[kernelR - k];
|
||||
}
|
||||
|
||||
h_Dst[y * imageW + x] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Reference column convolution filter
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void convolutionColumnCPU(float *h_Dst, float *h_Src, float *h_Kernel, int imageW, int imageH, int kernelR)
|
||||
{
|
||||
for (int y = 0; y < imageH; y++)
|
||||
for (int x = 0; x < imageW; x++) {
|
||||
float sum = 0;
|
||||
|
||||
for (int k = -kernelR; k <= kernelR; k++) {
|
||||
int d = y + k;
|
||||
|
||||
if (d >= 0 && d < imageH)
|
||||
sum += h_Src[d * imageW + x] * h_Kernel[kernelR - k];
|
||||
}
|
||||
|
||||
h_Dst[y * imageW + x] = sum;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
165
cpp/2_Concepts_and_Techniques/convolutionSeparable/main.cpp
Normal file
165
cpp/2_Concepts_and_Techniques/convolutionSeparable/main.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
/* 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 sample implements a separable convolution filter
|
||||
* of a 2D image with an arbitrary kernel.
|
||||
*/
|
||||
|
||||
// CUDA runtime
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
// Utilities and system includes
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_functions.h>
|
||||
|
||||
#include "convolutionSeparable_common.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Reference CPU convolution
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void convolutionRowCPU(float *h_Result, float *h_Data, float *h_Kernel, int imageW, int imageH, int kernelR);
|
||||
|
||||
extern "C" void
|
||||
convolutionColumnCPU(float *h_Result, float *h_Data, float *h_Kernel, int imageW, int imageH, int kernelR);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Main program
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// start logs
|
||||
printf("[%s] - Starting...\n", argv[0]);
|
||||
|
||||
float *h_Kernel, *h_Input, *h_Buffer, *h_OutputCPU, *h_OutputGPU;
|
||||
|
||||
float *d_Input, *d_Output, *d_Buffer;
|
||||
|
||||
const int imageW = 3072;
|
||||
const int imageH = 3072;
|
||||
const int iterations = 16;
|
||||
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
|
||||
// Use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
sdkCreateTimer(&hTimer);
|
||||
|
||||
printf("Image Width x Height = %i x %i\n\n", imageW, imageH);
|
||||
printf("Allocating and initializing host arrays...\n");
|
||||
h_Kernel = (float *)malloc(KERNEL_LENGTH * sizeof(float));
|
||||
h_Input = (float *)malloc(imageW * imageH * sizeof(float));
|
||||
h_Buffer = (float *)malloc(imageW * imageH * sizeof(float));
|
||||
h_OutputCPU = (float *)malloc(imageW * imageH * sizeof(float));
|
||||
h_OutputGPU = (float *)malloc(imageW * imageH * sizeof(float));
|
||||
srand(200);
|
||||
|
||||
for (unsigned int i = 0; i < KERNEL_LENGTH; i++) {
|
||||
h_Kernel[i] = (float)(rand() % 16);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < imageW * imageH; i++) {
|
||||
h_Input[i] = (float)(rand() % 16);
|
||||
}
|
||||
|
||||
printf("Allocating and initializing CUDA arrays...\n");
|
||||
checkCudaErrors(cudaMalloc((void **)&d_Input, imageW * imageH * sizeof(float)));
|
||||
checkCudaErrors(cudaMalloc((void **)&d_Output, imageW * imageH * sizeof(float)));
|
||||
checkCudaErrors(cudaMalloc((void **)&d_Buffer, imageW * imageH * sizeof(float)));
|
||||
|
||||
setConvolutionKernel(h_Kernel);
|
||||
checkCudaErrors(cudaMemcpy(d_Input, h_Input, imageW * imageH * sizeof(float), cudaMemcpyHostToDevice));
|
||||
|
||||
printf("Running GPU convolution (%u identical iterations)...\n\n", iterations);
|
||||
|
||||
for (int i = -1; i < iterations; i++) {
|
||||
// i == -1 -- warmup iteration
|
||||
if (i == 0) {
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
}
|
||||
|
||||
convolutionRowsGPU(d_Buffer, d_Input, imageW, imageH);
|
||||
|
||||
convolutionColumnsGPU(d_Output, d_Buffer, imageW, imageH);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&hTimer);
|
||||
double gpuTime = 0.001 * sdkGetTimerValue(&hTimer) / (double)iterations;
|
||||
printf("convolutionSeparable, Throughput = %.4f MPixels/sec, Time = %.5f s, "
|
||||
"Size = %u Pixels, NumDevsUsed = %i, Workgroup = %u\n",
|
||||
(1.0e-6 * (double)(imageW * imageH) / gpuTime),
|
||||
gpuTime,
|
||||
(imageW * imageH),
|
||||
1,
|
||||
0);
|
||||
|
||||
printf("\nReading back GPU results...\n\n");
|
||||
checkCudaErrors(cudaMemcpy(h_OutputGPU, d_Output, imageW * imageH * sizeof(float), cudaMemcpyDeviceToHost));
|
||||
|
||||
printf("Checking the results...\n");
|
||||
printf(" ...running convolutionRowCPU()\n");
|
||||
convolutionRowCPU(h_Buffer, h_Input, h_Kernel, imageW, imageH, KERNEL_RADIUS);
|
||||
|
||||
printf(" ...running convolutionColumnCPU()\n");
|
||||
convolutionColumnCPU(h_OutputCPU, h_Buffer, h_Kernel, imageW, imageH, KERNEL_RADIUS);
|
||||
|
||||
printf(" ...comparing the results\n");
|
||||
double sum = 0, delta = 0;
|
||||
|
||||
for (unsigned i = 0; i < imageW * imageH; i++) {
|
||||
delta += (h_OutputGPU[i] - h_OutputCPU[i]) * (h_OutputGPU[i] - h_OutputCPU[i]);
|
||||
sum += h_OutputCPU[i] * h_OutputCPU[i];
|
||||
}
|
||||
|
||||
double L2norm = sqrt(delta / sum);
|
||||
printf(" ...Relative L2 norm: %E\n\n", L2norm);
|
||||
printf("Shutting down...\n");
|
||||
|
||||
checkCudaErrors(cudaFree(d_Buffer));
|
||||
checkCudaErrors(cudaFree(d_Output));
|
||||
checkCudaErrors(cudaFree(d_Input));
|
||||
free(h_OutputGPU);
|
||||
free(h_OutputCPU);
|
||||
free(h_Buffer);
|
||||
free(h_Input);
|
||||
free(h_Kernel);
|
||||
|
||||
sdkDeleteTimer(&hTimer);
|
||||
|
||||
if (L2norm > 1e-6) {
|
||||
printf("Test failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Test passed\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
Reference in New Issue
Block a user