-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlp.cc
More file actions
483 lines (353 loc) · 12.7 KB
/
mlp.cc
File metadata and controls
483 lines (353 loc) · 12.7 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include <pthread.h>
#include <math.h>
#include <stdio.h>
//#include "carbon_user.h"
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/timeb.h>
#define BILLION 1E9
typedef struct
{
int tid;
int* num_threads;
double** test_input;
int* test_output;
double*** weights;
double** biases;
int* sizes;
int layers;
double** a;
pthread_barrier_t* barrier;
} thread_arg_t;
//double feed_forward(double *weights, double *a, double biase, int size );
double int_layer_one(double *weights, double *a, double biase, int size );
double int_layer_two(double *weights, double *a, double biase, int size );
double output_layer(double *weights, double *a, double biase, int size );
double sigmoid(double z);
int get_index(double *a, int size);
void initialize(double ***weights, double **biases, int layers, int *sizes);
void call_feedforward(int test_index, thread_arg_t *thread_arg);
void* evaluate(void* arg);
void data_reading(double*** weights, double** biases, double **test_input, int *test_output, int layers, int* sizes);
double exponential(double x);
thread_arg_t thread_args[1024];
pthread_t thread_handles[1024];
pthread_mutex_t locks[1024];
const int test_size = 100;
const int input_size = 784;
const int output_size = 10;
int main(int argc, char** argv){
// Program Arguments //
// const int num_threads = atoi(argv[1]);
int index = 1;
const int layers = atoi(argv[index++]);
int* sizes = (int*) malloc((layers+1)*sizeof(int));
for(int i = 0; i < layers + 1; i++){
sizes[i] = atoi(argv[index++]);
}
int* num_threads = (int*) malloc(layers*sizeof(int));
num_threads[0] = atoi(argv[index++]); //the last param
for(int i = 1; i < layers; i++){ //starting at 2nd location in array, give max number of threads (per layer)
if(sizes[i+1]>num_threads[0]) //if size of the next layer > num threads
num_threads[i] = num_threads[0];
else
num_threads[i] = sizes[i+1]; //give threads equal to size, if < num threads
printf("%d\n", num_threads[i]);
}
printf("Testing Input Memory Allocation...\n");
// Inputs of Test Data //
double** test_input = (double**) malloc(test_size*sizeof(double*));
for(int i = 0; i < test_size; i++){
test_input[i] = (double*) malloc(input_size*sizeof(double));
}
printf("Testing Output Memory Allocation...\n");
// Outputs of Test Data //
int* test_output = (int*) malloc(test_size*sizeof(int));
printf("Weights Memory Allocation...\n");
// Allocate memory space for weights //
double*** weights = (double***) malloc(layers*sizeof(double**));
double** biases = (double**) malloc(layers*sizeof(double*));
for(int i = 0; i < layers; i++ ){
weights[i] = (double**) malloc(sizes[i+1]*sizeof(double*));
biases[i] = (double*) malloc(sizes[i+1]*sizeof(double));
for(int j = 0; j < sizes[i+1]; j++){
weights[i][j] = (double*) malloc(sizes[i]*sizeof(double));
}
}
printf("Activation Memory Allocation...\n");
// Memory space for Activations //
double** a = (double**) malloc((layers+1)*sizeof(double*));
for(int i = 0; i < layers+1; i++){
a[i] = (double*) malloc(sizes[i]*sizeof(double));
}
// Read the training inputs and outputs from the files //
data_reading(weights, biases, test_input, test_output, layers, sizes);
pthread_barrier_t barrier;
pthread_barrier_init(&barrier, NULL, num_threads[0]);
for(int i=0; i<sizes[1]; i++)
pthread_mutex_init(&locks[i], NULL);
for (int i = 0; i < num_threads[0]; i++)
{
thread_args[i].tid = i; //thread id (each thread numbered 0-63)
thread_args[i].num_threads = num_threads;
thread_args[i].weights = weights; //the address to the first location of that 3D array
thread_args[i].test_output = test_output;
thread_args[i].test_input = test_input;
thread_args[i].biases = biases;
thread_args[i].sizes = sizes;
thread_args[i].layers = layers;
thread_args[i].a = a; //activation
thread_args[i].barrier = &barrier;
}
struct timespec requestStart, requestEnd;
clock_gettime(CLOCK_REALTIME, &requestStart);
printf("Creating the threads...\n");
//CarbonEnableModels();
for (int i = 1; i < num_threads[0]; i++){
int ret = pthread_create(&thread_handles[i], NULL,evaluate, (void*)&thread_args[i]);
if (ret != 0){
fprintf(stderr, "ERROR spawning thread %i\n", i);
exit(EXIT_FAILURE);
}
}
evaluate((void*)&thread_args[0]);
#ifdef DEBUG
fprintf(stderr, "Created Threads.\n");
#endif
printf("done...\n");
for (int i = 1; i < num_threads[0]; i++){
pthread_join(thread_handles[i], NULL); //as each thread finishes, continue
} //now every evaluate is completed
//CarbonDisableModels();
clock_gettime(CLOCK_REALTIME, &requestEnd);
double accum = ( requestEnd.tv_sec - requestStart.tv_sec ) + ( requestEnd.tv_nsec - requestStart.tv_nsec ) / BILLION;
printf( "%lf\n", accum );
return 0;
}
/* This function is used by evaluate() function
* to obtain the index of largest prediction in
* the output vector of Neural Network */
int get_index(double *a, int size){
double temp = 0.0;
int index = 0;
for(int i = 0; i < size; i++){
if(a[i] > temp){ temp = a[i]; index = i; }
}
return index;
}
/* This function is to calculate the output of each neron for given
* inputs, weights and biase. */
/*double feed_forward(double *weights, double *a, double biase, int size ){
double z = 0.0;
for(int i = 0; i < size ; i++){
z += weights[i]*a[i];
}
z += biase;
return sigmoid(z);
}*/
double int_layer_one(double *weights, double *a, double biase, int size ){
double z = 0.0;
for(int i = 0; i < size ; i++){
z += weights[i]*a[i];
}
z += biase;
double result = 1.0/(1.0 + exponential(-z));
return result;
}
double int_layer_two(double *weights, double *a, double biase, int size ){
double z = 0.0;
for(int i = 0; i < size ; i++){
z += weights[i]*a[i];
}
z += biase;
double result = 1.0/(1.0 + exponential(-z));
return result;
}
double output_layer(double *weights, double *a, double biase, int size ){
double z = 0.0;
for(int i = 0; i < size ; i++){
z += weights[i]*a[i];
}
z += biase;
double result = 1.0/(1.0 + exponential(-z));
return result;
}
/* This function to evaluate the effectiveness of the training, it calls the function return_output()
* to calculate the output of the Neural Network for each test data. For each test data obtained result
* then compared with the desired output.
*/
void *evaluate(void *arg){
thread_arg_t* thread_arg = (thread_arg_t*) arg;
int tid = thread_arg->tid;
int* num_threads = thread_arg->num_threads;
double** test_input = thread_arg->test_input;
int* test_output = thread_arg->test_output;
double*** weights = thread_arg->weights;
double** biases = thread_arg->biases;
int* sizes = thread_arg->sizes;
int layers = thread_arg->layers;
double** a = thread_arg->a;
int counter;
if(tid == 0){
counter = 0;
}
for(int k = 0; k < test_size; k++){
for(int i = 1; i < layers+1; i++){
if(i == 1){
// printf("layer %d, tid %d\n", i, tid);
//for (int j = start; j < stop; j++){
for (int j = tid; j < sizes[i]; j=j+num_threads[i-1]){
a[i][j] = int_layer_one(weights[i-1][j], test_input[k], biases[i-1][j], sizes[i-1]);
//a[i][j] = feed_forward(weights[i-1][j], test_input[k], biases[i-1][j], sizes[i-1]);
}
pthread_barrier_wait(thread_arg->barrier);
}
else if (i == 2){
// printf("layer %d, tid %d\n", i, tid);
for (int j = tid; j < sizes[i]; j=j+num_threads[i-1]){
a[i][j] = int_layer_two(weights[i-1][j], a[i-1], biases[i-1][j], sizes[i-1]);
//a[i][j] = feed_forward(weights[i-1][j], a[i-1], biases[i-1][j], sizes[i-1]);
}
pthread_barrier_wait(thread_arg->barrier);
}
else {
//for (int j = start; j < stop; j++){
// printf("layer %d, tid %d\n", i, tid);
for (int j = tid; j < sizes[i]; j=j+num_threads[i-1]){
a[i][j] = output_layer(weights[i-1][j], a[i-1], biases[i-1][j], sizes[i-1]);
//a[i][j] = feed_forward(weights[i-1][j], a[i-1], biases[i-1][j], sizes[i-1]);
}
pthread_barrier_wait(thread_arg->barrier);
}
// }
}
if(get_index(a[layers], sizes[layers]) == test_output[k] && tid == 0) {
counter++;
}
}
if(tid == 0){
printf("%d : %d\n", counter, test_size);
printf("%lf%%\n", 100.0*counter/test_size);
}
}
/* Reading the Training and Test Data and parsing training output data */
void data_reading(double*** weights, double** biases, double **test_input, int *test_output, int layers, int* sizes){
int rv;
FILE *file;
file = fopen("data/weights", "r");
if (file == NULL) {
printf("ERROR: Unable to open file '%s'.\n", "weights");
exit(1);
}
for(int i = 0 ; i < layers; i++){
for(int j = 0 ; j < sizes[i+1]; j++){
for(int k = 0; k < sizes[i]; k++){
rv = fscanf(file, "%lf", &weights[i][j][k]); //weights[current layer][output neurons][input neurons]
//weights[i][j][k] = drand48();
}
}
}
file = fopen("data/biases", "r");
if (file == NULL) {
printf("ERROR: Unable to open file '%s'.\n", "biases");
exit(1);
}
for(int i = 0 ; i < layers; i++){
for(int j = 0 ; j < sizes[i+1]; j++){
rv = fscanf(file, "%lf", &biases[i][j]);
//biases[i][j] = drand48();
}
}
printf("Reading the Test Input...\n");
file = fopen("data/test_input", "r");
if (file == NULL) {
printf("ERROR: Unable to open file '%s'.\n", "test_input");
exit(1);
}
for(int i = 0; i < test_size; i++){
for (int j=0; j< input_size; j++) {
rv = fscanf(file, "%lf", &test_input[i][j]);
}
}
fclose(file);
printf("Reading the Test Output...\n");
file = fopen("data/test_output", "r");
if (file == NULL) {
printf("ERROR: Unable to open file '%s'.\n", "test_output");
exit(1);
}
for(int i = 0; i < test_size; i++){
rv = fscanf(file, "%d", &test_output[i]);
}
fclose(file);
printf("Data parsing is done...\n");
}
/* Sigmoid function and its derivative*/
double sigmoid(double z){
return 1.0/(1.0 + exponential(-z));
}
double exponential(double x)
{
double sum = 1.0f; // initialize sum of series
for (int i = 100 - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
void read_parameters(double*** weights, double** biases, int layers, int* sizes){
int rv;
FILE *file;
file = fopen("weights", "r");
if(file == NULL)
{
printf( "Error openning file 'weights' !\n");
exit(1);
}
for(int i = 0; i < layers; i++){
for (int j=0; j< sizes[i+1]; j++) {
for(int k = 0; k < sizes[i]; k++){
rv = fscanf(file, "%lf", &weights[i][j][k]);
}
}
}
fclose(file);
file = fopen("biases", "r");
if(file == NULL)
{
printf("Error openning file 'weights' !\n");
exit(1);
}
for(int i = 0; i < layers; i++){
for (int j=0; j< sizes[i+1]; j++) {
rv = fscanf(file, "%lf", &biases[i][j]);
}
}
}
void write_parameters(double*** weights, double** biases, int layers, int* sizes){
FILE *file = fopen("weights", "w");
if(file == NULL)
{
printf("Error openning file 'weights' !\n");
exit(1);
}
for(int i = 0; i < layers; i++){
for(int j = 0; j < sizes[i+1]; j++){
for(int k = 0; k < sizes[i]; k++){
fprintf(file,"%lf\t", weights[i][j][k]);
}
}
}
fclose(file);
file = fopen("biases", "w");
if(file == NULL)
{
printf("Error openning file 'biases' !\n");
exit(1);
}
for(int i = 0 ; i < layers; i++){
for(int j = 0 ; j < sizes[i+1]; j++){
fprintf(file, "%lf\t", biases[i][j]);
}
}
fclose(file);
}