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
| #include "cuda_runtime.h" #include "device_launch_parameters.h"
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h>
static void HandleError(cudaError_t err, const char *file, int line) { if (err != cudaSuccess) { printf("%s in %s at line %d\n", cudaGetErrorString(err), file, line); exit(EXIT_FAILURE); } }
#define HANDLE_ERROR(err) (HandleError(err, __FILE__, __LINE__)) #define HANDLE_NULL(a) {if (a == NULL) { \ printf("Host memory failed in %s at line %d\n", \ __FILE__, __LINE__); \ exit(EXIT_FAILURE);}}
const int N = 1024 * 1024 * 64; const int threadsPerBlock = 256; const int blocksPerGrid = 64;
double function_for_cpu(double x) { return 4 / (1 + x * x); }
__device__ double function_for_gpu(double x) { return 4 / (1 + x * x); }
__global__ void trap(double *a, double *b, double *integral) {
__shared__ double cache[threadsPerBlock];
int tid = threadIdx.x + blockIdx.x * blockDim.x;
int cacheIndex = threadIdx.x;
double x, temp = 0; while (tid < N) { x = *a + (double)(*b - *a) / N * (tid + 0.5); temp += function_for_gpu(x);
tid += blockDim.x * gridDim.x; }
cache[cacheIndex] = temp;
__syncthreads();
int i = blockDim.x / 2; while (i != 0) { if (cacheIndex < i) cache[cacheIndex] += cache[cacheIndex + i]; __syncthreads(); i /= 2; }
if (cacheIndex == 0) integral[blockIdx.x] = cache[0]; }
void trap_by_cpu(double a, double b, double *integral) { int i; double x, temp = 0; for (i = 0; i < N; i++) { x = a + (double)(b - a) / N * (i + 0.5); temp += function_for_cpu(x); } temp *= (double)(b - a) / N; *integral = temp; }
int main(void) { double integral; double *partial_integral; double a, b;
double *dev_partial_integral; double *dev_a, *dev_b; cudaEvent_t start,stop; float tm;
clock_t clockBegin, clockEnd; float duration;
a = 0; b = 1;
clockBegin = clock(); trap_by_cpu(a, b, &integral); clockEnd = clock(); duration = (float)1000 * (clockEnd - clockBegin) / CLOCKS_PER_SEC; printf("CPU Result: %.20lf\n", integral); printf("CPU Elapsed time: %.6lfms\n", duration);
getchar();
cudaEventCreate(&start); cudaEventCreate(&stop); cudaEventRecord(start, 0);
partial_integral = (double*)malloc(blocksPerGrid * sizeof(double));
HANDLE_ERROR(cudaMalloc((void**)&dev_a, sizeof(double))); HANDLE_ERROR(cudaMalloc((void**)&dev_b, sizeof(double))); HANDLE_ERROR(cudaMalloc((void**)&dev_partial_integral, blocksPerGrid * sizeof(double)));
HANDLE_ERROR(cudaMemcpy(dev_a, &a, sizeof(double), cudaMemcpyHostToDevice)); HANDLE_ERROR(cudaMemcpy(dev_b, &b, sizeof(double), cudaMemcpyHostToDevice));
trap<<<blocksPerGrid, threadsPerBlock>>>(dev_a, dev_b, dev_partial_integral);
HANDLE_ERROR(cudaMemcpy(partial_integral, dev_partial_integral, blocksPerGrid * sizeof(double), cudaMemcpyDeviceToHost));
integral = 0; for (int i = 0; i < blocksPerGrid; i++) { integral += partial_integral[i]; } integral *= (double)(b - a) / N;
cudaEventRecord(stop,0); cudaEventSynchronize(stop); cudaEventElapsedTime(&tm, start, stop); printf("GPU Result: %.20lf\n", integral); printf("GPU Elapsed time:%.6f ms.\n", tm);
HANDLE_ERROR(cudaFree(dev_a)); HANDLE_ERROR(cudaFree(dev_b)); HANDLE_ERROR(cudaFree( dev_partial_integral));
free(partial_integral);
getchar(); }
|