Add a two-argument variant of ParallelFor() that uses the global CPU thread pool

This commit is contained in:
Antoine Pitrou committed 2018-05-24 16:08:46 +02:00
1 parent d0a527abab
commit 4567a2c3e3
2 files changed
+23 -15

No files matched your search

+2 -11
View File
@@ -40,7 +40,7 @@
#include "arrow/util/decimal.h"
#include "arrow/util/logging.h"
#include "arrow/util/macros.h"
#include "arrow/util/thread-pool.h"
#include "arrow/util/parallel.h"
#include "arrow/visitor_inline.h"
#include "arrow/compute/api.h"
@@ -1458,16 +1458,7 @@ class DataFrameBlockCreator {
};
if (use_threads) {
arrow::internal::ThreadPool* pool = arrow::internal::GetCpuThreadPool();
std::vector<std::future<Status>> futures;
for (int i = 0; i < table_->num_columns(); ++i) {
futures.push_back(pool->Submit(WriteColumn, i));
}
auto final_status = Status::OK();
for (auto& fut : futures) {
final_status &= fut.get();
}
return final_status;
return ParallelFor(table_->num_columns(), WriteColumn);
} else {
for (int i = 0; i < table_->num_columns(); ++i) {
RETURN_NOT_OK(WriteColumn(i));
+21 -4
View File
@@ -24,14 +24,31 @@
#include <vector>
#include "arrow/status.h"
#include "arrow/util/thread-pool.h"
namespace arrow {
// A parallelizer that takes a `Status(int)` function and calls it with
// arguments between 0 and `num_tasks - 1`, using the given number of
// concurrent threads.
// In most cases it's more appropriate to use the global CPU thread pool
// (see arrow/util/thread-pool.h)
// arguments between 0 and `num_tasks - 1`, on an arbitrary number of threads.
template <class FUNCTION>
Status ParallelFor(int num_tasks, FUNCTION&& func) {
auto pool = internal::GetCpuThreadPool();
std::vector<std::future<Status>> futures(num_tasks);
for (int i = 0; i < num_tasks; ++i) {
futures[i] = pool->Submit(func, i);
}
auto st = Status::OK();
for (auto& fut : futures) {
st &= fut.get();
}
return st;
}
// A variant of ParallelFor() with an explicit number of dedicated threads.
// In most cases it's more appropriate to use the 2-argument ParallelFor (above),
// or directly the global CPU thread pool (arrow/util/thread-pool.h).
template <class FUNCTION>
Status ParallelFor(int nthreads, int num_tasks, FUNCTION&& func) {