cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
common.cpp
Go to the documentation of this file.
1 
17 
30 #include "../include/common.hpp"
31 
32 using namespace std;
33 
34 
35 
36 
37 
45 bool setInputDataStream(stream<UdpWord> &sDataStream, const string dataStreamName,
46  const string inpFileName, int simCnt) {
47  string strLine;
48  ifstream inpFileStream;
49  string datFile = "../../../../test/" + inpFileName;
50  UdpWord udpWord;
51 
52  //-- STEP-1 : OPEN FILE
53  inpFileStream.open(datFile.c_str());
54  if ( !inpFileStream ) {
55  cout << "### ERROR : Could not open the input data file " << datFile << endl;
56  return(KO);
57  }
58 
59  //-- STEP-2 : SET DATA STREAM
60  while (inpFileStream) {
61 
62  if (!inpFileStream.eof()) {
63 
64  getline(inpFileStream, strLine);
65  if (strLine.empty()) continue;
66  sscanf(strLine.c_str(), "%llx %x %d", &udpWord.tdata, &udpWord.tkeep, &udpWord.tlast);
67 
68  // Write to sDataStream
69  if (sDataStream.full()) {
70  printf("### ERROR : Stream is full. Cannot write stream with data from file \"%s\".\n", inpFileName.c_str());
71  return(KO);
72  } else {
73  sDataStream.write(udpWord);
74  // Print Data to console
75  printf("[%4.4d] TB is filling input stream [%s] - Data write = {D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
76  simCnt, dataStreamName.c_str(),
77  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
78  }
79  }
80  }
81 
82  //-- STEP-3: CLOSE FILE
83  inpFileStream.close();
84 
85  return(OK);
86 }
87 
88 
89 
90 
91 
100 bool readDataStream(stream <UdpWord> &sDataStream, UdpWord *udpWord) {
101  // Get the DUT/Data results
102  sDataStream.read(*udpWord);
103  return(VALID);
104 }
105 
106 
107 
113 ap_uint<64> pack_ap_uint_64_ (ap_uint<8> *buffer) {
114 
115  ap_uint<64> value;
116 
117  value = buffer[7];
118  value = (value << 8 ) + buffer[6];
119  value = (value << 8 ) + buffer[5];
120  value = (value << 8 ) + buffer[4];
121  value = (value << 8 ) + buffer[3];
122  value = (value << 8 ) + buffer[2];
123  value = (value << 8 ) + buffer[1];
124  value = (value << 8 ) + buffer[0];
125 
126  return value ;
127 
128 }
129 
130 
136 void unpack_ap_uint_64_ (ap_uint<64> value, ap_uint<8> *buffer) {
137 
138  for (unsigned int i=0; i<8; i++) {
139  buffer[i] = (value >> 8*i );
140  }
141 }
142 
143 
150 bool dumpDataToFile(UdpWord *udpWord, ofstream &outFileStream) {
151  if (!outFileStream.is_open()) {
152  printf("### ERROR : Output file stream is not open. \n");
153  return(KO);
154  }
155  outFileStream << hex << noshowbase << setfill('0') << setw(16) << udpWord->tdata.to_uint64();
156  outFileStream << " ";
157  outFileStream << hex << noshowbase << setfill('0') << setw(2) << udpWord->tkeep.to_int();
158  outFileStream << " ";
159  outFileStream << setw(1) << udpWord->tlast.to_int() << "\n";
160  return(OK);
161 }
162 
163 
164 
165 
173 bool getOutputDataStream(stream<UdpWord> &sDataStream,
174  const string dataStreamName, const string outFileName, int simCnt)
175 {
176  string strLine;
177  ofstream outFileStream;
178  string datFile = "../../../../test/" + outFileName;
179  UdpWord udpWord;
180  bool rc = OK;
181 
182  //-- STEP-1 : OPEN FILE
183  outFileStream.open(datFile.c_str());
184  if ( !outFileStream ) {
185  cout << "### ERROR : Could not open the output data file " << datFile << endl;
186  return(KO);
187  }
188 
189  //-- STEP-2 : EMPTY STREAM AND DUMP DATA TO FILE
190  while (!sDataStream.empty()) {
191  if (readDataStream(sDataStream, &udpWord) == VALID) {
192  // Print DUT/Data to console
193  printf("[%4.4d] TB is draining output stream [%s] - Data read = {D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
194  simCnt, dataStreamName.c_str(),
195  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
196  if (!dumpDataToFile(&udpWord, outFileStream)) {
197  rc = KO;
198  break;
199  }
200  }
201  }
202 
203  //-- STEP-3: CLOSE FILE
204  outFileStream.close();
205 
206  return(rc);
207 }
208 
209 
210 
217 bool dumpStructToFile(varin *instruct, const string outFileName, int simCnt)
218 {
219  string strLine;
220  ofstream outFileStream;
221  string datFile = "../../../../test/" + outFileName;
222  UdpWord udpWord;
223  bool rc = OK;
224  unsigned int bytes_per_line = 8;
225 
226  //-- STEP-1 : OPEN FILE
227  outFileStream.open(datFile.c_str());
228  if ( !outFileStream ) {
229  cout << "### ERROR : Could not open the output data file " << datFile << endl;
230  return(KO);
231  }
232  ap_uint<8> value[bytes_per_line];
233  unsigned int total_bytes = 0;
234 
235 
236  intToFloatUnion intToFloat;
237 
238  //-- STEP-2 : DUMP STRING DATA TO FILE
239  for (unsigned int i = 0, j = 0; i < INSIZE; i+=sizeof(DtUsed), j++, total_bytes+=bytes_per_line) {
240  switch(j)
241  {
242  case 0:
243  intToFloat.i = instruct->loop_nm;
244  printf("DEBUG instruct->loop_nm = %u\n", (unsigned int)instruct->loop_nm);
245  break;
246  case 1:
247  intToFloat.i = instruct->seed;
248  printf("DEBUG instruct->seed = %u\n", (unsigned int)instruct->seed);
249  break;
250  case 2:
251  intToFloat.f = instruct->underlying;
252  printf("DEBUG instruct->underlying = %f\n", instruct->underlying);
253  break;
254  case 3:
255  intToFloat.f = instruct->volatility;
256  printf("DEBUG instruct->volatility = %f\n", instruct->volatility);
257  break;
258  case 4:
259  intToFloat.f = instruct->dividendYield;
260  printf("DEBUG instruct->dividendYield = %f\n", instruct->dividendYield);
261  break;
262  case 5:
263  intToFloat.f = instruct->riskFreeRate;
264  printf("DEBUG instruct->riskFreeRate = %f\n", instruct->riskFreeRate);
265  break;
266  case 6:
267  intToFloat.f = instruct->timeLength;
268  printf("DEBUG instruct->timeLength = %f\n", instruct->timeLength);
269  break;
270  case 7:
271  intToFloat.f = instruct->strike;
272  printf("DEBUG instruct->strike = %f\n", instruct->strike);
273  break;
274  case 8:
275  intToFloat.i = instruct->optionType;
276  printf("DEBUG instruct->optionType = %u\n", (unsigned int)instruct->optionType);
277  break;
278  case 9:
279  intToFloat.f = instruct->requiredTolerance;
280  printf("DEBUG instruct->requiredTolerance = %f\n", instruct->requiredTolerance);
281  break;
282  case 10:
283  intToFloat.i = instruct->requiredSamples;
284  printf("DEBUG instruct->requiredSamples = %u\n", (unsigned int)instruct->requiredSamples);
285  break;
286  case 11:
287  intToFloat.i = instruct->timeSteps;
288  printf("DEBUG instruct->timeSteps = %u\n", (unsigned int)instruct->timeSteps);
289  break;
290  case 12:
291  intToFloat.i = instruct->maxSamples;
292  printf("DEBUG instruct->maxSamples = %u\n", (unsigned int)instruct->maxSamples);
293  break;
294  default:
295  printf("ERROR: unknown value %u\n", j);
296  rc = KO;
297  break;
298  }
299 
300  udpWord.tdata = (ap_uint<64>)intToFloat.i;
301  udpWord.tkeep = 255;
302  // We are signaling a packet termination either at the end of the image or the end of MTU
303  if ((total_bytes >= (INSIZE - bytes_per_line)) ||
304  ((total_bytes + bytes_per_line) % PACK_SIZE == 0)) {
305  udpWord.tlast = 1;
306  }
307  else {
308  udpWord.tlast = 0;
309  }
310  printf("[%4.4d] IMG TB is dumping string to file [%s] - Data read [%u] = {val=%u, D=0x%16.16llX, K=0x%2.2X, L=%d} \n",
311  simCnt, datFile.c_str(), total_bytes, value,
312  udpWord.tdata.to_long(), udpWord.tkeep.to_int(), udpWord.tlast.to_int());
313  if (!dumpDataToFile(&udpWord, outFileStream)) {
314  rc = KO;
315  break;
316  }
317  }
318  //-- STEP-3: CLOSE FILE
319  outFileStream.close();
320 
321  return(rc);
322 }
323 
324 
325 
332 bool dumpFileToArray(const string inpFileName, DtUsed* out, int simCnt) {
333  string strLine;
334  ifstream inpFileStream;
335  string datFile = "../../../../test/" + inpFileName;
336  UdpWord udpWord;
337  unsigned int i = 0;
338  unsigned int bytes_per_line = 8;
339  ap_uint<8> value[bytes_per_line];
340  intToFloatUnion intToFloat;
341  //-- STEP-1 : OPEN FILE
342  inpFileStream.open(datFile.c_str());
343  if ( !inpFileStream ) {
344  cout << "### ERROR : Could not open the input data file " << datFile << endl;
345  return(KO);
346  }
347 
348  //-- STEP-2 : SET DATA STREAM
349  while (inpFileStream) {
350 
351  if (!inpFileStream.eof()) {
352 
353  getline(inpFileStream, strLine);
354  if (strLine.empty()) continue;
355  sscanf(strLine.c_str(), "%llx %x %d", &udpWord.tdata, &udpWord.tkeep, &udpWord.tlast);
356  //unpack_ap_uint_64_(udpWord.tdata, value);
357 
358  intToFloatUnion intToFloat;
359  intToFloat.i = (DtUsedInt)(udpWord.tdata);
360  assert(i<OUTDEP);
361  out[i++] = intToFloat.f;
362  }
363  }
364 
365  //-- STEP-3: CLOSE FILE
366  inpFileStream.close();
367 
368  return(OK);
369 }
370 
371 
372 static inline ssize_t
373 writeArrayToFile(const char *fname, DtUsed* out)
374 {
375  int rc;
376  FILE *fp;
377 
378  if ((fname == NULL) || (out == NULL))
379  return -EINVAL;
380 
381  fp = fopen(fname, "w+");
382  if (!fp) {
383  fprintf(stderr, "err: Cannot open file %s: %s\n",
384  fname, strerror(errno));
385  return -ENODEV;
386  }
387  for (unsigned int i = 0; i < OUTDEP; i++) {
388  rc = fprintf(fp,"%f\n", out[i]);
389  if (rc == -1) {
390  fprintf(stderr, "err: Cannot write to %s: %s\n",
391  fname, strerror(errno));
392  fclose(fp);
393  return -EIO;
394  }
395  }
396  fclose(fp);
397  return rc;
398 }
399 
400 
401 
408 unsigned int
409 readFileConfigToStruct(const char *fname, varin *instruct) {
410 
411  if ((fname == NULL) || (instruct == NULL))
412  return -EINVAL;
413 
414  std::ifstream ifile(fname, std::ios::in);
415 
416  //check to see that the file was opened correctly:
417  if (!ifile.is_open()) {
418  std::cerr << "There was a problem opening the input file!\n";
419  return -EIO;
420  }
421  unsigned int i, j;
422  for (i = 0, j = 0; i < INSIZE; i+=sizeof(DtUsed), j++) {
423  switch(j)
424  {
425  case 0:
426  ifile >> instruct->loop_nm;
427  printf("DEBUG instruct->loop_nm = %u\n", (unsigned int)instruct->loop_nm);
428  break;
429  case 1:
430  ifile >> instruct->seed;
431  printf("DEBUG instruct->seed = %u\n", (unsigned int)instruct->seed);
432  break;
433  case 2:
434  ifile >> instruct->underlying;
435  printf("DEBUG instruct->underlying = %f\n", instruct->underlying);
436  break;
437  case 3:
438  ifile >> instruct->volatility;
439  printf("DEBUG instruct->volatility = %f\n", instruct->volatility);
440  break;
441  case 4:
442  ifile >> instruct->dividendYield;
443  printf("DEBUG instruct->dividendYield = %f\n", instruct->dividendYield);
444  break;
445  case 5:
446  ifile >> instruct->riskFreeRate;
447  printf("DEBUG instruct->riskFreeRate = %f\n", instruct->riskFreeRate);
448  break;
449  case 6:
450  ifile >> instruct->timeLength;
451  printf("DEBUG instruct->timeLength = %f\n", instruct->timeLength);
452  break;
453  case 7:
454  ifile >> instruct->strike;
455  printf("DEBUG instruct->strike = %f\n", instruct->strike);
456  break;
457  case 8:
458  ifile >> instruct->optionType;
459  printf("DEBUG instruct->optionType = %u\n", (unsigned int)instruct->optionType);
460  break;
461  case 9:
462  ifile >> instruct->requiredTolerance;
463  printf("DEBUG instruct->requiredTolerance = %f\n", instruct->requiredTolerance);
464  break;
465  case 10:
466  ifile >> instruct->requiredSamples;
467  printf("DEBUG instruct->requiredSamples = %u\n", (unsigned int)instruct->requiredSamples);
468  break;
469  case 11:
470  ifile >> instruct->timeSteps;
471  printf("DEBUG instruct->timeSteps = %u\n", (unsigned int)instruct->timeSteps);
472  break;
473  case 12:
474  ifile >> instruct->maxSamples;
475  printf("DEBUG instruct->maxSamples = %u\n", (unsigned int)instruct->maxSamples);
476  break;
477  default:
478  break;
479  }
480  }
481  return (i);
482 }
483 
484 
int simCnt
Definition: tb_fmc.cpp:113
ap_uint< 64 > pack_ap_uint_64_(ap_uint< 8 > *buffer)
Pack an array of 8 x ap_uint<8> into a ap_uint<64> word.
Definition: common.cpp:116
void unpack_ap_uint_64_(ap_uint< 64 > value, ap_uint< 8 > *buffer)
Unpack an ap_uint<64> word to an array of 8 x ap_uint<8>.
Definition: common.cpp:139
bool dumpDataToFile(UdpWord *udpWord, ofstream &outFileStream)
Dump a data word to a file.
Definition: common.cpp:153
bool setInputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string inpFileName, int simCnt)
Initialize an input data stream from a file.
Definition: common.cpp:45
bool getOutputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string outFileName, int simCnt)
Fill an output file with data from an output stream.
Definition: common.cpp:176
#define INSIZE
long unsigned int DtUsedInt
Definition: config.h:74
#define OUTDEP
Definition: config.h:63
#define DtUsed
Definition: config.h:57
#define PACK_SIZE
Definition: config.h:51
bool readDataStream(stream< UdpAppData > &sDataStream, UdpAppData *udpWord)
Read data from a stream.
Definition: tb_nal.cpp:395
#define VALID
Definition: tb_nal.cpp:43
#define OK
Definition: nts_types.hpp:57
#define KO
Definition: nts_types.hpp:58
bool dumpFileToArray(const string inpFileName, double *out, int simCnt)
Initialize an input data stream from a file.
Definition: common.cpp:332
bool dumpStructToFile(varin *instruct, const string outFileName, int simCnt)
Fill an output file with data from an image.
Definition: common.cpp:217
out
Definition: test.py:12
ap_uint< 64 > tdata
ap_uint< 1 > tlast
ap_uint< 8 > tkeep
Definition: config.h:85
double dividendYield
Definition: config.h:90
DtUsedInt seed
Definition: config.h:87
DtUsedInt maxSamples
Definition: config.h:98
double riskFreeRate
Definition: config.h:91
double volatility
Definition: config.h:89
double requiredTolerance
Definition: config.h:95
DtUsedInt loop_nm
Definition: config.h:86
double underlying
Definition: config.h:88
DtUsedInt timeSteps
Definition: config.h:97
double strike
Definition: config.h:93
DtUsedInt requiredSamples
Definition: config.h:96
DtUsedInt optionType
Definition: config.h:94
double timeLength
Definition: config.h:92
double f
Definition: config.h:103
DtUsedInt i
Definition: config.h:104