-
Notifications
You must be signed in to change notification settings - Fork 12
/
WriteToCUDATextureAcrossKernels.cu
97 lines (70 loc) · 3.27 KB
/
WriteToCUDATextureAcrossKernels.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include "TimingGPU.cuh"
#include "Utilities.cuh"
texture<float, 1, cudaReadModeElementType> signal_texture;
#define BLOCKSIZE 32
/*************************************************/
/* KERNEL FUNCTION FOR MEDIAN FILTER CALCULATION */
/*************************************************/
__global__ void median_filter_periodic_boundary(float * __restrict__ d_out, const unsigned int N){
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if (tid < N) {
float signal_center = tex1D(signal_texture, (float)(tid + 0.5 - 0) / (float)N);
float signal_before = tex1D(signal_texture, (float)(tid + 0.5 - 1) / (float)N);
float signal_after = tex1D(signal_texture, (float)(tid + 0.5 + 1) / (float)N);
d_out[tid] = (signal_center + signal_before + signal_after) / 3.f;
}
}
/*************************************************/
/* KERNEL FUNCTION FOR MEDIAN FILTER CALCULATION */
/*************************************************/
__global__ void square(float * __restrict__ d_vec, const size_t pitch, const unsigned int N){
unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x;
if (tid < N) d_vec[tid] = 2.f * tid;
}
/********/
/* MAIN */
/********/
int main() {
const int N = 10;
// --- Input/output host array declaration and initialization
float *h_vec = (float *)malloc(N * sizeof(float));
for (int i = 0; i < N; i++) h_vec[i] = (float)i;
// --- Input/output host and device array vectors
size_t pitch;
float *d_vec; gpuErrchk(cudaMallocPitch(&d_vec, &pitch, N * sizeof(float), 1));
printf("pitch = %i\n", pitch);
float *d_out; gpuErrchk(cudaMalloc(&d_out, N * sizeof(float)));
gpuErrchk(cudaMemcpy(d_vec, h_vec, N * sizeof(float), cudaMemcpyHostToDevice));
// --- CUDA texture memory binding and properties definition
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
//Alternatively
//cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
size_t texture_offset = 0;
gpuErrchk(cudaBindTexture2D(&texture_offset, signal_texture, d_vec, channelDesc, N, 1, pitch));
signal_texture.normalized = true;
signal_texture.addressMode[0] = cudaAddressModeWrap;
// --- Median filter kernel execution
median_filter_periodic_boundary<<<iDivUp(N, BLOCKSIZE), BLOCKSIZE>>>(d_out, N);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
gpuErrchk(cudaMemcpy(h_vec, d_out, N * sizeof(float), cudaMemcpyDeviceToHost));
printf("\n\nFirst filtering\n");
for (int i=0; i<N; i++) printf("h_vec[%i] = %f\n", i, h_vec[i]);
// --- Square kernel execution
square<<<iDivUp(N, BLOCKSIZE), BLOCKSIZE>>>(d_vec, pitch, N);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
gpuErrchk(cudaMemcpy(h_vec, d_vec, N * sizeof(float), cudaMemcpyDeviceToHost));
printf("\n\nSquaring\n");
for (int i=0; i<N; i++) printf("h_vec[%i] = %f\n", i, h_vec[i]);
// --- Median filter kernel execution
median_filter_periodic_boundary<<<iDivUp(N, BLOCKSIZE), BLOCKSIZE>>>(d_out, N);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
printf("\n\nSecond filtering\n");
gpuErrchk(cudaMemcpy(h_vec, d_out, N * sizeof(float), cudaMemcpyDeviceToHost));
for (int i=0; i<N; i++) printf("h_vec[%i] = %f\n", i, h_vec[i]);
printf("Test finished\n");
return 0;
}