mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-26 06:38:16 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -25,10 +25,10 @@
|
||||
* 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 <helper_cuda.h>
|
||||
|
||||
#include "convolutionTexture_common.h"
|
||||
|
||||
@@ -52,111 +52,102 @@ inline int iAlignUp(int a, int b) { return (a % b != 0) ? (a - a % b + b) : a; }
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
__constant__ float c_Kernel[KERNEL_LENGTH];
|
||||
|
||||
extern "C" void setConvolutionKernel(float *h_Kernel) {
|
||||
cudaMemcpyToSymbol(c_Kernel, h_Kernel, KERNEL_LENGTH * sizeof(float));
|
||||
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 <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 <> __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 <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;
|
||||
}
|
||||
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;
|
||||
__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;
|
||||
}
|
||||
if (ix >= imageW || iy >= imageH) {
|
||||
return;
|
||||
}
|
||||
|
||||
float sum = 0;
|
||||
float sum = 0;
|
||||
|
||||
#if (UNROLL_INNER)
|
||||
sum = convolutionRow<2 * KERNEL_RADIUS>(x, y, texSrc);
|
||||
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];
|
||||
}
|
||||
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;
|
||||
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));
|
||||
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");
|
||||
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;
|
||||
__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;
|
||||
}
|
||||
if (ix >= imageW || iy >= imageH) {
|
||||
return;
|
||||
}
|
||||
|
||||
float sum = 0;
|
||||
float sum = 0;
|
||||
|
||||
#if (UNROLL_INNER)
|
||||
sum = convolutionColumn<2 * KERNEL_RADIUS>(x, y, texSrc);
|
||||
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];
|
||||
}
|
||||
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;
|
||||
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));
|
||||
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");
|
||||
convolutionColumnsKernel<<<blocks, threads>>>(d_Dst, imageW, imageH, texSrc);
|
||||
getLastCudaError("convolutionColumnsKernel() execution failed\n");
|
||||
}
|
||||
|
||||
@@ -39,23 +39,18 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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 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);
|
||||
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 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);
|
||||
extern "C" void
|
||||
convolutionColumnsGPU(float *d_Dst, cudaArray *a_Src, int imageW, int imageH, cudaTextureObject_t texSrc);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -30,46 +30,49 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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;
|
||||
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;
|
||||
for (int k = -kernelR; k <= kernelR; k++) {
|
||||
int d = x + k;
|
||||
|
||||
if (d < 0) d = 0;
|
||||
if (d < 0)
|
||||
d = 0;
|
||||
|
||||
if (d >= imageW) d = imageW - 1;
|
||||
if (d >= imageW)
|
||||
d = imageW - 1;
|
||||
|
||||
sum += h_Src[y * imageW + d] * h_Kernel[kernelR - k];
|
||||
}
|
||||
sum += h_Src[y * imageW + d] * h_Kernel[kernelR - k];
|
||||
}
|
||||
|
||||
h_Dst[y * imageW + x] = sum;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
for (int k = -kernelR; k <= kernelR; k++) {
|
||||
int d = y + k;
|
||||
|
||||
if (d < 0) d = 0;
|
||||
if (d < 0)
|
||||
d = 0;
|
||||
|
||||
if (d >= imageH) d = imageH - 1;
|
||||
if (d >= imageH)
|
||||
d = imageH - 1;
|
||||
|
||||
sum += h_Src[d * imageW + x] * h_Kernel[kernelR - k];
|
||||
}
|
||||
sum += h_Src[d * imageW + x] * h_Kernel[kernelR - k];
|
||||
}
|
||||
|
||||
h_Dst[y * imageW + x] = sum;
|
||||
}
|
||||
h_Dst[y * imageW + x] = sum;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,174 +33,165 @@
|
||||
* Refer to the "Performance" section of convolutionSeparable whitepaper.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <helper_functions.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;
|
||||
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>();
|
||||
cudaArray *a_Src;
|
||||
cudaTextureObject_t texSrc;
|
||||
cudaChannelFormatDesc floatTex = cudaCreateChannelDesc<float>();
|
||||
|
||||
float *d_Output;
|
||||
float *d_Output;
|
||||
|
||||
float gpuTime;
|
||||
float gpuTime;
|
||||
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
|
||||
const int imageW = 3072;
|
||||
const int imageH = 3072 / 2;
|
||||
const unsigned int iterations = 10;
|
||||
const int imageW = 3072;
|
||||
const int imageH = 3072 / 2;
|
||||
const unsigned int iterations = 10;
|
||||
|
||||
printf("[%s] - Starting...\n", argv[0]);
|
||||
printf("[%s] - Starting...\n", argv[0]);
|
||||
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
// use command-line specified CUDA device, otherwise use device with highest
|
||||
// Gflops/s
|
||||
findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
sdkCreateTimer(&hTimer);
|
||||
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)));
|
||||
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));
|
||||
cudaResourceDesc texRes;
|
||||
memset(&texRes, 0, sizeof(cudaResourceDesc));
|
||||
|
||||
texRes.resType = cudaResourceTypeArray;
|
||||
texRes.res.array.array = a_Src;
|
||||
texRes.resType = cudaResourceTypeArray;
|
||||
texRes.res.array.array = a_Src;
|
||||
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
cudaTextureDesc texDescr;
|
||||
memset(&texDescr, 0, sizeof(cudaTextureDesc));
|
||||
|
||||
texDescr.normalizedCoords = false;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeWrap;
|
||||
texDescr.addressMode[1] = cudaAddressModeWrap;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
texDescr.normalizedCoords = false;
|
||||
texDescr.filterMode = cudaFilterModeLinear;
|
||||
texDescr.addressMode[0] = cudaAddressModeWrap;
|
||||
texDescr.addressMode[1] = cudaAddressModeWrap;
|
||||
texDescr.readMode = cudaReadModeElementType;
|
||||
|
||||
checkCudaErrors(cudaCreateTextureObject(&texSrc, &texRes, &texDescr, NULL));
|
||||
checkCudaErrors(cudaCreateTextureObject(&texSrc, &texRes, &texDescr, NULL));
|
||||
|
||||
srand(2009);
|
||||
srand(2009);
|
||||
|
||||
for (unsigned int i = 0; i < KERNEL_LENGTH; i++) {
|
||||
h_Kernel[i] = (float)(rand() % 16);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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));
|
||||
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);
|
||||
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);
|
||||
}
|
||||
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));
|
||||
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));
|
||||
// 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);
|
||||
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);
|
||||
}
|
||||
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));
|
||||
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("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("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);
|
||||
printf("...running convolutionColumnsCPU()\n");
|
||||
convolutionColumnsCPU(h_OutputCPU, h_Buffer, h_Kernel, imageW, imageH, KERNEL_RADIUS);
|
||||
|
||||
double delta = 0;
|
||||
double sum = 0;
|
||||
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]);
|
||||
}
|
||||
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");
|
||||
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);
|
||||
checkCudaErrors(cudaFree(d_Output));
|
||||
checkCudaErrors(cudaFreeArray(a_Src));
|
||||
free(h_OutputGPU);
|
||||
free(h_Buffer);
|
||||
free(h_Input);
|
||||
free(h_Kernel);
|
||||
|
||||
sdkDeleteTimer(&hTimer);
|
||||
sdkDeleteTimer(&hTimer);
|
||||
|
||||
if (L2norm > 1e-6) {
|
||||
printf("Test failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (L2norm > 1e-6) {
|
||||
printf("Test failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Test passed\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
printf("Test passed\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user