MCPcopy Create free account
hub / github.com/dmlc/xgboost / MakeArrayInterfaceBatch

Function MakeArrayInterfaceBatch

tests/cpp/helpers.cc:282–331  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

280}
281
282std::pair<std::vector<std::string>, std::string> MakeArrayInterfaceBatch(
283 HostDeviceVector<float> const* storage, std::size_t n_samples, bst_feature_t n_features,
284 std::size_t batches, DeviceOrd device) {
285 std::vector<std::string> result(batches);
286 std::vector<Json> objects;
287
288 size_t const rows_per_batch = n_samples / batches;
289
290 auto make_interface = [storage, device, n_features](std::size_t offset, std::size_t rows) {
291 Json array_interface{Object()};
292 array_interface["data"] = std::vector<Json>(2);
293 if (device.IsCUDA()) {
294 array_interface["data"][0] =
295 Integer(reinterpret_cast<int64_t>(storage->DevicePointer() + offset));
296 array_interface["stream"] = Null{};
297 } else {
298 array_interface["data"][0] =
299 Integer(reinterpret_cast<int64_t>(storage->HostPointer() + offset));
300 }
301
302 array_interface["data"][1] = Boolean(false);
303
304 array_interface["shape"] = std::vector<Json>(2);
305 array_interface["shape"][0] = rows;
306 array_interface["shape"][1] = n_features;
307
308 array_interface["typestr"] = String("<f4");
309 array_interface["version"] = 3;
310 return array_interface;
311 };
312
313 auto j_interface = make_interface(0, n_samples);
314 size_t offset = 0;
315 for (size_t i = 0; i < batches - 1; ++i) {
316 objects.emplace_back(make_interface(offset, rows_per_batch));
317 offset += rows_per_batch * n_features;
318 }
319
320 size_t const remaining = n_samples - offset / n_features;
321 CHECK_LE(offset, n_samples * n_features);
322 objects.emplace_back(make_interface(offset, remaining));
323
324 for (size_t i = 0; i < batches; ++i) {
325 Json::Dump(objects[i], &result[i]);
326 }
327
328 std::string interface_str;
329 Json::Dump(j_interface, &interface_str);
330 return {result, interface_str};
331}
332
333std::pair<std::vector<std::string>, std::string> RandomDataGenerator::GenerateArrayInterfaceBatch(
334 HostDeviceVector<float>* storage, size_t batches) const {

Callers 2

ArrayIterForTestMethod · 0.85

Calls 3

HostPointerMethod · 0.80
IsCUDAMethod · 0.45
DevicePointerMethod · 0.45

Tested by

no test coverage detected