-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJob_Chapter2_4th.cu
152 lines (134 loc) · 3.13 KB
/
Job_Chapter2_4th.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
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
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <windows.h>
void checkResult(float *A, float *B, const int nx, const int ny)
{
int i = 0;
int j = 0;
int cnt = 0;
double err = 1.0E-6;
for (j = 0; j < ny; j++)
{
for (i = 0; i < nx; i++)
{
if (fabs(A[cnt] - B[cnt]) > err)
{
printf("Do not match...\n");
return;
}
cnt++;
}
}
printf("matched!\n");
}
void initialData(float *a, int nx, int ny)
{
int i = nx;
int j = ny;
int cnt = 0;
for (j = 0; j < ny; j++)
{
for (i = 0; i < nx; i++)
{
a[cnt] = cnt;
cnt++;
}
}
}
// summary matrix on CPU
void sumMatrixOnHost(float *A, float *B, float *C, const int nx, const int ny)
{
int i = 0;
int j = 0;
int cnt = 0;
for (j = 0; j < ny; j++)
{
for (i = 0; i < nx; i++)
{
C[cnt] = A[cnt] + B[cnt];
cnt++;
}
}
}
void PrintMatrix(float *a)
{
int i;
for (i = 1024*1024-10; i < 1024*1024; i++)
{
printf("%f ", a[i]);
}
printf("\n");
}
// summary matrix on GPU
__global__ void sumMatrixOnGPU(float *A, float *B, float *C, int nx, int ny)
{
int x = threadIdx.x + blockDim.x * blockIdx.x;
int y = threadIdx.y + blockDim.y * blockIdx.y;
int idx = y*nx + 2*x;
if (x < nx && y < ny)
{
for (int j = 0; j < 2; j++)
{
C[idx + j] = A[idx + j] + B[idx + j];
}
}
}
__global__ void test()
{
printf("hello\n");
}
int main(int argc, char *argv[])
{
int dev = 0;
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, dev);
cudaSetDevice(dev);
int nx = 1 << 10; // 此处对显卡的限制比较明显,书中可以让nx和ny分别为1<<14,所以nx*ny = 1<<28,但是我的显卡不行。
int ny = 1 << 10;
int nxy = nx * ny;
int nBytes = sizeof(float)*nxy;
printf("Matrix size: nx:%d, ny:%d\n", nx, ny);
float *h_A, *h_B, *h_C, *gpuRef;
float *d_A, *d_B, *d_C;
h_A = (float *)malloc(nBytes);
h_B = (float *)malloc(nBytes);
h_C = (float *)malloc(nBytes);
gpuRef = (float *)malloc(nBytes);
memset(gpuRef, 0, nBytes);
cudaMalloc((void **)&d_A, nBytes);
cudaMalloc((void **)&d_B, nBytes);
cudaMalloc((void **)&d_C, nBytes);
// initialize the data
initialData(h_A, nx, ny);
initialData(h_B, nx, ny);
// copy the data from CPU to GPU
cudaMemcpy(d_A, h_A, nBytes, cudaMemcpyHostToDevice);
cudaMemcpy(d_B, h_B, nBytes, cudaMemcpyHostToDevice);
// call the summary function
sumMatrixOnHost(h_A, h_B, h_C, nx, ny);
dim3 block(32);
dim3 grid((nx/2 + block.x - 1) / block.x, ny);
printf("sumMatrixOnGPU <<<(%d, %d), (%d, %d)>>>\n", grid.x, grid.y, block.x, block.y);
sumMatrixOnGPU << <grid, block >> >(d_A, d_B, d_C, nx, ny);
cudaDeviceSynchronize();
// copy the data from GPU to CPU
cudaMemcpy(gpuRef, d_C, nBytes, cudaMemcpyDeviceToHost);
// check the result
checkResult(h_C, gpuRef, nx, ny);
PrintMatrix(h_C);
PrintMatrix(gpuRef);
// free the memory
free(h_A);
free(h_B);
free(h_C);
free(gpuRef);
cudaFree(d_A);
cudaFree(d_B);
cudaFree(d_C);
cudaDeviceReset();
return 0;
}