-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.c
328 lines (318 loc) · 11.6 KB
/
multi.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include"multi.h"
#include"backpropagation.h"
#include"pthread.h"
void* Thread_Convolution(void *args)
{
struct ConvolutionArgs *convolution_args = (struct ConvolutionArgs*)args;
int channel = convolution_args->input->channelSize ;
int outputSize = convolution_args->output->rowSize;
int kernelSize = convolution_args->kernel[0]->rowSize ;
int kernelAmt = convolution_args->output->channelSize;
int stride = convolution_args->stride;
int paddingAmt = convolution_args->paddingAmt;
int maxRow = convolution_args->input->rowSize;
int maxColumn = convolution_args->input->columnSize;
for(int num = 0 ; num < kernelAmt ; num ++)
{
for(int j = 0 ; j < outputSize ; j++)
{
for(int k = 0 ; k < outputSize ; k++ )
{
float temp = 0; // store the temporary after convolution give the value back to the output
for(int i = 0 ; i < channel ; i ++)
{
for(int y = 0 ; y < kernelSize ; y ++)
{
for(int z = 0 ; z < kernelSize ; z++ )
{
int rowLoc = j*stride+y-paddingAmt;
int columnLoc = k*stride+z-paddingAmt;
//calculate the column is padding or not
if ((rowLoc < 0 || rowLoc >= maxRow ) || (columnLoc < 0 || columnLoc >= maxColumn ))
{
//use to ignore the padding term
continue;
}
temp += (convolution_args->input->feature[i][rowLoc][columnLoc] * convolution_args->kernel[num]->feature[i][y][z]);
//printf("%f",temp);
}
}
}
convolution_args->output->feature[num][j][k] = temp;
//printf("before :%f",convolution_args->output->feature[num][j][k]);
//printf(" after :%f\n",convolution_args->output->feature[num][j][k]);
}
}
}
free(args);
return NULL;
}
void Multi_Convolution(struct Matrix **input,struct Matrix **kernel,struct Matrix **output ,int paddingAmt,int stride)
{
for(int num = 0 ; num < BATCH ; num = num + THREADSIZE)
{
for(int i = 0 ; i < THREADSIZE ; i++)
{
CONV_Args[i].input = input[num + i];
CONV_Args[i].kernel = kernel;
CONV_Args[i].output = output[num + i];
CONV_Args[i].paddingAmt = paddingAmt;
CONV_Args[i].stride = stride;
if (pthread_create(&th[i], NULL, Thread_Convolution, &CONV_Args[i]) != 0)
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for(int i = 0 ; i < THREADSIZE ; i++)
{
if (pthread_join(th[i], NULL) != 0)
{
perror("pthread_join");
exit(EXIT_FAILURE);
}
}
}
}
/*
void* Thread_ReLU(void *args)
{
struct Args *relu_args = (struct Args*)args;
int channel = relu_args->input->channelSize;
int size = relu_args->input->rowSize;
for(int i = 0 ; i < channel ; i++)
{
for(int j = 0 ; j < size ; j++)
{
for(int k = 0; k < size ; k++)
{
if(relu_args->input->feature[i][j][k] < 0)
{
relu_args->output->feature[i][j][k] = 0 ;
}
else
{
relu_args->output->feature[i][j][k] = relu_args->input->feature[i][j][k] ;
}
}
}
}
free(args);
}
void Multi_ReLU (struct Matrix **input,struct Matrix **output)
{
for(int num = 0 ; num < BATCH ; num = num + THREADSIZE)
{
for(int i = 0 ; i < THREADSIZE ; i++)
{
matrixArgs[i].input = input[num + i];
matrixArgs[i].output = output[num + i];
if (pthread_create(&th[i], NULL, Thread_ReLU, &matrixArgs[i]) != 0)
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for(int i = 0 ; i < THREADSIZE ; i++)
{
if (pthread_join(th[i], NULL) != 0)
{
perror("pthread_join");
exit(EXIT_FAILURE);
}
}
}
}
*/
void* Thread_MaxPooLing(void *args)
{
struct MaxArgs *max_args = (struct MaxArgs*)args;
int channel = max_args->output->channelSize;
int size = max_args->output->rowSize;
for(int i = 0 ; i < channel ; i ++)
{
for(int j = 0 ; j < size ; j++)
{
for(int k = 0 ; k < size ; k++ )
{
float maximun = -FLT_MAX; // initial with minimun value of float
for(int y = 0 ; y < max_args->poolingSize ; y ++)
{
for(int z = 0 ; z < max_args->poolingSize ; z++ )
{
int rowLoc = j*max_args->stride+y-max_args->paddingAmt;
int columnLoc = k*max_args->stride+z-max_args->paddingAmt;
//calculate the column is padding or not
if ((rowLoc < 0 || rowLoc >= max_args->input->rowSize ) || (columnLoc < 0 || columnLoc >= max_args->input->columnSize))
{
if(maximun < 0)
{
maximun = 0;
}
continue;
}
if(max_args->input->feature[i][rowLoc][columnLoc] > maximun)
{
maximun = max_args->input->feature[i][rowLoc][columnLoc];
}
}
}
max_args->output->feature[i][j][k] = maximun;
}
}
}
free(args);
return NULL;
}
void Multi_MaxPooLing(struct Matrix **input,struct Matrix **output,int poolingSize,int paddingAmt,int stride)
{
for(int num = 0 ; num < BATCH ; num = num + THREADSIZE)
{
for(int i = 0 ; i < THREADSIZE ; i++)
{
Max_Args[i].input = input[num + i];
Max_Args[i].output = output[num + i];
Max_Args[i].poolingSize = poolingSize;
Max_Args[i].paddingAmt = paddingAmt;
Max_Args[i].stride = stride;
if (pthread_create(&th[i], NULL, Thread_MaxPooLing, &Max_Args[i]) != 0)
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for(int i = 0 ; i < THREADSIZE ; i++)
{
if (pthread_join(th[i], NULL) != 0)
{
perror("pthread_join");
exit(EXIT_FAILURE);
}
}
}
}
/*
void* Thread_ToZero(void *args)
{
struct Args *gradientArgs = (struct Args*)args;
int channel = gradientArgs->input->channelSize;
int size = gradientArgs->input->rowSize;
for(int i = 0 ; i < channel ; i++)
{
for(int j = 0 ; j < size ; j++)
{
for(int k = 0; k < size ; k++)
{
gradientArgs->output->feature[i][j][k] = 0 ;
}
}
}
free(args);
}
void Multi_ToZero (struct Matrix **input)
{
for(int num = 0 ; num < BATCH ; num = num + THREADSIZE)
{
for(int i = 0 ; i < THREADSIZE ; i++)
{
matrixArgs[i].input = input[num + i];
matrixArgs[i].output = input[num + i];
if (pthread_create(&th[i], NULL, Thread_ToZero, &matrixArgs[i]) != 0)
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for(int i = 0 ; i < THREADSIZE ; i++)
{
if (pthread_join(th[i], NULL) != 0)
{
perror("pthread_join");
exit(EXIT_FAILURE);
}
}
}
}
*/
void* Thread_Convolution_Variable(void *args)
{
//input = lastterm gradient
//output = gradient
struct ConvolutionArgs *convolution_args = (struct ConvolutionArgs*)args;
int channel = convolution_args->output->channelSize ;
int size = convolution_args->input->rowSize;
int kernelSize = convolution_args->kernel[0]->rowSize ;
int kernelAmt = convolution_args->input->channelSize;
int stride = convolution_args->stride;
int paddingAmt = convolution_args->paddingAmt;
int maxSize = convolution_args->output->rowSize;
for(int num = 0 ; num < kernelAmt ; num ++)
{
for(int j = 0 ; j < size ; j++)
{
for(int k = 0 ; k < size ; k++ )
{
for(int i = 0 ; i < channel ; i ++)
{
for(int y = 0 ; y < kernelSize ; y ++)
{
for(int z = 0 ; z < kernelSize ; z++ )
{
int rowLoc = j*stride+y-paddingAmt;
int columnLoc = k*stride+z-paddingAmt;
//calculate the column is padding or not
if ((rowLoc < 0 || rowLoc >= maxSize ) || (columnLoc < 0 || columnLoc >= maxSize ))
{
//use to ignore the padding term
continue;
}
//dL/dX = dL/dy * dy/dx (only the kernel which sliding and fix the input will affect the gradient )
convolution_args->output->feature[i][rowLoc][columnLoc] += (convolution_args->input->feature[num][j][k] *
convolution_args->kernel[num]->feature[i][y][z]);
}
}
}
}
}
}
free(args);
}
void Multi_GradientConvolution(struct Matrix **lastTermGradient ,struct Matrix **kernel ,struct Matrix **gradient,
struct Matrix **variable ,struct Matrix **gradientKernel, int stride, int paddingAmt)
{
int kernelAmt = lastTermGradient[0]->channelSize;
for(int num = 0 ; num < BATCH ; num = num + THREADSIZE)
{
for(int i = 0 ; i < THREADSIZE ; i++)
{
CONV_Args[i].input = lastTermGradient[num + i];
CONV_Args[i].kernel = kernel;
Matrix_ToZero(gradient[num + i]);
CONV_Args[i].output = gradient[num + i];
CONV_Args[i].paddingAmt = paddingAmt;
CONV_Args[i].stride = stride;
if (pthread_create(&th[i], NULL,Thread_Convolution_Variable, &CONV_Args[i]) != 0)
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for(int i = 0 ; i < THREADSIZE/2 ; i++)
{
if (pthread_join(th[i], NULL) != 0)
{
perror("pthread_join");
exit(EXIT_FAILURE);
}
}
}
for(int num = 0 ; num < kernelAmt ; num ++)
{
Matrix_ToZero(gradientKernel[num]);
}
Gradient_Convolution_Kernel(lastTermGradient,gradientKernel,variable,stride,paddingAmt);
for(int num = 0 ; num < kernelAmt ; num ++)
{
Back_Descent(kernel[num],gradientKernel[num]);
}
}