44 #include "../../../../../PracticalSockets/src/PracticalSockets.h"
45 #include "../include/config.h"
48 static inline unsigned int
49 fileRead(
const char *fname,
DtUsed *buff,
size_t len) {
51 if ((fname == NULL) || (buff == NULL) || (len == 0))
54 std::ifstream ifile(fname, std::ios::in);
57 if (!ifile.is_open()) {
58 std::cerr <<
"ERROR: There was a problem opening the input file " + string(fname)+
" \n";
65 while (ifile >> text) {
66 if((text ==
"Inf") || (text ==
"-Inf")) {
67 buff[i] = (
DtUsed)std::numeric_limits<double>::infinity();
69 else if((text ==
"nan") || (text ==
"-nan")) {
70 buff[i] = (
DtUsed)std::numeric_limits<double>::quiet_NaN();
73 buff[i] = (
DtUsed)atof(text.c_str());
99 if ((fname == NULL) || (instruct == NULL))
102 std::ofstream ifile(fname);
105 if (!ifile.is_open()) {
106 std::cerr <<
"There was a problem creating the output file!\n";
110 for (i = 0, j = 0; i <
sizeof(
varin); i+=
sizeof(
DtUsed), j++) {
114 ifile << instruct->
loop_nm << endl;
117 ifile << instruct->
seed << endl;
135 ifile << instruct->
strike << endl;
175 int main(
int argc,
char * argv[]) {
177 if ((argc < 2) || (argc > 3)) {
178 cerr <<
"Usage: " << argv[0] <<
" <Server Port> <optional simulation mode>" << endl;
182 unsigned short servPort = atoi(argv[1]);
183 if ((servPort <= 0) || (servPort <= 1024) || (servPort >= 65536)) {
184 cerr <<
"ERROR: Invalid port number " << servPort <<
185 ". Please select a value at range [1025-65535]. Aborting..." << endl;
188 unsigned int num_batch = 0;
189 string clean_cmd, synth_cmd;
192 cout <<
" ___________________________________________________________________ " << endl;
193 cout <<
"/ \\" << endl;
194 cout <<
"INFO: Proxy tb batch # " << ++num_batch << endl;
196 UDPSocket sock(servPort);
198 TCPServerSocket servSock(servPort);
199 TCPSocket *servsock = servSock.accept();
202 unsigned int recvMsgSize;
203 string sourceAddress =
"localhost";
207 cout <<
"Handling client ";
209 cout << servsock->getForeignAddress() <<
":";
210 }
catch (SocketException e) {
211 cerr <<
"Unable to get foreign address" << endl;
214 cout << servsock->getForeignPort();
215 }
catch (SocketException e) {
216 cerr <<
"Unable to get foreign port" << endl;
222 clock_t last_cycle_rx =
clock();
226 int input_string_total_len = 0;
228 int receiving_now_rx =
sizeof(instruct);
230 int total_pack_rx = 1 + (
sizeof(instruct) - 1) /
PACK_SIZE;
231 char * longbuf =
new char[
PACK_SIZE * total_pack_rx];
234 for (
unsigned int i = 0; i <
sizeof(instruct); ) {
236 recvMsgSize = sock.recvFrom(buffer,
BUF_LEN, sourceAddress, servPort);
238 recvMsgSize = servsock->recv(buffer, receiving_now_rx);
240 input_string_total_len += recvMsgSize;
242 memcpy( & longbuf[i *
PACK_SIZE], buffer, recvMsgSize);
245 memcpy(&instruct, & longbuf[0],
sizeof(instruct));
246 cout <<
"INFO: Received packet from " << sourceAddress <<
":" << servPort << endl;
248 printf(
"DEBUG instruct.loop_nm = %u\n", (
unsigned int)instruct.
loop_nm);
249 printf(
"DEBUG instruct.seed = %u\n", (
unsigned int)instruct.
seed);
250 printf(
"DEBUG instruct.underlying = %f\n", instruct.
underlying);
251 printf(
"DEBUG instruct.volatility = %f\n", instruct.
volatility);
252 printf(
"DEBUG instruct.dividendYield = %f\n", instruct.
dividendYield);
253 printf(
"DEBUG instruct.riskFreeRate = %f\n", instruct.
riskFreeRate);
254 printf(
"DEBUG instruct.timeLength = %f\n", instruct.
timeLength);
255 printf(
"DEBUG instruct.strike = %f\n", instruct.
strike);
256 printf(
"DEBUG instruct.optionType = %u\n", (
unsigned int)instruct.
optionType);
258 printf(
"DEBUG instruct.requiredSamples = %u\n", (
unsigned int)instruct.
requiredSamples);
259 printf(
"DEBUG instruct.timeSteps = %u\n", (
unsigned int)instruct.
timeSteps);
260 printf(
"DEBUG instruct.maxSamples = %u\n", (
unsigned int)instruct.
maxSamples);
262 if (
writeStructToConfFile(
"../../../../../../ROLE/quantitative_finance/hls/mceuropeanengine/etc/mce_from_net.conf", &instruct) !=
sizeof(
varin)) {
263 cerr <<
"ERROR: Cannot write struct to configuration file. Aborting ..." << endl;
269 string exec_cmd =
"make fcsim -j 4";
270 string ouf_file =
"../../../../../../ROLE/quantitative_finance/hls/mceuropeanengine/mceuropeanengine_prj/solution1/fcsim/build/hls_out.txt";
272 if (atoi(argv[2]) == 2) {
273 exec_cmd =
"make csim";
274 ouf_file =
"../../../../../../ROLE/quantitative_finance/hls/mceuropeanengine/mceuropeanengine_prj/solution1/csim/build/hls_out.txt";
276 else if (atoi(argv[2]) == 3) {
277 synth_cmd =
"make csynth && ";
278 exec_cmd =
"make cosim";
279 ouf_file =
"../../../../../../ROLE/quantitative_finance/hls/mceuropeanengine/mceuropeanengine_prj/solution1/sim/wrapc_pc/hls_out.txt";
281 else if (atoi(argv[2]) == 4) {
282 exec_cmd =
"make kcachegrind";
283 ouf_file =
"../../../../../../ROLE/quantitative_finance/mceuropeanengine/mceuropeanengine_prj/solution1/fcsim/build/hls_out.txt";
290 if (num_batch == 1) {
291 clean_cmd =
"make clean && ";
294 string str_command =
"cd ../../../../../../ROLE/quantitative_finance/hls/mceuropeanengine/ && " + clean_cmd + synth_cmd +
"\
295 INPUT_FILE=./etc/mce_from_net.conf " + exec_cmd +
" && \
296 cd ../../../../HOST/quantitative_finance/mceuropeanengine/languages/cplusplus/build/ ";
297 const char *command = str_command.c_str();
298 cout <<
"Calling TB with command:" << command << endl;
309 longbuf =
new char[
PACK_SIZE * total_pack_tx];
310 memset(longbuf, 0,
PACK_SIZE * total_pack_tx *
sizeof(
char));
313 unsigned int rc = fileRead(ouf_file.c_str(),
out, instruct.
loop_nm);
315 cerr <<
"ERROR: Cannot read file " << ouf_file <<
" . Aborting..."<< endl;
321 clock_t next_cycle_rx =
clock();
322 double duration_rx = (next_cycle_rx - last_cycle_rx) / (
double) CLOCKS_PER_SEC;
323 cout <<
"INFO: Effective FPS RX:" << (1 / duration_rx) <<
" \tkbps:" << (
PACK_SIZE *
324 total_pack_rx / duration_rx / 1024 * 8) << endl;
325 last_cycle_rx = next_cycle_rx;
329 cout <<
"INFO: Succesfully received out vector from TB " << endl;
330 cout <<
"INFO: Will forward it back to host app ... total_pack_tx=" << total_pack_tx << endl;
335 clock_t last_cycle_tx =
clock();
336 for (
unsigned int i = 0; i < total_pack_tx; i++) {
337 if ( i == total_pack_tx - 1 ) {
338 sending_now = bytes_in_last_pack_tx;
341 sock.sendTo( & longbuf[i *
PACK_SIZE], sending_now, sourceAddress, servPort);
343 servsock->send( & longbuf[i *
PACK_SIZE], sending_now);
347 clock_t next_cycle_tx =
clock();
348 double duration_tx = (next_cycle_tx - last_cycle_tx) / (
double) CLOCKS_PER_SEC;
349 cout <<
"INFO: Effective FPS TX:" << (1 / duration_tx) <<
" \tkbps:" << (
PACK_SIZE *
350 (total_pack_rx + total_pack_tx)/ duration_tx / 1024 * 8) << endl;
351 last_cycle_tx = next_cycle_tx;
354 cout <<
"\\___________________________________________________________________/" << endl;
359 }
catch (SocketException & e) {
360 cerr << e.what() << endl;
unsigned int writeStructToConfFile(const char *fname, varin *instruct)
Fill an output file with data from an image.
DtUsedInt requiredSamples