Files
ceres-solver/internal/ceres/ceres_cuda_kernels.cu
T
Joydeep Biswas 88e08cfe71 Mixed-precision Iterative Refinement Cholesky With CUDA
* Created a new class CUDADenseCholeskyMixedPrecision, which performs
  Cholesky factorization and solving in single (fp32) precision, and
  optionally performs iterative refinement.
* Added CUDA kernels for mixed-precision solve operations
* Added more detailed timing information to the FullReport about Schur
  elimination, reduced system solves, and back-substitution.

Some test performance numbers follow.
All tests were performed on an Ubuntu 20.04 desktop with an
Intel Core i9-9940X CPU and Nvidia Quadro RTX 6000 GPU.

Tests were launched as:
./bin/bundle_adjuster --input (problem_file) \
    --num_iterations 20
    --num_threads 28
    --linear_solver dense_schur
    --dense_linear_algebra_library (cuda|lapack)
    [--mixed_precision_solves]

==================================================
problem-21-11315-pre.txt
==================================================

--------------------------------------------------
Cuda Mixed Precision
--------------------------------------------------
Cost:
Initial                          4.413239e+06
Final                            3.037864e+04
Change                           4.382861e+06
  Linear solver                      0.250703 (14)
  ├ Schur eliminate                  0.234025 (14)
  ├ Reduced solve                    0.006643 (14)
  └ Backsubstitute                   0.006598 (12)

--------------------------------------------------
Cuda
--------------------------------------------------
Cost:
Initial                          4.413239e+06
Final                            3.037864e+04
Change                           4.382861e+06
  Linear solver                      0.257517 (12)
  ├ Schur eliminate                  0.233518 (12)
  ├ Reduced solve                    0.010621 (12)
  └ Backsubstitute                   0.007124 (12)

--------------------------------------------------
Lapack (OpenBLAS)
--------------------------------------------------
Cost:
Initial                          4.413239e+06
Final                            3.037864e+04
Change                           4.382861e+06
  Linear solver                      0.332349 (12)
  ├ Schur eliminate                  0.274748 (12)
  ├ Reduced solve                    0.015966 (12)
  └ Backsubstitute                   0.034192 (12)

==================================================
problem-257-65132-pre.txt
==================================================

--------------------------------------------------
Cuda Mixed Precision
--------------------------------------------------
Cost:
Initial                          2.456242e+07
Final                            9.677593e+04
Change                           2.446565e+07
  Linear solver                      1.332367 (20)
  ├ Schur eliminate                  1.021365 (20)
  ├ Reduced solve                    0.195472 (20)
  └ Backsubstitute                   0.075582 (20)

--------------------------------------------------
Cuda
--------------------------------------------------
Cost:
Initial                          2.456242e+07
Final                            9.677547e+04
Change                           2.446565e+07
  Linear solver                      1.810176 (20)
  ├ Schur eliminate                  1.012862 (20)
  ├ Reduced solve                    0.678704 (20)
  └ Backsubstitute                   0.083925 (20)

--------------------------------------------------
Lapack (OpenBLAS)
--------------------------------------------------
Cost:
Initial                          2.456242e+07
Final                            9.677547e+04
Change                           2.446565e+07
  Linear solver                      2.376273 (20)
  ├ Schur eliminate                  0.987613 (20)
  ├ Reduced solve                    1.043873 (20)
  └ Backsubstitute                   0.310402 (20)

==================================================
problem-744-543562-pre.txt
==================================================

--------------------------------------------------
Cuda Mixed Precision
--------------------------------------------------
Cost:
Initial                          1.434881e+08
Final                            1.546895e+06
Change                           1.419412e+08
  Linear solver                     27.010088 (20)
  ├ Schur eliminate                 24.362433 (20)
  ├ Reduced solve                    1.428542 (20)
  └ Backsubstitute                   0.814266 (20)

--------------------------------------------------
Cuda
--------------------------------------------------
Cost:
Initial                          1.434881e+08
Final                            1.546895e+06
Change                           1.419412e+08
  Linear solver                     32.342513 (20)
  ├ Schur eliminate                 24.638819 (20)
  ├ Reduced solve                    6.492090 (20)
  └ Backsubstitute                   0.802184 (20)

--------------------------------------------------
Lapack (OpenBLAS)
--------------------------------------------------
Cost:
Initial                          1.434881e+08
Final                            1.546895e+06
Change                           1.419412e+08
  Linear solver                     34.152224 (20)
  ├ Schur eliminate                 24.183723 (20)
  ├ Reduced solve                    8.784413 (20)
  └ Backsubstitute                   0.795044 (20)

Change-Id: I178887e776d8f4a1e8abb99bbc205bf8c278bf79
2022-07-13 06:55:31 -05:00

109 lines
4.5 KiB
Plaintext

// Ceres Solver - A fast non-linear least squares minimizer
// Copyright 2022 Google Inc. All rights reserved.
// http://ceres-solver.org/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Author: joydeepb@cs.utexas.edu (Joydeep Biswas)
#include "cuda_runtime.h"
namespace ceres::internal {
// As the CUDA Toolkit documentation says, "although arbitrary in this case, is
// a common choice". This is determined by the warp size, max block size, and
// multiprocessor sizes of recent GPUs. For complex kernels with significant
// register usage and unusual memory patterns, the occupancy calculator API
// might provide better performance. See "Occupancy Calculator" under the CUDA
// toolkit documentation.
constexpr int kCudaBlockSize = 256;
template<typename SrcType, typename DstType>
__global__ void TypeConversionKernel(const SrcType* __restrict__ input,
DstType* __restrict__ output,
const int size) {
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < size) {
output[i] = static_cast<DstType>(input[i]);
}
}
void CudaFP64ToFP32(const double* input,
float* output,
const int size,
cudaStream_t stream) {
const int num_blocks = (size + kCudaBlockSize - 1) / kCudaBlockSize;
TypeConversionKernel<double, float>
<<<num_blocks, kCudaBlockSize, 0, stream>>>(input, output, size);
}
void CudaFP32ToFP64(const float* input,
double* output,
const int size,
cudaStream_t stream) {
const int num_blocks = (size + kCudaBlockSize - 1) / kCudaBlockSize;
TypeConversionKernel<float, double>
<<<num_blocks, kCudaBlockSize, 0, stream>>>(input, output, size);
}
template<typename T>
__global__ void SetZeroKernel(T* __restrict__ output, const int size) {
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < size) {
output[i] = T(0.0);
}
}
void CudaSetZeroFP32(float* output, const int size, cudaStream_t stream) {
const int num_blocks = (size + kCudaBlockSize - 1) / kCudaBlockSize;
SetZeroKernel<float><<<num_blocks, kCudaBlockSize, 0, stream>>>(output, size);
}
void CudaSetZeroFP64(double* output, const int size, cudaStream_t stream) {
const int num_blocks = (size + kCudaBlockSize - 1) / kCudaBlockSize;
SetZeroKernel<double><<<num_blocks, kCudaBlockSize, 0, stream>>>(
output, size);
}
template <typename SrcType, typename DstType>
__global__ void XPlusEqualsYKernel(DstType* __restrict__ x,
const SrcType* __restrict__ y,
const int size) {
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < size) {
x[i] = x[i] + DstType(y[i]);
}
}
void CudaDsxpy(double* x,
float* y,
const int size,
cudaStream_t stream) {
const int num_blocks = (size + kCudaBlockSize - 1) / kCudaBlockSize;
XPlusEqualsYKernel<float, double>
<<<num_blocks, kCudaBlockSize, 0, stream>>>(x, y, size);
}
} // namespace ceres_cuda_kernels