1

With intel TBB I'm trying to read a file `serial', apply a function on each line of the file, and save the result to a vector of a type A.

struct A
{
   long long time;
   double price;
   A(long long t, double p) : time(t),price(p){};
}

I have created the following pipeline

vector<A> parallelFile(string fileName) {

ifstream fe(fileName);
string orden_fichero;
vector<A> ord;
parallel_pipeline( /*max_number_of_tokens=*/16,
  make_filter<void,string>(
          filter::serial,
          [&](flow_control& fc)->string
          {
            getline(fe,orden_fichero);
            if(fe.eof())
                {
              fc.stop(); // stop processing
              return NULL;
                }
                else
            {
              return orden_fichero;
            }
          }
        ) &
        make_filter<string,A>(
          filter::parallel,
          [](string p) ->A{
           auto position = p.find('"',28);
                 auto price = stod(p.substr(position+2));
                 auto time = dateToMs2(p.substr(0,26).c_str());
            return A(time, price);
          }
        ) &
        make_filter<A,void>(
          filter::serial,
          [&ord](A x) {ord.push_back(x);}
        )
      );
      return ord;
 }

 int main()
 {
    vector<A> david=parallelFile("input.txt");

    return 0;
 }

where dateToMs is a function that returns a long long.

Executing this pipline y have the following error:

TBB Warning: Exact exception propagation is requested by application but the 
linked library is built without support for it
terminate called after throwing an instance of 'tbb::captured_exception'
  what():  basic_string::_M_construct null not valid
Aborted (core dumped)

I have seen that the first filter of the pipline reads all the file and the error comes at the end of the file.

What I'm doing wrong?

edit: each line of the file have the following structure: dd-mm-yyyy hh:mm:ss.msmsms "CompanyName" ff.ff

where:

  • dd-mm-yyyy is the date in form day-month-year
  • hh:mm:ss.212313 is the date in hours minutes seconds and miliseconds
  • "companyName" is a string
  • ff.ff is a double
david.t_92
  • 1,971
  • 1
  • 11
  • 15
  • 3
    First you have TBB built without exceptions which makes it terminate instead of propagating exceptions. But the reason your source is throwing is that you're trying to construct `std::string` from a null pointer which is an error (actually UB but your implementation seems to check that condition, possibly just for debug). You might use an empty string instead, `string()`, instead of `NULL/nullptr`. –  Jan 24 '18 at 14:44
  • Sorry i'm very new using intel TBB, where I have to use and empy string?, when i declare `orden_fichero'? – david.t_92 Jan 24 '18 at 14:49
  • 1
    When you encounter EOF, you have a `return NULL`. I believe that's where you're trying to construct std::string from a null pointer. If you replace that with `return string()`, that should take care of the immediate problem at least. The immediate issue is not with how you're using TBB so much as how you're using `std::string`. –  Jan 24 '18 at 14:51
  • 1
    I've just tested if it works at 100%, thousand graces – david.t_92 Jan 24 '18 at 14:53
  • @TeamUpvote It is possible to parallelize the first statge of the pipline `when I read the document' – david.t_92 Jan 24 '18 at 16:38
  • It's a little bit tricky with text files but you might do it in two passes. First one can just load the entire thing to memory in a big string. Then you might count the number of lines along with the length of each line in one thread along with the starting string position of each line. This is done in one thread. Then, in a second parallel pass, go through the lines in the string across threads (read-only access) using the starting position and length you captured for each line and do all your parsing and splitting and so forth while writing results to an array of strings. –  Jan 24 '18 at 16:44
  • This is some very hasty pseudocode but it should communicate the idea: https://paste.ofcode.org/q5XF5GB7UspPHWztAPrWzN –  Jan 24 '18 at 17:01

0 Answers0