-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
176 lines (151 loc) · 5.02 KB
/
Copy pathutils.cpp
File metadata and controls
176 lines (151 loc) · 5.02 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <sys/time.h>
#include "defines.h"
#include "CL/opencl.h"
#include "AOCLUtils/aocl_utils.h"
using namespace aocl_utils;
unsigned int convert_endian_4bytes(unsigned int input){
unsigned char* bytes = (unsigned char*) &input;
return (unsigned int) bytes[3] | (((unsigned int) bytes[2]) << 8) | (((unsigned int) bytes[1]) << 16) | (((unsigned int) bytes[0]) << 24);
}
void write_weights_file(char *filename, float *weights, int num_weights) {
FILE *f = fopen(filename, "wb");
if (f == NULL){
printf("ERROR: could not open %s\n",filename);
return;
}
fwrite(weights, sizeof(float), num_weights, f);
fclose(f);
}
// TODO: You may need to modify this file to read in files with differently sized weights.
bool read_weights_file(char *filename, float *weights, int width) {
FILE *f = fopen(filename, "rb");
if (f == NULL){
printf("ERROR: could not open %s\n",filename);
return false;
}
//printf("%d\n", FEATURE_COUNT);
int read_elements = -1;
if (width == 16) {
read_elements = fread(weights, sizeof(short), FEATURE_COUNT, f);
}else if(width == 8) {
read_elements = fread(weights, sizeof(char), FEATURE_COUNT, f);
}else if(width == 4) {
read_elements = fread(weights, sizeof(char), FEATURE_COUNT/2, f);
}else {
read_elements = fread(weights, sizeof(float), FEATURE_COUNT, f);
//for (int i = 0; i<FEATURE_COUNT; i++) {
//printf("%f\n",weights[i]);
//}
}
fclose(f);
//TODO: Uncomment with fix
if (read_elements != FEATURE_COUNT){
if(width == 4 && read_elements != FEATURE_COUNT/2) {
printf("ERROR: read incorrect number of weights from %s\n", filename);
return false;
} else {
return true;
}
printf("ERROR: read incorrect number of weights from %s\n", filename);
return false;
}
return true;
}
int parse_MNIST_images(const char* file, unsigned char** X){
int n_items = 0, magic_num = 0, n_rows = 0, n_cols = 0, elements_read = 0;
FILE* f = fopen(file, "rb");
if (f == NULL){
printf("ERROR: Could not open %s\n",file);
return 0;
}
elements_read = fread(&magic_num, sizeof(int), 1, f );
if (convert_endian_4bytes(magic_num) != MNIST_IMAGE_FILE_MAGIC_NUMBER){
printf("WARNING: Magic number mismatch in %s.\n", file);
}
elements_read = fread(&n_items, sizeof(int), 1, f );
elements_read = fread(&n_rows, sizeof(int), 1, f );
elements_read = fread(&n_cols, sizeof(int), 1, f );
n_items = convert_endian_4bytes(n_items);
n_rows = convert_endian_4bytes(n_rows);
n_cols = convert_endian_4bytes(n_cols);
// n_rows * n_cols should equal FEATURE_COUNT (28*28 = 784)
if (n_rows*n_cols != FEATURE_COUNT){
printf("ERROR: Unexpected image size in %s.\n", file);
return 0;
}
*X = (cl_uchar*)alignedMalloc(n_items*FEATURE_COUNT*sizeof(unsigned char));
unsigned char* X_ptr = *X;
// Read in the pixels. Each pixel is 1 byte. There should be n_items*n_rows*n_cols pixels).
elements_read = fread(X_ptr, sizeof(unsigned char), n_rows*n_cols*n_items, f);
if (elements_read != n_rows*n_cols*n_items){
printf("ERROR: Unexpected file length for %s.\n", file);
free(*X);
return 0;
}
return n_items;
}
int parse_MNIST_labels(const char* file, unsigned char** y){
int n_items = 0;
int magic_num = 0;
int elements_read = 0;
FILE* f = fopen(file, "rb");
if (f == NULL){
printf("ERROR: Could not open %s\n",file);
return 0;
}
elements_read = fread(&magic_num, sizeof(int), 1, f );
if (convert_endian_4bytes(magic_num) != MNIST_LABEL_FILE_MAGIC_NUMBER){
printf("WARNING: Magic number mismatch in %s.\n", file);
}
elements_read = fread(&n_items, sizeof(int), 1, f );
n_items = convert_endian_4bytes(n_items);
*y = (cl_uchar*) alignedMalloc(n_items*sizeof(unsigned char));
// Read in the pixels. Each pixel is 1 byte. There should be n_items*n_rows*n_cols pixels).
elements_read = fread(*y, sizeof(unsigned char), n_items, f);
if (elements_read != n_items){
printf("ERROR: Unexpected file length for %s.\n", file);
free(*y);
return 0;
}
return n_items;
}
void parse_arguments(int argc, char *argv[], int *task, float *alpha, int *iterations, int *n_items_limit) {
// Setting task based on FIRST argument
*task = UNKNOWN;
if(argc >= 2) {
if(strcmp(argv[1], "train") == 0)
*task = TRAIN;
else if(strcmp(argv[1], "test") == 0)
*task = TEST;
}
if(*task != UNKNOWN) {
// Set default values.
*alpha = DEFAULT_ALPHA;
*iterations = DEFAULT_ITERATIONS;
*n_items_limit = DEFAULT_N_ITEMS_LIMIT;
// Read command line arguments.
for(int i = 2; i < argc - 1; i++) {
if (strcmp(argv[i], "--alpha") == 0)
*alpha = atof(argv[i + 1]);
else if (strcmp(argv[i], "--alpha_int") == 0)
*alpha = atoi(argv[i + 1]);
else if (strcmp(argv[i], "--iter") == 0)
*iterations = atoi(argv[i + 1]);
else if (strcmp(argv[i], "--nitems") == 0)
*n_items_limit = atoi(argv[i + 1]);
}
}
}
double get_wall_time(){
struct timeval time;
if (gettimeofday(&time,NULL)){
// Handle error
return 0;
}
return (double)time.tv_sec*1000 + (double)time.tv_usec /1000;
}