Skip to content

Commit

Permalink
Addressing comments from #1612.
Browse files Browse the repository at this point in the history
- pre-allocate memory for vectors
- avoid copying (moving) items to a different vector
  • Loading branch information
hkaiser committed Jun 22, 2015
1 parent f723524 commit 5495a8d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
1 change: 1 addition & 0 deletions hpx/parallel/util/detail/chunk_size.hpp
Expand Up @@ -102,6 +102,7 @@ namespace hpx { namespace parallel { namespace util { namespace detail
std::forward<F1>(f1), first, count, chunk_size);

std::vector<std::pair<FwdIter, std::size_t> > shape;
shape.reserve(count);

while (count != 0)
{
Expand Down
23 changes: 10 additions & 13 deletions hpx/parallel/util/foreach_partitioner.hpp
Expand Up @@ -45,12 +45,11 @@ namespace hpx { namespace parallel { namespace util
FwdIter last = first;
std::advance(last, count);

std::vector<hpx::future<Result> > workitems;
std::vector<hpx::future<Result> > inititems, workitems;
std::list<boost::exception_ptr> errors;

try {
// estimates a chunk size based on number of cores used
std::vector<hpx::future<Result> > inititems;
std::vector<std::pair<FwdIter, std::size_t> > shape =
get_static_shape(policy, inititems, f1,
first, count, chunk_size);
Expand All @@ -60,18 +59,17 @@ namespace hpx { namespace parallel { namespace util
return f1(elem.first, elem.second);
};

workitems.reserve(shape.size());
workitems = executor_traits::async_execute(
policy.executor(), f, shape);

std::move(inititems.begin(), inititems.end(),
std::back_inserter(workitems));
}
catch (...) {
detail::handle_local_exceptions<ExPolicy>::call(
boost::current_exception(), errors);
}

// wait for all tasks to finish
hpx::wait_all(inititems);
hpx::wait_all(workitems);
detail::handle_local_exceptions<ExPolicy>::call(
workitems, errors);
Expand All @@ -96,12 +94,11 @@ namespace hpx { namespace parallel { namespace util
FwdIter last = first;
std::advance(last, count);

std::vector<hpx::future<Result> > workitems;
std::vector<hpx::future<Result> > inititems, workitems;
std::list<boost::exception_ptr> errors;

try {
// estimates a chunk size based on number of cores used
std::vector<hpx::future<Result> > inititems;
std::vector<std::pair<FwdIter, std::size_t> > shape =
get_static_shape(policy, inititems, f1,
first, count, chunk_size);
Expand All @@ -111,11 +108,9 @@ namespace hpx { namespace parallel { namespace util
return f1(elem.first, elem.second);
};

workitems.reserve(shape.size());
workitems = executor_traits::async_execute(
policy.executor(), f, shape);

std::move(inititems.begin(), inititems.end(),
std::back_inserter(workitems));
}
catch (std::bad_alloc const&) {
return hpx::make_exceptional_future<FwdIter>(
Expand All @@ -127,13 +122,15 @@ namespace hpx { namespace parallel { namespace util

// wait for all tasks to finish
return hpx::lcos::local::dataflow(
[last, errors](std::vector<hpx::future<Result> > && r)
[last, errors](std::vector<hpx::future<Result> > && r1,
std::vector<hpx::future<Result> > && r2)
mutable -> FwdIter
{
detail::handle_local_exceptions<ExPolicy>::call(r, errors);
detail::handle_local_exceptions<ExPolicy>::call(r1, errors);
detail::handle_local_exceptions<ExPolicy>::call(r2, errors);
return last;
},
std::move(workitems));
std::move(inititems), std::move(workitems));
}
};

Expand Down

0 comments on commit 5495a8d

Please sign in to comment.