Skip to content

Commit

Permalink
[EM] Support mmap backed ellpack. (dmlc#10602)
Browse files Browse the repository at this point in the history
- Support resource view in ellpack.
- Define the CUDA version of MMAP resource.
- Define the CUDA version of malloc resource.
- Refactor cuda runtime API wrappers, and add memory access related wrappers.
- gather windows macros into a single header.
  • Loading branch information
trivialfis authored Jul 18, 2024
1 parent e9fbce9 commit 292bb67
Show file tree
Hide file tree
Showing 59 changed files with 887 additions and 644 deletions.
1 change: 1 addition & 0 deletions R-package/src/Makevars.in
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ OBJECTS= \
$(PKGROOT)/src/common/charconv.o \
$(PKGROOT)/src/common/column_matrix.o \
$(PKGROOT)/src/common/common.o \
$(PKGROOT)/src/common/cuda_rt_utils.o \
$(PKGROOT)/src/common/error_msg.o \
$(PKGROOT)/src/common/hist_util.o \
$(PKGROOT)/src/common/host_device_vector.o \
Expand Down
1 change: 1 addition & 0 deletions R-package/src/Makevars.win
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ OBJECTS= \
$(PKGROOT)/src/common/charconv.o \
$(PKGROOT)/src/common/column_matrix.o \
$(PKGROOT)/src/common/common.o \
$(PKGROOT)/src/common/cuda_rt_utils.o \
$(PKGROOT)/src/common/error_msg.o \
$(PKGROOT)/src/common/hist_util.o \
$(PKGROOT)/src/common/host_device_vector.o \
Expand Down
2 changes: 2 additions & 0 deletions include/xgboost/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define XGBOOST_BASE_H_

#include <dmlc/omp.h> // for omp_uint, omp_ulong
// Put the windefs here to guard as many files as possible.
#include <xgboost/windefs.h>

#include <cstdint> // for int32_t, uint64_t, int16_t
#include <ostream> // for ostream
Expand Down
7 changes: 4 additions & 3 deletions include/xgboost/collective/poll_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
* \author Tianqi Chen
*/
#pragma once
#include "xgboost/collective/result.h"
#include "xgboost/collective/socket.h"
#include <xgboost/collective/result.h>
#include <xgboost/collective/socket.h>

#if defined(_WIN32)
#include <xgboost/windefs.h>
// Socket API
#include <winsock2.h>
#include <ws2tcpip.h>

#else

#include <arpa/inet.h>
Expand Down
28 changes: 7 additions & 21 deletions include/xgboost/collective/socket.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/**
* Copyright (c) 2022-2024, XGBoost Contributors
* Copyright 2022-2024, XGBoost Contributors
*/
#pragma once

#if !defined(NOMINMAX) && defined(_WIN32)
#define NOMINMAX
#endif // !defined(NOMINMAX)

#include <cerrno> // errno, EINTR, EBADF
#include <climits> // HOST_NAME_MAX
#include <cstddef> // std::size_t
Expand All @@ -18,18 +14,12 @@

#if defined(__linux__)
#include <sys/ioctl.h> // for TIOCOUTQ, FIONREAD
#endif // defined(__linux__)

#if !defined(xgboost_IS_MINGW)

#if defined(__MINGW32__)
#define xgboost_IS_MINGW 1
#endif // defined(__MINGW32__)

#endif // xgboost_IS_MINGW
#endif // defined(__linux__)

#if defined(_WIN32)

// Guard the include.
#include <xgboost/windefs.h>
// Socket API
#include <winsock2.h>
#include <ws2tcpip.h>

Expand All @@ -41,9 +31,9 @@ using in_port_t = std::uint16_t;

#if !defined(xgboost_IS_MINGW)
using ssize_t = int;
#endif // !xgboost_IS_MINGW()
#endif // !xgboost_IS_MINGW()

#else // UNIX
#else // UNIX

#include <arpa/inet.h> // inet_ntop
#include <fcntl.h> // fcntl, F_GETFL, O_NONBLOCK
Expand Down Expand Up @@ -839,7 +829,3 @@ Result INetNToP(H const &host, std::string *p_out) {
} // namespace xgboost

#undef xgboost_CHECK_SYS_CALL

#if defined(xgboost_IS_MINGW)
#undef xgboost_IS_MINGW
#endif
33 changes: 33 additions & 0 deletions include/xgboost/windefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2024, XGBoost Contributors
*
* @brief Macro for Windows.
*/
#pragma once

#if !defined(xgboost_IS_WIN)

#if defined(_MSC_VER) || defined(__MINGW32__)
#define xgboost_IS_WIN 1
#endif // defined(_MSC_VER) || defined(__MINGW32__)

#endif // !defined(xgboost_IS_WIN)

#if defined(xgboost_IS_WIN)

#if !defined(NOMINMAX)
#define NOMINMAX
#endif // !defined(NOMINMAX)

// A macro used inside `windows.h` to avoid conflicts with `winsock2.h`
#define WIN32_LEAN_AND_MEAN

#if !defined(xgboost_IS_MINGW)

#if defined(__MINGW32__)
#define xgboost_IS_MINGW 1
#endif // defined(__MINGW32__)

#endif // xgboost_IS_MINGW

#endif // defined(xgboost_IS_WIN)
5 changes: 2 additions & 3 deletions src/c_api/c_api_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
#define XGBOOST_C_API_C_API_ERROR_H_

#include <dmlc/base.h>
#include <dmlc/logging.h>

#include "c_api_utils.h"
#include "xgboost/collective/result.h"
#include "c_api_utils.h" // for XGBoostAPIGuard
#include "xgboost/logging.h"

/*! \brief macro to guard beginning and end section of all functions */
#ifdef LOG_CAPI_INVOCATION
Expand Down
17 changes: 7 additions & 10 deletions src/cli_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,26 @@
* \brief The command line interface program of xgboost.
* This file is not included in dynamic library.
*/
#if !defined(NOMINMAX) && defined(_WIN32)
#define NOMINMAX
#endif // !defined(NOMINMAX)

#include <dmlc/timer.h>

#include <xgboost/learner.h>
#include <xgboost/base.h>
#include <xgboost/data.h>
#include <xgboost/json.h>
#include <xgboost/learner.h>
#include <xgboost/logging.h>
#include <xgboost/parameter.h>

#include <iomanip>
#include <ctime>
#include <string>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <string>
#include <vector>

#include "c_api/c_api_utils.h"
#include "common/common.h"
#include "common/config.h"
#include "common/io.h"
#include "common/version.h"
#include "c_api/c_api_utils.h"

namespace xgboost {
enum CLITask {
Expand Down
7 changes: 3 additions & 4 deletions src/collective/tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
#include <sys/socket.h> // socket, AF_INET6, AF_INET, connect, getsockname
#endif // defined(__unix__) || defined(__APPLE__)

#if !defined(NOMINMAX) && defined(_WIN32)
#define NOMINMAX
#endif // !defined(NOMINMAX)

#if defined(_WIN32)
// Guard the include
#include <xgboost/windefs.h>
// Socket API
#include <winsock2.h>
#include <ws2tcpip.h>
#endif // defined(_WIN32)
Expand Down
7 changes: 1 addition & 6 deletions src/common/common.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2015-2023 by Contributors
* Copyright 2015-2024, XGBoost Contributors
*/
#include "common.h"

Expand Down Expand Up @@ -54,9 +54,4 @@ void EscapeU8(std::string const &string, std::string *p_buffer) {
}
}
}

#if !defined(XGBOOST_USE_CUDA)
int AllVisibleGPUs() { return 0; }
#endif // !defined(XGBOOST_USE_CUDA)

} // namespace xgboost::common
40 changes: 16 additions & 24 deletions src/common/common.cu
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
/*!
* Copyright 2018-2022 XGBoost contributors
/**
* Copyright 2018-2024, XGBoost contributors
*/
#include "common.h"

namespace xgboost {
namespace common {
#include <thrust/system/cuda/error.h>
#include <thrust/system_error.h>

void SetDevice(std::int32_t device) {
if (device >= 0) {
dh::safe_cuda(cudaSetDevice(device));
}
}
#include "common.h"

int AllVisibleGPUs() {
int n_visgpus = 0;
try {
// When compiled with CUDA but running on CPU only device,
// cudaGetDeviceCount will fail.
dh::safe_cuda(cudaGetDeviceCount(&n_visgpus));
} catch (const dmlc::Error &) {
cudaGetLastError(); // reset error.
return 0;
namespace dh {
void ThrowOnCudaError(cudaError_t code, const char *file, int line) {
if (code != cudaSuccess) {
std::string f;
if (file != nullptr) {
f = file;
}
LOG(FATAL) << thrust::system_error(code, thrust::cuda_category(),
f + ": " + std::to_string(line))
.what();
}
return n_visgpus;
}

} // namespace common
} // namespace xgboost
} // namespace dh
38 changes: 11 additions & 27 deletions src/common/common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2015-2023 by XGBoost Contributors
* Copyright 2015-2024, XGBoost Contributors
* \file common.h
* \brief Common utilities
*/
Expand All @@ -19,9 +19,8 @@
#include "xgboost/base.h" // for XGBOOST_DEVICE
#include "xgboost/logging.h" // for LOG, LOG_FATAL, LogMessageFatal

// magic to define functions based on the compiler.
#if defined(__CUDACC__)
#include <thrust/system/cuda/error.h>
#include <thrust/system_error.h>

#define WITH_CUDA() true

Expand All @@ -31,23 +30,20 @@

#endif // defined(__CUDACC__)

#if defined(XGBOOST_USE_CUDA)
#include <cuda_runtime_api.h>
#endif

namespace dh {
#if defined(__CUDACC__)
#if defined(XGBOOST_USE_CUDA)
/*
* Error handling functions
* Error handling functions
*/
void ThrowOnCudaError(cudaError_t code, const char *file, int line);

#define safe_cuda(ans) ThrowOnCudaError((ans), __FILE__, __LINE__)

inline cudaError_t ThrowOnCudaError(cudaError_t code, const char *file,
int line) {
if (code != cudaSuccess) {
LOG(FATAL) << thrust::system_error(code, thrust::cuda_category(),
std::string{file} + ": " + // NOLINT
std::to_string(line)).what();
}
return code;
}
#endif // defined(__CUDACC__)
#endif // defined(XGBOOST_USE_CUDA)
} // namespace dh

namespace xgboost::common {
Expand Down Expand Up @@ -167,8 +163,6 @@ class Range {
Iterator end_;
};

int AllVisibleGPUs();

inline void AssertGPUSupport() {
#ifndef XGBOOST_USE_CUDA
LOG(FATAL) << "XGBoost version not compiled with GPU support.";
Expand All @@ -187,16 +181,6 @@ inline void AssertSYCLSupport() {
#endif // XGBOOST_USE_SYCL
}

void SetDevice(std::int32_t device);

#if !defined(XGBOOST_USE_CUDA)
inline void SetDevice(std::int32_t device) {
if (device >= 0) {
AssertGPUSupport();
}
}
#endif

/**
* @brief Last index of a group in a CSR style of index pointer.
*/
Expand Down
Loading

0 comments on commit 292bb67

Please sign in to comment.