-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuda.cu
185 lines (151 loc) · 4.35 KB
/
cuda.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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
// f(x1, x2, x3, ..., xM) = theta0 * x0 + theta1 * x1 + theta2 * x2 + ... + thetaM * xM
#define M 10
#define N 1000
#define MAX_ITERATIONS 1000
#define ALPHA 0.1
#define ACCURACY_TORLERANCE 0.001
// error handling helper
#define gpuErrchk(ans) \
{ \
gpuAssert((ans), __FILE__, __LINE__); \
}
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort = true)
{
if (code != cudaSuccess)
{
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort)
exit(code);
}
}
/// @brief The function we are trying to find coefficients for
double f(double *x, int x_row, double *theta)
{
int row = x_row * M;
double result = 0;
for (int i = 0; i < M; i++)
{
result += theta[i] * x[row + i];
}
return result;
}
void init(double inputs[N * M], double outputs[N], double theta[M])
{
srand(time(NULL));
for (int i = 0; i < M; i++)
theta[i] = (double)rand() / RAND_MAX;
for (int i = 0; i < N; i++)
{
int upper = (i + 1) * M;
for (int k = i * M; k < upper; k++)
{
inputs[k] = (double)rand() / RAND_MAX;
}
outputs[i] = f(inputs, i, theta);
}
}
void checkThetaAccuracy(double *theta, double *actualTheta)
{
int thetasAreAccurate = 1;
for (int i = 0; i < M; i++)
{
if (abs(theta[i] - actualTheta[i]) > ACCURACY_TORLERANCE)
{
thetasAreAccurate = 0;
break;
}
}
if (thetasAreAccurate)
printf("Thetas are accurate\n");
else
printf("Thetas are not accurate\n");
}
void printError(double inputs[N * M], double outputs[N], double *theta)
{
double error = 0;
for (int n = 0; n < N; n++)
{
double h = 0;
for (int i = 0; i < M; i++)
{
h += inputs[n * M + i] * theta[i];
}
error += abs(h - outputs[n]);
}
error /= N;
printf("error: %lf\n", error);
}
void printThetaMapping(double *expectedTheta, double *calculatedTheta)
{
puts("Expected theta vs computed theta");
for (int i = 0; i < M; i++)
{
printf("%lf -> %lf\n", expectedTheta[i], calculatedTheta[i]);
}
}
__global__ void ComputeThetas(double *inputs, double *outputs, double *theta, double *newTheta)
{
int k = threadIdx.x;
double t = 0;
for (int n = 0; n < N; n++)
{
int input_row = n * M;
double h = 0;
for (int i = 0; i < M; i++)
{
h += inputs[input_row + i] * theta[i];
}
t += (h - outputs[n]) * inputs[input_row + k];
}
t = theta[k] - ALPHA * t / N;
newTheta[k] = t;
}
int main()
{
double inputs[N * M]; // a 1D array instead of 2D because it is easy to copy in oneAPI
double outputs[N];
double actualTheta[M];
init(inputs, outputs, actualTheta);
// theta are the coefficients we are trying to find
double theta[M];
for (int i = 0; i < M; i++)
theta[i] = 0;
const int inputs_size = N * M * sizeof(double);
const int n_size = N * sizeof(double);
const int m_size = M * sizeof(double);
double *d_inputs;
double *d_outputs;
double *d_theta;
double *d_newTheta;
cudaMalloc(&d_inputs, inputs_size);
cudaMalloc(&d_outputs, n_size);
cudaMalloc(&d_theta, m_size);
cudaMalloc(&d_newTheta, m_size);
cudaMemcpy(d_inputs, inputs, inputs_size, cudaMemcpyHostToDevice);
cudaMemcpy(d_outputs, outputs, n_size, cudaMemcpyHostToDevice);
for (int i = 0; i < MAX_ITERATIONS; i++)
{
cudaMemcpy(d_theta, theta, m_size, cudaMemcpyHostToDevice);
ComputeThetas<<<1, M>>>(d_inputs, d_outputs, d_theta, d_newTheta);
double *newTheta = (double *)malloc(m_size);
cudaMemcpy(newTheta, d_newTheta, m_size, cudaMemcpyDeviceToHost);
for (int i = 0; i < M; i++)
theta[i] = newTheta[i];
free(newTheta);
}
cudaFree(d_inputs);
cudaFree(d_outputs);
cudaFree(d_theta);
cudaFree(d_newTheta);
// print theta mappins
printThetaMapping(actualTheta, theta);
// check if thetas are accurate
checkThetaAccuracy(theta, actualTheta);
// check error
printError(inputs, outputs, theta);
return 0;
}