c++ - break sub task of parallel_for_each -


i have big vector of items sorted based on 1 of fields, e.g. cost attribute, , want bit of processing on each of these items find maximum value of different attribute... constraint here cannot use item calculate maximum value if item's cost exceeds arbitrary price.

the single threaded for-loop looks this:

auto maxvalue = -max_flt; for(const auto& foo: foos) {      // break if cost high.     if(foo.cost() > 46290) {          break;     }      maxvalue = max(maxvalue , foo.value());  } 

i've been able somewhat convert parallel_for_each. (disclaimer: i'm new ppl.)

combinable<float> localmaxvalue([]{ return -max_flt; });  parallel_for_each(begin(foos), end(foos), [&](const auto& foo) {      // attempt out if cost high.     if(foo.getcost() > 46290) {         return;      }      localmaxvalue.local() = max(localmaxvalue.local(), foo.getvalue()); }  auto maxvalue = localmaxvalue.combine(     [](const auto& first, const auto& second) {          return max<float>(first, second);      }); 

the return statement inside parallel_for feels inefficient since it's still executing on every item, , in case, it's quite possible parallel_for end iterating on multiple portions of vector costed high.

how can take advantage of fact vector sorted cost?

i looked using cancellation token, approach seems incorrect cause sub tasks of parallel_for cancelled means may wrong maximum value.

is there cancellation token cancel specific sub task of parallel_for, or there better tool parallel_for in case?

if vector sorted cost can iterate on items cost lower cost limit.

if cost x. find first item iterator equal or larger x. can use std::lower_bound. use parallel_for_each beginning of vector iterator found.

combinable<float> localmaxvalue([]{ return -max_flt; });  //i'm assuming foos std::vector. int cost_limit = 46290; auto it_end = std::lower_bound(foos.begin(), foos.end(), cost_limit, [](const auto& foo, int cost_limit) {     return foo.getcost() < cost_limit; });  parallel_for_each(foos.begin(), foos.end(), [&](const auto& foo) {         localmaxvalue.local() = max(localmaxvalue.local(), foo.getvalue()); }  auto maxvalue = localmaxvalue.combine(     [](const auto& first, const auto& second) {          return max<float>(first, second);      }); 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -