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/convolutionTexture/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/2_Concepts_and_Techniques/convolutionTexture/.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/convolutionTexture/.vscode/extensions.json
vendored
Normal file
7
cpp/2_Concepts_and_Techniques/convolutionTexture/.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(convolutionTexture 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 convolutionTexture
|
||||
add_executable(convolutionTexture convolutionTexture.cu convolutionTexture_gold.cpp main.cpp)
|
||||
|
||||
target_compile_options(convolutionTexture PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(convolutionTexture PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(convolutionTexture PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
target_include_directories(convolutionTexture PUBLIC
|
||||
${CUDAToolkit_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
32
cpp/2_Concepts_and_Techniques/convolutionTexture/README.md
Normal file
32
cpp/2_Concepts_and_Techniques/convolutionTexture/README.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# convolutionTexture - Texture-based Separable Convolution
|
||||
|
||||
## Description
|
||||
|
||||
Texture-based implementation of a separable 2D convolution with a gaussian kernel. Used for performance comparison against convolutionSeparable.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Image Processing, Texture, 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, cudaMallocArray, cudaFreeArray, cudaFree, cudaMemcpyToArray, cudaDeviceSynchronize, cudaCreateTextureObject, cudaMemcpyToSymbol, cudaMalloc
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
|
||||
## References (for more details)
|
||||
@@ -0,0 +1,153 @@
|
||||
/* 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 <helper_cuda.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "convolutionTexture_common.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GPU-specific defines
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Maps to a single instruction on G8x / G9x / G10x
|
||||
#define IMAD(a, b, c) (__mul24((a), (b)) + (c))
|
||||
|
||||
// Use unrolled innermost convolution loop
|
||||
#define UNROLL_INNER 1
|
||||
|
||||
// Round a / b to nearest higher integer value
|
||||
inline int iDivUp(int a, int b) { return (a % b != 0) ? (a / b + 1) : (a / b); }
|
||||
|
||||
// Align a to nearest higher multiple of b
|
||||
inline int iAlignUp(int a, int b) { return (a % b != 0) ? (a - a % b + b) : a; }
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Convolution kernel and input array storage
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__constant__ float c_Kernel[KERNEL_LENGTH];
|
||||
|
||||
extern "C" void setConvolutionKernel(float *h_Kernel)
|
||||
{
|
||||
cudaMemcpyToSymbol(c_Kernel, h_Kernel, KERNEL_LENGTH * sizeof(float));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Loop unrolling templates, needed for best performance
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
template <int i> __device__ float convolutionRow(float x, float y, cudaTextureObject_t texSrc)
|
||||
{
|
||||
return tex2D<float>(texSrc, x + (float)(KERNEL_RADIUS - i), y) * c_Kernel[i] + convolutionRow<i - 1>(x, y, texSrc);
|
||||
}
|
||||
|
||||
template <> __device__ float convolutionRow<-1>(float x, float y, cudaTextureObject_t texSrc) { return 0; }
|
||||
|
||||
template <int i> __device__ float convolutionColumn(float x, float y, cudaTextureObject_t texSrc)
|
||||
{
|
||||
return tex2D<float>(texSrc, x, y + (float)(KERNEL_RADIUS - i)) * c_Kernel[i]
|
||||
+ convolutionColumn<i - 1>(x, y, texSrc);
|
||||
}
|
||||
|
||||
template <> __device__ float convolutionColumn<-1>(float x, float y, cudaTextureObject_t texSrc) { return 0; }
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Row convolution filter
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void convolutionRowsKernel(float *d_Dst, int imageW, int imageH, cudaTextureObject_t texSrc)
|
||||
{
|
||||
const int ix = IMAD(blockDim.x, blockIdx.x, threadIdx.x);
|
||||
const int iy = IMAD(blockDim.y, blockIdx.y, threadIdx.y);
|
||||
const float x = (float)ix + 0.5f;
|
||||
const float y = (float)iy + 0.5f;
|
||||
|
||||
if (ix >= imageW || iy >= imageH) {
|
||||
return;
|
||||
}
|
||||
|
||||
float sum = 0;
|
||||
|
||||
#if (UNROLL_INNER)
|
||||
sum = convolutionRow<2 * KERNEL_RADIUS>(x, y, texSrc);
|
||||
#else
|
||||
|
||||
for (int k = -KERNEL_RADIUS; k <= KERNEL_RADIUS; k++) {
|
||||
sum += tex2D<float>(texSrc, x + (float)k, y) * c_Kernel[KERNEL_RADIUS - k];
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
d_Dst[IMAD(iy, imageW, ix)] = sum;
|
||||
}
|
||||
|
||||
extern "C" void convolutionRowsGPU(float *d_Dst, cudaArray *a_Src, int imageW, int imageH, cudaTextureObject_t texSrc)
|
||||
{
|
||||
dim3 threads(16, 12);
|
||||
dim3 blocks(iDivUp(imageW, threads.x), iDivUp(imageH, threads.y));
|
||||
|
||||
convolutionRowsKernel<<<blocks, threads>>>(d_Dst, imageW, imageH, texSrc);
|
||||
getLastCudaError("convolutionRowsKernel() execution failed\n");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Column convolution filter
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__global__ void convolutionColumnsKernel(float *d_Dst, int imageW, int imageH, cudaTextureObject_t texSrc)
|
||||
{
|
||||
const int ix = IMAD(blockDim.x, blockIdx.x, threadIdx.x);
|
||||
const int iy = IMAD(blockDim.y, blockIdx.y, threadIdx.y);
|
||||
const float x = (float)ix + 0.5f;
|
||||
const float y = (float)iy + 0.5f;
|
||||
|
||||
if (ix >= imageW || iy >= imageH) {
|
||||
return;
|
||||
}
|
||||
|
||||
float sum = 0;
|
||||
|
||||
#if (UNROLL_INNER)
|
||||
sum = convolutionColumn<2 * KERNEL_RADIUS>(x, y, texSrc);
|
||||
#else
|
||||
|
||||
for (int k = -KERNEL_RADIUS; k <= KERNEL_RADIUS; k++) {
|
||||
sum += tex2D<float>(texSrc, x, y + (float)k) * c_Kernel[KERNEL_RADIUS - k];
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
d_Dst[IMAD(iy, imageW, ix)] = sum;
|
||||
}
|
||||
|
||||
extern "C" void
|
||||
convolutionColumnsGPU(float *d_Dst, cudaArray *a_Src, int imageW, int imageH, cudaTextureObject_t texSrc)
|
||||
{
|
||||
dim3 threads(16, 12);
|
||||
dim3 blocks(iDivUp(imageW, threads.x), iDivUp(imageH, threads.y));
|
||||
|
||||
convolutionColumnsKernel<<<blocks, threads>>>(d_Dst, imageW, imageH, texSrc);
|
||||
getLastCudaError("convolutionColumnsKernel() execution failed\n");
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/* 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 CONVOLUTIONTEXTURE_COMMON_H
|
||||
#define CONVOLUTIONTEXTURE_COMMON_H
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Convolution kernel size (the only parameter inlined in the code)
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#define KERNEL_RADIUS 8
|
||||
#define KERNEL_LENGTH (2 * KERNEL_RADIUS + 1)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Reference CPU convolution
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void convolutionRowsCPU(float *h_Dst, float *h_Src, float *h_Kernel, int imageW, int imageH, int kernelR);
|
||||
|
||||
extern "C" void convolutionColumnsCPU(float *h_Dst, float *h_Src, float *h_Kernel, int imageW, int imageH, int kernelR);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GPU texture-based convolution
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void setConvolutionKernel(float *h_Kernel);
|
||||
|
||||
extern "C" void convolutionRowsGPU(float *d_Dst, cudaArray *a_Src, int imageW, int imageH, cudaTextureObject_t texSrc);
|
||||
|
||||
extern "C" void
|
||||
convolutionColumnsGPU(float *d_Dst, cudaArray *a_Src, int imageW, int imageH, cudaTextureObject_t texSrc);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,78 @@
|
||||
/* 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 "convolutionTexture_common.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Reference row convolution filter
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void convolutionRowsCPU(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 = 0;
|
||||
|
||||
if (d >= imageW)
|
||||
d = imageW - 1;
|
||||
|
||||
sum += h_Src[y * imageW + d] * h_Kernel[kernelR - k];
|
||||
}
|
||||
|
||||
h_Dst[y * imageW + x] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Reference column convolution filter
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void convolutionColumnsCPU(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 = 0;
|
||||
|
||||
if (d >= imageH)
|
||||
d = imageH - 1;
|
||||
|
||||
sum += h_Src[d * imageW + x] * h_Kernel[kernelR - k];
|
||||
}
|
||||
|
||||
h_Dst[y * imageW + x] = sum;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
197
cpp/2_Concepts_and_Techniques/convolutionTexture/main.cpp
Normal file
197
cpp/2_Concepts_and_Techniques/convolutionTexture/main.cpp
Normal file
@@ -0,0 +1,197 @@
|
||||
/* 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 the same algorithm as the convolutionSeparable
|
||||
* CUDA Sample, but without using the shared memory at all.
|
||||
* Instead, it uses textures in exactly the same way an OpenGL-based
|
||||
* implementation would do.
|
||||
* Refer to the "Performance" section of convolutionSeparable whitepaper.
|
||||
*/
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_functions.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "convolutionTexture_common.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Main program
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
float *h_Kernel, *h_Input, *h_Buffer, *h_OutputCPU, *h_OutputGPU;
|
||||
|
||||
cudaArray *a_Src;
|
||||
cudaTextureObject_t texSrc;
|
||||
cudaChannelFormatDesc floatTex = cudaCreateChannelDesc<float>();
|
||||
|
||||
float *d_Output;
|
||||
|
||||
float gpuTime;
|
||||
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
|
||||
const int imageW = 3072;
|
||||
const int imageH = 3072 / 2;
|
||||
const unsigned int iterations = 10;
|
||||
|
||||
printf("[%s] - Starting...\n", argv[0]);
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
sdkCreateTimer(&hTimer);
|
||||
|
||||
printf("Initializing data...\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));
|
||||
checkCudaErrors(cudaMallocArray(&a_Src, &floatTex, imageW, imageH));
|
||||
checkCudaErrors(cudaMalloc((void **)&d_Output, imageW * imageH * sizeof(float)));
|
||||
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypeArray;
|
||||
texRes.res.array.array = a_Src;
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
texDescr.normalizedCoords = false;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeWrap;
|
||||
texDescr.addressMode[1] = cudaAddressModeWrap;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
|
||||
checkCudaErrors(cudaCreateTextureObject(&texSrc, &texRes, &texDescr, NULL));
|
||||
|
||||
srand(2009);
|
||||
|
||||
for (unsigned int i = 0; i < KERNEL_LENGTH; i++) {
|
||||
h_Kernel[i] = (float)(rand() % 16);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < imageW * imageH; i++) {
|
||||
h_Input[i] = (float)(rand() % 16);
|
||||
}
|
||||
|
||||
setConvolutionKernel(h_Kernel);
|
||||
checkCudaErrors(cudaMemcpyToArray(a_Src, 0, 0, h_Input, imageW * imageH * sizeof(float), cudaMemcpyHostToDevice));
|
||||
|
||||
printf("Running GPU rows convolution (%u identical iterations)...\n", iterations);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
|
||||
for (unsigned int i = 0; i < iterations; i++) {
|
||||
convolutionRowsGPU(d_Output, a_Src, imageW, imageH, texSrc);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&hTimer);
|
||||
gpuTime = sdkGetTimerValue(&hTimer) / (float)iterations;
|
||||
printf("Average convolutionRowsGPU() time: %f msecs; //%f Mpix/s\n",
|
||||
gpuTime,
|
||||
imageW * imageH * 1e-6 / (0.001 * gpuTime));
|
||||
|
||||
// While CUDA kernels can't write to textures directly, this copy is
|
||||
// inevitable
|
||||
printf("Copying convolutionRowGPU() output back to the texture...\n");
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
checkCudaErrors(
|
||||
cudaMemcpyToArray(a_Src, 0, 0, d_Output, imageW * imageH * sizeof(float), cudaMemcpyDeviceToDevice));
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&hTimer);
|
||||
gpuTime = sdkGetTimerValue(&hTimer);
|
||||
printf("cudaMemcpyToArray() time: %f msecs; //%f Mpix/s\n", gpuTime, imageW * imageH * 1e-6 / (0.001 * gpuTime));
|
||||
|
||||
printf("Running GPU columns convolution (%i iterations)\n", iterations);
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
convolutionColumnsGPU(d_Output, a_Src, imageW, imageH, texSrc);
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&hTimer);
|
||||
gpuTime = sdkGetTimerValue(&hTimer) / (float)iterations;
|
||||
printf("Average convolutionColumnsGPU() time: %f msecs; //%f Mpix/s\n",
|
||||
gpuTime,
|
||||
imageW * imageH * 1e-6 / (0.001 * gpuTime));
|
||||
|
||||
printf("Reading back GPU results...\n");
|
||||
checkCudaErrors(cudaMemcpy(h_OutputGPU, d_Output, imageW * imageH * sizeof(float), cudaMemcpyDeviceToHost));
|
||||
|
||||
printf("Checking the results...\n");
|
||||
printf("...running convolutionRowsCPU()\n");
|
||||
convolutionRowsCPU(h_Buffer, h_Input, h_Kernel, imageW, imageH, KERNEL_RADIUS);
|
||||
|
||||
printf("...running convolutionColumnsCPU()\n");
|
||||
convolutionColumnsCPU(h_OutputCPU, h_Buffer, h_Kernel, imageW, imageH, KERNEL_RADIUS);
|
||||
|
||||
double delta = 0;
|
||||
double sum = 0;
|
||||
|
||||
for (unsigned int i = 0; i < imageW * imageH; i++) {
|
||||
sum += h_OutputCPU[i] * h_OutputCPU[i];
|
||||
delta += (h_OutputGPU[i] - h_OutputCPU[i]) * (h_OutputGPU[i] - h_OutputCPU[i]);
|
||||
}
|
||||
|
||||
double L2norm = sqrt(delta / sum);
|
||||
printf("Relative L2 norm: %E\n", L2norm);
|
||||
printf("Shutting down...\n");
|
||||
|
||||
checkCudaErrors(cudaFree(d_Output));
|
||||
checkCudaErrors(cudaFreeArray(a_Src));
|
||||
free(h_OutputGPU);
|
||||
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