30 #include "../include/common.hpp"
46 const string inpFileName,
int simCnt) {
48 ifstream inpFileStream;
49 string datFile =
"../../../../test/" + inpFileName;
53 inpFileStream.open(datFile.c_str());
54 if ( !inpFileStream ) {
55 cout <<
"### ERROR : Could not open the input data file " << datFile << endl;
60 while (inpFileStream) {
62 if (!inpFileStream.eof()) {
64 getline(inpFileStream, strLine);
65 if (strLine.empty())
continue;
66 sscanf(strLine.c_str(),
"%llx %x %d", &udpWord.
tdata, &udpWord.
tkeep, &udpWord.
tlast);
69 if (sDataStream.full()) {
70 printf(
"### ERROR : Stream is full. Cannot write stream with data from file \"%s\".\n", inpFileName.c_str());
73 sDataStream.write(udpWord);
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());
83 inpFileStream.close();
102 sDataStream.read(*udpWord);
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];
138 for (
unsigned int i=0; i<8; i++) {
139 buffer[i] = (value >> 8*i );
151 if (!outFileStream.is_open()) {
152 printf(
"### ERROR : Output file stream is not open. \n");
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";
174 const string dataStreamName,
const string outFileName,
int simCnt)
177 ofstream outFileStream;
178 string datFile =
"../../../../test/" + outFileName;
183 outFileStream.open(datFile.c_str());
184 if ( !outFileStream ) {
185 cout <<
"### ERROR : Could not open the output data file " << datFile << endl;
190 while (!sDataStream.empty()) {
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());
204 outFileStream.close();
220 ofstream outFileStream;
221 string datFile =
"../../../../test/" + outFileName;
224 unsigned int bytes_per_line = 8;
227 outFileStream.open(datFile.c_str());
228 if ( !outFileStream ) {
229 cout <<
"### ERROR : Could not open the output data file " << datFile << endl;
232 ap_uint<8> value[bytes_per_line];
233 unsigned int total_bytes = 0;
239 for (
unsigned int i = 0, j = 0; i <
INSIZE; i+=
sizeof(
DtUsed), j++, total_bytes+=bytes_per_line) {
244 printf(
"DEBUG instruct->loop_nm = %u\n", (
unsigned int)instruct->
loop_nm);
247 intToFloat.
i = instruct->
seed;
248 printf(
"DEBUG instruct->seed = %u\n", (
unsigned int)instruct->
seed);
252 printf(
"DEBUG instruct->underlying = %f\n", instruct->
underlying);
256 printf(
"DEBUG instruct->volatility = %f\n", instruct->
volatility);
260 printf(
"DEBUG instruct->dividendYield = %f\n", instruct->
dividendYield);
264 printf(
"DEBUG instruct->riskFreeRate = %f\n", instruct->
riskFreeRate);
268 printf(
"DEBUG instruct->timeLength = %f\n", instruct->
timeLength);
271 intToFloat.
f = instruct->
strike;
272 printf(
"DEBUG instruct->strike = %f\n", instruct->
strike);
276 printf(
"DEBUG instruct->optionType = %u\n", (
unsigned int)instruct->
optionType);
284 printf(
"DEBUG instruct->requiredSamples = %u\n", (
unsigned int)instruct->
requiredSamples);
288 printf(
"DEBUG instruct->timeSteps = %u\n", (
unsigned int)instruct->
timeSteps);
292 printf(
"DEBUG instruct->maxSamples = %u\n", (
unsigned int)instruct->
maxSamples);
295 printf(
"ERROR: unknown value %u\n", j);
300 udpWord.
tdata = (ap_uint<64>)intToFloat.
i;
303 if ((total_bytes >= (
INSIZE - bytes_per_line)) ||
304 ((total_bytes + bytes_per_line) %
PACK_SIZE == 0)) {
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());
319 outFileStream.close();
334 ifstream inpFileStream;
335 string datFile =
"../../../../test/" + inpFileName;
338 unsigned int bytes_per_line = 8;
339 ap_uint<8> value[bytes_per_line];
342 inpFileStream.open(datFile.c_str());
343 if ( !inpFileStream ) {
344 cout <<
"### ERROR : Could not open the input data file " << datFile << endl;
349 while (inpFileStream) {
351 if (!inpFileStream.eof()) {
353 getline(inpFileStream, strLine);
354 if (strLine.empty())
continue;
355 sscanf(strLine.c_str(),
"%llx %x %d", &udpWord.
tdata, &udpWord.
tkeep, &udpWord.
tlast);
361 out[i++] = intToFloat.
f;
366 inpFileStream.close();
372 static inline ssize_t
373 writeArrayToFile(
const char *fname,
DtUsed*
out)
378 if ((fname == NULL) || (
out == NULL))
381 fp = fopen(fname,
"w+");
383 fprintf(stderr,
"err: Cannot open file %s: %s\n",
384 fname, strerror(errno));
387 for (
unsigned int i = 0; i <
OUTDEP; i++) {
388 rc = fprintf(fp,
"%f\n",
out[i]);
390 fprintf(stderr,
"err: Cannot write to %s: %s\n",
391 fname, strerror(errno));
409 readFileConfigToStruct(
const char *fname,
varin *instruct) {
411 if ((fname == NULL) || (instruct == NULL))
414 std::ifstream ifile(fname, std::ios::in);
417 if (!ifile.is_open()) {
418 std::cerr <<
"There was a problem opening the input file!\n";
422 for (i = 0, j = 0; i <
INSIZE; i+=
sizeof(
DtUsed), j++) {
427 printf(
"DEBUG instruct->loop_nm = %u\n", (
unsigned int)instruct->
loop_nm);
430 ifile >> instruct->
seed;
431 printf(
"DEBUG instruct->seed = %u\n", (
unsigned int)instruct->
seed);
435 printf(
"DEBUG instruct->underlying = %f\n", instruct->
underlying);
439 printf(
"DEBUG instruct->volatility = %f\n", instruct->
volatility);
443 printf(
"DEBUG instruct->dividendYield = %f\n", instruct->
dividendYield);
447 printf(
"DEBUG instruct->riskFreeRate = %f\n", instruct->
riskFreeRate);
451 printf(
"DEBUG instruct->timeLength = %f\n", instruct->
timeLength);
454 ifile >> instruct->
strike;
455 printf(
"DEBUG instruct->strike = %f\n", instruct->
strike);
459 printf(
"DEBUG instruct->optionType = %u\n", (
unsigned int)instruct->
optionType);
467 printf(
"DEBUG instruct->requiredSamples = %u\n", (
unsigned int)instruct->
requiredSamples);
471 printf(
"DEBUG instruct->timeSteps = %u\n", (
unsigned int)instruct->
timeSteps);
475 printf(
"DEBUG instruct->maxSamples = %u\n", (
unsigned int)instruct->
maxSamples);
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.
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>.
bool dumpDataToFile(UdpWord *udpWord, ofstream &outFileStream)
Dump a data word to a file.
bool setInputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string inpFileName, int simCnt)
Initialize an input data stream from a file.
bool getOutputDataStream(stream< UdpWord > &sDataStream, const string dataStreamName, const string outFileName, int simCnt)
Fill an output file with data from an output stream.
long unsigned int DtUsedInt
bool readDataStream(stream< UdpAppData > &sDataStream, UdpAppData *udpWord)
Read data from a stream.
bool dumpFileToArray(const string inpFileName, double *out, int simCnt)
Initialize an input data stream from a file.
bool dumpStructToFile(varin *instruct, const string outFileName, int simCnt)
Fill an output file with data from an image.
DtUsedInt requiredSamples