-1

I have a class similar to this one:

struct orden
{
 long long time;
 double price;
 const char* time1;
 orden(const char* t, double p) : time1(t),price(p){};
void convertToMs()
{
  time = dateToMs2(time1);
}
};

The what I'm doing is to read a file in serial, execute the constuctor of the struct and save the generated object in a vector of orden:

vector<orden> ord;
ifstream fe(filename)
while(getline(fe,order_line))
{
  price = stod(order_line.substr(position+2));
  time = order_line.substr(0,26).c_str();
  ord.push_back(time, price);
}

Then, what I want to do in parallel is to execute the member function convertToMs(). But I have problems because the compiler ask me to execut only constant functions inside parallel for.

I have tryed some thinks that I have found like this one Without sucess because they are not saving the result of the execution in a parameter of the object.

What I'm doing wrong?

Thank you in advance

EDIT:

If I use

struct orden
{
 long long time;
 double price;
 const char* time1;
 orden(const char* t, double p) : time1(t),price(p){};
 void convertToMs() const
 {
   time = dateToMs2(time1);
 }
};

I have the following error:

In file included from pract1.cpp:5:0:
orden.h: In member function ‘void orden::convertToMs() const’:
orden.h:29:27: error: assignment of member ‘orden::time’ in read-only object
 time = dateToMs2(time1);

If I delete the const i have the following one:

pract1.cpp: In lambda function:
pract1.cpp:158:43: error: passing ‘const value_type {aka const orden}’ as ‘this’ argument discards qualifiers [-fpermissive]
     ordersOfTheCompany[j].convertToMs();
                                       ^
In file included from pract1.cpp:5:0:
orden.h:27:8: note:   in call to ‘void orden::convertToMs()’
void convertToMs()

EDIT 2:

Sorry again, i have the folloging code:

vector<orden> ord;
while(getline(fe,order_line))
{
  price = stod(order_line.substr(position+2));
  time = order_line.substr(0,26).c_str();
  ord.push_back(time, price);
}
tbb::parallel_for(tbb::blocked_range<size_t>(0,ord.size()),
[=](tbb::blocked_range<size_t>& r) {

  for(std::size_t j=r.begin(); j!=r.end(); ++j)
  {
    ord[j].convertToMs();
  }});

I'm trying to execute the member function in a parallel for

david.t_92
  • 1,971
  • 1
  • 11
  • 15
  • 1
    What error message are you getting from the compiler? This also lacks a [mcve]. In particular, it lacks declarations of several of the variables that are used. – 1201ProgramAlarm Jan 27 '18 at 20:51
  • @1201ProgramAlarm Sorry for that, I have edited the question with the error – david.t_92 Jan 27 '18 at 21:29
  • You have a problem with parallel_for, but there is none in the code shown. –  Jan 27 '18 at 23:52

1 Answers1

0

The documentation for parallel_for has an example similar to what you're trying to do. It saves the result into a separate array from the input array.

In your case, you'd want to add something like

std::vector<long long> outTime;
outTime.resize(ord.size());

right before the parallel_for call. Then, in the body of the lambda you execute, change the statement to

outTime[j] = ord[j].convertToMs();

after changing convertToMs to be a const function that returns the converted value rather than saving it locally. After the parallel_for returns, copy the results out of outTime back into ord.

You could have 2 versions of convertToMs, a non-const one that saves the result internally, and a const one that returns the converted time.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56