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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| #include "../common/common.h" #include <cuda_runtime.h> #include <stdio.h>
#define RADIUS 4 #define BDIM 32
__constant__ float coef[RADIUS + 1];
#define a0 0.00000f #define a1 0.80000f #define a2 -0.20000f #define a3 0.03809f #define a4 -0.00357f
void initialData(float *in, const int size) { for (int i = 0; i < size; i++) { in[i] = (float)( rand() & 0xFF ) / 100.0f; } }
void printData(float *in, const int size) { for (int i = RADIUS; i < size; i++) { printf("%f ", in[i]); }
printf("\n"); }
void setup_coef_constant (void) { const float h_coef[] = {a0, a1, a2, a3, a4}; CHECK(cudaMemcpyToSymbol( coef, h_coef, (RADIUS + 1) * sizeof(float))); }
void cpu_stencil_1d (float *in, float *out, int isize) { for( int i = RADIUS; i <= isize; i++ ) { float tmp = 0.0f; tmp += a1 * (in[i + 1] - in[i - 1]) + a2 * (in[i + 2] - in[i - 2]) + a3 * (in[i + 3] - in[i - 3]) + a4 * (in[i + 4] - in[i - 4]); out[i] = tmp; } }
void checkResult(float *hostRef, float *gpuRef, const int size) { double epsilon = 1.0E-6; bool match = 1;
for (int i = RADIUS; i < size; i++) { if (abs(hostRef[i] - gpuRef[i]) > epsilon) { match = 0; printf("different on %dth element: host %f gpu %f\n", i, hostRef[i], gpuRef[i]); break; } }
if (!match) printf("Arrays do not match.\n\n"); }
__global__ void stencil_1d(float *in, float *out) { __shared__ float smem[BDIM + 2 * RADIUS];
int idx = threadIdx.x + blockIdx.x * blockDim.x;
int sidx = threadIdx.x + RADIUS;
smem[sidx] = in[idx];
if (threadIdx.x < RADIUS) { smem[sidx - RADIUS] = in[idx - RADIUS]; smem[sidx + BDIM] = in[idx + BDIM]; }
__syncthreads();
float tmp = 0.0f; #pragma unroll
for (int i = 1; i <= RADIUS; i++) { tmp += coef[i] * (smem[sidx + i] - smem[sidx - i]); }
out[idx] = tmp; }
__global__ void stencil_1d_read_only (float* in, float* out, const float *__restrict__ dcoef) { __shared__ float smem[BDIM + 2 * RADIUS];
int idx = threadIdx.x + blockIdx.x * blockDim.x;
int sidx = threadIdx.x + RADIUS;
smem[sidx] = in[idx];
if (threadIdx.x < RADIUS) { smem[sidx - RADIUS] = in[idx - RADIUS]; smem[sidx + BDIM] = in[idx + BDIM]; }
__syncthreads();
float tmp = 0.0f; #pragma unroll
for (int i = 1; i <= RADIUS; i++) { tmp += dcoef[i] * (smem[sidx + i] - smem[sidx - i]); }
out[idx] = tmp; }
int main(int argc, char **argv) { int dev = 0; cudaDeviceProp deviceProp; CHECK(cudaGetDeviceProperties(&deviceProp, dev)); printf("%s starting transpose at ", argv[0]); printf("device %d: %s ", dev, deviceProp.name); CHECK(cudaSetDevice(dev));
int isize = 1 << 24;
size_t nBytes = (isize + 2 * RADIUS) * sizeof(float); printf("array size: %d ", isize);
bool iprint = 0;
float *h_in = (float *)malloc(nBytes); float *hostRef = (float *)malloc(nBytes); float *gpuRef = (float *)malloc(nBytes);
float *d_in, *d_out, *d_coef; CHECK(cudaMalloc((float**)&d_in, nBytes)); CHECK(cudaMalloc((float**)&d_out, nBytes)); CHECK(cudaMalloc((float**)&d_coef, (RADIUS + 1) * sizeof(float)));
const float h_coef[] = {a0, a1, a2, a3, a4}; CHECK(cudaMemcpy(d_coef, h_coef, (RADIUS + 1) * sizeof(float), cudaMemcpyHostToDevice);)
initialData(h_in, isize + 2 * RADIUS);
CHECK(cudaMemcpy(d_in, h_in, nBytes, cudaMemcpyHostToDevice));
setup_coef_constant ();
dim3 block (BDIM, 1); dim3 grid (isize / block.x, 1); printf("(grid, block) %d,%d \n ", grid.x, block.x);
stencil_1d<<<grid, block>>>(d_in + RADIUS, d_out + RADIUS);
CHECK(cudaMemcpy(gpuRef, d_out, nBytes, cudaMemcpyDeviceToHost));
cpu_stencil_1d(h_in, hostRef, isize);
checkResult(hostRef, gpuRef, isize);
stencil_1d_read_only<<<grid, block>>>(d_in + RADIUS, d_out + RADIUS, d_coef); CHECK(cudaMemcpy(gpuRef, d_out, nBytes, cudaMemcpyDeviceToHost)); checkResult(hostRef, gpuRef, isize);
if(iprint) { printData(gpuRef, isize); printData(hostRef, isize); }
CHECK(cudaFree(d_in)); CHECK(cudaFree(d_out)); CHECK(cudaFree(d_coef)); free(h_in); free(hostRef); free(gpuRef);
CHECK(cudaDeviceReset()); return EXIT_SUCCESS; }
|