cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
araongao2015
Explorer
Explorer
4,249 Views
Registered: ‎11-01-2015

A question about reading data from an external file

Hi,

 

Could you check the C++ source code below? If the stimulus and sti_val are declared as int, this code can work well. By contrast,

if they are declared as ap_int<8>, error occurs. It will generate critical warning like below:

 

CRITICAL WARNING: [SIM 1] 'csim_design' failed: nonzero return value.

 

I use Vivado HLS 2016.1.

 

My question is whether Vivado HLS 2016.1 can support read data from external file for ap_int or ap_fixed data.

 

Thanks

 

#include <iostream>
#include <iomanip>
#include <fstream>
#include <ap_int.h>


using namespace std;

int main ()
{
  int stimulus[4] = {0};
  int sti_val;
  char *stimulus_source = "a.txt";
  ifstream sti (stimulus_source);

  int i = 0;

  if (!sti.is_open())
  {
    cout << "The file is unable to open!" << '\n';
    return 1;
  }
  else
  {
    while (sti >> sti_val)
    {
      cout << "The current value of i: " << i << '\n';
      stimulus[i] = sti_val;
      i++;
    }
  }
  sti.close();

  for (i = 0; i < 4; i++)
  {
    cout << "The ith value of stimulus is: " << stimulus[i] << '\n';
  }
  return 0;
}

0 Kudos
3 Replies
u4223374
Advisor
Advisor
4,214 Views
Registered: ‎04-26-2015

I normally use the standard "C" functions (ie fread/fwrite, fscanf/fprintf) and those are not happy with the ap_int types. Better to read things in using regular ints and cast them across later. In your case, something like this might work:

 

#include <iostream>
#include <iomanip>
#include <fstream>
#include <ap_int.h>
#include <stdint.h>

using namespace std;

int main ()
{
  ap_uint<8> stimulus[4] = {0};
  int32_t sti_val;
  char *stimulus_source = "a.txt";
  ifstream sti (stimulus_source);

  int i = 0;

  if (!sti.is_open())
  {
    cout << "The file is unable to open!" << '\n';
    return 1;
  }
  else
  {
    while (sti >> sti_val) // sti_val is now a normal integer, so it'll be fine.
    {
      cout << "The current value of i: " << i << '\n';
      stimulus[i] = sti_val; // Conversion to ap_int<8> occurs automatically; you may want to check bounds.
      i++;
    }
  }
  sti.close();

  for (i = 0; i < 4; i++)
  {
    cout << "The ith value of stimulus is: " << int32_t(stimulus[i]) << '\n'; // Explicitly cast it to an int32_t.
  }
  return 0;
}
0 Kudos
araongao2015
Explorer
Explorer
3,750 Views
Registered: ‎11-01-2015

Hi @u4223374,

 

Thank you. Yes, it can work. But if the data type is ap_int<72>, this means it cannot be indicated by normal int. In this case, what should I do? Bye the way, I try to define a float variable to receive data from external file and convert it ap_int. Do you have any other more effective method?

 

From this point of view, I don't think Vivado HLS support reading file efficiently and effectively.

 

Thanks,

Regards

0 Kudos
araongao2015
Explorer
Explorer
3,748 Views
Registered: ‎11-01-2015

It is surprising that if the data type is ap_fixed, this error will not occur.

 

Thanks,

Regards

0 Kudos