Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions cpp/src/arrow/python/arrow_to_pandas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1370,14 +1370,14 @@ class DataFrameBlockCreator {
const std::shared_ptr<Table>& table, MemoryPool* pool)
: table_(table), options_(options), pool_(pool) {}

Status Convert(int nthreads, PyObject** output) {
Status Convert(PyObject** output) {
column_types_.resize(table_->num_columns());
column_block_placement_.resize(table_->num_columns());
type_counts_.clear();
blocks_.clear();

RETURN_NOT_OK(CreateBlocks());
RETURN_NOT_OK(WriteTableToBlocks(nthreads));
RETURN_NOT_OK(WriteTableToBlocks());

return GetResultList(output);
}
Expand Down Expand Up @@ -1450,23 +1450,21 @@ class DataFrameBlockCreator {
return Status::OK();
}

Status WriteTableToBlocks(int nthreads) {
Status WriteTableToBlocks() {
auto WriteColumn = [this](int i) {
std::shared_ptr<PandasBlock> block;
RETURN_NOT_OK(this->GetBlock(i, &block));
return block->Write(this->table_->column(i), i, this->column_block_placement_[i]);
};

int num_tasks = table_->num_columns();
nthreads = std::min<int>(nthreads, num_tasks);
if (nthreads == 1) {
for (int i = 0; i < num_tasks; ++i) {
if (options_.use_threads) {
return ParallelFor(table_->num_columns(), WriteColumn);
} else {
for (int i = 0; i < table_->num_columns(); ++i) {
RETURN_NOT_OK(WriteColumn(i));
}
} else {
RETURN_NOT_OK(ParallelFor(nthreads, num_tasks, WriteColumn));
return Status::OK();
}
return Status::OK();
}

Status AppendBlocks(const BlockMap& blocks, PyObject* list) {
Expand Down Expand Up @@ -1866,15 +1864,15 @@ Status ConvertColumnToPandas(PandasOptions options, const std::shared_ptr<Column
}

Status ConvertTableToPandas(PandasOptions options, const std::shared_ptr<Table>& table,
int nthreads, MemoryPool* pool, PyObject** out) {
return ConvertTableToPandas(options, std::unordered_set<std::string>(), table, nthreads,
pool, out);
MemoryPool* pool, PyObject** out) {
return ConvertTableToPandas(options, std::unordered_set<std::string>(), table, pool,
out);
}

Status ConvertTableToPandas(PandasOptions options,
const std::unordered_set<std::string>& categorical_columns,
const std::shared_ptr<Table>& table, int nthreads,
MemoryPool* pool, PyObject** out) {
const std::shared_ptr<Table>& table, MemoryPool* pool,
PyObject** out) {
std::shared_ptr<Table> current_table = table;
if (!categorical_columns.empty()) {
FunctionContext ctx;
Expand All @@ -1894,7 +1892,7 @@ Status ConvertTableToPandas(PandasOptions options,
}

DataFrameBlockCreator helper(options, current_table, pool);
return helper.Convert(nthreads, out);
return helper.Convert(out);
}

} // namespace py
Expand Down
10 changes: 6 additions & 4 deletions cpp/src/arrow/python/arrow_to_pandas.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ struct PandasOptions {
bool strings_to_categorical;
bool zero_copy_only;
bool integer_object_nulls;
bool use_threads;

PandasOptions()
: strings_to_categorical(false),
zero_copy_only(false),
integer_object_nulls(false) {}
integer_object_nulls(false),
use_threads(false) {}
};

ARROW_EXPORT
Expand All @@ -68,7 +70,7 @@ Status ConvertColumnToPandas(PandasOptions options, const std::shared_ptr<Column
// tuple item: (indices: ndarray[int32], block: ndarray[TYPE, ndim=2])
ARROW_EXPORT
Status ConvertTableToPandas(PandasOptions options, const std::shared_ptr<Table>& table,
int nthreads, MemoryPool* pool, PyObject** out);
MemoryPool* pool, PyObject** out);

/// Convert a whole table as efficiently as possible to a pandas.DataFrame.
///
Expand All @@ -77,8 +79,8 @@ Status ConvertTableToPandas(PandasOptions options, const std::shared_ptr<Table>&
ARROW_EXPORT
Status ConvertTableToPandas(PandasOptions options,
const std::unordered_set<std::string>& categorical_columns,
const std::shared_ptr<Table>& table, int nthreads,
MemoryPool* pool, PyObject** out);
const std::shared_ptr<Table>& table, MemoryPool* pool,
PyObject** out);

} // namespace py
} // namespace arrow
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/arrow/python/python-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ TEST(PandasConversionTest, TestObjectBlockWriteFails) {
PyObject* out;
Py_BEGIN_ALLOW_THREADS;
PandasOptions options;
options.use_threads = true;
MemoryPool* pool = default_memory_pool();
ASSERT_RAISES(UnknownError, ConvertTableToPandas(options, table, 2, pool, &out));
ASSERT_RAISES(UnknownError, ConvertTableToPandas(options, table, pool, &out));
Py_END_ALLOW_THREADS;
}

Expand Down
37 changes: 37 additions & 0 deletions cpp/src/arrow/status-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,41 @@ TEST(StatusTest, TestToString) {
ASSERT_EQ(file_error.ToString(), ss.str());
}

TEST(StatusTest, AndStatus) {
Status a = Status::OK();
Status b = Status::OK();
Status c = Status::Invalid("invalid value");
Status d = Status::IOError("file error");

Status res;
res = a & b;
ASSERT_TRUE(res.ok());
res = a & c;
ASSERT_TRUE(res.IsInvalid());
res = d & c;
ASSERT_TRUE(res.IsIOError());

res = Status::OK();
res &= c;
ASSERT_TRUE(res.IsInvalid());
res &= d;
ASSERT_TRUE(res.IsInvalid());

// With rvalues
res = Status::OK() & Status::Invalid("foo");
ASSERT_TRUE(res.IsInvalid());
res = Status::Invalid("foo") & Status::OK();
ASSERT_TRUE(res.IsInvalid());
res = Status::Invalid("foo") & Status::IOError("bar");
ASSERT_TRUE(res.IsInvalid());

res = Status::OK();
res &= Status::OK();
ASSERT_TRUE(res.ok());
res &= Status::Invalid("foo");
ASSERT_TRUE(res.IsInvalid());
res &= Status::IOError("bar");
ASSERT_TRUE(res.IsInvalid());
}

} // namespace arrow
6 changes: 3 additions & 3 deletions cpp/src/arrow/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ Status::Status(StatusCode code, const std::string& msg) {
state_->msg = msg;
}

void Status::CopyFrom(const State* state) {
void Status::CopyFrom(const Status& s) {
delete state_;
if (state == nullptr) {
if (s.state_ == nullptr) {
state_ = nullptr;
} else {
state_ = new State(*state);
state_ = new State(*s.state_);
}
}

Expand Down
64 changes: 60 additions & 4 deletions cpp/src/arrow/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstring>
#include <iosfwd>
#include <string>
#include <utility>

#ifdef ARROW_EXTRA_ERROR_CONTEXT
#include <sstream>
Expand Down Expand Up @@ -100,7 +101,17 @@ class ARROW_EXPORT Status {

// Copy the specified status.
Status(const Status& s);
void operator=(const Status& s);
Status& operator=(const Status& s);

// Move the specified status.
Status(Status&& s);
Status& operator=(Status&& s);

// AND the statuses.
Status operator&(const Status& s) const;
Status operator&(Status&& s) const;
Status& operator&=(const Status& s);
Status& operator&=(Status&& s);

// Return a success status.
static Status OK() { return Status(); }
Expand Down Expand Up @@ -207,23 +218,68 @@ class ARROW_EXPORT Status {
// a `State` structure containing the error code and message(s)
State* state_;

void CopyFrom(const State* s);
void CopyFrom(const Status& s);
void MoveFrom(Status& s);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struck me as a tad odd (vs. Status&& s); I guess it doesn't matter too much because this is an internal API, so I think it's OK as is

};

static inline std::ostream& operator<<(std::ostream& os, const Status& x) {
os << x.ToString();
return os;
}

inline void Status::MoveFrom(Status& s) {
delete state_;
state_ = s.state_;
s.state_ = NULL;
}

inline Status::Status(const Status& s)
: state_((s.state_ == NULL) ? NULL : new State(*s.state_)) {}

inline void Status::operator=(const Status& s) {
inline Status& Status::operator=(const Status& s) {
// The following condition catches both aliasing (when this == &s),
// and the common case where both s and *this are ok.
if (state_ != s.state_) {
CopyFrom(s.state_);
CopyFrom(s);
}
return *this;
}

inline Status::Status(Status&& s) : state_(s.state_) { s.state_ = NULL; }

inline Status& Status::operator=(Status&& s) {
MoveFrom(s);
return *this;
}

inline Status Status::operator&(const Status& s) const {
if (ok()) {
return s;
} else {
return *this;
}
}

inline Status Status::operator&(Status&& s) const {
if (ok()) {
return std::move(s);
} else {
return *this;
}
}

inline Status& Status::operator&=(const Status& s) {
if (ok() && !s.ok()) {
CopyFrom(s);
}
return *this;
}

inline Status& Status::operator&=(Status&& s) {
if (ok() && !s.ok()) {
MoveFrom(s);
}
return *this;
}

} // namespace arrow
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/util/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ uint8_t* pointer_logical_and(const uint8_t* address, uintptr_t bits) {
void parallel_memcopy(uint8_t* dst, const uint8_t* src, int64_t nbytes,
uintptr_t block_size, int num_threads) {
// XXX This function is really using `num_threads + 1` threads.
auto pool = CPUThreadPool();
auto pool = GetCpuThreadPool();

uint8_t* left = pointer_logical_and(src + block_size - 1, ~(block_size - 1));
uint8_t* right = pointer_logical_and(src + nbytes, ~(block_size - 1));
Expand Down
23 changes: 23 additions & 0 deletions cpp/src/arrow/util/parallel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,32 @@
#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`, 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) {
std::vector<std::thread> thread_pool;
Expand Down
28 changes: 20 additions & 8 deletions cpp/src/arrow/util/thread-pool-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class TestThreadPool : public ::testing::Test {

std::shared_ptr<ThreadPool> MakeThreadPool() { return MakeThreadPool(4); }

std::shared_ptr<ThreadPool> MakeThreadPool(size_t threads) {
std::shared_ptr<ThreadPool> MakeThreadPool(int threads) {
std::shared_ptr<ThreadPool> pool;
Status st = ThreadPool::Make(threads, &pool);
return pool;
Expand Down Expand Up @@ -163,7 +163,7 @@ class TestThreadPool : public ::testing::Test {

TEST_F(TestThreadPool, ConstructDestruct) {
// Stress shutdown-at-destruction logic
for (size_t threads : {1, 2, 3, 8, 32, 70}) {
for (int threads : {1, 2, 3, 8, 32, 70}) {
auto pool = this->MakeThreadPool(threads);
}
}
Expand Down Expand Up @@ -223,21 +223,31 @@ TEST_F(TestThreadPool, QuickShutdown) {
TEST_F(TestThreadPool, SetCapacity) {
auto pool = this->MakeThreadPool(3);
ASSERT_EQ(pool->GetCapacity(), 3);
ASSERT_EQ(pool->GetActualCapacity(), 3);

ASSERT_OK(pool->SetCapacity(5));
ASSERT_EQ(pool->GetCapacity(), 5);
ASSERT_EQ(pool->GetActualCapacity(), 5);

ASSERT_OK(pool->SetCapacity(2));
// Wait for workers to wake up and secede
busy_wait(0.5, [&] { return pool->GetCapacity() == 2; });
ASSERT_EQ(pool->GetCapacity(), 2);
// Wait for workers to wake up and secede
busy_wait(0.5, [&] { return pool->GetActualCapacity() == 2; });
ASSERT_EQ(pool->GetActualCapacity(), 2);

ASSERT_OK(pool->SetCapacity(5));
ASSERT_EQ(pool->GetCapacity(), 5);
ASSERT_EQ(pool->GetActualCapacity(), 5);

// Downsize while tasks are pending
for (int i = 0; i < 10; ++i) {
ASSERT_OK(pool->Spawn(std::bind(sleep_for, 0.01 /* seconds */)));
}
ASSERT_OK(pool->SetCapacity(2));
busy_wait(0.5, [&] { return pool->GetCapacity() == 2; });
ASSERT_EQ(pool->GetCapacity(), 2);
busy_wait(0.5, [&] { return pool->GetActualCapacity() == 2; });
ASSERT_EQ(pool->GetActualCapacity(), 2);

// Ensure nothing got stuck
ASSERT_OK(pool->Shutdown());
}
Expand Down Expand Up @@ -274,14 +284,16 @@ TEST_F(TestThreadPool, Submit) {

TEST(TestGlobalThreadPool, Capacity) {
// Sanity check
auto pool = CPUThreadPool();
size_t capacity = pool->GetCapacity();
auto pool = GetCpuThreadPool();
int capacity = pool->GetCapacity();
ASSERT_GT(capacity, 0);
ASSERT_EQ(pool->GetActualCapacity(), capacity);
ASSERT_EQ(GetCpuThreadPoolCapacity(), capacity);

// Exercise default capacity heuristic
ASSERT_OK(DelEnvVar("OMP_NUM_THREADS"));
ASSERT_OK(DelEnvVar("OMP_THREAD_LIMIT"));
size_t hw_capacity = std::thread::hardware_concurrency();
int hw_capacity = std::thread::hardware_concurrency();
ASSERT_EQ(ThreadPool::DefaultCapacity(), hw_capacity);
ASSERT_OK(SetEnvVar("OMP_NUM_THREADS", "13"));
ASSERT_EQ(ThreadPool::DefaultCapacity(), 13);
Expand Down
Loading