mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Add Eigen/GPU module: A standalone GPU library dispatch layer where DeviceMatrix<Scalar> operations map 1:1 to cuBLAS/cuSOLVER calls. CPU and GPU solvers coexist in the same binary with compatible syntax. Core infrastructure: - DeviceMatrix<Scalar>: RAII dense column-major GPU memory wrapper with async host transfer (fromHost/toHost) and CUDA event-based cross-stream synchronization. - GpuContext: Unified execution context owning a CUDA stream + cuBLAS handle + cuSOLVER handle. Thread-local default with explicit override via setThreadLocal(). Stream-borrowing constructor for integration. - DeviceBuffer: Typed RAII device allocation with move semantics. cuBLAS dispatch (expression syntax): - GEMM: d_C = d_A.adjoint() * d_B (cublasXgemm) - TRSM: d_X = d_A.triangularView<Lower>().solve(d_B) (cublasXtrsm) - SYMM/HEMM: d_C = d_A.selfadjointView<Lower>() * d_B (cublasXsymm) - SYRK/HERK: d_C = d_A * d_A.adjoint() (cublasXsyrk) cuSOLVER dispatch: - GpuLLT: Cached Cholesky factorization (cusolverDnXpotrf + Xpotrs) - GpuLU: Cached LU factorization (cusolverDnXgetrf + Xgetrs) - Solver chaining: auto x = d_A.llt().solve(d_B) - Solver expressions with .device(ctx) for explicit stream control. CI: Bump CUDA container to Ubuntu 22.04 (CMake 3.22), GCC 10->11, Clang 12->14. Bump cmake_minimum_required to 3.17 for FindCUDAToolkit. Tests: gpu_cublas.cpp, gpu_cusolver_llt.cpp, gpu_cusolver_lu.cpp, gpu_device_matrix.cpp, gpu_library_example.cu Benchmarks: bench_gpu_solvers.cpp, bench_gpu_chaining.cpp, bench_gpu_batching.cpp
116 lines
3.4 KiB
C++
116 lines
3.4 KiB
C++
// This file is part of Eigen, a lightweight C++ template library
|
|
// for linear algebra.
|
|
//
|
|
// Copyright (C) 2026 Rasmus Munk Larsen <rmlarsen@gmail.com>
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
// Solver expression types for DeviceMatrix.
|
|
//
|
|
// Each expression maps 1:1 to cuSOLVER library calls:
|
|
// LltSolveExpr → cusolverDnXpotrf + cusolverDnXpotrs
|
|
// LuSolveExpr → cusolverDnXgetrf + cusolverDnXgetrs
|
|
//
|
|
// Usage:
|
|
// d_X = d_A.llt().solve(d_B); // Cholesky solve
|
|
// d_X.device(ctx) = d_A.lu().solve(d_B); // LU solve on explicit stream
|
|
|
|
#ifndef EIGEN_GPU_DEVICE_SOLVER_EXPR_H
|
|
#define EIGEN_GPU_DEVICE_SOLVER_EXPR_H
|
|
|
|
// IWYU pragma: private
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
namespace Eigen {
|
|
|
|
// Forward declarations.
|
|
template <typename Scalar_>
|
|
class DeviceMatrix;
|
|
class GpuContext;
|
|
|
|
// ---- LLT solve expression ---------------------------------------------------
|
|
// d_A.llt().solve(d_B) → LltSolveExpr → cusolverDnXpotrf + cusolverDnXpotrs
|
|
|
|
template <typename Scalar_, int UpLo_ = Lower>
|
|
class LltSolveExpr {
|
|
public:
|
|
using Scalar = Scalar_;
|
|
enum { UpLo = UpLo_ };
|
|
|
|
LltSolveExpr(const DeviceMatrix<Scalar>& A, const DeviceMatrix<Scalar>& B) : A_(A), B_(B) {}
|
|
const DeviceMatrix<Scalar>& matrix() const { return A_; }
|
|
const DeviceMatrix<Scalar>& rhs() const { return B_; }
|
|
|
|
private:
|
|
const DeviceMatrix<Scalar>& A_;
|
|
const DeviceMatrix<Scalar>& B_;
|
|
};
|
|
|
|
// ---- LU solve expression ----------------------------------------------------
|
|
// d_A.lu().solve(d_B) → LuSolveExpr → cusolverDnXgetrf + cusolverDnXgetrs
|
|
|
|
template <typename Scalar_>
|
|
class LuSolveExpr {
|
|
public:
|
|
using Scalar = Scalar_;
|
|
|
|
LuSolveExpr(const DeviceMatrix<Scalar>& A, const DeviceMatrix<Scalar>& B) : A_(A), B_(B) {}
|
|
const DeviceMatrix<Scalar>& matrix() const { return A_; }
|
|
const DeviceMatrix<Scalar>& rhs() const { return B_; }
|
|
|
|
private:
|
|
const DeviceMatrix<Scalar>& A_;
|
|
const DeviceMatrix<Scalar>& B_;
|
|
};
|
|
|
|
// ---- DeviceLLTView: d_A.llt() → view with .solve() and .device() -----------
|
|
|
|
template <typename Scalar_, int UpLo_ = Lower>
|
|
class DeviceLLTView {
|
|
public:
|
|
using Scalar = Scalar_;
|
|
|
|
explicit DeviceLLTView(const DeviceMatrix<Scalar>& m) : mat_(m) {}
|
|
|
|
/** Build a solve expression: d_A.llt().solve(d_B).
|
|
* The expression is evaluated when assigned to a DeviceMatrix. */
|
|
LltSolveExpr<Scalar, UpLo_> solve(const DeviceMatrix<Scalar>& rhs) const { return {mat_, rhs}; }
|
|
|
|
// For cached factorizations, use the explicit GpuLLT API directly:
|
|
// GpuLLT<double> llt;
|
|
// llt.compute(d_A);
|
|
// auto d_X1 = llt.solve(d_B1);
|
|
// auto d_X2 = llt.solve(d_B2);
|
|
|
|
private:
|
|
const DeviceMatrix<Scalar>& mat_;
|
|
};
|
|
|
|
// ---- DeviceLUView: d_A.lu() → view with .solve() and .device() -------------
|
|
|
|
template <typename Scalar_>
|
|
class DeviceLUView {
|
|
public:
|
|
using Scalar = Scalar_;
|
|
|
|
explicit DeviceLUView(const DeviceMatrix<Scalar>& m) : mat_(m) {}
|
|
|
|
/** Build a solve expression: d_A.lu().solve(d_B). */
|
|
LuSolveExpr<Scalar> solve(const DeviceMatrix<Scalar>& rhs) const { return {mat_, rhs}; }
|
|
|
|
// For cached factorizations, use the explicit GpuLU API directly:
|
|
// GpuLU<double> lu;
|
|
// lu.compute(d_A);
|
|
// auto d_X1 = lu.solve(d_B1);
|
|
// auto d_X2 = lu.solve(d_B2);
|
|
|
|
private:
|
|
const DeviceMatrix<Scalar>& mat_;
|
|
};
|
|
|
|
} // namespace Eigen
|
|
|
|
#endif // EIGEN_GPU_DEVICE_SOLVER_EXPR_H
|