Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support v3 cuda array interface. #6776

Merged
merged 7 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/data/array_interface.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*!
* Copyright 2021 by Contributors
*/
#include "array_interface.h"
#include "../common/common.h"

namespace xgboost {
void ArrayInterfaceHandler::SyncCudaStream(int64_t stream) {
switch (stream) {
case 0:
LOG(FATAL) << "Invalid stream ID in array interface: " << stream;
case 1:
// default legacy stream
break;
case 2:
// default per-thread stream
default:
dh::safe_cuda(cudaStreamSynchronize(reinterpret_cast<cudaStream_t>(stream)));
}
}
} // namespace xgboost
49 changes: 35 additions & 14 deletions src/data/array_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "xgboost/logging.h"
#include "xgboost/span.h"
#include "../common/bitfield.h"
#include "../common/common.h"

namespace xgboost {
// Common errors in parsing columnar format.
Expand All @@ -41,7 +42,7 @@ struct ArrayInterfaceErrors {
return str.c_str();
}
static char const* Version() {
return "Only version 1 and 2 of `__cuda_array_interface__' are supported.";
return "Only version <= 3 of `__cuda_array_interface__' are supported.";
}
static char const* OfType(std::string const& type) {
static std::string str;
Expand Down Expand Up @@ -119,9 +120,18 @@ class ArrayInterfaceHandler {
}

static void Validate(std::map<std::string, Json> const& array) {
if (array.find("version") == array.cend()) {
auto version_it = array.find("version");
if (version_it == array.cend()) {
LOG(FATAL) << "Missing `version' field for array interface";
}
auto stream_it = array.find("stream");
if (stream_it != array.cend() && !IsA<Null>(stream_it->second)) {
// is cuda, check the version.
if (get<Integer const>(version_it->second) > 3) {
LOG(FATAL) << ArrayInterfaceErrors::Version();
}
}

if (array.find("typestr") == array.cend()) {
LOG(FATAL) << "Missing `typestr' field for array interface";
}
Expand Down Expand Up @@ -233,25 +243,31 @@ class ArrayInterfaceHandler {
}
return p_data;
}

static void SyncCudaStream(int64_t stream);
};

#if !defined(XGBOOST_USE_CUDA)
inline void ArrayInterfaceHandler::SyncCudaStream(int64_t stream) {
common::AssertGPUSupport();
}
#endif // !defined(XGBOOST_USE_CUDA)

// A view over __array_interface__
class ArrayInterface {
void Initialize(std::map<std::string, Json> const &column,
void Initialize(std::map<std::string, Json> const &array,
bool allow_mask = true) {
ArrayInterfaceHandler::Validate(column);
auto typestr = get<String const>(column.at("typestr"));
ArrayInterfaceHandler::Validate(array);
auto typestr = get<String const>(array.at("typestr"));
this->AssignType(StringView{typestr});

auto shape = ArrayInterfaceHandler::ExtractShape(column);
num_rows = shape.first;
num_cols = shape.second;

data = ArrayInterfaceHandler::ExtractData(column, StringView{typestr}, shape);
std::tie(num_rows, num_cols) = ArrayInterfaceHandler::ExtractShape(array);
data = ArrayInterfaceHandler::ExtractData(
array, StringView{typestr}, std::make_pair(num_rows, num_cols));

if (allow_mask) {
common::Span<RBitField8::value_type> s_mask;
size_t n_bits = ArrayInterfaceHandler::ExtractMask(column, &s_mask);
size_t n_bits = ArrayInterfaceHandler::ExtractMask(array, &s_mask);

valid = RBitField8(s_mask);

Expand All @@ -261,12 +277,18 @@ class ArrayInterface {
<< "XGBoost doesn't support internal broadcasting.";
}
} else {
CHECK(column.find("mask") == column.cend())
CHECK(array.find("mask") == array.cend())
<< "Masked array is not yet supported.";
}

ArrayInterfaceHandler::ExtractStride(column, strides, num_rows, num_cols,
ArrayInterfaceHandler::ExtractStride(array, strides, num_rows, num_cols,
typestr[2] - '0');

auto stream_it = array.find("stream");
if (stream_it != array.cend() && !IsA<Null>(stream_it->second)) {
int64_t stream = get<Integer const>(stream_it->second);
ArrayInterfaceHandler::SyncCudaStream(stream);
}
}

public:
Expand Down Expand Up @@ -377,7 +399,6 @@ class ArrayInterface {
bst_feature_t num_cols;
size_t strides[2]{0, 0};
void* data;

Type type;
};

Expand Down
42 changes: 42 additions & 0 deletions tests/cpp/data/test_array_interface.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*!
* Copyright 2021 by Contributors
*/
#include <gtest/gtest.h>
#include <xgboost/host_device_vector.h>
#include "../helpers.h"
#include "../../../src/data/array_interface.h"

namespace xgboost {

__global__ void SleepForTest(uint64_t *out, uint64_t duration) {
auto start = clock64();
auto t = 0;
while (t < duration) {
t = clock64() - start;
}
out[0] = t;
}

TEST(ArrayInterface, Stream) {
size_t constexpr kRows = 10, kCols = 10;
HostDeviceVector<float> storage;
auto arr_str = RandomDataGenerator{kRows, kCols, 0}.GenerateArrayInterface(&storage);

cudaStream_t stream;
cudaStreamCreate(&stream);

auto j_arr =Json::Load(StringView{arr_str});
j_arr["stream"] = Integer(reinterpret_cast<int64_t>(stream));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this a safe cast?

Copy link
Member Author

Choose a reason for hiding this comment

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

When you start reinterpreting bits,it's hard to argue what's safe ..

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the stream a memory address, or is it something else, like a struct?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's an opaque pointer.

Copy link
Collaborator

@hcho3 hcho3 Mar 25, 2021

Choose a reason for hiding this comment

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

Got it. Then casting it to an integer should be safe.

Json::Dump(j_arr, &arr_str);

dh::caching_device_vector<uint64_t> out(1, 0);
uint64_t dur = 1e9;
dh::LaunchKernel{1, 1, 0, stream}(SleepForTest, out.data().get(), dur);
ArrayInterface arr(arr_str);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a nice test to test whether stream has been properly synchronized.


auto t = out[0];
CHECK_GE(t, dur);

cudaStreamDestroy(stream);
}
} // namespace xgboost