-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
83 lines (66 loc) · 2.77 KB
/
Parser.cpp
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
#include "Parser.h"
Parser::Parser(SharedMem * shmem, vector<string> files, const string path)
{
DEBUG_PRINT("Parser Constructer");
if (!path.empty()){
m_MNISTDataDirectory = path;
}
vector<string>::iterator fileIt = files.begin();
unsigned char buf;
FILE * fp_images = fopen((m_MNISTDataDirectory + *(fileIt++)).c_str(), "r");
FILE * fp_labels = fopen((m_MNISTDataDirectory + *(fileIt++)).c_str(), "r");
if (fp_images == NULL || fp_labels == NULL) {
cerr << "Error Opening File" << endl;
abort();
}
traversePastHeader(fp_images);
traversePastHeader(fp_labels);
while (!feof(fp_images) && !feof(fp_labels)) {
while(!shmem->consumed()) {
//DEBUG_PRINT("Parser waiting");
usleep(1); // this should be modified so that consumer wakes producer
}
DEBUG_PRINT("Parser taking action");
do {
fread(&buf, sizeof(unsigned char), 1, fp_images);
} while (m_digit.addPixel(buf));
fread(&buf, sizeof(unsigned char), 1, fp_labels);
m_digit.setLabel(buf);
// Confirmed through GDB 30/12/18, m_digit.m_label is correct however
// shmem->getDigit().m_label does not. Implies below line is not functional.
shmem->setDigit(m_digit);
shmem->setConsumed(false);
m_digit.clean();
}
fclose(fp_images);
fclose(fp_labels);
}
void Parser::traversePastHeader(FILE * fp)
{
unsigned int buf;
fread(&buf, sizeof(buf), 1, fp);
unsigned int magic_num = endianSwap(buf);
/*****************************************************************************
* The MNIST data begins with a "magic number" which has now been read and
* swapped endianness. For the files they provide there are two cases with
* this number:
*
* Case 1: magic_num == 2049. In this case the file being read is a label
* file and there is only one more item in the header, the number
* of items in the file.
* Case 2: magic_num == 2051. In this case the file being read is an image
* file and there are 3 more items in the header, the number of
* imageshttps://stackoverflow.com/questions/15879761/segmentation-fault-on-fopen, number of rows, and number of columns.
*
* **************************************************************************/
for (unsigned int i = 2048; i < magic_num; i++) {
fread(&buf, sizeof(buf), 1, fp);
}
}
unsigned int Parser::endianSwap(unsigned int c)
{
return ((c >> 24) & 0xff) | // move byte 3 to byte 0
((c << 8) & 0xff0000) | // move byte 1 to byte 2
((c >> 8) & 0xff00) | // move byte 2 to byte 1
((c << 24) & 0xff000000); // byte 0 to byte 3
}