forked from dcampora/cl_forward
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IndependentEntrypoint.cpp
194 lines (168 loc) · 4.68 KB
/
IndependentEntrypoint.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
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
/**
* Autocontained cross-platform independent entrypoint
* for Gaudi offloaded algorithms.
*
* Intended for testing and debugging, completely decoupled
* from the Gaudi framework.
*
* author - Daniel Campora
* email - [email protected]
*
* June, 2014
* CERN
*/
#include <iostream>
#include <string>
#include <cstring>
#include <exception>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <algorithm>
/**
* execute entrypoint of algorithm
* Same signature as offloaded gaudi-algorithm
*
* @param output
* @param input
*/
extern int independent_execute(
const std::vector<std::vector<unsigned char> >& input,
std::vector<std::vector<unsigned char> >& output);
/**
* Post execution entrypoint
* @param output
*/
extern void independent_post_execute(
const std::vector<std::vector<unsigned char> >& output);
void printUsage(char* argv[]){
std::cerr << "Usage: "
<< argv[0] << " <comma separated input filenames>"
<< std::endl;
}
/**
* Generic StrException launcher
*/
class StrException : public std::exception
{
public:
std::string s;
StrException(std::string ss) : s(ss) {}
~StrException() throw () {} // Updated
const char* what() const throw() { return s.c_str(); }
};
/**
* Checks file existence
* @param name
* @return
*/
bool fileExists (const std::string& name) {
if (FILE *file = fopen(name.c_str(), "r")) {
fclose(file);
return true;
} else {
return false;
}
}
/**
* Reads some data from an input file, following the
* specified format of choice
*
* Format expected in file:
*
* int funcNameLen
* char* funcName
* int dataSize
* char* data
*/
void readFileIntoVector(std::string filename, std::vector<unsigned char> & output){
// Check if file exists
if (!fileExists(filename)){
throw StrException("Error: File " + filename + " does not exist.");
}
std::ifstream infile (filename.c_str(), std::ifstream::binary);
// get size of file
infile.seekg(0, std::ifstream::end);
int size = infile.tellg();
infile.seekg(0);
// Read format expected:
// int funcNameLen
// char* funcName
// int dataSize
// char* data
int funcNameLen;
int dataSize;
std::vector<char> funcName;
char* pFuncNameLen = (char*) &funcNameLen;
char* pDataSize = (char*) &dataSize;
infile.read(pFuncNameLen, sizeof(int));
funcName.resize(funcNameLen);
infile.read(&(funcName[0]), funcNameLen);
infile.read(pDataSize, sizeof(int));
// read content of infile with a vector
output.resize(dataSize);
infile.read ((char*) &(output[0]), dataSize);
infile.close();
}
/**
* This is if the function is called on its own
* (ie. non-gaudi execution)
*
* In that case, the file input is expected.
* As a convention, multiple files would be specified
* with comma-separated values
*
* @param argc
* @param argv
* @return
*/
int main(int argc, char *argv[])
{
std::string filename;
int fileNumber = 1;
std::string delimiter = ",";
std::vector<std::vector<unsigned char> > input;
// Get params (getopt independent)
if (argc != 2){
printUsage(argv);
return 0;
}
filename = std::string(argv[1]);
// Check how many files were specified and
// call the entrypoint with the suggested format
if(filename.empty()){
std::cerr << "No filename specified" << std::endl;
printUsage(argv);
return -1;
}
size_t numberOfOcurrences = std::count(filename.begin(), filename.end(), ',') + 1;
input.resize(numberOfOcurrences);
int input_index = 0;
size_t posFound = filename.find(delimiter);
if (posFound != std::string::npos){
size_t prevFound = 0;
while(prevFound != std::string::npos){
if (posFound == std::string::npos){
readFileIntoVector(filename.substr(prevFound, posFound-prevFound), input[input_index]);
prevFound = posFound;
}
else {
readFileIntoVector(filename.substr(prevFound, posFound-prevFound), input[input_index]);
prevFound = posFound + 1;
posFound = filename.find(delimiter, posFound + 1);
}
input_index++;
}
}
else {
readFileIntoVector(filename, input[0]);
}
// Print out first byte from formatter->inputPointer
std::cout << input.size() << " files read" << std::endl;
// Call offloaded algo
std::vector<std::vector<unsigned char> > output;
independent_execute(input, output);
// Post execution entrypoint
independent_post_execute(output);
return 0;
}